aboutsummaryrefslogtreecommitdiffstats
path: root/abspath.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2024-04-13 11:34:43 +0200
committerMattias Andrée <maandree@kth.se>2024-04-13 11:34:43 +0200
commit53b8e053312ff1753e5b243060716d2612355f16 (patch)
tree081da1044c3bcdc292532a94b4c2bb78c9be4d68 /abspath.c
parentAdd -lm (diff)
downloadlibsimple-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.c10
1 files changed, 9 insertions, 1 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;
}