Diff

util-src/pposix.c @ 10921:6eb5d2bb11af

util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions Actually just an alias of pushnil, but it does make it more obvious where the failure conditions are, which is good for readability.
author Kim Alvefur <zash@zash.se>
date Sun, 07 Jun 2020 02:25:56 +0200
parent 10799:763bb2ce3f60
child 11656:c368b4f6ee04
line wrap: on
line diff
--- a/util-src/pposix.c	Sun Jun 07 02:14:55 2020 +0200
+++ b/util-src/pposix.c	Sun Jun 07 02:25:56 2020 +0200
@@ -64,6 +64,9 @@
 #if (LUA_VERSION_NUM < 503)
 #define lua_isinteger(L, n) lua_isnumber(L, n)
 #endif
+#if (LUA_VERSION_NUM < 504)
+#define luaL_pushfail lua_pushnil
+#endif
 
 #include <fcntl.h>
 #if defined(__linux__)
@@ -413,7 +416,7 @@
 	struct passwd *p;
 
 	if(!lua_isstring(L, 1)) {
-		lua_pushnil(L);
+		luaL_pushfail(L);
 		lua_pushstring(L, "invalid-username");
 		return 2;
 	}
@@ -421,7 +424,7 @@
 	p = getpwnam(lua_tostring(L, 1));
 
 	if(!p) {
-		lua_pushnil(L);
+		luaL_pushfail(L);
 		lua_pushstring(L, "no-such-user");
 		return 2;
 	}
@@ -440,7 +443,7 @@
 			break;
 
 		default:
-			lua_pushnil(L);
+			luaL_pushfail(L);
 			lua_pushstring(L, "invalid-gid");
 			return 2;
 	}
@@ -450,17 +453,17 @@
 	if(ret) {
 		switch(errno) {
 			case ENOMEM:
-				lua_pushnil(L);
+				luaL_pushfail(L);
 				lua_pushstring(L, "no-memory");
 				break;
 
 			case EPERM:
-				lua_pushnil(L);
+				luaL_pushfail(L);
 				lua_pushstring(L, "permission-denied");
 				break;
 
 			default:
-				lua_pushnil(L);
+				luaL_pushfail(L);
 				lua_pushstring(L, "unknown-error");
 		}
 	} else {
@@ -672,7 +675,7 @@
 	struct utsname uname_info;
 
 	if(uname(&uname_info) != 0) {
-		lua_pushnil(L);
+		luaL_pushfail(L);
 		lua_pushstring(L, strerror(errno));
 		return 2;
 	}
@@ -702,7 +705,7 @@
 	/* If the second argument is nil or nothing, unset the var */
 	if(lua_isnoneornil(L, 2)) {
 		if(unsetenv(var) != 0) {
-			lua_pushnil(L);
+			luaL_pushfail(L);
 			lua_pushstring(L, strerror(errno));
 			return 2;
 		}
@@ -714,7 +717,7 @@
 	value = luaL_checkstring(L, 2);
 
 	if(setenv(var, value, 1) != 0) {
-		lua_pushnil(L);
+		luaL_pushfail(L);
 		lua_pushstring(L, strerror(errno));
 		return 2;
 	}
@@ -776,7 +779,7 @@
 
 			case ENOSPC: /* No space left */
 			default: /* Other issues */
-				lua_pushnil(L);
+				luaL_pushfail(L);
 				lua_pushstring(L, strerror(err));
 				lua_pushinteger(L, err);
 				return 3;
@@ -803,7 +806,7 @@
 		return luaL_error(L, "atomic_append() failed in ftruncate(): %s", strerror(errno));
 	}
 
-	lua_pushnil(L);
+	luaL_pushfail(L);
 	lua_pushstring(L, strerror(err));
 	lua_pushinteger(L, err);
 	return 3;