2015-03-28 19:30:35 +01:00
|
|
|
/*
|
|
|
|
* vim:ts=4:sw=4:expandtab
|
|
|
|
*
|
|
|
|
* i3 - an improved dynamic tiling window manager
|
2015-04-04 02:17:56 +02:00
|
|
|
* © 2009 Michael Stapelberg and contributors (see also: LICENSE)
|
2015-03-28 19:30:35 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libi3.h"
|
|
|
|
#include <err.h>
|
|
|
|
#include <glob.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 *resolve_tilde(const char *path) {
|
|
|
|
static glob_t globbuf;
|
|
|
|
char *head, *tail, *result;
|
|
|
|
|
|
|
|
tail = strchr(path, '/');
|
2015-05-06 16:28:29 +02:00
|
|
|
head = sstrndup(path, tail ? (size_t)(tail - path) : strlen(path));
|
2015-03-28 19:30:35 +01:00
|
|
|
|
|
|
|
int res = glob(head, GLOB_TILDE, NULL, &globbuf);
|
|
|
|
free(head);
|
|
|
|
/* no match, or many wildcard matches are bad */
|
|
|
|
if (res == GLOB_NOMATCH || globbuf.gl_pathc != 1)
|
|
|
|
result = sstrdup(path);
|
|
|
|
else if (res != 0) {
|
|
|
|
err(EXIT_FAILURE, "glob() failed");
|
|
|
|
} else {
|
|
|
|
head = globbuf.gl_pathv[0];
|
|
|
|
result = scalloc(strlen(head) + (tail ? strlen(tail) : 0) + 1);
|
|
|
|
strncpy(result, head, strlen(head));
|
|
|
|
if (tail)
|
|
|
|
strncat(result, tail, strlen(tail));
|
|
|
|
}
|
|
|
|
globfree(&globbuf);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|