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 #define PATH_SEP_STR "/"
15 #define PATH_SEP (PATH_SEP_STR[0])
17 char *path_cwd(const tal_t *ctx)
22 /* *This* is why people hate C. */
23 cwd = tal_arr(ctx, char, len);
24 while (cwd && !getcwd(cwd, len)) {
25 if (errno != ERANGE || !tal_resize(&cwd, len *= 2))
31 char *path_join(const tal_t *ctx, const char *base, const char *a)
36 if (unlikely(!a) && taken(a)) {
42 if (a[0] == PATH_SEP) {
45 return tal_strdup(ctx, a);
48 if (unlikely(!base) && taken(base))
52 ret = tal_dup_arr(ctx, char, base, len, 1 + strlen(a) + 1);
55 if (ret[len-1] != PATH_SEP)
56 ret[len++] = PATH_SEP;
70 static void pushd_destroy(struct path_pushd *pushd)
75 struct path_pushd *path_pushd(const tal_t *ctx, const char *dir)
77 struct path_pushd *old = tal(ctx, struct path_pushd);
82 if (unlikely(!dir) && taken(dir))
85 if (!tal_add_destructor(old, pushd_destroy))
88 old->fd = open(".", O_RDONLY);
91 else if (chdir(dir) != 0)
100 bool path_popd(struct path_pushd *olddir)
102 bool ok = (fchdir(olddir->fd) == 0);
112 struct path_pushd *path_pushd(const tal_t *ctx, const char *dir)
114 struct path_pushd *old = tal(ctx, struct path_pushd);
119 old->olddir = path_cwd(old);
120 if (unlikely(!old->olddir))
122 else if (unlikely(!dir) && is_taken(dir))
124 else if (chdir(dir) != 0)
133 bool path_popd(struct path_pushd *olddir)
135 bool ok = (chdir(olddir->olddir) == 0);
140 #endif /* !HAVE_FCHDIR */
142 char *path_canon(const tal_t *ctx, const char *a)
145 char *oldcwd, *path, *p;
148 struct path_pushd *olddir;
150 /* A good guess as to size. */
152 if (a[0] != PATH_SEP) {
153 tmpctx = oldcwd = path_cwd(ctx);
156 len += strlen(oldcwd) + strlen(PATH_SEP_STR);
158 path = tal_array(tmpctx, char, len);
162 len = strlen(oldcwd);
163 memcpy(path, oldcwd, len);
164 path[len++] = PATH_SEP;
166 tmpctx = path = tal_array(ctx, char, len);
171 strcpy(path + len, a);
173 p = strrchr(path, PATH_SEP);
176 olddir = path_pushd(tmpctx, path);
180 /* Make OS canonicalize path for us. */
181 path = path_cwd(tmpctx);
185 /* Append rest of old path. */
188 size_t oldlen = tal_array_length(path);
189 if (path[oldlen-1] != PATH_SEP) {
190 /* Include / to append. */
195 path = tal_realloc(NULL, path, char, oldlen+len+1);
198 memcpy(path + oldlen, p, len+1);
201 path = tal_steal(ctx, path);
203 /* This can happen if old cwd is deleted. */
204 if (!path_popd(olddir))
205 path = tal_free(path);
211 if (unlikely(!a) && is_taken(a))
214 path = tal_arr(ctx, char, PATH_MAX);
215 if (path && !realpath(a, path))
216 path = tal_free(path);
224 /* Symlinks make this hard! */
225 char *path_rel(const tal_t *ctx, const char *from, const char *to)
227 char *cfrom, *cto, *ret, *p;
229 size_t common, num_back, i, postlen;
231 /* This frees from if we're supposed to take it. */
232 tmpctx = cfrom = path_canon(ctx, from);
236 /* From is a directory, so we append / to it. */
237 if (!streq(cfrom, PATH_SEP_STR)) {
238 if (!tal_resize(&cfrom, strlen(cfrom)+2))
241 strcat(cfrom, PATH_SEP_STR);
244 /* This frees to if we're supposed to take it. */
245 cto = path_canon(tmpctx, to);
251 /* How much is in common? */
252 for (common = i = 0; cfrom[i] && cto[i]; i++) {
253 if (cfrom[i] != cto[i])
255 if (cfrom[i] == PATH_SEP)
259 /* Skip over / if matches end of other path. */
260 if (!cfrom[i] && cto[i] == PATH_SEP) {
263 } else if (!cto[i] && cfrom[i] == PATH_SEP) {
268 /* Normalize so strings point past common area. */
272 /* One .. for every path element remaining in 'from', to get
273 * back to common prefix. Then the rest of 'to'. */
274 num_back = strcount(cfrom, PATH_SEP_STR);
275 postlen = strlen(cto) + 1;
277 /* Nothing left? That's ".". */
278 if (num_back == 0 && postlen == 1) {
279 ret = tal_strdup(ctx, ".");
283 ret = tal_arr(ctx, char,
284 strlen(".." PATH_SEP_STR) * num_back + postlen);
288 for (i = 0, p = ret; i < num_back; i++, p += strlen(".." PATH_SEP_STR))
289 memcpy(p, ".." PATH_SEP_STR, strlen(".." PATH_SEP_STR));
290 /* Nothing to append? Trim the final / */
293 memcpy(p, cto, postlen);
306 char *path_readlink(const tal_t *ctx, const char *linkname)
308 ssize_t len, maxlen = 64; /* good first guess. */
311 if (unlikely(!linkname) && is_taken(linkname))
314 ret = tal_arr(ctx, char, maxlen + 1);
317 len = readlink(linkname, ret, maxlen);
323 if (!tal_resize(&ret, maxlen *= 2 + 1))
341 char *path_simplify(const tal_t *ctx, const char *path)
343 size_t i, j, start, len;
347 ret = tal_strdup(ctx, path);
351 /* Always need first / if there is one. */
352 if (ret[0] == PATH_SEP)
357 for (i = j = start; !ended; i += len) {
358 /* Get length of this segment, including terminator. */
359 for (len = 0; ret[i+len] != PATH_SEP; len++) {
367 /* Empty segment is //; ignore first one. */
371 /* Always ignore slashdot. */
372 if (len == 2 && ret[i] == '.')
375 /* .. => remove previous if there is one, unless symlink. */
376 if (len == 3 && ret[i] == '.' && ret[i+1] == '.') {
380 /* eg. /foo/, foo/ or foo/bar/ */
381 assert(ret[j-1] == PATH_SEP);
384 /* Avoid stepping back over ..! */
386 || strends(ret, PATH_SEP_STR"..")) {
391 if (lstat(ret, &st) == 0
392 && !S_ISLNK(st.st_mode)) {
393 char *sep = strrchr(ret, PATH_SEP);
403 /* nul term in case we're at end */
410 memmove(ret + j, ret + i, len);
411 /* Don't count nul terminator. */
415 /* Empty string created by ../ elimination. */
419 } else if (j > 1 && ret[j-1] == PATH_SEP) {
427 char *path_basename(const tal_t *ctx, const char *path)
432 if (unlikely(!path) && taken(path))
435 sep = strrchr(path, PATH_SEP);
437 return tal_strdup(ctx, path);
439 /* Trailing slashes need to be trimmed. */
443 for (end = sep; end != path; end--)
444 if (*end != PATH_SEP)
447 /* Find *previous* / */
448 for (sep = end; sep >= path && *sep != PATH_SEP; sep--);
450 /* All /? Just return / */
452 ret = tal_strdup(ctx, PATH_SEP_STR);
454 ret = tal_strndup(ctx, sep+1, end - sep);
456 ret = tal_strdup(ctx, sep + 1);
463 /* This reuses str if we're to take it. */
464 static char *fixed_string(const tal_t *ctx,
465 const char *str, const char *path)
467 char *ret = tal_dup_arr(ctx, char, path, 0, strlen(str)+1);
473 char *path_dirname(const tal_t *ctx, const char *path)
477 if (unlikely(!path) && taken(path))
480 sep = strrchr(path, PATH_SEP);
482 return fixed_string(ctx, ".", path);
484 /* Trailing slashes need to be trimmed. */
488 for (end = sep; end != path; end--)
489 if (*end != PATH_SEP)
492 /* Find *previous* / */
493 for (sep = end; sep > path && *sep != PATH_SEP; sep--);
496 /* In case there are multiple / in a row. */
497 while (sep > path && sep[-1] == PATH_SEP)
501 if (path_is_abs(path))
502 return tal_strndup(ctx, path, 1);
504 return fixed_string(ctx, ".", path);
506 return tal_strndup(ctx, path, sep - path);
509 bool path_is_abs(const char *path)
511 return path[0] == PATH_SEP;
514 bool path_is_file(const char *path)
518 return stat(path, &st) == 0 && S_ISREG(st.st_mode);
521 bool path_is_dir(const char *path)
525 return stat(path, &st) == 0 && S_ISDIR(st.st_mode);
528 char **path_split(const tal_t *ctx, const char *path)
530 bool empty = path && !path[0];
531 char **ret = tal_strsplit(ctx, path, PATH_SEP_STR, STR_NO_EMPTY);
533 /* Handle the "/" case */
534 if (ret && !empty && !ret[0]) {
535 if (!tal_resize(&ret, 2))
539 ret[0] = tal_strdup(ret, PATH_SEP_STR);
548 size_t path_ext_off(const char *path)
550 const char *dot, *base;
552 dot = strrchr(path, '.');
554 base = strrchr(path, PATH_SEP);