From 381dd1ce98f0d1caab73ee9ae6d7a9cf9d597c6f Mon Sep 17 00:00:00 2001 From: Leah Neukirchen Date: Mon, 9 Mar 2020 17:14:45 +0100 Subject: [PATCH] Add os/lstat. --- src/core/os.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/core/os.c b/src/core/os.c index 5a674ae2..c05c2287 100644 --- a/src/core/os.c +++ b/src/core/os.c @@ -911,7 +911,7 @@ static const struct OsStatGetter os_stat_getters[] = { {NULL, NULL} }; -static Janet os_stat(int32_t argc, Janet *argv) { +static Janet os_stat_or_lstat(int do_lstat, int32_t argc, Janet *argv) { janet_arity(argc, 1, 2); const char *path = janet_getcstring(argv, 0); JanetTable *tab = NULL; @@ -930,11 +930,17 @@ static Janet os_stat(int32_t argc, Janet *argv) { /* Build result */ #ifdef JANET_WINDOWS + (void) do_lstat; struct _stat st; int res = _stat(path, &st); #else struct stat st; - int res = stat(path, &st); + int res; + if (do_lstat) { + res = lstat(path, &st); + } else { + res = stat(path, &st); + } #endif if (-1 == res) { return janet_wrap_nil(); @@ -957,6 +963,14 @@ static Janet os_stat(int32_t argc, Janet *argv) { } } +static Janet os_stat(int32_t argc, Janet *argv) { + return os_stat_or_lstat(0, argc, argv); +} + +static Janet os_lstat(int32_t argc, Janet *argv) { + return os_stat_or_lstat(1, argc, argv); +} + static Janet os_chmod(int32_t argc, Janet *argv) { janet_fixarity(argc, 2); const char *path = janet_getcstring(argv, 0); @@ -1085,6 +1099,11 @@ static const JanetReg os_cfuns[] = { "\t:changed - timestamp when file last chnaged (permissions changed)\n" "\t:modified - timestamp when file last modified (content changed)\n") }, + { + "os/lstat", os_lstat, + JDOC("(os/lstat path &opt tab|key)\n\n" + "Like os/stat, but don't follow symlinks.\n") + }, { "os/chmod", os_chmod, JDOC("(os/chmod path mode)\n\n"