aboutsummaryrefslogtreecommitdiffstats
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
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>
-rw-r--r--abspath.c10
-rw-r--r--enabspath.c2
-rw-r--r--libsimple/path.h9
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__)))