aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2024-04-13 11:10:20 +0200
committerMattias Andrée <maandree@kth.se>2024-04-13 11:10:20 +0200
commite2346f713d55f3bcb81c8b724aaa0d9d27b8f6bd (patch)
treef18493a4c3f4a045985563d8c87aabf9f3e10bed
parentAdd assume_aligned to libsimple_getcwd (diff)
downloadlibsimple-e2346f713d55f3bcb81c8b724aaa0d9d27b8f6bd.tar.gz
libsimple-e2346f713d55f3bcb81c8b724aaa0d9d27b8f6bd.tar.bz2
libsimple-e2346f713d55f3bcb81c8b724aaa0d9d27b8f6bd.tar.xz
Add libsimple_abspath with e- and en- versions
Signed-off-by: Mattias Andrée <maandree@kth.se>
-rw-r--r--Makefile3
-rw-r--r--abspath.c44
-rw-r--r--eabspath.c18
-rw-r--r--enabspath.c25
-rw-r--r--libsimple/path.h38
5 files changed, 127 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index 02a409d..d9c2b5a 100644
--- a/Makefile
+++ b/Makefile
@@ -69,6 +69,7 @@ HDR =\
OBJ =\
abs.o\
+ abspath.o\
aligned_allocn.o\
aligned_allocz.o\
aligned_alloczn.o\
@@ -99,6 +100,7 @@ OBJ =\
difftimeval.o\
doubletotimespec.o\
doubletotimeval.o\
+ eabspath.o\
ealigned_alloc.o\
ealigned_allocn.o\
ealigned_allocz.o\
@@ -127,6 +129,7 @@ OBJ =\
ememalignzn.o\
ememalloc.o\
ememdup.o\
+ enabspath.o\
enaligned_alloc.o\
enaligned_allocn.o\
enaligned_allocz.o\
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
diff --git a/eabspath.c b/eabspath.c
new file mode 100644
index 0000000..41b7ea0
--- /dev/null
+++ b/eabspath.c
@@ -0,0 +1,18 @@
+/* See LICENSE file for copyright and license details. */
+#include "common.h"
+#ifndef TEST
+
+
+extern inline char *libsimple_eabspath(const char *, const char *);
+
+
+#else
+#include "test.h"
+
+int
+main(void) /* TODO test */
+{
+ return 0;
+}
+
+#endif
diff --git a/enabspath.c b/enabspath.c
new file mode 100644
index 0000000..039144e
--- /dev/null
+++ b/enabspath.c
@@ -0,0 +1,25 @@
+/* See LICENSE file for copyright and license details. */
+#include "common.h"
+#ifndef TEST
+
+
+char *
+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);
+ return ret;
+}
+
+
+#else
+#include "test.h"
+
+int
+main(void) /* TODO test */
+{
+ return 0;
+}
+
+#endif
diff --git a/libsimple/path.h b/libsimple/path.h
index 0e6ad7b..ace64ac 100644
--- a/libsimple/path.h
+++ b/libsimple/path.h
@@ -16,7 +16,7 @@ char *libsimple_getcwd(void); /* TODO man */
* @return The current working directory
*/
LIBSIMPLE_GCC_ONLY__(__attribute__((__malloc__, __assume_aligned__(1), __warn_unused_result__, __returns_nonnull__)))
-char *libsimple_engetcwd(int status); /* TODO man */
+char *libsimple_engetcwd(int); /* TODO man */
/**
* Version of `libsimple_getcwd` that calls `libsimple_eprintf` on error
@@ -29,3 +29,39 @@ libsimple_egetcwd(void) /* TODO man */
{
return libsimple_engetcwd(libsimple_default_failure_exit);
}
+
+
+/**
+ * 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
+ * @return `path` as an absolute path, or `NULL` on failure
+ */
+LIBSIMPLE_GCC_ONLY__(__attribute__((__malloc__, __assume_aligned__(1), __warn_unused_result__, __nonnull__(1))))
+char *libsimple_abspath(const char *, const char *); /* TODO man */
+
+/**
+ * Version of `libsimple_abspath` that calls `libsimple_enprintf` on error
+ *
+ * @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
+ * @return `path` as an absolute path
+ */
+LIBSIMPLE_GCC_ONLY__(__attribute__((__malloc__, __assume_aligned__(1), __warn_unused_result__, __nonnull__(2), __returns_nonnull__)))
+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
+ * @return `path` as an absolute path
+ */
+LIBSIMPLE_GCC_ONLY__(__attribute__((__malloc__, __assume_aligned__(1), __warn_unused_result__, __nonnull__(1), __returns_nonnull__)))
+inline char *
+libsimple_eabspath(const char *p__, const char *r__) /* TODO man */
+{
+ return libsimple_enabspath(libsimple_default_failure_exit, p__, r__);
+}