From e2346f713d55f3bcb81c8b724aaa0d9d27b8f6bd Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 13 Apr 2024 11:10:20 +0200 Subject: Add libsimple_abspath with e- and en- versions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- abspath.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 abspath.c (limited to 'abspath.c') diff --git a/abspath.c b/abspath.c new file mode 100644 index 0000000..60454ff --- /dev/null +++ b/abspath.c @@ -0,0 +1,44 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" +#ifndef TEST + + +char * +libsimple_abspath(const char *path, const char *relto) +{ + size_t size; + int add_slash; + char *p, *ret; + + if (*path == '/') { + ret = strdup(path); + return ret; + } + + while (path[0] == '.' && path[1] == '/') + path = &path[2]; + + add_slash = (strchr(relto, '\0')[-1] != '/'); + size = strlen(relto) + strlen(path) + (size_t)(1 + add_slash); + + ret = malloc(size); + if (ret) { + p = stpcpy(ret, relto); + if (add_slash) + *p++ = '/'; + stpcpy(p, path); + } + return ret; +} + + +#else +#include "test.h" + +int +main(void) /* TODO test */ +{ + return 0; +} + +#endif -- cgit v1.2.3-70-g09d2 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(-) (limited to 'abspath.c') 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 From 84b6dbef3adccc3eb588afb5d48d9c4fc0c2a3e7 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 1 Jul 2024 19:49:10 +0200 Subject: Fix libsimple_abspath MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- abspath.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'abspath.c') diff --git a/abspath.c b/abspath.c index 57cd644..7cc936b 100644 --- a/abspath.c +++ b/abspath.c @@ -18,7 +18,7 @@ libsimple_abspath(const char *path, const char *relto) while (path[0] == '.' && path[1] == '/') path = &path[2]; - if (relto) { + if (!relto) { relto_free = libsimple_getcwd(); if (!relto_free) return NULL; -- cgit v1.2.3-70-g09d2