From 7e810418c0ada687f8b4e28f7e8dcd8120707dde Mon Sep 17 00:00:00 2001 From: Ayabusa Date: Thu, 11 Jun 2026 19:19:06 +0200 Subject: [PATCH] Wrote a path lib to be used --- src/cli.c | 1 + src/path.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/path.h | 10 +++++++ 3 files changed, 89 insertions(+) create mode 100644 src/path.c create mode 100644 src/path.h diff --git a/src/cli.c b/src/cli.c index 0e4803b..beeac75 100644 --- a/src/cli.c +++ b/src/cli.c @@ -68,6 +68,7 @@ void print_help() { printf( "Usage: thoryum [options]\n" "Thoryum is an install helper, and update wrapper for the Thorium browser.\n" + "Made by Ayabusa, in 2026\n" "\n" "Options:\n" " -h\n" diff --git a/src/path.c b/src/path.c new file mode 100644 index 0000000..34cd107 --- /dev/null +++ b/src/path.c @@ -0,0 +1,78 @@ +#include "path.h" +#include +#include +#include +#include + +path init_path(const char *pth) { + if (!pth) + return NULL; + path ret = strdup(pth); + if (!ret) + err(1, "Failed to allocate memory in init_path, with '%s', aborting.\n", pth); + return ret; +} + +void free_path(path *pth) { + if (pth && *pth) { + free(*pth); + *pth = NULL; + } +} + +path cat_path(path dst, path suffix) { + if (!dst) + return suffix; + else if (!suffix) + return dst; + + int dst_n = strlen(dst); + int suffix_n = strlen(suffix); + path new_dst = realloc(dst, dst_n + suffix_n + 2); // 2 because '\0' and / between the two if needed + if (!new_dst) + err(1, "Failed to reallocate memory in cat_path with dst='%s' and suffix='%s'", dst, suffix); + else + dst = new_dst; + + if (dst[dst_n - 1] != '/') { + dst[dst_n] = '/'; + dst[dst_n + 1] = '\0'; + dst_n++; + } + if (suffix[0] == '/') + suffix++; + + strcpy(dst + dst_n, suffix); + return dst; +} + +// Tests +// cc -Wall -Wextra -g -fsanitize=address path.c +/* +int main() { + { + path pth = init_path("/bin/"); + pth = cat_path(pth, "/hello/world"); + printf("%s\n", pth); // '/bin/hello/world' + free_path(&pth); + } + { + path pth = init_path("/bin"); + pth = cat_path(pth, "hello/world"); + printf("%s\n", pth); // '/bin/hello/world' + free_path(&pth); + } + { + path pth = init_path("/bin"); + pth = cat_path(pth, "/hello/world"); + printf("%s\n", pth); // '/bin/hello/world' + free_path(&pth); + } + { + path pth = init_path("/bin"); + pth = cat_path(pth, "/hello/world"); + printf("%s\n", pth); // '/bin/hello/world' + free_path(&pth); + } +} +*/ diff --git a/src/path.h b/src/path.h new file mode 100644 index 0000000..7a64f9f --- /dev/null +++ b/src/path.h @@ -0,0 +1,10 @@ +#ifndef PATH_H +#define PATH_H + +typedef char *path; + +path init_path(const char *pth); +void free_path(path *pth); +path cat_path(path dst, const path suffix); + +#endif -- 2.43.0