1 /* Licensed under BSD-MIT - see LICENSE file for details */
2 #include <ccan/tal/path/path.h>
3 #include <ccan/str/str.h>
4 #include <ccan/tal/str/str.h>
14 char *path_cwd(const tal_t *ctx)
19 /* *This* is why people hate C. */
20 cwd = tal_arr(ctx, char, len);
21 while (cwd && !getcwd(cwd, len)) {
22 if (errno != ERANGE || !tal_resize(&cwd, len *= 2))
28 char *path_join(const tal_t *ctx, const char *base, const char *a)
33 if (unlikely(!a) && taken(a)) {
39 if (a[0] == PATH_SEP) {
42 return tal_strdup(ctx, a);
45 if (unlikely(!base) && taken(base))
49 ret = tal_dup_arr(ctx, char, base, len, 1 + strlen(a) + 1);
52 if (len != 0 && ret[len-1] != PATH_SEP)
53 ret[len++] = PATH_SEP;
67 static void pushd_destroy(struct path_pushd *pushd)
72 struct path_pushd *path_pushd(const tal_t *ctx, const char *dir)
74 struct path_pushd *old = tal(ctx, struct path_pushd);
79 if (unlikely(!dir) && taken(dir))
82 if (!tal_add_destructor(old, pushd_destroy))
85 old->fd = open(".", O_RDONLY);
88 else if (chdir(dir) != 0)
97 bool path_popd(struct path_pushd *olddir)
99 bool ok = (fchdir(olddir->fd) == 0);
109 struct path_pushd *path_pushd(const tal_t *ctx, const char *dir)
111 struct path_pushd *old = tal(ctx, struct path_pushd);
116 old->olddir = path_cwd(old);
117 if (unlikely(!old->olddir))
119 else if (unlikely(!dir) && is_taken(dir))
121 else if (chdir(dir) != 0)
130 bool path_popd(struct path_pushd *olddir)
132 bool ok = (chdir(olddir->olddir) == 0);
137 #endif /* !HAVE_FCHDIR */
139 char *path_canon(const tal_t *ctx, const char *a)
142 char *oldcwd, *path, *p;
145 struct path_pushd *olddir;
147 /* A good guess as to size. */
149 if (a[0] != PATH_SEP) {
150 tmpctx = oldcwd = path_cwd(ctx);
153 len += strlen(oldcwd) + strlen(PATH_SEP_STR);
155 path = tal_array(tmpctx, char, len);
159 len = strlen(oldcwd);
160 memcpy(path, oldcwd, len);
161 path[len++] = PATH_SEP;
163 tmpctx = path = tal_array(ctx, char, len);
168 strcpy(path + len, a);
170 p = strrchr(path, PATH_SEP);
173 olddir = path_pushd(tmpctx, path);
177 /* Make OS canonicalize path for us. */
178 path = path_cwd(tmpctx);
182 /* Append rest of old path. */
185 size_t oldlen = tal_array_length(path);
186 if (path[oldlen-1] != PATH_SEP) {
187 /* Include / to append. */
192 path = tal_realloc(NULL, path, char, oldlen+len+1);
195 memcpy(path + oldlen, p, len+1);
198 path = tal_steal(ctx, path);
200 /* This can happen if old cwd is deleted. */
201 if (!path_popd(olddir))
202 path = tal_free(path);
208 if (unlikely(!a) && is_taken(a))
211 path = tal_arr(ctx, char, PATH_MAX);
212 if (path && !realpath(a, path))
213 path = tal_free(path);
221 /* Symlinks make this hard! */
222 char *path_rel(const tal_t *ctx, const char *from, const char *to)
224 char *cfrom, *cto, *ret, *p;
226 size_t common, num_back, i, postlen;
228 /* This frees from if we're supposed to take it. */
229 tmpctx = cfrom = path_canon(ctx, from);
233 /* From is a directory, so we append / to it. */
234 if (!streq(cfrom, PATH_SEP_STR)) {
235 if (!tal_resize(&cfrom, strlen(cfrom)+2))
238 strcat(cfrom, PATH_SEP_STR);
241 /* This frees to if we're supposed to take it. */
242 cto = path_canon(tmpctx, to);
248 /* How much is in common? */
249 for (common = i = 0; cfrom[i] && cto[i]; i++) {
250 if (cfrom[i] != cto[i])
252 if (cfrom[i] == PATH_SEP)
256 /* Skip over / if matches end of other path. */
257 if (!cfrom[i] && cto[i] == PATH_SEP) {
260 } else if (!cto[i] && cfrom[i] == PATH_SEP) {
265 /* Normalize so strings point past common area. */
269 /* One .. for every path element remaining in 'from', to get
270 * back to common prefix. Then the rest of 'to'. */
271 num_back = strcount(cfrom, PATH_SEP_STR);
272 postlen = strlen(cto) + 1;
274 /* Nothing left? That's ".". */
275 if (num_back == 0 && postlen == 1) {
276 ret = tal_strdup(ctx, ".");
280 ret = tal_arr(ctx, char,
281 strlen(".." PATH_SEP_STR) * num_back + postlen);
285 for (i = 0, p = ret; i < num_back; i++, p += strlen(".." PATH_SEP_STR))
286 memcpy(p, ".." PATH_SEP_STR, strlen(".." PATH_SEP_STR));
287 /* Nothing to append? Trim the final / */
290 memcpy(p, cto, postlen);
303 char *path_readlink(const tal_t *ctx, const char *linkname)
305 ssize_t maxlen = 64; /* good first guess. */
308 if (unlikely(!linkname) && is_taken(linkname))
311 ret = tal_arr(ctx, char, maxlen + 1);
314 ssize_t len = readlink(linkname, ret, maxlen);
324 if (!tal_resize(&ret, maxlen *= 2 + 1))
339 char *path_simplify(const tal_t *ctx, const char *path)
341 size_t i, j, start, len;
345 ret = tal_strdup(ctx, path);
349 /* Always need first / if there is one. */
350 if (ret[0] == PATH_SEP)
355 for (i = j = start; !ended; i += len) {
356 /* Get length of this segment, including terminator. */
357 for (len = 0; ret[i+len] != PATH_SEP; len++) {
365 /* Empty segment is //; ignore first one. */
369 /* Always ignore slashdot. */
370 if (len == 2 && ret[i] == '.')
373 /* .. => remove previous if there is one, unless symlink. */
374 if (len == 3 && ret[i] == '.' && ret[i+1] == '.') {
378 /* eg. /foo/, foo/ or foo/bar/ */
379 assert(ret[j-1] == PATH_SEP);
382 /* Avoid stepping back over ..! */
384 || strends(ret, PATH_SEP_STR"..")) {
389 if (lstat(ret, &st) == 0
390 && !S_ISLNK(st.st_mode)) {
391 char *sep = strrchr(ret, PATH_SEP);
401 /* nul term in case we're at end */
408 memmove(ret + j, ret + i, len);
409 /* Don't count nul terminator. */
413 /* Empty string created by ../ elimination. */
417 } else if (j > 1 && ret[j-1] == PATH_SEP) {
425 char *path_basename(const tal_t *ctx, const char *path)
430 if (unlikely(!path) && taken(path))
433 sep = strrchr(path, PATH_SEP);
435 return tal_strdup(ctx, path);
437 /* Trailing slashes need to be trimmed. */
441 for (end = sep; end != path; end--)
442 if (*end != PATH_SEP)
445 /* Find *previous* / */
446 for (sep = end; sep >= path && *sep != PATH_SEP; sep--);
448 /* All /? Just return / */
450 ret = tal_strdup(ctx, PATH_SEP_STR);
452 ret = tal_strndup(ctx, sep+1, end - sep);
454 ret = tal_strdup(ctx, sep + 1);
461 /* This reuses str if we're to take it. */
462 static char *fixed_string(const tal_t *ctx,
463 const char *str, const char *path)
465 char *ret = tal_dup_arr(ctx, char, path, 0, strlen(str)+1);
471 char *path_dirname(const tal_t *ctx, const char *path)
475 if (unlikely(!path) && taken(path))
478 sep = strrchr(path, PATH_SEP);
480 return fixed_string(ctx, ".", path);
482 /* Trailing slashes need to be trimmed. */
486 for (end = sep; end != path; end--)
487 if (*end != PATH_SEP)
490 /* Find *previous* / */
491 for (sep = end; sep > path && *sep != PATH_SEP; sep--);
494 /* In case there are multiple / in a row. */
495 while (sep > path && sep[-1] == PATH_SEP)
499 if (path_is_abs(path))
500 return tal_strndup(ctx, path, 1);
502 return fixed_string(ctx, ".", path);
504 return tal_strndup(ctx, path, sep - path);
507 bool path_is_abs(const char *path)
509 return path[0] == PATH_SEP;
512 bool path_is_file(const char *path)
516 return stat(path, &st) == 0 && S_ISREG(st.st_mode);
519 bool path_is_dir(const char *path)
523 return stat(path, &st) == 0 && S_ISDIR(st.st_mode);
526 char **path_split(const tal_t *ctx, const char *path)
528 bool empty = path && !path[0];
529 char **ret = tal_strsplit(ctx, path, PATH_SEP_STR, STR_NO_EMPTY);
531 /* Handle the "/" case */
532 if (ret && !empty && !ret[0]) {
533 if (!tal_resize(&ret, 2))
537 ret[0] = tal_strdup(ret, PATH_SEP_STR);
546 size_t path_ext_off(const char *path)
548 const char *dot, *base;
550 dot = strrchr(path, '.');
552 base = strrchr(path, PATH_SEP);