From ae5fee63f160643a397d18e9e0658275606bc4d4 Mon Sep 17 00:00:00 2001 From: Kimplul Date: Tue, 7 Mar 2023 02:18:26 +0200 Subject: initial commit --- src/path.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/path.c (limited to 'src/path.c') diff --git a/src/path.c b/src/path.c new file mode 100644 index 0000000..c9160bb --- /dev/null +++ b/src/path.c @@ -0,0 +1,54 @@ +#include +#include +#include +#include +#include + +#include +#include + +char *ct_basename(const char *file) +{ + size_t l = strlen(file); + size_t n = l - 1; + while (--n) { + if (file[n] == '/') + break; + } + + return strndup(file + n + 1, l - n); +} + +char *ct_dirname(const char *file) +{ + size_t l = strlen(file); + size_t n = l - 1; + while (--n) { + if (file[n] == '/') + break; + } + + return strndup(file, n); +} + +char *ct_cwdname() +{ + size_t size; + long path_max = pathconf(".", _PC_PATH_MAX); + if (path_max == -1) + size = 1024; + else + size = (size_t)path_max; + + char *buf = malloc(size); + if (!buf) + return NULL; + + if (!getcwd(buf, size)) { + error("%s\n", strerror(errno)); + free(buf); + return NULL; + } + + return buf; +} -- cgit v1.3