diff --git a/libexec/flua/modules/lposix.c b/libexec/flua/modules/lposix.c --- a/libexec/flua/modules/lposix.c +++ b/libexec/flua/modules/lposix.c @@ -165,6 +165,39 @@ } +static int +lua_dup2(lua_State *L) +{ + int error, oldd, newd; + + enforce_max_args(L, 2); + + oldd = luaL_checkinteger(L, 1); + if (oldd < 0) { + error = EBADF; + goto err; + } + + newd = luaL_checkinteger(L, 2); + if (newd < 0) { + error = EBADF; + goto err; + } + + error = dup2(oldd, newd); + if (error >= 0) { + lua_pushinteger(L, error); + return (1); + } + + error = errno; +err: + lua_pushnil(L); + lua_pushstring(L, strerror(error)); + lua_pushinteger(L, error); + return (3); +} + static int lua_fnmatch(lua_State *L) { @@ -479,6 +512,7 @@ REG_SIMPLE(_exit), REG_SIMPLE(chown), REG_DEF(close, lua_pclose), + REG_SIMPLE(dup2), REG_SIMPLE(fork), REG_SIMPLE(getpid), REG_SIMPLE(pipe),