]> git.ayabusa.dev Git - thoryum.git/commitdiff
Wrote a path lib to be used
authorAyabusa <lebgpub@gmail.com>
Thu, 11 Jun 2026 17:19:06 +0000 (19:19 +0200)
committerAyabusa <lebgpub@gmail.com>
Thu, 11 Jun 2026 17:19:06 +0000 (19:19 +0200)
src/cli.c
src/path.c [new file with mode: 0644]
src/path.h [new file with mode: 0644]

index 0e4803b31a13800d9b5469d1394acc45e07d5267..beeac75b925b1531f8f20210e85491ef38b1d092 100644 (file)
--- 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 (file)
index 0000000..34cd107
--- /dev/null
@@ -0,0 +1,78 @@
+#include "path.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <err.h>
+
+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 (file)
index 0000000..7a64f9f
--- /dev/null
@@ -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