don't use wordexp.h for tilde expansion
wordexp.h is not supported by OpenBSD. Therefore do tilde expansion only via glob(). rename glob_path() to resolve_tilde() since it should not do globbing.
This commit is contained in:
parent
ca8d775487
commit
4d4ce82b35
|
@ -126,9 +126,11 @@ struct Config {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This function resolves ~ in pathnames.
|
* This function resolves ~ in pathnames.
|
||||||
|
* It may resolve wildcards in the first part of the path, but if no match
|
||||||
|
* or multiple matches are found, it just returns a copy of path as given.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
char *glob_path(const char *path);
|
char *resolve_tilde(const char *path);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if the given path exists by calling stat().
|
* Checks if the given path exists by calling stat().
|
||||||
|
|
43
src/config.c
43
src/config.c
|
@ -18,7 +18,6 @@
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <glob.h>
|
#include <glob.h>
|
||||||
#include <wordexp.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
/* We need Xlib for XStringToKeysym */
|
/* We need Xlib for XStringToKeysym */
|
||||||
|
@ -39,26 +38,32 @@ struct modes_head modes;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This function resolves ~ in pathnames.
|
* This function resolves ~ in pathnames.
|
||||||
|
* It may resolve wildcards in the first part of the path, but if no match
|
||||||
|
* or multiple matches are found, it just returns a copy of path as given.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
char *glob_path(const char *path) {
|
char *resolve_tilde(const char *path) {
|
||||||
static glob_t globbuf;
|
static glob_t globbuf;
|
||||||
if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
|
char *head, *tail, *result;
|
||||||
die("glob() failed");
|
|
||||||
char *result = sstrdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
|
|
||||||
globfree(&globbuf);
|
|
||||||
|
|
||||||
/* If the file does not exist yet, we still may need to resolve tilde,
|
tail = strchr(path, '/');
|
||||||
* so call wordexp */
|
head = strndup(path, tail ? tail - path : strlen(path));
|
||||||
if (strcmp(result, path) == 0) {
|
|
||||||
wordexp_t we;
|
int res = glob(head, GLOB_TILDE, NULL, &globbuf);
|
||||||
wordexp(path, &we, WRDE_NOCMD);
|
free(head);
|
||||||
if (we.we_wordc > 0) {
|
/* no match, or many wildcard matches are bad */
|
||||||
free(result);
|
if(res == GLOB_NOMATCH || globbuf.gl_pathc != 1)
|
||||||
result = sstrdup(we.we_wordv[0]);
|
result = sstrdup(path);
|
||||||
}
|
else if (res != 0) {
|
||||||
wordfree(&we);
|
die("glob() failed");
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
head = globbuf.gl_pathv[0];
|
||||||
|
result = smalloc(strlen(head) + (tail ? strlen(tail) : 0) + 1);
|
||||||
|
strncpy(result, head, strlen(head));
|
||||||
|
strncat(result, tail, strlen(tail));
|
||||||
|
}
|
||||||
|
globfree(&globbuf);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -239,7 +244,7 @@ static char *get_config_path() {
|
||||||
if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
|
if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
|
||||||
xdg_config_home = "~/.config";
|
xdg_config_home = "~/.config";
|
||||||
|
|
||||||
xdg_config_home = glob_path(xdg_config_home);
|
xdg_config_home = resolve_tilde(xdg_config_home);
|
||||||
if (asprintf(&config_path, "%s/i3/config", xdg_config_home) == -1)
|
if (asprintf(&config_path, "%s/i3/config", xdg_config_home) == -1)
|
||||||
die("asprintf() failed");
|
die("asprintf() failed");
|
||||||
free(xdg_config_home);
|
free(xdg_config_home);
|
||||||
|
@ -255,7 +260,7 @@ static char *get_config_path() {
|
||||||
char *buf = strdup(xdg_config_dirs);
|
char *buf = strdup(xdg_config_dirs);
|
||||||
char *tok = strtok(buf, ":");
|
char *tok = strtok(buf, ":");
|
||||||
while (tok != NULL) {
|
while (tok != NULL) {
|
||||||
tok = glob_path(tok);
|
tok = resolve_tilde(tok);
|
||||||
if (asprintf(&config_path, "%s/i3/config", tok) == -1)
|
if (asprintf(&config_path, "%s/i3/config", tok) == -1)
|
||||||
die("asprintf() failed");
|
die("asprintf() failed");
|
||||||
free(tok);
|
free(tok);
|
||||||
|
@ -269,7 +274,7 @@ static char *get_config_path() {
|
||||||
free(buf);
|
free(buf);
|
||||||
|
|
||||||
/* 3: check traditional paths */
|
/* 3: check traditional paths */
|
||||||
config_path = glob_path("~/.i3/config");
|
config_path = resolve_tilde("~/.i3/config");
|
||||||
if (path_exists(config_path))
|
if (path_exists(config_path))
|
||||||
return config_path;
|
return config_path;
|
||||||
|
|
||||||
|
|
16
src/ipc.c
16
src/ipc.c
|
@ -502,20 +502,20 @@ void ipc_new_client(EV_P_ struct ev_io *w, int revents) {
|
||||||
int ipc_create_socket(const char *filename) {
|
int ipc_create_socket(const char *filename) {
|
||||||
int sockfd;
|
int sockfd;
|
||||||
|
|
||||||
char *globbed = glob_path(filename);
|
char *resolved = resolve_tilde(filename);
|
||||||
DLOG("Creating IPC-socket at %s\n", globbed);
|
DLOG("Creating IPC-socket at %s\n", resolved);
|
||||||
char *copy = sstrdup(globbed);
|
char *copy = sstrdup(resolved);
|
||||||
const char *dir = dirname(copy);
|
const char *dir = dirname(copy);
|
||||||
if (!path_exists(dir))
|
if (!path_exists(dir))
|
||||||
mkdirp(dir);
|
mkdirp(dir);
|
||||||
free(copy);
|
free(copy);
|
||||||
|
|
||||||
/* Unlink the unix domain socket before */
|
/* Unlink the unix domain socket before */
|
||||||
unlink(globbed);
|
unlink(resolved);
|
||||||
|
|
||||||
if ((sockfd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) {
|
if ((sockfd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) {
|
||||||
perror("socket()");
|
perror("socket()");
|
||||||
free(globbed);
|
free(resolved);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -524,14 +524,14 @@ int ipc_create_socket(const char *filename) {
|
||||||
struct sockaddr_un addr;
|
struct sockaddr_un addr;
|
||||||
memset(&addr, 0, sizeof(struct sockaddr_un));
|
memset(&addr, 0, sizeof(struct sockaddr_un));
|
||||||
addr.sun_family = AF_LOCAL;
|
addr.sun_family = AF_LOCAL;
|
||||||
strcpy(addr.sun_path, globbed);
|
strncpy(addr.sun_path, resolved, sizeof(addr.sun_path) - 1);
|
||||||
if (bind(sockfd, (struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0) {
|
if (bind(sockfd, (struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0) {
|
||||||
perror("bind()");
|
perror("bind()");
|
||||||
free(globbed);
|
free(resolved);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
free(globbed);
|
free(resolved);
|
||||||
set_nonblock(sockfd);
|
set_nonblock(sockfd);
|
||||||
|
|
||||||
if (listen(sockfd, 5) < 0) {
|
if (listen(sockfd, 5) < 0) {
|
||||||
|
|
Loading…
Reference in New Issue