Split fl_path_exists() into Unix and Windows

This commit is contained in:
Lassi Kortela 2019-08-18 13:39:08 +03:00
parent 3448e8f150
commit 7025b8cd32
5 changed files with 24 additions and 9 deletions

View File

@ -2,7 +2,6 @@
Extra femtoLisp builtin functions Extra femtoLisp builtin functions
*/ */
#include <sys/stat.h>
#include <sys/time.h> #include <sys/time.h>
#include <sys/types.h> #include <sys/types.h>
@ -346,17 +345,11 @@ static value_t fl_path_cwd(value_t *args, uint32_t nargs)
return FL_T; return FL_T;
} }
#ifdef _WIN32
#define stat _stat
#endif
static value_t fl_path_exists(value_t *args, uint32_t nargs) static value_t fl_path_exists(value_t *args, uint32_t nargs)
{ {
argcount("path.exists?", nargs, 1); argcount("path.exists?", nargs, 1);
char *str = tostring(args[0], "path.exists?"); char *str = tostring(args[0], "path.exists?");
struct stat sbuf; return os_path_exists(str) ? FL_T : FL_F;
if (stat(str, &sbuf) == -1)
return FL_F;
return FL_T;
} }
static value_t fl_os_getenv(value_t *args, uint32_t nargs) static value_t fl_os_getenv(value_t *args, uint32_t nargs)

View File

@ -21,7 +21,6 @@
#include <unistd.h> #include <unistd.h>
#include <sys/time.h> #include <sys/time.h>
#include <sys/select.h> #include <sys/select.h>
#include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#endif #endif

1
c/os.h
View File

@ -2,3 +2,4 @@ void path_to_dirname(char *path);
void get_cwd(char *buf, size_t size); void get_cwd(char *buf, size_t size);
int set_cwd(char *buf); int set_cwd(char *buf);
char *get_exename(char *buf, size_t size); char *get_exename(char *buf, size_t size);
int os_path_exists(const char *path);

View File

@ -1,5 +1,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h>
#include <assert.h> #include <assert.h>
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
@ -99,3 +101,13 @@ value_t builtin_user_real_uid(value_t *args, uint32_t nargs)
argcount("user-real-uid", nargs, 0); argcount("user-real-uid", nargs, 0);
return fixnum(getuid()); return fixnum(getuid());
} }
int os_path_exists(const char *path)
{
struct stat st;
if (stat(path, &st) == -1) {
return FL_F;
}
return FL_T;
}

View File

@ -31,3 +31,13 @@ char *get_exename(char *buf, size_t size)
return NULL; return NULL;
return buf; return buf;
} }
int os_path_exists(const char *path)
{
struct stat st;
if (_stat(path, &st) == -1) {
return FL_F;
}
return FL_T;
}