diff options
Diffstat (limited to 'firmware/common/pathfuncs.c')
-rw-r--r-- | firmware/common/pathfuncs.c | 29 |
1 files changed, 25 insertions, 4 deletions
diff --git a/firmware/common/pathfuncs.c b/firmware/common/pathfuncs.c index b942fdf022..6b70078eb1 100644 --- a/firmware/common/pathfuncs.c +++ b/firmware/common/pathfuncs.c | |||
@@ -448,6 +448,10 @@ void path_remove_dot_segments (char *dstpath, const char *path) | |||
448 | } | 448 | } |
449 | 449 | ||
450 | /* Appends one path to another, adding separators between components if needed. | 450 | /* Appends one path to another, adding separators between components if needed. |
451 | * basepath_max can be used to truncate the basepath if desired | ||
452 | * NOTE: basepath is truncated after copying to the buffer so there must be enough | ||
453 | * free space for the entirety of the basepath even if the resulting string would fit | ||
454 | * | ||
451 | * Return value and behavior is otherwise as strlcpy so that truncation may be | 455 | * Return value and behavior is otherwise as strlcpy so that truncation may be |
452 | * detected. | 456 | * detected. |
453 | * | 457 | * |
@@ -455,9 +459,11 @@ void path_remove_dot_segments (char *dstpath, const char *path) | |||
455 | * PA_SEP_HARD adds a separator even if the base path is empty | 459 | * PA_SEP_HARD adds a separator even if the base path is empty |
456 | * PA_SEP_SOFT adds a separator only if the base path is not empty | 460 | * PA_SEP_SOFT adds a separator only if the base path is not empty |
457 | */ | 461 | */ |
458 | size_t path_append(char *buf, const char *basepath, | 462 | size_t path_append_ex(char *buf, const char *basepath, size_t basepath_max, |
459 | const char *component, size_t bufsize) | 463 | const char *component, size_t bufsize) |
460 | { | 464 | { |
465 | size_t len; | ||
466 | bool separate = false; | ||
461 | const char *base = basepath && basepath[0] ? basepath : buf; | 467 | const char *base = basepath && basepath[0] ? basepath : buf; |
462 | if (!base) | 468 | if (!base) |
463 | return bufsize; /* won't work to get lengths from buf */ | 469 | return bufsize; /* won't work to get lengths from buf */ |
@@ -474,11 +480,20 @@ size_t path_append(char *buf, const char *basepath, | |||
474 | 480 | ||
475 | /* if basepath is not null or empty, buffer contents are replaced, | 481 | /* if basepath is not null or empty, buffer contents are replaced, |
476 | otherwise buf contains the base path */ | 482 | otherwise buf contains the base path */ |
477 | size_t len = base == buf ? strlen(buf) : strlcpy(buf, basepath, bufsize); | ||
478 | 483 | ||
479 | bool separate = false; | 484 | if (base == buf) |
485 | len = strlen(buf); | ||
486 | else | ||
487 | { | ||
488 | len = strlcpy(buf, basepath, bufsize); | ||
489 | if (basepath_max < len && basepath != component) | ||
490 | { | ||
491 | len = basepath_max; | ||
492 | buf[basepath_max] = '\0'; | ||
493 | } | ||
494 | } | ||
480 | 495 | ||
481 | if (!basepath || !component) | 496 | if (!basepath || !component || basepath_max == 0) |
482 | separate = !len || base[len-1] != PATH_SEPCH; | 497 | separate = !len || base[len-1] != PATH_SEPCH; |
483 | else if (component[0]) | 498 | else if (component[0]) |
484 | separate = len && base[len-1] != PATH_SEPCH; | 499 | separate = len && base[len-1] != PATH_SEPCH; |
@@ -496,6 +511,12 @@ size_t path_append(char *buf, const char *basepath, | |||
496 | return len + strlcpy(buf, component ?: "", bufsize); | 511 | return len + strlcpy(buf, component ?: "", bufsize); |
497 | } | 512 | } |
498 | 513 | ||
514 | |||
515 | size_t path_append(char *buf, const char *basepath, | ||
516 | const char *component, size_t bufsize) | ||
517 | { | ||
518 | return path_append_ex(buf, basepath, -1u, component, bufsize); | ||
519 | } | ||
499 | /* Returns the location and length of the next path component, consuming the | 520 | /* Returns the location and length of the next path component, consuming the |
500 | * input in the process. | 521 | * input in the process. |
501 | * | 522 | * |