diff options
author | Mattias Andrée <maandree@kth.se> | 2024-04-13 11:34:43 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2024-04-13 11:34:43 +0200 |
commit | 53b8e053312ff1753e5b243060716d2612355f16 (patch) | |
tree | 081da1044c3bcdc292532a94b4c2bb78c9be4d68 /abspath.c | |
parent | Add -lm (diff) | |
download | libsimple-53b8e053312ff1753e5b243060716d2612355f16.tar.gz libsimple-53b8e053312ff1753e5b243060716d2612355f16.tar.bz2 libsimple-53b8e053312ff1753e5b243060716d2612355f16.tar.xz |
libsimple_abspath: accept NULL for cwd
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r-- | abspath.c | 10 |
1 files changed, 9 insertions, 1 deletions
@@ -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; } |