From 09eac7708cee05fc442189ff634556b447040027 Mon Sep 17 00:00:00 2001 From: takase1121 <20792268+takase1121@users.noreply.github.com> Date: Sat, 10 Oct 2020 13:02:58 +0800 Subject: [PATCH] fix popen errors --- src/api/system.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/api/system.c b/src/api/system.c index 3286ab0..768db38 100644 --- a/src/api/system.c +++ b/src/api/system.c @@ -417,8 +417,8 @@ static int f_popen(lua_State *L) { CloseHandle(pi.hProcess); CloseHandle(pi.hThread); #else - FILE* pipe = popen(cmd, "rb"); - if (pipe == NULL) { luaL_error(L, "Failed to start process"); } + FILE* pipe = popen(cmd, "r"); + if (pipe == NULL) { luaL_error(L, "Failed to start process: %d", errno); } size_t read; while(!feof(pipe)) { @@ -426,7 +426,8 @@ static int f_popen(lua_State *L) { read = fread(b, sizeof(char), LUAL_BUFFERSIZE, pipe); luaL_addsize(&stdoutBuf, read); } - exitCode = WEXITSTATUS(pclose(pipe)); + int status = pclose(pipe); + if (WIFEXITED(status)) { exitCode = WEXITSTATUS(status); } #endif luaL_pushresult(&stdoutBuf); lua_pushnumber(L, exitCode);