2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
16 * This function resolves ~ in pathnames.
17 * It may resolve wildcards in the first part of the path, but if no match
18 * or multiple matches are found, it just returns a copy of path as given.
21 char *resolve_tilde(const char *path) {
22 static glob_t globbuf;
23 char *head, *tail, *result;
25 tail = strchr(path, '/');
26 head = sstrndup(path, tail ? (size_t)(tail - path) : strlen(path));
28 int res = glob(head, GLOB_TILDE, NULL, &globbuf);
30 /* no match, or many wildcard matches are bad */
31 if (res == GLOB_NOMATCH || globbuf.gl_pathc != 1)
32 result = sstrdup(path);
34 err(EXIT_FAILURE, "glob() failed");
36 head = globbuf.gl_pathv[0];
37 result = scalloc(strlen(head) + (tail ? strlen(tail) : 0) + 1, 1);
38 strncpy(result, head, strlen(head));
40 strncat(result, tail, strlen(tail));