Lines 19-45
Link Here
|
19 |
* |
19 |
* |
20 |
*/ |
20 |
*/ |
21 |
char *resolve_tilde(const char *path) { |
21 |
char *resolve_tilde(const char *path) { |
22 |
static glob_t globbuf; |
22 |
char *home, *expanded; |
23 |
char *head, *tail, *result; |
|
|
24 |
|
23 |
|
25 |
tail = strchr(path, '/'); |
24 |
if (strncmp(path, "~/", 2) == 0) { |
26 |
head = sstrndup(path, tail ? (size_t)(tail - path) : strlen(path)); |
25 |
home = getenv("HOME"); |
27 |
|
26 |
if (home != NULL) { |
28 |
int res = glob(head, GLOB_TILDE, NULL, &globbuf); |
27 |
/* new length: sum - 1 (omit '~') + 1 (for '\0') */ |
29 |
free(head); |
28 |
expanded = scalloc(strlen(home)+strlen(path), 1); |
30 |
/* no match, or many wildcard matches are bad */ |
29 |
strcpy(expanded, home); |
31 |
if (res == GLOB_NOMATCH || globbuf.gl_pathc != 1) |
30 |
strcat(expanded, path+1); |
32 |
result = sstrdup(path); |
31 |
return expanded; |
33 |
else if (res != 0) { |
32 |
} |
34 |
err(EXIT_FAILURE, "glob() failed"); |
|
|
35 |
} else { |
36 |
head = globbuf.gl_pathv[0]; |
37 |
result = scalloc(strlen(head) + (tail ? strlen(tail) : 0) + 1, 1); |
38 |
strncpy(result, head, strlen(head)); |
39 |
if (tail) |
40 |
strncat(result, tail, strlen(tail)); |
41 |
} |
33 |
} |
42 |
globfree(&globbuf); |
|
|
43 |
|
34 |
|
44 |
return result; |
35 |
return sstrdup(path); |
45 |
} |
36 |
} |