Comparison

util-src/net.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 10637:aa304109fa1b
child 12575:1f6f05a98fcd
comparison
equal deleted inserted replaced
10920:c171b4c59bd1 10921:6eb5d2bb11af
31 #include <lauxlib.h> 31 #include <lauxlib.h>
32 32
33 #if (LUA_VERSION_NUM == 501) 33 #if (LUA_VERSION_NUM == 501)
34 #define luaL_setfuncs(L, R, N) luaL_register(L, NULL, R) 34 #define luaL_setfuncs(L, R, N) luaL_register(L, NULL, R)
35 #endif 35 #endif
36 #if (LUA_VERSION_NUM < 504)
37 #define luaL_pushfail lua_pushnil
38 #endif
36 39
37 /* Enumerate all locally configured IP addresses */ 40 /* Enumerate all locally configured IP addresses */
38 41
39 static const char *const type_strings[] = { 42 static const char *const type_strings[] = {
40 "both", 43 "both",
57 const char ipv6 = (type == 0 || type == 2); 60 const char ipv6 = (type == 0 || type == 2);
58 61
59 #ifndef _WIN32 62 #ifndef _WIN32
60 63
61 if(getifaddrs(&addr) < 0) { 64 if(getifaddrs(&addr) < 0) {
62 lua_pushnil(L); 65 luaL_pushfail(L);
63 lua_pushfstring(L, "getifaddrs failed (%d): %s", errno, 66 lua_pushfstring(L, "getifaddrs failed (%d): %s", errno,
64 strerror(errno)); 67 strerror(errno));
65 return 2; 68 return 2;
66 } 69 }
67 70
139 lua_pushlstring(L, buf, family == AF_INET6 ? 16 : 4); 142 lua_pushlstring(L, buf, family == AF_INET6 ? 16 : 4);
140 return 1; 143 return 1;
141 144
142 case -1: 145 case -1:
143 errno_ = errno; 146 errno_ = errno;
144 lua_pushnil(L); 147 luaL_pushfail(L);
145 lua_pushstring(L, strerror(errno_)); 148 lua_pushstring(L, strerror(errno_));
146 lua_pushinteger(L, errno_); 149 lua_pushinteger(L, errno_);
147 return 3; 150 return 3;
148 151
149 default: 152 default:
150 case 0: 153 case 0:
151 lua_pushnil(L); 154 luaL_pushfail(L);
152 lua_pushstring(L, strerror(EINVAL)); 155 lua_pushstring(L, strerror(EINVAL));
153 lua_pushinteger(L, EINVAL); 156 lua_pushinteger(L, EINVAL);
154 return 3; 157 return 3;
155 } 158 }
156 159
168 } 171 }
169 else if(l == 4) { 172 else if(l == 4) {
170 family = AF_INET; 173 family = AF_INET;
171 } 174 }
172 else { 175 else {
173 lua_pushnil(L); 176 luaL_pushfail(L);
174 lua_pushstring(L, strerror(EAFNOSUPPORT)); 177 lua_pushstring(L, strerror(EAFNOSUPPORT));
175 lua_pushinteger(L, EAFNOSUPPORT); 178 lua_pushinteger(L, EAFNOSUPPORT);
176 return 3; 179 return 3;
177 } 180 }
178 181
179 if(!inet_ntop(family, ipaddr, buf, INET6_ADDRSTRLEN)) 182 if(!inet_ntop(family, ipaddr, buf, INET6_ADDRSTRLEN))
180 { 183 {
181 errno_ = errno; 184 errno_ = errno;
182 lua_pushnil(L); 185 luaL_pushfail(L);
183 lua_pushstring(L, strerror(errno_)); 186 lua_pushstring(L, strerror(errno_));
184 lua_pushinteger(L, errno_); 187 lua_pushinteger(L, errno_);
185 return 3; 188 return 3;
186 } 189 }
187 190