aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2024-04-13 10:54:41 +0200
committerMattias Andrée <maandree@kth.se>2024-04-13 10:54:41 +0200
commit8ea489c16ce6c790084110fcd742150eb59db274 (patch)
tree6f672ef69379c8929ed7653107f6a7bf3a41ca1b
parentEmpty signed commit (diff)
downloadlibsimple-8ea489c16ce6c790084110fcd742150eb59db274.tar.gz
libsimple-8ea489c16ce6c790084110fcd742150eb59db274.tar.bz2
libsimple-8ea489c16ce6c790084110fcd742150eb59db274.tar.xz
Add libsimple_getcwd with e- and en- versions
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r--Makefile4
-rw-r--r--egetcwd.c18
-rw-r--r--engetcwd.c25
-rw-r--r--getcwd.c40
-rw-r--r--libsimple.h1
-rw-r--r--libsimple/path.h31
6 files changed, 119 insertions, 0 deletions
diff --git a/Makefile b/Makefile
index a66da02..02a409d 100644
--- a/Makefile
+++ b/Makefile
@@ -41,6 +41,7 @@ SUBHDR =\
libsimple/memelem.h\
libsimple/net.h\
libsimple/overflow.h\
+ libsimple/path.h\
libsimple/posix_memalign.h\
libsimple/posix_memalignz.h\
libsimple/printf.h\
@@ -113,6 +114,7 @@ OBJ =\
ealigned_wmemdup.o\
ecalloc.o\
ecallocn.o\
+ egetcwd.o\
egmtime.o\
elocaltime.o\
emalloc.o\
@@ -140,6 +142,7 @@ OBJ =\
enaligned_wmemdup.o\
encalloc.o\
encallocn.o\
+ engetcwd.o\
engmtime.o\
enlocaltime.o\
enmalloc.o\
@@ -231,6 +234,7 @@ OBJ =\
ewcsndup.o\
ewmemdup.o\
generate_seed.o\
+ getcwd.o\
getenv_e.o\
getenv_ne.o\
gmtime.o\
diff --git a/egetcwd.c b/egetcwd.c
new file mode 100644
index 0000000..ac32bab
--- /dev/null
+++ b/egetcwd.c
@@ -0,0 +1,18 @@
+/* See LICENSE file for copyright and license details. */
+#include "common.h"
+#ifndef TEST
+
+
+extern inline char *libsimple_egetcwd(void);
+
+
+#else
+#include "test.h"
+
+int
+main(void) /* TODO test */
+{
+ return 0;
+}
+
+#endif
diff --git a/engetcwd.c b/engetcwd.c
new file mode 100644
index 0000000..e5fce2b
--- /dev/null
+++ b/engetcwd.c
@@ -0,0 +1,25 @@
+/* See LICENSE file for copyright and license details. */
+#include "common.h"
+#ifndef TEST
+
+
+char *
+libsimple_engetcwd(int status)
+{
+ char *ret = libsimple_getcwd();
+ if (!ret)
+ libsimple_enprintf(status, "libsimple_getcwd:");
+ return ret;
+}
+
+
+#else
+#include "test.h"
+
+int
+main(void) /* TODO test */
+{
+ return 0;
+}
+
+#endif
diff --git a/getcwd.c b/getcwd.c
new file mode 100644
index 0000000..38c7bcf
--- /dev/null
+++ b/getcwd.c
@@ -0,0 +1,40 @@
+/* See LICENSE file for copyright and license details. */
+#include "common.h"
+#ifndef TEST
+
+
+char *
+libsimple_getcwd(void)
+{
+ char *cwd = NULL, *new;
+ size_t cwd_size = 0;
+
+ for (;;) {
+ new = realloc(cwd, cwd_size += 512);
+ if (!new)
+ goto error;
+ cwd = new;
+ if (getcwd(cwd, cwd_size))
+ break;
+ if (errno != ERANGE)
+ goto error;
+ }
+
+ return cwd;
+
+error:
+ free(cwd);
+ return NULL;
+}
+
+
+#else
+#include "test.h"
+
+int
+main(void) /* TODO test */
+{
+ return 0;
+}
+
+#endif
diff --git a/libsimple.h b/libsimple.h
index 1d72f46..2b45dfa 100644
--- a/libsimple.h
+++ b/libsimple.h
@@ -168,6 +168,7 @@
#include "libsimple/random.h"
#include "libsimple/abs.h"
#include "libsimple/net.h"
+#include "libsimple/path.h"
/**
diff --git a/libsimple/path.h b/libsimple/path.h
new file mode 100644
index 0000000..99aea28
--- /dev/null
+++ b/libsimple/path.h
@@ -0,0 +1,31 @@
+/* See LICENSE file for copyright and license details. */
+
+
+/**
+ * Get the current working directory
+ *
+ * @return The current working directory, or `NULL` on failure
+ */
+LIBSIMPLE_GCC_ONLY__(__attribute__((__malloc__, __warn_unused_result__)))
+char *libsimple_getcwd(void); /* TODO man */
+
+/**
+ * Version of `libsimple_getcwd` that calls `libsimple_enprintf` on error
+ *
+ * @param status Exit value in case of failure
+ * @return The current working directory
+ */
+LIBSIMPLE_GCC_ONLY__(__attribute__((__malloc__, __warn_unused_result__, __returns_nonnull__)))
+char *libsimple_engetcwd(int status); /* TODO man */
+
+/**
+ * Version of `libsimple_getcwd` that calls `libsimple_eprintf` on error
+ *
+ * @return The current working directory
+ */
+LIBSIMPLE_GCC_ONLY__(__attribute__((__malloc__, __warn_unused_result__, __returns_nonnull__)))
+inline char *
+libsimple_egetcwd(void) /* TODO man */
+{
+ return libsimple_engetcwd(libsimple_default_failure_exit);
+}