diff --git a/src/lib/builtins/basic.c b/src/lib/builtins/basic.c index 5d3ad09..8a6e8fd 100644 --- a/src/lib/builtins/basic.c +++ b/src/lib/builtins/basic.c @@ -52,48 +52,6 @@ static int bin_print(Runtime *runtime, Object **argv, unsigned int argc, Object return 0; } -// Returns the length written in buff (not considering the zero byte) -static size_t getCurrentScriptFolder(Runtime *runtime, char *buff, size_t buffsize) -{ - const char *path = Runtime_GetCurrentScriptAbsolutePath(runtime); - if(path == NULL) { - if(getcwd(buff, sizeof(buffsize)) == NULL) - return 0; - return strlen(buff); - } - - // This following block is a custom implementation - // of [dirname], which doesn't write into the input - // string and is way buggier. It will for sure give - // problems in the future!! - size_t dir_len; - { - // This is buggy code!! - size_t path_len = strlen(path); - assert(path_len > 0); // Not empty - assert(path[0] == '/'); // Is absolute - assert(path[path_len-1] != '/'); // Doesn't end with a slash. - - size_t popped = 0; - while(path[path_len-1-popped] != '/') - popped += 1; - - assert(path_len > popped); - - dir_len = path_len - popped; - - assert(dir_len < path_len); - assert(path[dir_len-1] == '/'); - } - - if(dir_len >= buffsize) - return 0; - - memcpy(buff, path, dir_len); - buff[dir_len] = '\0'; - return dir_len; -} - static int bin_import(Runtime *runtime, Object **argv, unsigned int argc, Object **rets, unsigned int maxretc, Error *error) { assert(argc == 1); @@ -128,7 +86,7 @@ static int bin_import(Runtime *runtime, Object **argv, unsigned int argc, Object } strcpy(full_path, path); } else { - size_t written = getCurrentScriptFolder(runtime, full_path, sizeof(full_path)); + size_t written = Runtime_GetCurrentScriptFolder(runtime, full_path, sizeof(full_path)); if(written == 0) { Error_Report(error, 1, "Internal buffer is too small"); return -1; diff --git a/src/lib/runtime/runtime.c b/src/lib/runtime/runtime.c index 581d09f..4831116 100644 --- a/src/lib/runtime/runtime.c +++ b/src/lib/runtime/runtime.c @@ -28,7 +28,10 @@ ** +--------------------------------------------------------------------------+ */ +#include #include +#include +#include "../utils/path.h" #include "../utils/defs.h" #include "../utils/stack.h" #include "runtime.h" @@ -56,6 +59,49 @@ struct xRuntime { Heap *heap; }; + +// Returns the length written in buff (not considering the zero byte) +size_t Runtime_GetCurrentScriptFolder(Runtime *runtime, char *buff, size_t buffsize) +{ + const char *path = Runtime_GetCurrentScriptAbsolutePath(runtime); + if(path == NULL) { + if(getcwd(buff, sizeof(buffsize)) == NULL) + return 0; + return strlen(buff); + } + + // This following block is a custom implementation + // of [dirname], which doesn't write into the input + // string and is way buggier. It will for sure give + // problems in the future!! + size_t dir_len; + { + // This is buggy code!! + size_t path_len = strlen(path); + assert(path_len > 0); // Not empty + assert(Path_IsAbsolute(path)); // Is absolute + assert(path[path_len-1] != '/'); // Doesn't end with a slash. + + size_t popped = 0; + while(path[path_len-1-popped] != '/') + popped += 1; + + assert(path_len > popped); + + dir_len = path_len - popped; + + assert(dir_len < path_len); + assert(path[dir_len-1] == '/'); + } + + if(dir_len >= buffsize) + return 0; + + memcpy(buff, path, dir_len); + buff[dir_len] = '\0'; + return dir_len; +} + const char *Runtime_GetCurrentScriptAbsolutePath(Runtime *runtime) { Executable *exe = Runtime_GetCurrentExecutable(runtime); diff --git a/src/lib/runtime/runtime.h b/src/lib/runtime/runtime.h index d1e345f..a1812a1 100644 --- a/src/lib/runtime/runtime.h +++ b/src/lib/runtime/runtime.h @@ -51,6 +51,7 @@ Object* Runtime_GetBuiltins(Runtime *runtime); void Runtime_SetBuiltins(Runtime *runtime, Object *builtins); int Runtime_GetCurrentIndex(Runtime *runtime); Executable *Runtime_GetCurrentExecutable(Runtime *runtime); +size_t Runtime_GetCurrentScriptFolder(Runtime *runtime, char *buff, size_t buffsize); const char *Runtime_GetCurrentScriptAbsolutePath(Runtime *runtime); Snapshot *Snapshot_New(Runtime *runtime); void Snapshot_Free(Snapshot *snapshot); diff --git a/src/lib/utils/path.c b/src/lib/utils/path.c new file mode 100644 index 0000000..5b4a995 --- /dev/null +++ b/src/lib/utils/path.c @@ -0,0 +1,36 @@ +#include +#include +#include +#include "path.h" + +_Bool Path_IsAbsolute(const char *path) +{ + return path[0] == '/'; +} + +const char *Path_MakeAbsolute(const char *path, char *buff, size_t buffsize) +{ + assert(path != NULL); + + if(Path_IsAbsolute(path)) + return path; // It's already absolute. + + if(getcwd(buff, buffsize) == NULL) + return NULL; + + size_t written = strlen(buff); + assert(buff[written-1] != '/'); + + if(written+1 >= buffsize) + return NULL; // No space for the / following the cwd. + + buff[written++] = '/'; + + size_t n = strlen(path); + if(written + n >= buffsize) + return NULL; + + memcpy(buff + written, path, n); + buff[written + n] = '\0'; + return buff; +} \ No newline at end of file diff --git a/src/lib/utils/path.h b/src/lib/utils/path.h new file mode 100644 index 0000000..5f0e00d --- /dev/null +++ b/src/lib/utils/path.h @@ -0,0 +1,5 @@ +#ifndef PATH_H +#define PATH_H +_Bool Path_IsAbsolute(const char *path); +const char *Path_MakeAbsolute(const char *path, char *buff, size_t buffsize); +#endif /* PATH_H */ diff --git a/src/lib/utils/source.c b/src/lib/utils/source.c index 709723e..13d3738 100644 --- a/src/lib/utils/source.c +++ b/src/lib/utils/source.c @@ -36,6 +36,7 @@ #include #include #include "source.h" +#include "path.h" struct xSource { char *name; @@ -82,32 +83,6 @@ const char *Source_GetAbsolutePath(Source *src) return NULL; } -const char *makePathAbsolute(const char *path, char *buff, size_t buffsize) -{ - if(path[0] == '/') - // It's already absolute. - return path; - - if(getcwd(buff, buffsize) == NULL) - return NULL; - - size_t written = strlen(buff); - assert(buff[written-1] != '/'); - - if(written+1 >= buffsize) - return NULL; // No space for the / following the cwd. - - buff[written++] = '/'; - - size_t n = strlen(path); - if(written + n >= buffsize) - return NULL; - - memcpy(buff + written, path, n); - buff[written + n] = '\0'; - return buff; -} - Source *Source_FromFile(const char *file, Error *error) { assert(file != NULL); @@ -118,7 +93,7 @@ Source *Source_FromFile(const char *file, Error *error) } char maybe[1024]; - const char *abs_path = makePathAbsolute(file, maybe, sizeof(maybe)); + const char *abs_path = Path_MakeAbsolute(file, maybe, sizeof(maybe)); if(abs_path == NULL) { Error_Report(error, 0, "Internal buffer is too small"); return NULL;