aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2025-08-02 02:21:48 +0900
committernsfisis <nsfisis@gmail.com>2025-08-15 10:06:21 +0900
commit242c8a93d9a754df07c750f63259e3e23d5fc5d4 (patch)
tree2524e714c3ff9fae131c9913a4d8f3b3d311c77e
parent3d64c9165ba8ae6a06ece0817ae246b21f6f661a (diff)
downloadducc-242c8a93d9a754df07c750f63259e3e23d5fc5d4.tar.gz
ducc-242c8a93d9a754df07c750f63259e3e23d5fc5d4.tar.zst
ducc-242c8a93d9a754df07c750f63259e3e23d5fc5d4.zip
feat: do not use hard-coded ducc include path
-rw-r--r--main.c1
-rw-r--r--preprocess.c9
-rw-r--r--std.h6
-rw-r--r--sys.c15
4 files changed, 30 insertions, 1 deletions
diff --git a/main.c b/main.c
index 3d06c02..4b2143d 100644
--- a/main.c
+++ b/main.c
@@ -3,6 +3,7 @@
#include "std.h"
#include "common.c"
#include "io.c"
+#include "sys.c"
#include "preprocess.c"
#include "tokenize.c"
#include "ast.c"
diff --git a/preprocess.c b/preprocess.c
index 8607fdf..6547028 100644
--- a/preprocess.c
+++ b/preprocess.c
@@ -796,9 +796,16 @@ void pp_dump(PpToken* t, int include_whitespace) {
}
}
+char* get_ducc_include_path() {
+ const char* self_dir = get_self_dir();
+ char* buf = calloc(strlen(self_dir) + strlen("/include") + 1, sizeof(char));
+ sprintf(buf, "%s/include", self_dir);
+ return buf;
+}
+
PpToken* do_preprocess(InFile* src, int depth, PpMacros* pp_macros) {
Preprocessor* pp = preprocessor_new(src, depth, pp_macros);
- add_include_path(pp, "/home/ken/src/ducc/include");
+ add_include_path(pp, get_ducc_include_path());
add_include_path(pp, "/usr/include");
pp_tokenize_all(pp);
process_pp_directives(pp);
diff --git a/std.h b/std.h
index ecffad7..7ba993a 100644
--- a/std.h
+++ b/std.h
@@ -38,3 +38,9 @@ int vfprintf(FILE*, const char*, va_list);
#define F_OK 0
#define R_OK 4
int access(const char*, int);
+
+#define PATH_MAX 4096
+
+typedef long ssize_t;
+ssize_t readlink(const char*, char*, size_t);
+char* dirname(char*);
diff --git a/sys.c b/sys.c
new file mode 100644
index 0000000..aa7b13d
--- /dev/null
+++ b/sys.c
@@ -0,0 +1,15 @@
+char* get_self_path() {
+ char* buf = calloc(PATH_MAX, sizeof(char));
+ ssize_t len = readlink("/proc/self/exe", buf, PATH_MAX - 1);
+ if (len == -1 || len == PATH_MAX - 1) {
+ return NULL;
+ }
+ buf[len] = '\0';
+ return buf;
+}
+
+// It returns a path not including final / except for root directory.
+char* get_self_dir() {
+ char* path = get_self_path();
+ return dirname(path);
+}