| 469 | } |
| 470 | |
| 471 | char *path_dirname(const tal_t *ctx, const char *path) |
| 472 | { |
| 473 | const char *sep; |
| 474 | |
| 475 | if (unlikely(!path) && taken(path)) |
| 476 | return NULL; |
| 477 | |
| 478 | sep = strrchr(path, PATH_SEP); |
| 479 | if (!sep) |
| 480 | return fixed_string(ctx, ".", path); |
| 481 | |
| 482 | /* Trailing slashes need to be trimmed. */ |
| 483 | if (!sep[1]) { |
| 484 | const char *end; |
| 485 | |
| 486 | for (end = sep; end != path; end--) |
| 487 | if (*end != PATH_SEP) |
| 488 | break; |
| 489 | |
| 490 | /* Find *previous* / */ |
| 491 | for (sep = end; sep > path && *sep != PATH_SEP; sep--); |
| 492 | } |
| 493 | |
| 494 | /* In case there are multiple / in a row. */ |
| 495 | while (sep > path && sep[-1] == PATH_SEP) |
| 496 | sep--; |
| 497 | |
| 498 | if (sep == path) { |
| 499 | if (path_is_abs(path)) |
| 500 | return tal_strndup(ctx, path, 1); |
| 501 | else |
| 502 | return fixed_string(ctx, ".", path); |
| 503 | } |
| 504 | return tal_strndup(ctx, path, sep - path); |
| 505 | } |
| 506 | |
| 507 | bool path_is_abs(const char *path) |
| 508 | { |