From: Brendan Hansen Date: Thu, 13 May 2021 22:30:32 +0000 (-0500) Subject: using non-relative paths in file lookups X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=bb4df1c2008d6eff852f1b480ae1889f9ec3d5bc;p=onyx.git using non-relative paths in file lookups --- diff --git a/bin/onyx b/bin/onyx index 1d0686ec..c8087029 100755 Binary files a/bin/onyx and b/bin/onyx differ diff --git a/include/bh.h b/include/bh.h index 4b3586da..962d1803 100644 --- a/include/bh.h +++ b/include/bh.h @@ -372,6 +372,7 @@ i32 bh_file_read(bh_file* file, void* buffer, isize buff_size); i32 bh_file_write(bh_file* file, void* buffer, isize buff_size); i64 bh_file_size(bh_file* file); b32 bh_file_exists(char const* filename); +char* bh_path_get_full_name(char const* filename, bh_allocator a); #define bh_file_read_contents(allocator_, x) _Generic((x), \ bh_file*: bh_file_read_contents_bh_file, \ @@ -1548,6 +1549,27 @@ b32 bh_file_exists(char const* filename) { return stat(filename, &s) != -1; } +char* bh_path_get_full_name(char const* filename, bh_allocator a) { +#if defined(_BH_WINDOWS) + #error "Not supported." +#elif defined(_BH_LINUX) + char* p = realpath(filename, NULL); + + // Check if the file did not exists. + // :Cleanup should this return NULL? + if (p == NULL) return (char *) filename; + + i32 len = strlen(p); + char* result = bh_alloc_array(a, char, len + 1); + memmove(result, p, len); + result[len] = 0; + + free(p); + + return result; +#endif +} + #endif // ifndef BH_NO_FILE diff --git a/include/small_windows.h b/include/small_windows.h index c00208b0..22d8f7a4 100644 --- a/include/small_windows.h +++ b/include/small_windows.h @@ -358,6 +358,8 @@ GB_DLL_IMPORT BOOL WINAPI GetFileAttributesExW(wchar_t const *path, GET_FILEEX GB_DLL_IMPORT BOOL WINAPI CopyFileW(wchar_t const *old_f, wchar_t const *new_f, BOOL fail_if_exists); GB_DLL_IMPORT BOOL WINAPI MoveFileW(wchar_t const *old_f, wchar_t const *new_f); +GB_DLL_IMPORT DWORD WINAPI GetFullPathNameA(char const *lpFileName, DWORD nBufferLength, char *lpBuffer, char **lpFilePart); + GB_DLL_IMPORT HMODULE WINAPI LoadLibraryA (char const *filename); GB_DLL_IMPORT BOOL WINAPI FreeLibrary (HMODULE module); GB_DLL_IMPORT FARPROC WINAPI GetProcAddress(HMODULE module, char const *name); diff --git a/src/onyx.c b/src/onyx.c index ce3c4b06..ec2e54d5 100644 --- a/src/onyx.c +++ b/src/onyx.c @@ -237,7 +237,7 @@ static char* lookup_included_file(char* filename, char* relative_to) { else bh_snprintf(path, 256, "%s%s", *folder, fn); - if (bh_file_exists(path)) return path; + if (bh_file_exists(path)) return bh_path_get_full_name(path, global_scratch_allocator); } return fn;