From: Lukas Krickl Date: Sat, 9 Dec 2023 17:26:48 +0000 (+0100) Subject: WIP: path resolver X-Git-Url: https://git.krickl.dev/?a=commitdiff_plain;h=dca0cf9469a0bd5f4a6873bf596a710f9866a16e;p=ulas%2F.git WIP: path resolver --- diff --git a/include/ulas.h b/include/ulas.h index 2b6d11f..c57f73a 100644 --- a/include/ulas.h +++ b/include/ulas.h @@ -7,6 +7,7 @@ // if this is used as a path use stdin or stdout instead #define ULAS_STDFILEPATH "-" +#define ULAS_PATHSEP "/" #define ULAS_SYMNAMEMAX 256 #define ULAS_PATHMAX 4096 @@ -411,7 +412,7 @@ extern struct ulas_config ulascfg; struct ulas_config ulas_cfg_from_env(void); void ulas_init(struct ulas_config cfg); void ulas_free(void); -FILE* ulas_incpathfopen(const char *path, const char *mode); +FILE *ulas_incpathfopen(const char *path, const char *mode); int ulas_main(struct ulas_config cfg); diff --git a/src/ulas.c b/src/ulas.c index ccb82c0..a6b2758 100644 --- a/src/ulas.c +++ b/src/ulas.c @@ -62,12 +62,36 @@ void ulas_free(void) { ulas_preprocfree(&ulas.pp); } +FILE *ulas_incpathfopen(const char *path, const char *mode) { + char pathbuf[ULAS_PATHMAX]; + memset(pathbuf, 0, ULAS_PATHMAX); + + for (int i = 0; i < ulas.include_paths_len; i++) { + pathbuf[0] = '\0'; + char *ip = ulas.include_paths[i]; + int len = strlen(ip); + + strcat(pathbuf, ip); + if (ip[len-1] != ULAS_PATHSEP[0]) { + strcat(pathbuf, ULAS_PATHSEP); + } + strcat(pathbuf, path); -FILE* ulas_incpathfopen(const char *path, const char *mode) { - // TODO: loop inc-paths + FILE *f = fopen(pathbuf, mode); + if (f != NULL) { + return f; + } + } + + // TODO: loop inc-paths // lastly check . - return NULL; + FILE *f = fopen(path, mode); + if (f == NULL) { + ULASERR("%s: %s\n", path, strerror(errno)); + } + + return f; } int ulas_icntr(void) { return ulas.icntr++; }