diff options
author | William Wilgus <wilgus.william@gmail.com> | 2024-03-29 16:09:56 -0400 |
---|---|---|
committer | William Wilgus <me.theuser@yahoo.com> | 2024-03-30 11:01:53 -0400 |
commit | fe00906abb355a40888e7a2ce785ddfe3cfc525f (patch) | |
tree | 78ebfcc04d1a540636d3574eb3f27925fa88a4bd /firmware/common | |
parent | 777098fca9765900202d8d9c1698c57f136c7338 (diff) | |
download | rockbox-fe00906abb355a40888e7a2ce785ddfe3cfc525f.tar.gz rockbox-fe00906abb355a40888e7a2ce785ddfe3cfc525f.zip |
pathfuncs.c add path_strip_leading_separators()
added to path_append as well
Change-Id: Ieb6ec4f4c475ca5e60c8246c7f044bcc7651f6bf
Diffstat (limited to 'firmware/common')
-rw-r--r-- | firmware/common/pathfuncs.c | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/firmware/common/pathfuncs.c b/firmware/common/pathfuncs.c index fa296cc2ed..e9218ea2c7 100644 --- a/firmware/common/pathfuncs.c +++ b/firmware/common/pathfuncs.c | |||
@@ -339,6 +339,23 @@ size_t path_dirname(const char *name, const char **nameptr) | |||
339 | return q - name; | 339 | return q - name; |
340 | } | 340 | } |
341 | 341 | ||
342 | /* Removes leading separators from a path | ||
343 | * "" *nameptr->NUL, count=0: "" | ||
344 | * "/" *nameptr->/, count=1: "/" | ||
345 | * "//" *nameptr->2nd /, count=2: "/" | ||
346 | * "a/" *nameptr->a/, count=0: "a/" | ||
347 | * "//b//" *nameptr->2nd /, count=2: "/b//" | ||
348 | * "/c/" *nameptr->/, count=1: "/c/" | ||
349 | */ | ||
350 | size_t path_strip_leading_separators(const char *name, const char **nameptr) | ||
351 | { | ||
352 | const char *p = name; | ||
353 | *nameptr = p; | ||
354 | while (*(p) == PATH_SEPCH && *(++p) == PATH_SEPCH) | ||
355 | *nameptr = p; | ||
356 | return p - name; | ||
357 | } | ||
358 | |||
342 | /* Removes trailing separators from a path | 359 | /* Removes trailing separators from a path |
343 | * "" *nameptr->NUL, len=0: "" | 360 | * "" *nameptr->NUL, len=0: "" |
344 | * "/" *nameptr->/, len=1: "/" | 361 | * "/" *nameptr->/, len=1: "/" |
@@ -462,7 +479,7 @@ void path_remove_dot_segments (char *dstpath, const char *path) | |||
462 | size_t path_append_ex(char *buf, const char *basepath, size_t basepath_max, | 479 | size_t path_append_ex(char *buf, const char *basepath, size_t basepath_max, |
463 | const char *component, size_t bufsize) | 480 | const char *component, size_t bufsize) |
464 | { | 481 | { |
465 | size_t len; | 482 | size_t len = 0; |
466 | bool separate = false; | 483 | bool separate = false; |
467 | const char *base = basepath && basepath[0] ? basepath : buf; | 484 | const char *base = basepath && basepath[0] ? basepath : buf; |
468 | if (!base) | 485 | if (!base) |
@@ -471,7 +488,7 @@ size_t path_append_ex(char *buf, const char *basepath, size_t basepath_max, | |||
471 | if (!buf) | 488 | if (!buf) |
472 | bufsize = 0; | 489 | bufsize = 0; |
473 | 490 | ||
474 | if (path_is_absolute(component)) | 491 | if (path_is_absolute(component)) /* starts with a '/' path separator */ |
475 | { | 492 | { |
476 | /* 'component' is absolute; replace all */ | 493 | /* 'component' is absolute; replace all */ |
477 | basepath = component; | 494 | basepath = component; |
@@ -484,8 +501,9 @@ size_t path_append_ex(char *buf, const char *basepath, size_t basepath_max, | |||
484 | 501 | ||
485 | if (base == buf) | 502 | if (base == buf) |
486 | len = strlen(buf); | 503 | len = strlen(buf); |
487 | else | 504 | else if (basepath) |
488 | { | 505 | { |
506 | path_strip_leading_separators(basepath, &basepath); | ||
489 | len = strlcpy(buf, basepath, bufsize); | 507 | len = strlcpy(buf, basepath, bufsize); |
490 | if (basepath_max < len) | 508 | if (basepath_max < len) |
491 | { | 509 | { |