MCPcopy Create free account
hub / github.com/ElementsProject/lightning / path_simplify

Function path_simplify

ccan/ccan/tal/path/path.c:351–439  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

349}
350
351char *path_simplify(const tal_t *ctx, const char *path)
352{
353 size_t i, j, start, len;
354 char *ret;
355 bool ended = false;
356
357 ret = tal_strdup(ctx, path);
358 if (!ret)
359 return NULL;
360
361 /* Always need first / if there is one. */
362 if (ret[0] == PATH_SEP)
363 start = 1;
364 else
365 start = 0;
366
367 for (i = j = start; !ended; i += len) {
368 /* Get length of this segment, including terminator. */
369 for (len = 0; ret[i+len] != PATH_SEP; len++) {
370 if (!ret[i+len]) {
371 ended = true;
372 break;
373 }
374 }
375 len++;
376
377 /* Empty segment is //; ignore first one. */
378 if (len == 1)
379 continue;
380
381 /* Always ignore slashdot. */
382 if (len == 2 && ret[i] == '.')
383 continue;
384
385 /* .. => remove previous if there is one, unless symlink. */
386 if (len == 3 && ret[i] == '.' && ret[i+1] == '.') {
387 struct stat st;
388
389 if (j > start) {
390 /* eg. /foo/, foo/ or foo/bar/ */
391 assert(ret[j-1] == PATH_SEP);
392 ret[j-1] = '\0';
393
394 /* Avoid stepping back over ..! */
395 if (streq(ret, "..")
396 || strends(ret, PATH_SEP_STR"..")) {
397 ret[j-1] = PATH_SEP;
398 goto copy;
399 }
400
401 if (lstat(ret, &st) == 0
402 && !S_ISLNK(st.st_mode)) {
403 char *sep = strrchr(ret, PATH_SEP);
404 if (sep)
405 j = sep - ret + 1;
406 else
407 j = 0;
408 continue;

Callers 2

mainFunction · 0.85
relativeFunction · 0.85

Calls 1

strendsFunction · 0.85

Tested by 1

mainFunction · 0.68