From 53b8e053312ff1753e5b243060716d2612355f16 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 13 Apr 2024 11:34:43 +0200 Subject: libsimple_abspath: accept NULL for cwd MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- abspath.c | 10 +++++++++- enabspath.c | 2 +- libsimple/path.h | 9 ++++++--- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/abspath.c b/abspath.c index 60454ff..57cd644 100644 --- a/abspath.c +++ b/abspath.c @@ -8,7 +8,7 @@ libsimple_abspath(const char *path, const char *relto) { size_t size; int add_slash; - char *p, *ret; + char *p, *ret, *relto_free = NULL; if (*path == '/') { ret = strdup(path); @@ -18,6 +18,13 @@ libsimple_abspath(const char *path, const char *relto) while (path[0] == '.' && path[1] == '/') path = &path[2]; + if (relto) { + relto_free = libsimple_getcwd(); + if (!relto_free) + return NULL; + relto = relto_free; + } + add_slash = (strchr(relto, '\0')[-1] != '/'); size = strlen(relto) + strlen(path) + (size_t)(1 + add_slash); @@ -28,6 +35,7 @@ libsimple_abspath(const char *path, const char *relto) *p++ = '/'; stpcpy(p, path); } + free(relto_free); return ret; } diff --git a/enabspath.c b/enabspath.c index 039144e..528f9a9 100644 --- a/enabspath.c +++ b/enabspath.c @@ -8,7 +8,7 @@ libsimple_enabspath(int status, const char *path, const char *relto) { char *ret = libsimple_abspath(path, relto); if (!ret) - libsimple_enprintf(status, "libsimple_abspath %s %s:", path, relto); + libsimple_enprintf(status, "libsimple_abspath %s %s:", path, relto ? relto : "NULL"); return ret; } diff --git a/libsimple/path.h b/libsimple/path.h index ace64ac..65f2751 100644 --- a/libsimple/path.h +++ b/libsimple/path.h @@ -35,7 +35,8 @@ libsimple_egetcwd(void) /* TODO man */ * Turn a path into an absolute path if it is a relative path * * @param path The path to transform - * @param relto The directory `path` is relative to if it is a relative path + * @param relto The directory `path` is relative to if it is a relative path, + * `NULL` for the current working directory * @return `path` as an absolute path, or `NULL` on failure */ LIBSIMPLE_GCC_ONLY__(__attribute__((__malloc__, __assume_aligned__(1), __warn_unused_result__, __nonnull__(1)))) @@ -46,7 +47,8 @@ char *libsimple_abspath(const char *, const char *); /* TODO man */ * * @param status Exit value in case of failure * @param path The path to transform - * @param relto The directory `path` is relative to if it is a relative path + * @param relto The directory `path` is relative to if it is a relative path, + * `NULL` for the current working directory * @return `path` as an absolute path */ LIBSIMPLE_GCC_ONLY__(__attribute__((__malloc__, __assume_aligned__(1), __warn_unused_result__, __nonnull__(2), __returns_nonnull__))) @@ -56,7 +58,8 @@ char *libsimple_enabspath(int, const char *, const char *); /* TODO man */ * Version of `libsimple_abspath` that calls `libsimple_eprintf` on error * * @param path The path to transform - * @param relto The directory `path` is relative to if it is a relative path + * @param relto The directory `path` is relative to if it is a relative path, + * `NULL` for the current working directory * @return `path` as an absolute path */ LIBSIMPLE_GCC_ONLY__(__attribute__((__malloc__, __assume_aligned__(1), __warn_unused_result__, __nonnull__(1), __returns_nonnull__))) -- cgit v1.2.3-70-g09d2