From 8ea489c16ce6c790084110fcd742150eb59db274 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 13 Apr 2024 10:54:41 +0200 Subject: Add libsimple_getcwd 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 --- Makefile | 4 ++++ egetcwd.c | 18 ++++++++++++++++++ engetcwd.c | 25 +++++++++++++++++++++++++ getcwd.c | 40 ++++++++++++++++++++++++++++++++++++++++ libsimple.h | 1 + libsimple/path.h | 31 +++++++++++++++++++++++++++++++ 6 files changed, 119 insertions(+) create mode 100644 egetcwd.c create mode 100644 engetcwd.c create mode 100644 getcwd.c create mode 100644 libsimple/path.h 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); +} -- cgit v1.2.3-70-g09d2