flua: Add wrappers for sys/utsname.h

This allows one to invoke uname from lua scripts.

Reviewed by:	bapt, kevans, emaste
MFC after:	1 month
Differential Revision:	https://reviews.freebsd.org/D42017

(cherry picked from commit 1726db7af6b3738eb04d962b351d7f4017e1fc77)
This commit is contained in:
Mark Johnston 2024-09-05 15:16:29 +00:00
parent 9a9a2165af
commit cde4ab289d
3 changed files with 48 additions and 1 deletions

View file

@ -58,6 +58,7 @@ static const luaL_Reg loadedlibs[] = {
/* FreeBSD Extensions */
{"lfs", luaopen_lfs},
{"posix.sys.stat", luaopen_posix_sys_stat},
{"posix.sys.utsname", luaopen_posix_sys_utsname},
{"posix.unistd", luaopen_posix_unistd},
{"ucl", luaopen_ucl},
{NULL, NULL}

View file

@ -24,8 +24,8 @@
*
*/
#include <sys/cdefs.h>
#include <sys/stat.h>
#include <sys/utsname.h>
#include <errno.h>
#include <grp.h>
@ -130,12 +130,50 @@ lua_getpid(lua_State *L)
return 1;
}
static int
lua_uname(lua_State *L)
{
struct utsname name;
int error, n;
n = lua_gettop(L);
luaL_argcheck(L, n == 0, 1, "too many arguments");
error = uname(&name);
if (error != 0) {
error = errno;
lua_pushnil(L);
lua_pushstring(L, strerror(error));
lua_pushinteger(L, error);
return (3);
}
lua_newtable(L);
#define setkv(f) do { \
lua_pushstring(L, name.f); \
lua_setfield(L, -2, #f); \
} while (0)
setkv(sysname);
setkv(nodename);
setkv(release);
setkv(version);
setkv(machine);
#undef setkv
return (1);
}
#define REG_SIMPLE(n) { #n, lua_ ## n }
static const struct luaL_Reg sys_statlib[] = {
REG_SIMPLE(chmod),
{ NULL, NULL },
};
static const struct luaL_Reg sys_utsnamelib[] = {
REG_SIMPLE(uname),
{ NULL, NULL },
};
static const struct luaL_Reg unistdlib[] = {
REG_SIMPLE(getpid),
REG_SIMPLE(chown),
@ -150,6 +188,13 @@ luaopen_posix_sys_stat(lua_State *L)
return 1;
}
int
luaopen_posix_sys_utsname(lua_State *L)
{
luaL_newlib(L, sys_utsnamelib);
return 1;
}
int
luaopen_posix_unistd(lua_State *L)
{

View file

@ -8,4 +8,5 @@
#include <lua.h>
int luaopen_posix_sys_stat(lua_State *L);
int luaopen_posix_sys_utsname(lua_State *L);
int luaopen_posix_unistd(lua_State *L);