summaryrefslogtreecommitdiff
path: root/firmware
diff options
context:
space:
mode:
Diffstat (limited to 'firmware')
-rw-r--r--firmware/common/pathfuncs.c24
-rw-r--r--firmware/export/pathfuncs.h1
2 files changed, 22 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 */
350size_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)
462size_t path_append_ex(char *buf, const char *basepath, size_t basepath_max, 479size_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 {
diff --git a/firmware/export/pathfuncs.h b/firmware/export/pathfuncs.h
index 1b18f22d06..03495e5ab2 100644
--- a/firmware/export/pathfuncs.h
+++ b/firmware/export/pathfuncs.h
@@ -87,6 +87,7 @@ int make_volume_root(int volume, char *dst);
87int path_strip_drive(const char *name, const char **nameptr, bool greedy); 87int path_strip_drive(const char *name, const char **nameptr, bool greedy);
88size_t path_basename(const char *name, const char **nameptr); 88size_t path_basename(const char *name, const char **nameptr);
89size_t path_dirname(const char *name, const char **nameptr); 89size_t path_dirname(const char *name, const char **nameptr);
90size_t path_strip_leading_separators(const char *name, const char **nameptr);
90size_t path_strip_trailing_separators(const char *name, const char **nameptr); 91size_t path_strip_trailing_separators(const char *name, const char **nameptr);
91void path_correct_separators(char *dstpath, const char *path); 92void path_correct_separators(char *dstpath, const char *path);
92void path_remove_dot_segments(char *dstpath, const char *path); 93void path_remove_dot_segments(char *dstpath, const char *path);