diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2020-05-22 15:39:29 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2020-05-22 15:39:29 -0300 |
commit | efcf24be0c22cba57b298161bf4ab0561fd3c08e (patch) | |
tree | 5e6846467880a472a00ec02f3e27ca800884e161 /loslib.c | |
parent | 17dbaa8639505c9ad1a9946591f5960123fbd741 (diff) | |
download | lua-efcf24be0c22cba57b298161bf4ab0561fd3c08e.tar.gz lua-efcf24be0c22cba57b298161bf4ab0561fd3c08e.tar.bz2 lua-efcf24be0c22cba57b298161bf4ab0561fd3c08e.zip |
'luaL_execresult' does not assume -1 status as error
ISO C is silent about the return of 'system'. Windows sets 'errno' in
case of errors. Linux has several different error cases, with different
return values. ISO C allows 'system' to set 'errno' even if there are no
errors. Here we assume that a status==0 is success (which is the case
on several platforms), otherwise it is an error. If there is an error
number, gives the error based on it. (The worst a spurious 'errno'
can do is to generate a bad error message.) Otherwise uses the normal
results.
Diffstat (limited to 'loslib.c')
-rw-r--r-- | loslib.c | 6 |
1 files changed, 4 insertions, 2 deletions
@@ -10,6 +10,7 @@ | |||
10 | #include "lprefix.h" | 10 | #include "lprefix.h" |
11 | 11 | ||
12 | 12 | ||
13 | #include <errno.h> | ||
13 | #include <locale.h> | 14 | #include <locale.h> |
14 | #include <stdlib.h> | 15 | #include <stdlib.h> |
15 | #include <string.h> | 16 | #include <string.h> |
@@ -138,10 +139,11 @@ | |||
138 | 139 | ||
139 | 140 | ||
140 | 141 | ||
141 | |||
142 | static int os_execute (lua_State *L) { | 142 | static int os_execute (lua_State *L) { |
143 | const char *cmd = luaL_optstring(L, 1, NULL); | 143 | const char *cmd = luaL_optstring(L, 1, NULL); |
144 | int stat = system(cmd); | 144 | int stat; |
145 | errno = 0; | ||
146 | stat = system(cmd); | ||
145 | if (cmd != NULL) | 147 | if (cmd != NULL) |
146 | return luaL_execresult(L, stat); | 148 | return luaL_execresult(L, stat); |
147 | else { | 149 | else { |