diff options
| author | 2024-09-04 16:39:14 -0400 | |
|---|---|---|
| committer | 2024-09-04 16:39:14 -0400 | |
| commit | 65d523bd0417d5cf9b9b20167ee5754eb7b5e791 (patch) | |
| tree | 5870d53c7c62f58bcb75ed09143575c8ee67525a /src/lua_api.c | |
| parent | command history scrolling implemented (diff) | |
added api functions for isFile, isDirectory, isReadable, and isWriteable
Diffstat (limited to 'src/lua_api.c')
| -rw-r--r-- | src/lua_api.c | 176 |
1 files changed, 113 insertions, 63 deletions
diff --git a/src/lua_api.c b/src/lua_api.c index 1040478..ef5bf9a 100644 --- a/src/lua_api.c +++ b/src/lua_api.c @@ -26,6 +26,7 @@ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <sys/stat.h> #include <unistd.h> // global for checking if debug_mode is toggled @@ -83,6 +84,36 @@ static int execute_command(lua_State *L, const char *line) { return status; } +static char *get_expanded_path(const char *check_item) { + uid_t uid = getuid(); + struct passwd *pw = getpwuid(uid); + if (!pw) { + perror("retrieve home dir"); + return NULL; + } + + if (check_item == NULL) { + // passed nothing + return NULL; + } + + char path[PATH_MAX]; + char *tilda = strchr(check_item, '~'); + if (tilda) { + strcpy(path, pw->pw_dir); + strcat(path, tilda + 1); + } else { + strcpy(path, check_item); + } + char *exp_path = realpath(path, NULL); + // if the path doesnt exist + if (!exp_path) { + return NULL; + } + + return exp_path; +} + // -- Lua wrappers -- static int l_execute_command(lua_State *L) { const char *command = luaL_checkstring(L, 1); @@ -115,84 +146,95 @@ static int l_debug(lua_State *L) { } static int l_cd(lua_State *L) { - bool rc; - uid_t uid = getuid(); - struct passwd *pw = getpwuid(uid); - if (!pw) { - perror("retrieve home dir"); - rc = false; - lua_pushboolean(L, rc); + const char *newdir = luaL_checkstring(L, 1); + char *exp_path = get_expanded_path(newdir); + // if the path doesnt exist + if (exp_path == NULL) { + lua_pushboolean(L, false); return 1; } - const char *newdir = luaL_checkstring(L, 1); - if (newdir == NULL) { - if (chdir(pw->pw_dir) != 0) { - perror("lush: cd"); - } - } else { - char path[PATH_MAX]; - char extended_path[PATH_MAX]; - char *tilda = strchr(newdir, '~'); - if (tilda) { - strcpy(path, pw->pw_dir); - strcat(path, tilda + 1); - } else { - strcpy(path, newdir); - } - char *exp_path = realpath(path, extended_path); - if (!exp_path) { - perror("realpath"); - rc = false; - lua_pushboolean(L, rc); - return 1; - } - if (chdir(exp_path) != 0) { - perror("lush: cd"); - } + if (chdir(exp_path) != 0) { + perror("lush: cd"); + free(exp_path); + lua_pushboolean(L, false); } - rc = true; - lua_pushboolean(L, rc); + + lua_pushboolean(L, true); + free(exp_path); return 1; } static int l_exists(lua_State *L) { - bool rc; - uid_t uid = getuid(); - struct passwd *pw = getpwuid(uid); - if (!pw) { - perror("retrieve home dir"); - rc = false; - lua_pushboolean(L, rc); + const char *check_item = luaL_checkstring(L, 1); + char *exp_path = get_expanded_path(check_item); + // if the path doesnt exist + if (exp_path == NULL) { + lua_pushboolean(L, false); return 1; } + lua_pushboolean(L, true); + free(exp_path); + return 1; +} +static int l_is_file(lua_State *L) { const char *check_item = luaL_checkstring(L, 1); - if (check_item == NULL) { - // passed nothing - rc = false; - lua_pushboolean(L, rc); + char *exp_path = get_expanded_path(check_item); + // if the path doesnt exist + if (exp_path == NULL) { + lua_pushboolean(L, false); + return 1; + } + struct stat path_stat; + stat(exp_path, &path_stat); + bool rc = S_ISREG(path_stat.st_mode); + lua_pushboolean(L, rc); + free(exp_path); + return 1; +} + +static int l_is_dir(lua_State *L) { + const char *check_item = luaL_checkstring(L, 1); + char *exp_path = get_expanded_path(check_item); + // if the path doesnt exist + if (exp_path == NULL) { + lua_pushboolean(L, false); + return 1; + } + struct stat path_stat; + stat(exp_path, &path_stat); + bool rc = S_ISDIR(path_stat.st_mode); + lua_pushboolean(L, rc); + free(exp_path); + return 1; +} + +static int l_is_readable(lua_State *L) { + const char *check_item = luaL_checkstring(L, 1); + char *exp_path = get_expanded_path(check_item); + // if the path doesnt exist + if (exp_path == NULL) { + lua_pushboolean(L, false); + return 1; + } + bool rc = access(exp_path, R_OK) == 0; + lua_pushboolean(L, rc); + free(exp_path); + return 1; +} + +static int l_is_writeable(lua_State *L) { + const char *check_item = luaL_checkstring(L, 1); + char *exp_path = get_expanded_path(check_item); + // if the path doesnt exist + if (exp_path == NULL) { + lua_pushboolean(L, false); return 1; - } else { - char path[PATH_MAX]; - char extended_path[PATH_MAX]; - char *tilda = strchr(check_item, '~'); - if (tilda) { - strcpy(path, pw->pw_dir); - strcat(path, tilda + 1); - } else { - strcpy(path, check_item); - } - char *exp_path = realpath(path, extended_path); - // if the path doesnt exist - if (!exp_path) { - rc = false; - lua_pushboolean(L, rc); - return 1; - } } - rc = true; + bool rc = access(exp_path, W_OK) == 0; lua_pushboolean(L, rc); + free(exp_path); return 1; } @@ -212,6 +254,14 @@ void lua_register_api(lua_State *L) { lua_setfield(L, -2, "cd"); lua_pushcfunction(L, l_exists); lua_setfield(L, -2, "exists"); + lua_pushcfunction(L, l_is_file); + lua_setfield(L, -2, "isFile"); + lua_pushcfunction(L, l_is_dir); + lua_setfield(L, -2, "isDirectory"); + lua_pushcfunction(L, l_is_readable); + lua_setfield(L, -2, "isReadable"); + lua_pushcfunction(L, l_is_writeable); + lua_setfield(L, -2, "isWriteable"); // set the table as global lua_setglobal(L, "lush"); } |
