diff --git a/PRIMITIVES.md b/PRIMITIVES.md index be4a38c..bef44e3 100644 --- a/PRIMITIVES.md +++ b/PRIMITIVES.md @@ -215,6 +215,8 @@ The implementation of these primitives can be found in the file [stdlib.slisp](s * Does the given string look like a binary number, with 0b-prefix? * `binary` * Parse the given string, with 0b-prefix, as a binary number and return the integer value. +* `cd` + * Change the present working directory. (Lookup the value with `pwd`.) * `dec2binary` * Convert an integer to a string containing the binary version of that number (e.g. 7 -> 0b111) * `dec2hex` @@ -256,10 +258,18 @@ The implementation of these primitives can be found in the file [stdlib.slisp](s * `getenv` * Return the value of the given environmental variable, nor NIL if not found. * Uses `environment`. +* `getgid` + * Return the group ID of the current user. +* `getpid` + * Return the current process ID. +* `getuid` + * Return the ID of the current user. * `hex?` * Does the given string look like a hex number, with 0x prefix? * `hex` * Parse the given string, with 0x-prefix, as a hex number and return the integer value. +* `hostname` + * Return the system hostname. * `join` * Join all (string) items of a list into a single string. * `join-by` @@ -314,6 +324,8 @@ The implementation of these primitives can be found in the file [stdlib.slisp](s * Print "anything" by invoking `print`, then outputting a newline. * `putc` * Print the given character. +* `pwd` + * Return the present working directory. (Change with `cd`.) * `random` * Return a random integer between zero and N-1. * `random_string` diff --git a/stdlib.slisp b/stdlib.slisp index 58aac0e..6d65768 100644 --- a/stdlib.slisp +++ b/stdlib.slisp @@ -1233,6 +1233,13 @@ Incrementing by the given step each time." (u64! m (* i 8) 0) m)) +(defconst SYS_chdir 80) + +(defun cd (path) + "Change to the given working directory" + (if (< (syscall SYS_chdir path) 0) + nil 1)) + (defun getc () "Read a single character from STDIN, and return it." (let ((str "s")) @@ -1256,6 +1263,46 @@ Uses (environment) to get all environmental variables, and filters." (getenv-loop name (cdr xs)))) nil)) +(defconst SYS_getgid 104) + +(defun getgid () + "Return the group ID of the current user." + (let ((res (syscall SYS_getgid))) + (if (< res 0) + nil res ))) + +(defconst SYS_getpid 39) + +(defun getpid () + "Return the process ID of the current process." + (let ((res (syscall SYS_getpid))) + (if (< res 0) + nil res ))) + +(defconst SYS_getuid 102) + +(defun getuid () + "Return the ID of the current user." + (let ((res (syscall SYS_getuid))) + (if (< res 0) + nil res ))) + +(defconst SYS_uname 63) + +(defun hostname () + "Return the system hostname, via the uname syscall." + (let ((len 1024) + (hst "") + (i 0) + (buf (alloc len))) + (if (< (syscall SYS_uname buf len) 0) + nil + (do + (while (!= (strat buf (+ i 65)) 0) + (set! hst (strcat hst (string (chr (strat buf (+ i 65)))))) + (set! i (+ i 1))) + hst)))) + (defconst SYS_clock_gettime 228) (defun now () @@ -1276,6 +1323,16 @@ Uses (environment) to get all environmental variables, and filters." ((int? c) (syscall 1 1 (string (chr (& 255 c))) 1)) (t (do (println "putc called with bogus arg") (exit 1))))) +(defconst SYS_getcwd 79) + +(defun pwd () + "Return the current working directory" + (let ((len 1024) + (buf (alloc len))) + (if (>= (syscall SYS_getcwd buf len) 0) + buf nil))) + + (defconst SYS_getrandom 318) (defun random (n)