commit 5ed012a829b67428e9eea8db141539d2e9bc95fe
parent 812fd2300bf32c2fe713e3abe728b36d9459fe54
Author: Henry Unite <unitehenry@gmail.com>
Date: Thu, 9 Apr 2026 06:00:02 +0000
feat: path root
Diffstat:
2 files changed, 26 insertions(+), 5 deletions(-)
diff --git a/src/path_root.c b/src/path_root.c
@@ -2,14 +2,37 @@
#include <libgen.h>
#include <stdlib.h>
#include <string.h>
+#include <stdio.h>
void path_root(char **root, char *path) {
char *path_copy = strdup(path);
+
if (!path_copy) {
*root = NULL;
return;
}
+
char *dir = dirname(path_copy);
- *root = strdup(dir);
+
+ char *last_dir = malloc(strlen(dir) + 1);
+
+ strcpy(last_dir, dir);
+
+ while (strcmp(dir, ".") != 0) {
+ dir = dirname(path_copy);
+
+ if (strcmp(dir, ".") != 0) {
+ free(last_dir);
+
+ last_dir = malloc(strlen(dir) + 1);
+
+ strcpy(last_dir, dir);
+ }
+ }
+
+ *root = strdup(last_dir);
+
+ free(last_dir);
+
free(path_copy);
-}
-\ No newline at end of file
+}
diff --git a/src/to_write_path.c b/src/to_write_path.c
@@ -25,4 +25,4 @@ void to_write_path(char **write_path, char *read_path) {
snprintf(to_dir, strlen(BUILD_DIRECTORY) + 2, "%s/", BUILD_DIRECTORY);
*write_path = str_replace(*write_path, from_dir, to_dir);
-}
-\ No newline at end of file
+}