lua-users home
lua-l archive

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


This is admittedly not a pretty test case but it illustrates the problem if
it, and its output, are examined.  Included below are the output of several
runs, the expected output being the third, "Patched 5.2".

Unpatched 5.1:
    lua test-popen.lua
    Lua version: Lua 5.1
    sh: foobarbaz: not found
    cmd: foobarbaz
      rc: true
      etype: nil
    cmd: cat>/dev/null
      rc: true
      etype: nil
    cmd: exit 3
      rc: true
      etype: nil

Unpatched 5.2:
    lua test-popen.lua
    Lua version: Lua 5.2
    sh: foobarbaz: not found
    cmd: foobarbaz
      rc: 32512
      etype: nil
    cmd: cat>/dev/null
      rc: 0
      etype: nil
    cmd: exit 3
      rc: 768
      etype: nil

Patched 5.2:
    lua test-popen.lua
    Lua version: Lua 5.2
    sh: foobarbaz: not found
    cmd: foobarbaz
      rc: 127
      etype: e
    cmd: cat>/dev/null
      rc: 0
      etype: e
    cmd: exit 3
      rc: 3
      etype: e

-- Test the behavior of file:close() on pipes.
-- This may only work on Linux

local cmds =
        {
        "foobarbaz"     , -- No such command
        "cat>/dev/null" , -- Normal exit
        "exit 3"        , -- Non-zero exit
        };


io.write( "Lua version: ", _VERSION, "\n" );


for _, cmd in ipairs(cmds)
    do

    local file = assert( io.popen( cmd, "w" ) );

    file:write( "Hello.\n" );

    local rc, etype = file:close();

    if type(rc) == "boolean" then -- Lua 5.1 returned boolean
        rc = rc and "true" or "false";
    else
        rc = rc or "nil";
        end

    etype = etype or "nil";

    io.write( "cmd: ", cmd,
                "\n  rc: ", rc,
                "\n  etype: ", etype, "\n" );

    end