lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Fri, Jan 18, 2013 at 4:03 PM, Rena <hyperhacker@gmail.com> wrote:
> On Fri, Jan 18, 2013 at 6:58 PM, Richard Geary <richardg.work@gmail.com> wrote:
>> This was first mentioned on the mailing list in 2004, but the bug still
>> exists today :
>> print( os.execute("exit 1") )
>> 256
>>
>> The reason this is bad, is running os.exit(os.execute(cmd)) will always
>> return 0, as only the lowest 8 bits of the exit code are used.
>>
>> The cause is the os_execute function is returning the full return value of
>> the call to system(), without processing it for the exit code. The fix is :
>>
>> static int os_execute (lua_State *L) {
>>   int rv = system(luaL_optstring(L, 1, NULL));
>>   int exitCode = 0;
>>   if (WIFEXITED(rv)) {
>>  exitCode = WEXITSTATUS(rv);
>>   }
>>   lua_pushinteger(L, exitCode);
>>   return 1;
>> }
>> (based on Lua 5.1.4, adapt slightly for Lua 5.2.1 which checks for a shell)
>>
>>
>
> What's the other information returned by system() then? Maybe that
> should still be given as an additional return value.
>
> --
> Sent from my Game Boy.
>

According to man 3 system, the _XOPEN_SOURCE feature test macro
enables returning flags corresponding to the macros used by wait(2).

http://linux.die.net/man/2/wait

Not sure how useful this actually is. I've never needed it.

/s/ Adam