| 423 | } |
| 424 | |
| 425 | char *path_basename(const tal_t *ctx, const char *path) |
| 426 | { |
| 427 | const char *sep; |
| 428 | char *ret; |
| 429 | |
| 430 | if (unlikely(!path) && taken(path)) |
| 431 | return NULL; |
| 432 | |
| 433 | sep = strrchr(path, PATH_SEP); |
| 434 | if (!sep) |
| 435 | return tal_strdup(ctx, path); |
| 436 | |
| 437 | /* Trailing slashes need to be trimmed. */ |
| 438 | if (!sep[1]) { |
| 439 | const char *end; |
| 440 | |
| 441 | for (end = sep; end != path; end--) |
| 442 | if (*end != PATH_SEP) |
| 443 | break; |
| 444 | |
| 445 | /* Find *previous* / */ |
| 446 | for (sep = end; sep >= path && *sep != PATH_SEP; sep--); |
| 447 | |
| 448 | /* All /? Just return / */ |
| 449 | if (end == sep) |
| 450 | ret = tal_strdup(ctx, PATH_SEP_STR); |
| 451 | else |
| 452 | ret = tal_strndup(ctx, sep+1, end - sep); |
| 453 | } else |
| 454 | ret = tal_strdup(ctx, sep + 1); |
| 455 | |
| 456 | if (taken(path)) |
| 457 | tal_free(path); |
| 458 | return ret; |
| 459 | } |
| 460 | |
| 461 | /* This reuses str if we're to take it. */ |
| 462 | static char *fixed_string(const tal_t *ctx, |