path-util: allow NULLs in arguments to path_join()
This removes the need to remember to put strempty() in places, thus reducing the likelihood of a stupid mistake.
This commit is contained in:
+11
-17
@@ -488,11 +488,10 @@ char* path_join_internal(const char *first, ...) {
|
||||
bool slash;
|
||||
size_t sz;
|
||||
|
||||
assert(first);
|
||||
|
||||
/* Joins all listed strings until NULL and places an "/" between them unless the strings end/begin already with
|
||||
* one so that it is unnecessary. Note that "/" which are already duplicate won't be removed. The string
|
||||
* returned is hence always equal or longer than the sum of the lengths of each individual string.
|
||||
/* Joins all listed strings until the sentinel and places a "/" between them unless the strings end/begin
|
||||
* already with one so that it is unnecessary. Note that slashes which are already duplicate won't be
|
||||
* removed. The string returned is hence always equal to or longer than the sum of the lengths of each
|
||||
* individual string.
|
||||
*
|
||||
* Note: any listed empty string is simply skipped. This can be useful for concatenating strings of which some
|
||||
* are optional.
|
||||
@@ -503,22 +502,18 @@ char* path_join_internal(const char *first, ...) {
|
||||
* path_join("foo/", "bar") → "foo/bar"
|
||||
* path_join("", "foo", "", "bar", "") → "foo/bar" */
|
||||
|
||||
sz = strlen(first);
|
||||
sz = strlen_ptr(first);
|
||||
va_start(ap, first);
|
||||
while ((p = va_arg(ap, char*))) {
|
||||
|
||||
if (*p == 0) /* Skip empty items */
|
||||
continue;
|
||||
|
||||
sz += 1 + strlen(p);
|
||||
}
|
||||
while ((p = va_arg(ap, char*)) != (const char*) -1)
|
||||
if (!isempty(p))
|
||||
sz += 1 + strlen(p);
|
||||
va_end(ap);
|
||||
|
||||
joined = new(char, sz + 1);
|
||||
if (!joined)
|
||||
return NULL;
|
||||
|
||||
if (first[0] != 0) {
|
||||
if (!isempty(first)) {
|
||||
q = stpcpy(joined, first);
|
||||
slash = endswith(first, "/");
|
||||
} else {
|
||||
@@ -529,9 +524,8 @@ char* path_join_internal(const char *first, ...) {
|
||||
}
|
||||
|
||||
va_start(ap, first);
|
||||
while ((p = va_arg(ap, char*))) {
|
||||
|
||||
if (*p == 0) /* Skip empty items */
|
||||
while ((p = va_arg(ap, char*)) != (const char*) -1) {
|
||||
if (isempty(p))
|
||||
continue;
|
||||
|
||||
if (!slash && p[0] != '/')
|
||||
|
||||
Reference in New Issue
Block a user