From 2323ca6c9ec3c8ee76b9acf13745b80b92952a6a Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 18 Mar 2019 23:47:45 +0100 Subject: Add struct, import caching, binop ops etc --- src/std/file.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) (limited to 'src/std/file.c') diff --git a/src/std/file.c b/src/std/file.c index 177ccbc..7de2b6c 100644 --- a/src/std/file.c +++ b/src/std/file.c @@ -6,6 +6,8 @@ #include #include #include +#include +#include #include #include @@ -177,7 +179,7 @@ typedef enum { static CHECK_RESULT int file_get_type(const char *filepath, FileType *type) { struct stat file_stats; if(stat(filepath, &file_stats) == -1) { - amal_log_perror(filepath); + amal_log_error("file_get_type: %s failed: ", filepath, strerror(errno)); return -1; } @@ -202,12 +204,11 @@ int read_whole_file(const char *filepath, char **data, usize *size) { return -2; } - result = 0; file = fopen(filepath, "rb"); if(!file) { int error; error = errno; - amal_log_perror(filepath); + amal_log_error("read_whole_file: %s failed: ", filepath, strerror(error)); return error; } @@ -241,6 +242,21 @@ int read_whole_file(const char *filepath, char **data, usize *size) { return result; } +int file_get_canonical_path(const char *filepath, char **result_path, usize *result_path_size) { + char result_path_tmp[PATH_MAX]; + if(!realpath(filepath, result_path_tmp)) { + int ret; + ret = errno; + amal_log_error("file_get_canonical_path: realpath failed for path %s, error: %s", filepath, strerror(ret)); + return ret; + } + *result_path_size = strnlen(result_path_tmp, PATH_MAX); + return_if_error(am_malloc(*result_path_size + 1, (void**)result_path)); + am_memcpy(*result_path, result_path_tmp, *result_path_size); + (*result_path)[*result_path_size] = '\0'; + return 0; +} + BufferView file_get_parent_directory(BufferView filepath) { int i; for(i = filepath.size - 1; i >= 0; --i) { -- cgit v1.2.3