From 7025b8cd324f9fc3dc87fec82db4d329e5e909db Mon Sep 17 00:00:00 2001 From: Lassi Kortela Date: Sun, 18 Aug 2019 13:39:08 +0300 Subject: [PATCH] Split fl_path_exists() into Unix and Windows --- c/builtins.c | 9 +-------- c/ios.c | 1 - c/os.h | 1 + c/os_unix.c | 12 ++++++++++++ c/os_windows.c | 10 ++++++++++ 5 files changed, 24 insertions(+), 9 deletions(-) diff --git a/c/builtins.c b/c/builtins.c index 4cfff47..b574d54 100644 --- a/c/builtins.c +++ b/c/builtins.c @@ -2,7 +2,6 @@ Extra femtoLisp builtin functions */ -#include #include #include @@ -346,17 +345,11 @@ static value_t fl_path_cwd(value_t *args, uint32_t nargs) return FL_T; } -#ifdef _WIN32 -#define stat _stat -#endif static value_t fl_path_exists(value_t *args, uint32_t nargs) { argcount("path.exists?", nargs, 1); char *str = tostring(args[0], "path.exists?"); - struct stat sbuf; - if (stat(str, &sbuf) == -1) - return FL_F; - return FL_T; + return os_path_exists(str) ? FL_T : FL_F; } static value_t fl_os_getenv(value_t *args, uint32_t nargs) diff --git a/c/ios.c b/c/ios.c index 51097bf..f663ba5 100644 --- a/c/ios.c +++ b/c/ios.c @@ -21,7 +21,6 @@ #include #include #include -#include #include #endif diff --git a/c/os.h b/c/os.h index a73076a..34fcfee 100644 --- a/c/os.h +++ b/c/os.h @@ -2,3 +2,4 @@ void path_to_dirname(char *path); void get_cwd(char *buf, size_t size); int set_cwd(char *buf); char *get_exename(char *buf, size_t size); +int os_path_exists(const char *path); diff --git a/c/os_unix.c b/c/os_unix.c index f8b9cd2..84531f3 100644 --- a/c/os_unix.c +++ b/c/os_unix.c @@ -1,5 +1,7 @@ #include +#include + #include #include #include @@ -99,3 +101,13 @@ value_t builtin_user_real_uid(value_t *args, uint32_t nargs) argcount("user-real-uid", nargs, 0); return fixnum(getuid()); } + +int os_path_exists(const char *path) +{ + struct stat st; + + if (stat(path, &st) == -1) { + return FL_F; + } + return FL_T; +} diff --git a/c/os_windows.c b/c/os_windows.c index e10c894..13d809b 100644 --- a/c/os_windows.c +++ b/c/os_windows.c @@ -31,3 +31,13 @@ char *get_exename(char *buf, size_t size) return NULL; return buf; } + +int os_path_exists(const char *path) +{ + struct stat st; + + if (_stat(path, &st) == -1) { + return FL_F; + } + return FL_T; +}