aboutsummaryrefslogtreecommitdiffstats
path: root/src/lua_api.c
diff options
context:
space:
mode:
authorGravatar BanceDev 2024-09-04 09:04:00 -0400
committerGravatar BanceDev 2024-09-04 09:04:00 -0400
commiteea119487cfbaa5ca693aa1d8ac162d0238bf31e (patch)
treec3476115be1e96bf0093d36ff09a134b7ae98e63 /src/lua_api.c
parentadded missing lua registrations (diff)
added exists function to lua api
Diffstat (limited to '')
-rw-r--r--src/lua_api.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/lua_api.c b/src/lua_api.c
index 4dc2149..9f4bdc8 100644
--- a/src/lua_api.c
+++ b/src/lua_api.c
@@ -154,6 +154,46 @@ static int l_cd(lua_State *L) {
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);
+ return 1;
+ }
+
+ const char *check_item = luaL_checkstring(L, 1);
+ if (check_item == NULL) {
+ // passed nothing
+ rc = false;
+ lua_pushboolean(L, rc);
+ 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;
+ lua_pushboolean(L, rc);
+ return 1;
+}
+
// -- register Lua functions --
void lua_register_api(lua_State *L) {
@@ -168,6 +208,8 @@ void lua_register_api(lua_State *L) {
lua_setfield(L, -2, "debug");
lua_pushcfunction(L, l_cd);
lua_setfield(L, -2, "cd");
+ lua_pushcfunction(L, l_exists);
+ lua_setfield(L, -2, "exists");
// set the table as global
lua_setglobal(L, "lush");
}