diff options
Diffstat (limited to 'src/libnm-systemd-shared/src/basic/path-util.c')
-rw-r--r-- | src/libnm-systemd-shared/src/basic/path-util.c | 54 |
1 files changed, 29 insertions, 25 deletions
diff --git a/src/libnm-systemd-shared/src/basic/path-util.c b/src/libnm-systemd-shared/src/basic/path-util.c index bf93990fde..40a819d47d 100644 --- a/src/libnm-systemd-shared/src/basic/path-util.c +++ b/src/libnm-systemd-shared/src/basic/path-util.c @@ -519,17 +519,17 @@ char* path_extend_internal(char **x, ...) { va_list ap; bool slash; - /* 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. + /* 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 the individual strings. * * The first argument may be an already allocated string that is extended via realloc() if * non-NULL. path_extend() and path_join() are macro wrappers around this function, making use of the * first parameter to distinguish the two operations. * - * Note: any listed empty string is simply skipped. This can be useful for concatenating strings of which some - * are optional. + * Note: any listed empty string is simply skipped. This can be useful for concatenating strings of + * which some are optional. * * Examples: * @@ -587,7 +587,7 @@ char* path_extend_internal(char **x, ...) { } static int check_x_access(const char *path, int *ret_fd) { - _cleanup_close_ int fd = -1; + _cleanup_close_ int fd = -EBADF; int r; /* We need to use O_PATH because there may be executables for which we have only exec @@ -615,7 +615,7 @@ static int check_x_access(const char *path, int *ret_fd) { } static int find_executable_impl(const char *name, const char *root, char **ret_filename, int *ret_fd) { - _cleanup_close_ int fd = -1; + _cleanup_close_ int fd = -EBADF; _cleanup_free_ char *path_name = NULL; int r; @@ -1164,31 +1164,35 @@ bool path_is_normalized(const char *p) { return true; } -char *file_in_same_dir(const char *path, const char *filename) { - char *e, *ret; - size_t k; +int file_in_same_dir(const char *path, const char *filename, char **ret) { + _cleanup_free_ char *b = NULL; + int r; assert(path); assert(filename); + assert(ret); - /* This removes the last component of path and appends - * filename, unless the latter is absolute anyway or the - * former isn't */ + /* This removes the last component of path and appends filename, unless the latter is absolute anyway + * or the former isn't */ if (path_is_absolute(filename)) - return strdup(filename); - - e = strrchr(path, '/'); - if (!e) - return strdup(filename); + b = strdup(filename); + else { + _cleanup_free_ char *dn = NULL; - k = strlen(filename); - ret = new(char, (e + 1 - path) + k + 1); - if (!ret) - return NULL; + r = path_extract_directory(path, &dn); + if (r == -EDESTADDRREQ) /* no path prefix */ + b = strdup(filename); + else if (r < 0) + return r; + else + b = path_join(dn, filename); + } + if (!b) + return -ENOMEM; - memcpy(mempcpy(ret, path, e + 1 - path), filename, k + 1); - return ret; + *ret = TAKE_PTR(b); + return 0; } bool hidden_or_backup_file(const char *filename) { |