Initial commit of newLISP.
[newlisp.git] / modules / unix.lsp
blobd69229923c4de183a27a7a8870623f8aed180130
1 ;; @module unix.lsp
2 ;; @description Itreface to various UNIX libc functions
3 ;; @version 0.1 alpha - mostly untested
4 ;; @author L.M. 2006-Sep-12, lutz@nuevatec.com
5 ;;
6 ;; <h2>UNIX libc function support</h2>
7 ;; To use this module include the following line at the beginning of
8 ;; the program file:
9 ;; <pre>
10 ;; (load "/usr/share/newlisp/modules/unix.lsp")
11 ;; </pre>
13 ;; All functions are named like the UNIX functions they implelement but with
14 ;; the unix: namespace prefix. All functions work similar to the UNIX libc functions
15 ;; they implement but return 'nil' instead of '-1' for failure and 'true' instead
16 ;; of 0 for success. Note that in the implementaion most parameter types are casted
17 ;; even though redundant in most situation. The addional cast avoids segfaulting
18 ;; of the imported functions when supplied with wrong parameter types, i.e. for strings.
19 ;; Some times the 'int' cast is used to supply default values of an argument, in case
20 ;; the argument is not given.
22 ;; The arguments are given in, is the order used by the equivalent libc functions, not
23 ;; the order as used by shell commandline utilities with the same name.
25 ;; Some of the wordings from functions descriptions are taken from BSD man pages.
27 ;; This module contains wrappers for the following libc functions:
28 ;; 'getpid', 'getppid', 'getuid', 'geteuid', 'getgid', 'getegid',
29 ;; 'setuid', 'seteuid', 'setgid', 'setegid',
30 ;; 'kill', 'chmod', 'ioctl', 'mkfifo', 'mltemp', 'syslog'
31 ;;
33 (context 'unix)
35 (if
36 ;; LINUX and BSDs
37 (< (& 0xF (last (sys-info))) 3) (set 'library "/usr/lib/libc.so")
38 ;; Mac OSX / Darwin
39 (= (& 0xF (last (sys-info))) 3) (set 'library "/usr/lib/libc.dylib")
40 ;; Solaris
41 (= (& 0xF (last (sys-info))) 4) (set 'library "/usr/lib/libc.so")
42 true (throw-error "Cannot load library")
45 ;; @syntax (unix:getpid)
46 ;; @return Returns the current process ID
48 (import library "getpid")
50 ;; @syntax (unix:getppid)
51 ;; @return Returns the parents process ID.
53 (import library "getppid")
55 ;; @syntax (unix:getuid)
56 ;; @return Returns the real and effective user ID
58 (import library "getuid")`
60 ;; @syntax (unix:geteuid)
61 ;; @return Returns the effective user ID
63 (import library "geteuid")
65 ;; @syntax (unix:getgid)
66 ;; @return Returns the real group ID.
68 (import library "getgid")
70 ;; @syntax (unix:getegid)
71 ;; @return Returns the effective group ID
73 (import library "getegid")
75 ;; @syntax (unix:setuid <num-id>)
76 ;; @param <num-id> The number of the real and effective user IDs to set.
77 ;; @return Returns 'true' or 'nil' depending on success
79 ;; Set the real and effective user IDs.
80 ;; If no argument or the argument given is not a number then sets
81 ;; current real and effective user IDs.
83 (set '_setuid (import library "setuid"))
85 (define (setuid id)
86 (zero? (_setuid (int id (getuid))))
89 ;; @syntax (unix:seteuid <num-id>)
90 ;; @param <num-id> The number of the effective user ID to set.
91 ;; @return Returns 'true' or 'nil' depending on success
93 ;; Sets the effective user ID.
94 ;; If no argument or the argument given is not a number then sets
95 ;; current effective user ID.
97 (set '_seteuid (import library "seteuid"))
99 (define (seteuid id)
100 (zero? (_seteuid (int id (geteuid))))
104 ;; @syntax (unix:setgid <num-id>)
105 ;; @param <num-id> The number of the real and effective group ID to set.
106 ;; @return Returns 'true' or 'nil' depending on success
108 ;; Sets the real and effective group IDs.
109 ;; If no argument or the argument given is not a number then sets
110 ;; current real and effective group IDs.
112 (set '_setgid (import library "setgid"))
114 (define (setgid id)
115 (zero? (_setgid (int id (getgid))))
119 ;; @syntax (unix:setegid <num-id>)
120 ;; @param <num-id> the number of the effective group ID to set.
121 ;; @return Returns 'true' or 'nil' depending on success
123 ;; Sets the effective group ID.
124 ;; If no argument or the argument given is not a number then sets
125 ;; current effective group ID.
127 (set '_setegid (import library "setegid"))
129 (define (setegid id)
130 (zero? (_setegid (int id (getegid))))
133 ;; @syntax (unix:kill <num-pid> <num-sig>)
134 ;; @param <num-pid> The process ID of the process to send a signal to.
135 ;; @param <num-sig> The signal to send to the process in <num-pid>
136 ;; @return Returns 'true' or 'nil' depending on success
138 ;; Send a signal to a process.
139 ;; If no argument is given for <num-sig> or <num-sig> is zero, then
140 ;; the validiy of the <num-pid> is checked and 'nil' is returned for
141 ;; an invalid <num-pid>.
142 ;; Note that kill works has parameters backwards compared to the
143 ;; kill used in on the shell command line. '(unix:kill ...)' works
144 ;; like the libc 'kill()' counterpart.
146 (set '_kill (import library "kill"))
148 (define (kill pid sig)
149 (zero? (_kill (int pid 0) (int sig 0)))
152 ;; @example
153 ;; (unix:kill 2652 9)
155 ;; Sends the 'SIGKILL' signal 9 to process ID '2652'
157 ;; @syntax (unix:chmod <str-path> <num-mode>)
158 ;; @param <str-path> The path name of the file or directory
159 ;; @param <num-option> The permission pattern as a number
160 ;; @return Returns 'true' or 'nil' depending on success
162 ;; Sets the permission pattern for a UNIX filesystem node. The
163 ;; pattern is best given as an octal number, i.e.: '0755' for
164 ;; a -rwxr-xr-x permission pattern, '0644' for '-rw-r--r--' etc.
165 ;; If no <int-option> is given unix:chmod assumes '0644'.
167 (set '_chmod (import library "chmod"))
169 (define (chmod path option)
170 (zero? (_chmod (string path) (int option 0644)))
173 ;; @example
174 ;; (unix:chmod "myfile.txt" 0644)
176 ;; Gives '-rw-r--r--' permission to the file 'myfile.txt'
178 ;; @syntax (unix:ioctl <num-id> <num-request> <str-argp>)
179 ;; @param <num-id> A number for file descriptor.
180 ;; @param <num-request> A number for a request.
181 ;; @param <str-argp> An argument string.
182 ;; @return Returns 'true' or 'nil' depending on success
184 ;; Manipulates the underlying device parameters of special
185 ;; files.
187 (set '_ioctl (import library "ioctl"))
189 (define (ioctl id request argp)
190 (zero? (_ioctl (int id 0) (int request 0) (string argp)))
193 ;; @syntax (unix:mkfifo <str-path> <num-mode>)
194 ;; @param <str-path> The path of the new fifo filename.
195 ;; @param <num-mode> The persmissions mode number.
196 ;; @return Returns 'true' or 'nil' depending on success
198 ;; Creates a new fifo file with name path. The access permissions
199 ;; are specified by mode and restricted by the 'umask(2)' of the calling
200 ;; process.
202 (set '_mkfifo (import library "mkfifo"))
204 (define (mkfifo path mode)
205 (zero? (_mkfifo (string path) (int mode)))
208 ;; @example
209 ;; (unix:mkfifo "myfifo" 0755)
211 ;; This will create a fifo node with 'prwxr-xr-x' permissions
214 ;; @syntax (unix:mktemp <str-template>)
215 ;; @param <str-template> A file template with Xs appended, i.e. '/tmp/temp.XXXX'.
216 ;; @return Returns 'true' or 'nil' depending on success
218 ;; The function takes the given file name template and overwrites a
219 ;; portion of it to create a file name. This file name is guaranteed not to
220 ;; exist at the time of function invocation and is suitable for use by the
221 ;; application. The template may be any file name with some number of 'X's
222 ;; appended to it, for example '/tmp/temp.XXXXXX'. The trailing 'X's are
223 ;; replaced with a unique alphanumeric combination.
225 (set '_mktemp (import library "mktemp"))
227 (define (mktemp template)
228 (zero? (_mktemp (string template)))
231 ;; @syntax (unix:syslog <num-priority> <str-message> ...)
232 ;; @param <num-priority> The priority of the message.
233 ;; @param <str-message> The message string and template in printf format.
234 ;; @return Returns 'true' or 'nil' depending on success
236 ;; The function writes message to the system message logger. The
237 ;; message is then written to the system console, log files, logged-in
238 ;; users, or forwarded to other machines as appropriate.
240 (set '_syslog (import library "syslog"))
242 (define (syslog priority message)
243 (zero? (_syslog (int priority 0) (string message)))
246 ; eof