shithub: lu9-p9

Download patch

ref: afb18ac80f88ac730d95fb6f6e442c77b90f47ea
parent: fbdd4e91a0d9285cd2bbe05659444493211edc8e
author: kvik <[email protected]>
date: Mon Apr 19 10:09:01 EDT 2021

proc: implement p9.(pid ppid user sysname)

--- a/p9.c
+++ b/p9.c
@@ -176,6 +176,11 @@
 	{"sleep", p9_sleep},
 	{"alarm", p9_alarm},
 	{"rfork", p9_rfork},
+	{"wdir", p9_wdir},
+	{"pid", p9_pid},
+	{"ppid", p9_ppid},
+	{"user", p9_user},
+	{"sysname", p9_sysname},
 	
 	{"cleanname", p9_cleanname},
 	
--- a/proc.c
+++ b/proc.c
@@ -51,6 +51,56 @@
 }
 
 static int
+p9_wdir(lua_State *L)
+{
+	const char *path;
+	char *buf;
+	luaL_Buffer b;
+	
+	path = luaL_optstring(L, 1, nil);
+	if(path != nil){
+		if(chdir(path) == -1)
+			return error(L, "chdir: %r");
+		lua_pushboolean(L, 1);
+		return 1;
+	}
+	luaL_buffinitsize(L, &b, Iosize);
+	buf = luaL_buffaddr(&b);
+	if(getwd(buf, Iosize) == nil)
+		return error(L, "getwd: %r");
+	luaL_pushresultsize(&b, strlen(buf));
+	return 1;
+}
+
+static int
+p9_user(lua_State *L)
+{
+	lua_pushstring(L, getuser());
+	return 1;
+}
+
+static int
+p9_sysname(lua_State *L)
+{
+	lua_pushstring(L, sysname());
+	return 1;
+}
+
+static int
+p9_pid(lua_State *L)
+{
+	lua_pushinteger(L, getpid());
+	return 1;
+}
+
+static int
+p9_ppid(lua_State *L)
+{
+	lua_pushinteger(L, getppid());
+	return 1;
+}
+
+static int
 p9_rfork(lua_State *L)
 {
 	int flags, i, n, r;
--- a/test.lua
+++ b/test.lua
@@ -229,7 +229,18 @@
 	assert(p9.sleep(0) == true)
 end
 
+-- wdir
+do
+	local cwd = assert(p9.wdir())
+	assert(p9.wdir("/dev") and p9.wdir() == "/dev")
+	assert(p9.wdir(cwd))
+end
 
+-- proc info
+do
+	assert(p9.user() and p9.sysname())
+	assert(p9.pid() and p9.ppid())
+end
 
 -- Environment variables
 do