flua: add posix.unistd.dup2()

Reviewed by:	emaste
Sponsored by:	The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D50176
This commit is contained in:
Isaac Freund 2025-05-05 09:03:37 +00:00 committed by Ed Maste
parent 4bc53dc71c
commit 909aa67813

View file

@ -165,6 +165,39 @@ err:
}
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 @@ static const struct luaL_Reg unistdlib[] = {
REG_SIMPLE(_exit),
REG_SIMPLE(chown),
REG_DEF(close, lua_pclose),
REG_SIMPLE(dup2),
REG_SIMPLE(fork),
REG_SIMPLE(getpid),
REG_SIMPLE(pipe),