Commit | Line | Data |
---|---|---|
7dc6e754 | 1 | /* Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc. |
0f2d19dd JB |
2 | * |
3 | * This program is free software; you can redistribute it and/or modify | |
4 | * it under the terms of the GNU General Public License as published by | |
5 | * the Free Software Foundation; either version 2, or (at your option) | |
6 | * any later version. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | * GNU General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License | |
14 | * along with this software; see the file COPYING. If not, write to | |
82892bed JB |
15 | * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
16 | * Boston, MA 02111-1307 USA | |
0f2d19dd JB |
17 | * |
18 | * As a special exception, the Free Software Foundation gives permission | |
19 | * for additional uses of the text contained in its release of GUILE. | |
20 | * | |
21 | * The exception is that, if you link the GUILE library with other files | |
22 | * to produce an executable, this does not by itself cause the | |
23 | * resulting executable to be covered by the GNU General Public License. | |
24 | * Your use of that executable is in no way restricted on account of | |
25 | * linking the GUILE library code into it. | |
26 | * | |
27 | * This exception does not however invalidate any other reasons why | |
28 | * the executable file might be covered by the GNU General Public License. | |
29 | * | |
30 | * This exception applies only to the code released by the | |
31 | * Free Software Foundation under the name GUILE. If you copy | |
32 | * code from other Free Software Foundation releases into a copy of | |
33 | * GUILE, as the General Public License permits, the exception does | |
34 | * not apply to the code that you add in this way. To avoid misleading | |
35 | * anyone as to the status of such modified files, you must delete | |
36 | * this exception notice from them. | |
37 | * | |
38 | * If you write modifications of your own for GUILE, it is your choice | |
39 | * whether to permit this exception to apply to your modifications. | |
82892bed | 40 | * If you do not wish that, delete this exception notice. */ |
1bbd0b84 GB |
41 | |
42 | /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999, | |
43 | gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */ | |
44 | ||
0f2d19dd JB |
45 | \f |
46 | ||
47 | #include <stdio.h> | |
48 | #include "_scm.h" | |
20e6290e | 49 | #include "fports.h" |
20e6290e | 50 | #include "scmsigs.h" |
20e6290e | 51 | #include "feature.h" |
0f2d19dd | 52 | |
1bbd0b84 | 53 | #include "scm_validate.h" |
20e6290e | 54 | #include "posix.h" |
0f2d19dd JB |
55 | \f |
56 | ||
02b754d3 GH |
57 | #ifdef HAVE_STRING_H |
58 | #include <string.h> | |
59 | #endif | |
0f2d19dd JB |
60 | #ifdef TIME_WITH_SYS_TIME |
61 | # include <sys/time.h> | |
62 | # include <time.h> | |
63 | #else | |
64 | # if HAVE_SYS_TIME_H | |
65 | # include <sys/time.h> | |
66 | # else | |
67 | # include <time.h> | |
68 | # endif | |
69 | #endif | |
70 | ||
71 | #ifdef HAVE_UNISTD_H | |
72 | #include <unistd.h> | |
95b88819 GH |
73 | #else |
74 | #ifndef ttyname | |
75 | extern char *ttyname(); | |
76 | #endif | |
0f2d19dd JB |
77 | #endif |
78 | ||
3594582b | 79 | #ifdef LIBC_H_WITH_UNISTD_H |
bab0f4e5 JB |
80 | #include <libc.h> |
81 | #endif | |
82 | ||
8cc71382 | 83 | #include <sys/types.h> |
0f2d19dd JB |
84 | #include <sys/stat.h> |
85 | #include <fcntl.h> | |
86 | ||
87 | #include <pwd.h> | |
88 | ||
89 | #if HAVE_SYS_WAIT_H | |
90 | # include <sys/wait.h> | |
91 | #endif | |
92 | #ifndef WEXITSTATUS | |
93 | # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8) | |
94 | #endif | |
95 | #ifndef WIFEXITED | |
96 | # define WIFEXITED(stat_val) (((stat_val) & 255) == 0) | |
97 | #endif | |
98 | ||
99 | #include <signal.h> | |
100 | ||
0f2d19dd JB |
101 | extern FILE *popen (); |
102 | extern char ** environ; | |
103 | ||
104 | #include <grp.h> | |
105 | #include <sys/utsname.h> | |
106 | ||
107 | #if HAVE_DIRENT_H | |
108 | # include <dirent.h> | |
109 | # define NAMLEN(dirent) strlen((dirent)->d_name) | |
110 | #else | |
111 | # define dirent direct | |
112 | # define NAMLEN(dirent) (dirent)->d_namlen | |
113 | # if HAVE_SYS_NDIR_H | |
114 | # include <sys/ndir.h> | |
115 | # endif | |
116 | # if HAVE_SYS_DIR_H | |
117 | # include <sys/dir.h> | |
118 | # endif | |
119 | # if HAVE_NDIR_H | |
120 | # include <ndir.h> | |
121 | # endif | |
122 | #endif | |
123 | ||
0f2d19dd JB |
124 | #ifdef HAVE_SETLOCALE |
125 | #include <locale.h> | |
126 | #endif | |
127 | ||
bab0f4e5 JB |
128 | /* Some Unix systems don't define these. CPP hair is dangerous, but |
129 | this seems safe enough... */ | |
130 | #ifndef R_OK | |
131 | #define R_OK 4 | |
132 | #endif | |
133 | ||
134 | #ifndef W_OK | |
135 | #define W_OK 2 | |
136 | #endif | |
137 | ||
138 | #ifndef X_OK | |
139 | #define X_OK 1 | |
140 | #endif | |
141 | ||
142 | #ifndef F_OK | |
143 | #define F_OK 0 | |
144 | #endif | |
398609a5 JB |
145 | |
146 | /* On NextStep, <utime.h> doesn't define struct utime, unless we | |
147 | #define _POSIX_SOURCE before #including it. I think this is less | |
148 | of a kludge than defining struct utimbuf ourselves. */ | |
149 | #ifdef UTIMBUF_NEEDS_POSIX | |
150 | #define _POSIX_SOURCE | |
151 | #endif | |
152 | ||
153 | #ifdef HAVE_SYS_UTIME_H | |
154 | #include <sys/utime.h> | |
155 | #endif | |
156 | ||
157 | #ifdef HAVE_UTIME_H | |
158 | #include <utime.h> | |
159 | #endif | |
160 | ||
161 | /* Please don't add any more #includes or #defines here. The hack | |
162 | above means that _POSIX_SOURCE may be #defined, which will | |
163 | encourage header files to do strange things. */ | |
164 | ||
0f2d19dd | 165 | \f |
bc45012d JB |
166 | SCM_SYMBOL (sym_read_pipe, "read pipe"); |
167 | SCM_SYMBOL (sym_write_pipe, "write pipe"); | |
0f2d19dd | 168 | |
a1ec6916 | 169 | SCM_DEFINE (scm_pipe, "pipe", 0, 0, 0, |
1bbd0b84 | 170 | (), |
4079f87e GB |
171 | "Creates a pipe which can be used for communication. The return value |
172 | is a pair in which the CAR contains an input port and the CDR an | |
173 | output port. Data written to the output port can be read from the | |
174 | input port. Note that both ports are buffered so it may be necessary | |
175 | to flush the output port before data will actually be sent across the pipe. | |
176 | Alternatively a buffer can be added to the port using @code{setvbuf} | |
177 | (see below).") | |
1bbd0b84 | 178 | #define FUNC_NAME s_scm_pipe |
0f2d19dd JB |
179 | { |
180 | int fd[2], rv; | |
0f2d19dd | 181 | SCM p_rd, p_wt; |
02b754d3 | 182 | |
0f2d19dd JB |
183 | rv = pipe (fd); |
184 | if (rv) | |
1bbd0b84 | 185 | SCM_SYSERROR; |
ee149d03 JB |
186 | |
187 | p_rd = scm_fdes_to_port (fd[0], "r", sym_read_pipe); | |
188 | p_wt = scm_fdes_to_port (fd[1], "w", sym_write_pipe); | |
0f2d19dd JB |
189 | return scm_cons (p_rd, p_wt); |
190 | } | |
1bbd0b84 | 191 | #undef FUNC_NAME |
0f2d19dd JB |
192 | |
193 | ||
0e958795 | 194 | #ifdef HAVE_GETGROUPS |
a1ec6916 | 195 | SCM_DEFINE (scm_getgroups, "getgroups", 0, 0, 0, |
1bbd0b84 | 196 | (), |
4079f87e | 197 | "Returns a vector of integers representing the current supplimentary group IDs.") |
1bbd0b84 | 198 | #define FUNC_NAME s_scm_getgroups |
0f2d19dd JB |
199 | { |
200 | SCM grps, ans; | |
201 | int ngroups = getgroups (0, NULL); | |
02b754d3 | 202 | if (!ngroups) |
1bbd0b84 | 203 | SCM_SYSERROR; |
0f2d19dd JB |
204 | SCM_NEWCELL(grps); |
205 | SCM_DEFER_INTS; | |
206 | { | |
207 | GETGROUPS_T *groups; | |
208 | int val; | |
209 | ||
1bbd0b84 | 210 | groups = SCM_MUST_MALLOC_TYPE_NUM(GETGROUPS_T,ngroups); |
0f2d19dd JB |
211 | val = getgroups(ngroups, groups); |
212 | if (val < 0) | |
213 | { | |
ee149d03 | 214 | int en = errno; |
0f2d19dd | 215 | scm_must_free((char *)groups); |
ee149d03 | 216 | errno = en; |
1bbd0b84 | 217 | SCM_SYSERROR; |
0f2d19dd JB |
218 | } |
219 | SCM_SETCHARS(grps, groups); /* set up grps as a GC protect */ | |
220 | SCM_SETLENGTH(grps, 0L + ngroups * sizeof(GETGROUPS_T), scm_tc7_string); | |
a8741caa | 221 | ans = scm_make_vector (SCM_MAKINUM(ngroups), SCM_UNDEFINED); |
0f2d19dd JB |
222 | while (--ngroups >= 0) SCM_VELTS(ans)[ngroups] = SCM_MAKINUM(groups[ngroups]); |
223 | SCM_SETCHARS(grps, groups); /* to make sure grps stays around. */ | |
ee149d03 | 224 | SCM_ALLOW_INTS; |
0f2d19dd JB |
225 | return ans; |
226 | } | |
1bbd0b84 GB |
227 | } |
228 | #undef FUNC_NAME | |
0e958795 | 229 | #endif |
0f2d19dd JB |
230 | |
231 | ||
a1ec6916 | 232 | SCM_DEFINE (scm_getpwuid, "getpw", 0, 1, 0, |
1bbd0b84 | 233 | (SCM user), |
4079f87e GB |
234 | "Look up an entry in the user database. @var{obj} can be an integer, |
235 | a string, or omitted, giving the behaviour of getpwuid, getpwnam | |
236 | or getpwent respectively.") | |
1bbd0b84 | 237 | #define FUNC_NAME s_scm_getpwuid |
0f2d19dd JB |
238 | { |
239 | SCM result; | |
240 | struct passwd *entry; | |
241 | SCM *ve; | |
242 | ||
a8741caa | 243 | result = scm_make_vector (SCM_MAKINUM (7), SCM_UNSPECIFIED); |
0f2d19dd JB |
244 | ve = SCM_VELTS (result); |
245 | if (SCM_UNBNDP (user) || SCM_FALSEP (user)) | |
246 | { | |
0f2d19dd | 247 | SCM_SYSCALL (entry = getpwent ()); |
3d781d49 JB |
248 | if (! entry) |
249 | { | |
3d781d49 JB |
250 | return SCM_BOOL_F; |
251 | } | |
0f2d19dd JB |
252 | } |
253 | else if (SCM_INUMP (user)) | |
254 | { | |
0f2d19dd JB |
255 | entry = getpwuid (SCM_INUM (user)); |
256 | } | |
257 | else | |
258 | { | |
1bbd0b84 | 259 | SCM_VALIDATE_ROSTRING(1,user); |
0f2d19dd JB |
260 | if (SCM_SUBSTRP (user)) |
261 | user = scm_makfromstr (SCM_ROCHARS (user), SCM_ROLENGTH (user), 0); | |
0f2d19dd JB |
262 | entry = getpwnam (SCM_ROCHARS (user)); |
263 | } | |
264 | if (!entry) | |
1bbd0b84 | 265 | SCM_MISC_ERROR ("entry not found", SCM_EOL); |
02b754d3 | 266 | |
0f2d19dd JB |
267 | ve[0] = scm_makfrom0str (entry->pw_name); |
268 | ve[1] = scm_makfrom0str (entry->pw_passwd); | |
269 | ve[2] = scm_ulong2num ((unsigned long) entry->pw_uid); | |
270 | ve[3] = scm_ulong2num ((unsigned long) entry->pw_gid); | |
271 | ve[4] = scm_makfrom0str (entry->pw_gecos); | |
272 | if (!entry->pw_dir) | |
273 | ve[5] = scm_makfrom0str (""); | |
274 | else | |
275 | ve[5] = scm_makfrom0str (entry->pw_dir); | |
276 | if (!entry->pw_shell) | |
277 | ve[6] = scm_makfrom0str (""); | |
278 | else | |
279 | ve[6] = scm_makfrom0str (entry->pw_shell); | |
0f2d19dd JB |
280 | return result; |
281 | } | |
1bbd0b84 | 282 | #undef FUNC_NAME |
0f2d19dd JB |
283 | |
284 | ||
0e958795 | 285 | #ifdef HAVE_SETPWENT |
a1ec6916 | 286 | SCM_DEFINE (scm_setpwent, "setpw", 0, 1, 0, |
1bbd0b84 | 287 | (SCM arg), |
4079f87e GB |
288 | "If called with a true argument, initialize or reset the password data |
289 | stream. Otherwise, close the stream. The @code{setpwent} and | |
290 | @code{endpwent} procedures are implemented on top of this.") | |
1bbd0b84 | 291 | #define FUNC_NAME s_scm_setpwent |
0f2d19dd JB |
292 | { |
293 | if (SCM_UNBNDP (arg) || SCM_FALSEP (arg)) | |
294 | endpwent (); | |
295 | else | |
296 | setpwent (); | |
297 | return SCM_UNSPECIFIED; | |
298 | } | |
1bbd0b84 | 299 | #undef FUNC_NAME |
0e958795 | 300 | #endif |
0f2d19dd JB |
301 | |
302 | ||
303 | ||
304 | /* Combines getgrgid and getgrnam. */ | |
a1ec6916 | 305 | SCM_DEFINE (scm_getgrgid, "getgr", 0, 1, 0, |
1bbd0b84 | 306 | (SCM name), |
4079f87e GB |
307 | "Look up an entry in the group database. @var{obj} can be an integer, |
308 | a string, or omitted, giving the behaviour of getgrgid, getgrnam | |
309 | or getgrent respectively.") | |
1bbd0b84 | 310 | #define FUNC_NAME s_scm_getgrgid |
0f2d19dd JB |
311 | { |
312 | SCM result; | |
313 | struct group *entry; | |
314 | SCM *ve; | |
a8741caa | 315 | result = scm_make_vector (SCM_MAKINUM (4), SCM_UNSPECIFIED); |
0f2d19dd | 316 | ve = SCM_VELTS (result); |
0f2d19dd | 317 | if (SCM_UNBNDP (name) || (name == SCM_BOOL_F)) |
3d781d49 JB |
318 | { |
319 | SCM_SYSCALL (entry = getgrent ()); | |
320 | if (! entry) | |
321 | { | |
3d781d49 JB |
322 | return SCM_BOOL_F; |
323 | } | |
324 | } | |
0f2d19dd JB |
325 | else if (SCM_INUMP (name)) |
326 | SCM_SYSCALL (entry = getgrgid (SCM_INUM (name))); | |
327 | else | |
328 | { | |
1bbd0b84 | 329 | SCM_VALIDATE_ROSTRING(1,name); |
89958ad0 | 330 | SCM_COERCE_SUBSTR (name); |
ae2fa5bc | 331 | SCM_SYSCALL (entry = getgrnam (SCM_ROCHARS (name))); |
0f2d19dd JB |
332 | } |
333 | if (!entry) | |
1bbd0b84 | 334 | SCM_SYSERROR; |
02b754d3 | 335 | |
0f2d19dd JB |
336 | ve[0] = scm_makfrom0str (entry->gr_name); |
337 | ve[1] = scm_makfrom0str (entry->gr_passwd); | |
338 | ve[2] = scm_ulong2num ((unsigned long) entry->gr_gid); | |
339 | ve[3] = scm_makfromstrs (-1, entry->gr_mem); | |
0f2d19dd JB |
340 | return result; |
341 | } | |
1bbd0b84 | 342 | #undef FUNC_NAME |
0f2d19dd JB |
343 | |
344 | ||
345 | ||
a1ec6916 | 346 | SCM_DEFINE (scm_setgrent, "setgr", 0, 1, 0, |
1bbd0b84 | 347 | (SCM arg), |
4079f87e GB |
348 | "If called with a true argument, initialize or reset the group data |
349 | stream. Otherwise, close the stream. The @code{setgrent} and | |
350 | @code{endgrent} procedures are implemented on top of this.") | |
1bbd0b84 | 351 | #define FUNC_NAME s_scm_setgrent |
0f2d19dd JB |
352 | { |
353 | if (SCM_UNBNDP (arg) || SCM_FALSEP (arg)) | |
354 | endgrent (); | |
355 | else | |
356 | setgrent (); | |
357 | return SCM_UNSPECIFIED; | |
358 | } | |
1bbd0b84 | 359 | #undef FUNC_NAME |
0f2d19dd JB |
360 | |
361 | ||
362 | ||
a1ec6916 | 363 | SCM_DEFINE (scm_kill, "kill", 2, 0, 0, |
1bbd0b84 | 364 | (SCM pid, SCM sig), |
4079f87e GB |
365 | "Sends a signal to the specified process or group of processes. |
366 | ||
367 | @var{pid} specifies the processes to which the signal is sent: | |
368 | ||
369 | @table @r | |
370 | @item @var{pid} greater than 0 | |
371 | The process whose identifier is @var{pid}. | |
372 | @item @var{pid} equal to 0 | |
373 | All processes in the current process group. | |
374 | @item @var{pid} less than -1 | |
375 | The process group whose identifier is -@var{pid} | |
376 | @item @var{pid} equal to -1 | |
377 | If the process is privileged, all processes except for some special | |
378 | system processes. Otherwise, all processes with the current effective | |
379 | user ID. | |
380 | @end table | |
381 | ||
382 | @var{sig} should be specified using a variable corresponding to | |
383 | the Unix symbolic name, e.g., | |
384 | ||
385 | @defvar SIGHUP | |
386 | Hang-up signal. | |
387 | @end defvar | |
388 | ||
389 | @defvar SIGINT | |
390 | Interrupt signal. | |
391 | @end defvar") | |
1bbd0b84 | 392 | #define FUNC_NAME s_scm_kill |
0f2d19dd | 393 | { |
47c6b75e GB |
394 | SCM_VALIDATE_INUM(1,pid); |
395 | SCM_VALIDATE_INUM(2,sig); | |
0f2d19dd | 396 | /* Signal values are interned in scm_init_posix(). */ |
02b754d3 | 397 | if (kill ((int) SCM_INUM (pid), (int) SCM_INUM (sig)) != 0) |
1bbd0b84 | 398 | SCM_SYSERROR; |
02b754d3 | 399 | return SCM_UNSPECIFIED; |
0f2d19dd | 400 | } |
1bbd0b84 | 401 | #undef FUNC_NAME |
0f2d19dd | 402 | |
d00ae47e | 403 | #ifdef HAVE_WAITPID |
a1ec6916 | 404 | SCM_DEFINE (scm_waitpid, "waitpid", 1, 1, 0, |
1bbd0b84 | 405 | (SCM pid, SCM options), |
4079f87e GB |
406 | "This procedure collects status information from a child process which |
407 | has terminated or (optionally) stopped. Normally it will | |
408 | suspend the calling process until this can be done. If more than one | |
409 | child process is eligible then one will be chosen by the operating system. | |
410 | ||
411 | The value of @var{pid} determines the behaviour: | |
412 | ||
413 | @table @r | |
414 | @item @var{pid} greater than 0 | |
415 | Request status information from the specified child process. | |
416 | @item @var{pid} equal to -1 or WAIT_ANY | |
417 | Request status information for any child process. | |
418 | @item @var{pid} equal to 0 or WAIT_MYPGRP | |
419 | Request status information for any child process in the current process | |
420 | group. | |
421 | @item @var{pid} less than -1 | |
422 | Request status information for any child process whose process group ID | |
423 | is -@var{PID}. | |
424 | @end table | |
425 | ||
426 | The @var{options} argument, if supplied, should be the bitwise OR of the | |
427 | values of zero or more of the following variables: | |
428 | ||
429 | @defvar WNOHANG | |
430 | Return immediately even if there are no child processes to be collected. | |
431 | @end defvar | |
432 | ||
433 | @defvar WUNTRACED | |
434 | Report status information for stopped processes as well as terminated | |
435 | processes. | |
436 | @end defvar | |
437 | ||
438 | The return value is a pair containing: | |
439 | ||
440 | @enumerate | |
441 | @item | |
442 | The process ID of the child process, or 0 if @code{WNOHANG} was | |
443 | specified and no process was collected. | |
444 | @item | |
445 | The integer status value. | |
446 | @end enumerate") | |
1bbd0b84 | 447 | #define FUNC_NAME s_scm_waitpid |
0f2d19dd JB |
448 | { |
449 | int i; | |
450 | int status; | |
451 | int ioptions; | |
47c6b75e | 452 | SCM_VALIDATE_INUM(1,pid); |
0f2d19dd JB |
453 | if (SCM_UNBNDP (options)) |
454 | ioptions = 0; | |
455 | else | |
456 | { | |
47c6b75e | 457 | SCM_VALIDATE_INUM(2,options); |
0f2d19dd JB |
458 | /* Flags are interned in scm_init_posix. */ |
459 | ioptions = SCM_INUM (options); | |
460 | } | |
461 | SCM_SYSCALL (i = waitpid (SCM_INUM (pid), &status, ioptions)); | |
02b754d3 | 462 | if (i == -1) |
1bbd0b84 | 463 | SCM_SYSERROR; |
02b754d3 | 464 | return scm_cons (SCM_MAKINUM (0L + i), SCM_MAKINUM (0L + status)); |
0f2d19dd | 465 | } |
1bbd0b84 | 466 | #undef FUNC_NAME |
d00ae47e | 467 | #endif /* HAVE_WAITPID */ |
0f2d19dd | 468 | |
a1ec6916 | 469 | SCM_DEFINE (scm_status_exit_val, "status:exit-val", 1, 0, 0, |
1bbd0b84 | 470 | (SCM status), |
4079f87e GB |
471 | "Returns the exit status value, as would be |
472 | set if a process ended normally through a | |
473 | call to @code{exit} or @code{_exit}, if any, otherwise @code{#f}.") | |
1bbd0b84 | 474 | #define FUNC_NAME s_scm_status_exit_val |
67ec3667 | 475 | { |
e67dc2be JB |
476 | int lstatus; |
477 | ||
47c6b75e | 478 | SCM_VALIDATE_INUM(1,status); |
e67dc2be JB |
479 | |
480 | /* On Ultrix, the WIF... macros assume their argument is an lvalue; | |
481 | go figure. SCM_INUM does not yield an lvalue. */ | |
482 | lstatus = SCM_INUM (status); | |
483 | if (WIFEXITED (lstatus)) | |
484 | return (SCM_MAKINUM (WEXITSTATUS (lstatus))); | |
67ec3667 GH |
485 | else |
486 | return SCM_BOOL_F; | |
487 | } | |
1bbd0b84 | 488 | #undef FUNC_NAME |
e67dc2be | 489 | |
a1ec6916 | 490 | SCM_DEFINE (scm_status_term_sig, "status:term-sig", 1, 0, 0, |
1bbd0b84 | 491 | (SCM status), |
4079f87e GB |
492 | "Returns the signal number which terminated the |
493 | process, if any, otherwise @code{#f}.") | |
1bbd0b84 | 494 | #define FUNC_NAME s_scm_status_term_sig |
67ec3667 | 495 | { |
e67dc2be JB |
496 | int lstatus; |
497 | ||
47c6b75e | 498 | SCM_VALIDATE_INUM(1,status); |
e67dc2be JB |
499 | |
500 | lstatus = SCM_INUM (status); | |
501 | if (WIFSIGNALED (lstatus)) | |
502 | return SCM_MAKINUM (WTERMSIG (lstatus)); | |
67ec3667 GH |
503 | else |
504 | return SCM_BOOL_F; | |
505 | } | |
1bbd0b84 | 506 | #undef FUNC_NAME |
0f2d19dd | 507 | |
a1ec6916 | 508 | SCM_DEFINE (scm_status_stop_sig, "status:stop-sig", 1, 0, 0, |
1bbd0b84 | 509 | (SCM status), |
4079f87e GB |
510 | "Returns the signal number which stopped the |
511 | process, if any, otherwise @code{#f}.") | |
1bbd0b84 | 512 | #define FUNC_NAME s_scm_status_stop_sig |
67ec3667 | 513 | { |
e67dc2be JB |
514 | int lstatus; |
515 | ||
47c6b75e | 516 | SCM_VALIDATE_INUM(1,status); |
e67dc2be JB |
517 | |
518 | lstatus = SCM_INUM (status); | |
519 | if (WIFSTOPPED (lstatus)) | |
520 | return SCM_MAKINUM (WSTOPSIG (lstatus)); | |
67ec3667 GH |
521 | else |
522 | return SCM_BOOL_F; | |
523 | } | |
1bbd0b84 | 524 | #undef FUNC_NAME |
0f2d19dd | 525 | |
a1ec6916 | 526 | SCM_DEFINE (scm_getppid, "getppid", 0, 0, 0, |
1bbd0b84 | 527 | (), |
4079f87e | 528 | "Returns an integer representing the process ID of the parent process.") |
1bbd0b84 | 529 | #define FUNC_NAME s_scm_getppid |
0f2d19dd JB |
530 | { |
531 | return SCM_MAKINUM (0L + getppid ()); | |
532 | } | |
1bbd0b84 | 533 | #undef FUNC_NAME |
0f2d19dd JB |
534 | |
535 | ||
536 | ||
a1ec6916 | 537 | SCM_DEFINE (scm_getuid, "getuid", 0, 0, 0, |
1bbd0b84 | 538 | (), |
4079f87e | 539 | "Returns an integer representing the current real user ID.") |
1bbd0b84 | 540 | #define FUNC_NAME s_scm_getuid |
0f2d19dd JB |
541 | { |
542 | return SCM_MAKINUM (0L + getuid ()); | |
543 | } | |
1bbd0b84 | 544 | #undef FUNC_NAME |
0f2d19dd JB |
545 | |
546 | ||
547 | ||
a1ec6916 | 548 | SCM_DEFINE (scm_getgid, "getgid", 0, 0, 0, |
1bbd0b84 | 549 | (), |
4079f87e | 550 | "Returns an integer representing the current real group ID.") |
1bbd0b84 | 551 | #define FUNC_NAME s_scm_getgid |
0f2d19dd JB |
552 | { |
553 | return SCM_MAKINUM (0L + getgid ()); | |
554 | } | |
1bbd0b84 | 555 | #undef FUNC_NAME |
0f2d19dd JB |
556 | |
557 | ||
558 | ||
a1ec6916 | 559 | SCM_DEFINE (scm_geteuid, "geteuid", 0, 0, 0, |
1bbd0b84 | 560 | (), |
4079f87e GB |
561 | "Returns an integer representing the current effective user ID. |
562 | If the system does not support effective IDs, then the real ID | |
563 | is returned. @code{(feature? 'EIDs)} reports whether the system | |
564 | supports effective IDs.") | |
1bbd0b84 | 565 | #define FUNC_NAME s_scm_geteuid |
0f2d19dd JB |
566 | { |
567 | #ifdef HAVE_GETEUID | |
568 | return SCM_MAKINUM (0L + geteuid ()); | |
569 | #else | |
570 | return SCM_MAKINUM (0L + getuid ()); | |
571 | #endif | |
572 | } | |
1bbd0b84 | 573 | #undef FUNC_NAME |
0f2d19dd JB |
574 | |
575 | ||
576 | ||
a1ec6916 | 577 | SCM_DEFINE (scm_getegid, "getegid", 0, 0, 0, |
1bbd0b84 | 578 | (), |
4079f87e GB |
579 | "Returns an integer representing the current effective group ID. |
580 | If the system does not support effective IDs, then the real ID | |
581 | is returned. @code{(feature? 'EIDs)} reports whether the system | |
582 | supports effective IDs.") | |
1bbd0b84 | 583 | #define FUNC_NAME s_scm_getegid |
0f2d19dd JB |
584 | { |
585 | #ifdef HAVE_GETEUID | |
586 | return SCM_MAKINUM (0L + getegid ()); | |
587 | #else | |
588 | return SCM_MAKINUM (0L + getgid ()); | |
589 | #endif | |
590 | } | |
1bbd0b84 | 591 | #undef FUNC_NAME |
0f2d19dd JB |
592 | |
593 | ||
a1ec6916 | 594 | SCM_DEFINE (scm_setuid, "setuid", 1, 0, 0, |
1bbd0b84 | 595 | (SCM id), |
4079f87e GB |
596 | "Sets both the real and effective user IDs to the integer @var{id}, provided |
597 | the process has appropriate privileges. | |
598 | The return value is unspecified.") | |
1bbd0b84 | 599 | #define FUNC_NAME s_scm_setuid |
0f2d19dd | 600 | { |
47c6b75e | 601 | SCM_VALIDATE_INUM(1,id); |
02b754d3 | 602 | if (setuid (SCM_INUM (id)) != 0) |
1bbd0b84 | 603 | SCM_SYSERROR; |
02b754d3 | 604 | return SCM_UNSPECIFIED; |
0f2d19dd | 605 | } |
1bbd0b84 | 606 | #undef FUNC_NAME |
0f2d19dd | 607 | |
a1ec6916 | 608 | SCM_DEFINE (scm_setgid, "setgid", 1, 0, 0, |
1bbd0b84 | 609 | (SCM id), |
4079f87e GB |
610 | "Sets both the real and effective group IDs to the integer @var{id}, provided |
611 | the process has appropriate privileges. | |
612 | The return value is unspecified.") | |
1bbd0b84 | 613 | #define FUNC_NAME s_scm_setgid |
0f2d19dd | 614 | { |
47c6b75e | 615 | SCM_VALIDATE_INUM(1,id); |
02b754d3 | 616 | if (setgid (SCM_INUM (id)) != 0) |
1bbd0b84 | 617 | SCM_SYSERROR; |
02b754d3 | 618 | return SCM_UNSPECIFIED; |
0f2d19dd | 619 | } |
1bbd0b84 | 620 | #undef FUNC_NAME |
0f2d19dd | 621 | |
a1ec6916 | 622 | SCM_DEFINE (scm_seteuid, "seteuid", 1, 0, 0, |
1bbd0b84 | 623 | (SCM id), |
4079f87e GB |
624 | "Sets the effective user ID to the integer @var{id}, provided the process |
625 | has appropriate privileges. If effective IDs are not supported, the | |
626 | real ID is set instead -- @code{(feature? 'EIDs)} reports whether the | |
627 | system supports effective IDs. | |
628 | The return value is unspecified.") | |
1bbd0b84 | 629 | #define FUNC_NAME s_scm_seteuid |
0f2d19dd | 630 | { |
02b754d3 GH |
631 | int rv; |
632 | ||
47c6b75e | 633 | SCM_VALIDATE_INUM(1,id); |
0f2d19dd | 634 | #ifdef HAVE_SETEUID |
02b754d3 | 635 | rv = seteuid (SCM_INUM (id)); |
0f2d19dd | 636 | #else |
02b754d3 | 637 | rv = setuid (SCM_INUM (id)); |
0f2d19dd | 638 | #endif |
02b754d3 | 639 | if (rv != 0) |
1bbd0b84 | 640 | SCM_SYSERROR; |
02b754d3 | 641 | return SCM_UNSPECIFIED; |
0f2d19dd | 642 | } |
1bbd0b84 | 643 | #undef FUNC_NAME |
0f2d19dd | 644 | |
0e958795 | 645 | #ifdef HAVE_SETEGID |
a1ec6916 | 646 | SCM_DEFINE (scm_setegid, "setegid", 1, 0, 0, |
1bbd0b84 | 647 | (SCM id), |
4079f87e GB |
648 | "Sets the effective group ID to the integer @var{id}, provided the process |
649 | has appropriate privileges. If effective IDs are not supported, the | |
650 | real ID is set instead -- @code{(feature? 'EIDs)} reports whether the | |
651 | system supports effective IDs. | |
652 | The return value is unspecified.") | |
1bbd0b84 | 653 | #define FUNC_NAME s_scm_setegid |
0f2d19dd | 654 | { |
02b754d3 GH |
655 | int rv; |
656 | ||
47c6b75e | 657 | SCM_VALIDATE_INUM(1,id); |
0f2d19dd | 658 | #ifdef HAVE_SETEUID |
02b754d3 | 659 | rv = setegid (SCM_INUM (id)); |
0f2d19dd | 660 | #else |
02b754d3 | 661 | rv = setgid (SCM_INUM (id)); |
0f2d19dd | 662 | #endif |
02b754d3 | 663 | if (rv != 0) |
1bbd0b84 | 664 | SCM_SYSERROR; |
02b754d3 GH |
665 | return SCM_UNSPECIFIED; |
666 | ||
0f2d19dd | 667 | } |
1bbd0b84 | 668 | #undef FUNC_NAME |
0e958795 | 669 | #endif |
0f2d19dd | 670 | |
a1ec6916 | 671 | SCM_DEFINE (scm_getpgrp, "getpgrp", 0, 0, 0, |
1bbd0b84 | 672 | (), |
4079f87e GB |
673 | "Returns an integer representing the current process group ID. |
674 | This is the POSIX definition, not BSD.") | |
1bbd0b84 | 675 | #define FUNC_NAME s_scm_getpgrp |
0f2d19dd JB |
676 | { |
677 | int (*fn)(); | |
4625e44f | 678 | fn = (int (*) ()) getpgrp; |
0f2d19dd JB |
679 | return SCM_MAKINUM (fn (0)); |
680 | } | |
1bbd0b84 | 681 | #undef FUNC_NAME |
0f2d19dd | 682 | |
a1ec6916 | 683 | SCM_DEFINE (scm_setpgid, "setpgid", 2, 0, 0, |
1bbd0b84 | 684 | (SCM pid, SCM pgid), |
4079f87e GB |
685 | "Move the process @var{pid} into the process group @var{pgid}. @var{pid} or |
686 | @var{pgid} must be integers: they can be zero to indicate the ID of the | |
687 | current process. | |
688 | Fails on systems that do not support job control. | |
689 | The return value is unspecified.") | |
1bbd0b84 | 690 | #define FUNC_NAME s_scm_setpgid |
0f2d19dd | 691 | { |
1fd838af | 692 | #ifdef HAVE_SETPGID |
47c6b75e GB |
693 | SCM_VALIDATE_INUM(1,pid); |
694 | SCM_VALIDATE_INUM(2,pgid); | |
02b754d3 GH |
695 | /* FIXME(?): may be known as setpgrp. */ |
696 | if (setpgid (SCM_INUM (pid), SCM_INUM (pgid)) != 0) | |
1bbd0b84 | 697 | SCM_SYSERROR; |
02b754d3 | 698 | return SCM_UNSPECIFIED; |
1fd838af | 699 | #else |
1bbd0b84 | 700 | SCM_SYSMISSING; |
1fd838af JB |
701 | /* not reached. */ |
702 | return SCM_BOOL_F; | |
703 | #endif | |
0f2d19dd | 704 | } |
1bbd0b84 | 705 | #undef FUNC_NAME |
0f2d19dd | 706 | |
a1ec6916 | 707 | SCM_DEFINE (scm_setsid, "setsid", 0, 0, 0, |
1bbd0b84 | 708 | (), |
4079f87e GB |
709 | "Creates a new session. The current process becomes the session leader |
710 | and is put in a new process group. The process will be detached | |
711 | from its controlling terminal if it has one. | |
712 | The return value is an integer representing the new process group ID.") | |
1bbd0b84 | 713 | #define FUNC_NAME s_scm_setsid |
0f2d19dd | 714 | { |
1fd838af | 715 | #ifdef HAVE_SETSID |
0f2d19dd | 716 | pid_t sid = setsid (); |
02b754d3 | 717 | if (sid == -1) |
1bbd0b84 | 718 | SCM_SYSERROR; |
02b754d3 | 719 | return SCM_UNSPECIFIED; |
1fd838af | 720 | #else |
1bbd0b84 | 721 | SCM_SYSMISSING; |
1fd838af JB |
722 | /* not reached. */ |
723 | return SCM_BOOL_F; | |
724 | #endif | |
0f2d19dd | 725 | } |
1bbd0b84 | 726 | #undef FUNC_NAME |
0f2d19dd | 727 | |
a1ec6916 | 728 | SCM_DEFINE (scm_ttyname, "ttyname", 1, 0, 0, |
1bbd0b84 | 729 | (SCM port), |
4079f87e GB |
730 | "Returns a string with the name of the serial terminal device underlying |
731 | @var{port}.") | |
1bbd0b84 | 732 | #define FUNC_NAME s_scm_ttyname |
0f2d19dd JB |
733 | { |
734 | char *ans; | |
735 | int fd; | |
78446828 MV |
736 | |
737 | port = SCM_COERCE_OUTPORT (port); | |
1bbd0b84 | 738 | SCM_VALIDATE_OPPORT(1,port); |
0f2d19dd JB |
739 | if (scm_tc16_fport != SCM_TYP16 (port)) |
740 | return SCM_BOOL_F; | |
ee149d03 | 741 | fd = SCM_FPORT_FDES (port); |
02b754d3 GH |
742 | SCM_SYSCALL (ans = ttyname (fd)); |
743 | if (!ans) | |
1bbd0b84 | 744 | SCM_SYSERROR; |
0f2d19dd | 745 | /* ans could be overwritten by another call to ttyname */ |
02b754d3 | 746 | return (scm_makfrom0str (ans)); |
0f2d19dd | 747 | } |
1bbd0b84 | 748 | #undef FUNC_NAME |
0f2d19dd JB |
749 | |
750 | ||
a1ec6916 | 751 | SCM_DEFINE (scm_ctermid, "ctermid", 0, 0, 0, |
1bbd0b84 | 752 | (), |
4079f87e GB |
753 | "Returns a string containing the file name of the controlling terminal |
754 | for the current process.") | |
1bbd0b84 | 755 | #define FUNC_NAME s_scm_ctermid |
0f2d19dd | 756 | { |
1fd838af | 757 | #ifdef HAVE_CTERMID |
0f2d19dd | 758 | char *result = ctermid (NULL); |
02b754d3 | 759 | if (*result == '\0') |
1bbd0b84 | 760 | SCM_SYSERROR; |
02b754d3 | 761 | return scm_makfrom0str (result); |
1fd838af | 762 | #else |
1bbd0b84 | 763 | SCM_SYSMISSING; |
1fd838af JB |
764 | /* not reached. */ |
765 | return SCM_BOOL_F; | |
766 | #endif | |
0f2d19dd | 767 | } |
1bbd0b84 | 768 | #undef FUNC_NAME |
0f2d19dd | 769 | |
a1ec6916 | 770 | SCM_DEFINE (scm_tcgetpgrp, "tcgetpgrp", 1, 0, 0, |
1bbd0b84 | 771 | (SCM port), |
4079f87e GB |
772 | "Returns the process group ID of the foreground |
773 | process group associated with the terminal open on the file descriptor | |
774 | underlying @var{port}. | |
775 | ||
776 | If there is no foreground process group, the return value is a | |
777 | number greater than 1 that does not match the process group ID | |
778 | of any existing process group. This can happen if all of the | |
779 | processes in the job that was formerly the foreground job have | |
780 | terminated, and no other job has yet been moved into the | |
781 | foreground.") | |
1bbd0b84 | 782 | #define FUNC_NAME s_scm_tcgetpgrp |
0f2d19dd | 783 | { |
1fd838af | 784 | #ifdef HAVE_TCGETPGRP |
0f2d19dd JB |
785 | int fd; |
786 | pid_t pgid; | |
78446828 MV |
787 | |
788 | port = SCM_COERCE_OUTPORT (port); | |
789 | ||
1bbd0b84 | 790 | SCM_VALIDATE_OPFPORT(1,port); |
ee149d03 JB |
791 | fd = SCM_FPORT_FDES (port); |
792 | if ((pgid = tcgetpgrp (fd)) == -1) | |
1bbd0b84 | 793 | SCM_SYSERROR; |
02b754d3 | 794 | return SCM_MAKINUM (pgid); |
1fd838af | 795 | #else |
1bbd0b84 | 796 | SCM_SYSMISSING; |
1fd838af JB |
797 | /* not reached. */ |
798 | return SCM_BOOL_F; | |
799 | #endif | |
1bbd0b84 GB |
800 | } |
801 | #undef FUNC_NAME | |
0f2d19dd | 802 | |
a1ec6916 | 803 | SCM_DEFINE (scm_tcsetpgrp, "tcsetpgrp", 2, 0, 0, |
1bbd0b84 | 804 | (SCM port, SCM pgid), |
4079f87e GB |
805 | "Set the foreground process group ID for the terminal used by the file |
806 | descriptor underlying @var{port} to the integer @var{pgid}. | |
807 | The calling process | |
808 | must be a member of the same session as @var{pgid} and must have the same | |
809 | controlling terminal. The return value is unspecified.") | |
1bbd0b84 | 810 | #define FUNC_NAME s_scm_tcsetpgrp |
0f2d19dd | 811 | { |
1fd838af | 812 | #ifdef HAVE_TCSETPGRP |
0f2d19dd | 813 | int fd; |
78446828 MV |
814 | |
815 | port = SCM_COERCE_OUTPORT (port); | |
816 | ||
1bbd0b84 | 817 | SCM_VALIDATE_OPFPORT(1,port); |
47c6b75e | 818 | SCM_VALIDATE_INUM(2,pgid); |
ee149d03 JB |
819 | fd = SCM_FPORT_FDES (port); |
820 | if (tcsetpgrp (fd, SCM_INUM (pgid)) == -1) | |
1bbd0b84 | 821 | SCM_SYSERROR; |
02b754d3 | 822 | return SCM_UNSPECIFIED; |
1fd838af | 823 | #else |
1bbd0b84 | 824 | SCM_SYSMISSING; |
1fd838af JB |
825 | /* not reached. */ |
826 | return SCM_BOOL_F; | |
827 | #endif | |
1bbd0b84 GB |
828 | } |
829 | #undef FUNC_NAME | |
830 | ||
0f2d19dd JB |
831 | |
832 | /* Copy exec args from an SCM vector into a new C array. */ | |
1cc91f1b | 833 | |
0f2d19dd | 834 | static char ** |
3eeba8d4 | 835 | scm_convert_exec_args (SCM args, int pos, const char *subr) |
0f2d19dd JB |
836 | { |
837 | char **execargv; | |
838 | int num_args; | |
839 | int i; | |
6afcd3b2 GH |
840 | |
841 | SCM_ASSERT (SCM_NULLP (args) | |
0c95b57d | 842 | || (SCM_CONSP (args)), |
6afcd3b2 | 843 | args, pos, subr); |
0f2d19dd JB |
844 | num_args = scm_ilength (args); |
845 | execargv = (char **) | |
6afcd3b2 | 846 | scm_must_malloc ((num_args + 1) * sizeof (char *), subr); |
0f2d19dd JB |
847 | for (i = 0; SCM_NNULLP (args); args = SCM_CDR (args), ++i) |
848 | { | |
849 | scm_sizet len; | |
850 | char *dst; | |
851 | char *src; | |
0c95b57d | 852 | SCM_ASSERT (SCM_ROSTRINGP (SCM_CAR (args)), |
6afcd3b2 | 853 | SCM_CAR (args), SCM_ARGn, subr); |
0f2d19dd | 854 | len = 1 + SCM_ROLENGTH (SCM_CAR (args)); |
6afcd3b2 | 855 | dst = (char *) scm_must_malloc ((long) len, subr); |
0f2d19dd JB |
856 | src = SCM_ROCHARS (SCM_CAR (args)); |
857 | while (len--) | |
858 | dst[len] = src[len]; | |
859 | execargv[i] = dst; | |
860 | } | |
861 | execargv[i] = 0; | |
0f2d19dd JB |
862 | return execargv; |
863 | } | |
864 | ||
a1ec6916 | 865 | SCM_DEFINE (scm_execl, "execl", 1, 0, 1, |
1bbd0b84 | 866 | (SCM filename, SCM args), |
4079f87e GB |
867 | "Executes the file named by @var{path} as a new process image. |
868 | The remaining arguments are supplied to the process; from a C program | |
869 | they are accessable as the @code{argv} argument to @code{main}. | |
870 | Conventionally the first @var{arg} is the same as @var{path}. | |
871 | All arguments must be strings. | |
872 | ||
873 | If @var{arg} is missing, @var{path} is executed with a null | |
874 | argument list, which may have system-dependent side-effects. | |
875 | ||
876 | This procedure is currently implemented using the @code{execv} system | |
877 | call, but we call it @code{execl} because of its Scheme calling interface.") | |
1bbd0b84 | 878 | #define FUNC_NAME s_scm_execl |
0f2d19dd JB |
879 | { |
880 | char **execargv; | |
1bbd0b84 | 881 | SCM_VALIDATE_ROSTRING(1,filename); |
6afcd3b2 | 882 | SCM_COERCE_SUBSTR (filename); |
1bbd0b84 | 883 | execargv = scm_convert_exec_args (args, SCM_ARG2, FUNC_NAME); |
0f2d19dd | 884 | execv (SCM_ROCHARS (filename), execargv); |
1bbd0b84 | 885 | SCM_SYSERROR; |
02b754d3 GH |
886 | /* not reached. */ |
887 | return SCM_BOOL_F; | |
0f2d19dd | 888 | } |
1bbd0b84 | 889 | #undef FUNC_NAME |
0f2d19dd | 890 | |
a1ec6916 | 891 | SCM_DEFINE (scm_execlp, "execlp", 1, 0, 1, |
1bbd0b84 | 892 | (SCM filename, SCM args), |
4079f87e GB |
893 | "Similar to @code{execl}, however if |
894 | @var{filename} does not contain a slash | |
895 | then the file to execute will be located by searching the | |
896 | directories listed in the @code{PATH} environment variable. | |
897 | ||
898 | This procedure is currently implemented using the @code{execlv} system | |
899 | call, but we call it @code{execlp} because of its Scheme calling interface.") | |
1bbd0b84 | 900 | #define FUNC_NAME s_scm_execlp |
0f2d19dd JB |
901 | { |
902 | char **execargv; | |
1bbd0b84 | 903 | SCM_VALIDATE_ROSTRING(1,filename); |
6afcd3b2 | 904 | SCM_COERCE_SUBSTR (filename); |
1bbd0b84 | 905 | execargv = scm_convert_exec_args (args, SCM_ARG2, FUNC_NAME); |
0f2d19dd | 906 | execvp (SCM_ROCHARS (filename), execargv); |
1bbd0b84 | 907 | SCM_SYSERROR; |
02b754d3 GH |
908 | /* not reached. */ |
909 | return SCM_BOOL_F; | |
0f2d19dd | 910 | } |
1bbd0b84 | 911 | #undef FUNC_NAME |
0f2d19dd | 912 | |
6afcd3b2 | 913 | static char ** |
3eeba8d4 | 914 | environ_list_to_c (SCM envlist, int arg, const char *proc) |
6afcd3b2 GH |
915 | { |
916 | int num_strings; | |
917 | char **result; | |
918 | int i = 0; | |
919 | ||
0c95b57d | 920 | SCM_ASSERT (SCM_NULLP (envlist) || SCM_CONSP (envlist), |
6afcd3b2 GH |
921 | envlist, arg, proc); |
922 | num_strings = scm_ilength (envlist); | |
923 | result = (char **) malloc ((num_strings + 1) * sizeof (char *)); | |
924 | if (result == NULL) | |
925 | scm_memory_error (proc); | |
926 | while (SCM_NNULLP (envlist)) | |
927 | { | |
928 | int len; | |
929 | char *src; | |
930 | ||
368cf54d | 931 | SCM_ASSERT (SCM_ROSTRINGP (SCM_CAR (envlist)), |
6afcd3b2 GH |
932 | envlist, arg, proc); |
933 | len = 1 + SCM_ROLENGTH (SCM_CAR (envlist)); | |
934 | result[i] = malloc ((long) len); | |
935 | if (result[i] == NULL) | |
936 | scm_memory_error (proc); | |
937 | src = SCM_ROCHARS (SCM_CAR (envlist)); | |
938 | while (len--) | |
939 | result[i][len] = src[len]; | |
940 | envlist = SCM_CDR (envlist); | |
941 | i++; | |
942 | } | |
943 | result[i] = 0; | |
6afcd3b2 GH |
944 | return result; |
945 | } | |
946 | ||
a1ec6916 | 947 | SCM_DEFINE (scm_execle, "execle", 2, 0, 1, |
1bbd0b84 | 948 | (SCM filename, SCM env, SCM args), |
4079f87e GB |
949 | "Similar to @code{execl}, but the environment of the new process is |
950 | specified by @var{env}, which must be a list of strings as returned by the | |
951 | @code{environ} procedure. | |
952 | ||
953 | This procedure is currently implemented using the @code{execve} system | |
954 | call, but we call it @code{execle} because of its Scheme calling interface.") | |
1bbd0b84 | 955 | #define FUNC_NAME s_scm_execle |
6afcd3b2 GH |
956 | { |
957 | char **execargv; | |
958 | char **exec_env; | |
959 | ||
1bbd0b84 | 960 | SCM_VALIDATE_ROSTRING(1,filename); |
6afcd3b2 GH |
961 | SCM_COERCE_SUBSTR (filename); |
962 | ||
1bbd0b84 GB |
963 | execargv = scm_convert_exec_args (args, SCM_ARG1, FUNC_NAME); |
964 | exec_env = environ_list_to_c (env, SCM_ARG2, FUNC_NAME); | |
6afcd3b2 | 965 | execve (SCM_ROCHARS (filename), execargv, exec_env); |
1bbd0b84 | 966 | SCM_SYSERROR; |
6afcd3b2 GH |
967 | /* not reached. */ |
968 | return SCM_BOOL_F; | |
969 | } | |
1bbd0b84 | 970 | #undef FUNC_NAME |
6afcd3b2 | 971 | |
a1ec6916 | 972 | SCM_DEFINE (scm_fork, "primitive-fork", 0, 0, 0, |
1bbd0b84 | 973 | (), |
4079f87e GB |
974 | "Creates a new \"child\" process by duplicating the current \"parent\" process. |
975 | In the child the return value is 0. In the parent the return value is | |
976 | the integer process ID of the child. | |
977 | ||
978 | This procedure has been renamed from @code{fork} to avoid a naming conflict | |
979 | with the scsh fork.") | |
1bbd0b84 | 980 | #define FUNC_NAME s_scm_fork |
0f2d19dd | 981 | { |
bab0f4e5 | 982 | int pid; |
0f2d19dd JB |
983 | pid = fork (); |
984 | if (pid == -1) | |
1bbd0b84 | 985 | SCM_SYSERROR; |
02b754d3 | 986 | return SCM_MAKINUM (0L+pid); |
0f2d19dd | 987 | } |
1bbd0b84 | 988 | #undef FUNC_NAME |
0f2d19dd JB |
989 | |
990 | ||
a1ec6916 | 991 | SCM_DEFINE (scm_uname, "uname", 0, 0, 0, |
1bbd0b84 | 992 | (), |
4079f87e GB |
993 | "Returns an object with some information about the computer system the |
994 | program is running on.") | |
1bbd0b84 | 995 | #define FUNC_NAME s_scm_uname |
0f2d19dd JB |
996 | { |
997 | #ifdef HAVE_UNAME | |
998 | struct utsname buf; | |
a8741caa | 999 | SCM ans = scm_make_vector (SCM_MAKINUM(5), SCM_UNSPECIFIED); |
0f2d19dd | 1000 | SCM *ve = SCM_VELTS (ans); |
e1a191a8 | 1001 | if (uname (&buf) < 0) |
1bbd0b84 | 1002 | SCM_SYSERROR; |
0f2d19dd JB |
1003 | ve[0] = scm_makfrom0str (buf.sysname); |
1004 | ve[1] = scm_makfrom0str (buf.nodename); | |
1005 | ve[2] = scm_makfrom0str (buf.release); | |
1006 | ve[3] = scm_makfrom0str (buf.version); | |
1007 | ve[4] = scm_makfrom0str (buf.machine); | |
1008 | /* | |
02b754d3 | 1009 | a linux special? |
0f2d19dd JB |
1010 | ve[5] = scm_makfrom0str (buf.domainname); |
1011 | */ | |
1012 | return ans; | |
1013 | #else | |
1bbd0b84 | 1014 | SCM_SYSMISSING; |
02b754d3 GH |
1015 | /* not reached. */ |
1016 | return SCM_BOOL_F; | |
0f2d19dd JB |
1017 | #endif |
1018 | } | |
1bbd0b84 | 1019 | #undef FUNC_NAME |
0f2d19dd | 1020 | |
a1ec6916 | 1021 | SCM_DEFINE (scm_environ, "environ", 0, 1, 0, |
1bbd0b84 | 1022 | (SCM env), |
4079f87e GB |
1023 | "If @var{env} is omitted, returns the current environment as a list of strings. |
1024 | Otherwise it sets the current environment, which is also the | |
1025 | default environment for child processes, to the supplied list of strings. | |
1026 | Each member of @var{env} should be of the form | |
1027 | @code{NAME=VALUE} and values of @code{NAME} should not be duplicated. | |
1028 | If @var{env} is supplied then the return value is unspecified.") | |
1bbd0b84 | 1029 | #define FUNC_NAME s_scm_environ |
0f2d19dd JB |
1030 | { |
1031 | if (SCM_UNBNDP (env)) | |
1032 | return scm_makfromstrs (-1, environ); | |
1033 | else | |
1034 | { | |
0f2d19dd | 1035 | char **new_environ; |
6afcd3b2 | 1036 | |
1bbd0b84 | 1037 | new_environ = environ_list_to_c (env, SCM_ARG1, FUNC_NAME); |
0f2d19dd JB |
1038 | /* Free the old environment, except when called for the first |
1039 | * time. | |
1040 | */ | |
1041 | { | |
1042 | char **ep; | |
1043 | static int first = 1; | |
1044 | if (!first) | |
1045 | { | |
1046 | for (ep = environ; *ep != NULL; ep++) | |
19468eff GH |
1047 | free (*ep); |
1048 | free ((char *) environ); | |
0f2d19dd JB |
1049 | } |
1050 | first = 0; | |
1051 | } | |
1052 | environ = new_environ; | |
1053 | return SCM_UNSPECIFIED; | |
1054 | } | |
1055 | } | |
1bbd0b84 | 1056 | #undef FUNC_NAME |
0f2d19dd | 1057 | |
9ee5fce4 MD |
1058 | #ifdef L_tmpnam |
1059 | ||
a1ec6916 | 1060 | SCM_DEFINE (scm_tmpnam, "tmpnam", 0, 0, 0, |
1bbd0b84 | 1061 | (), |
4079f87e GB |
1062 | "Create a new file in the file system with a unique name. The return |
1063 | value is the name of the new file. This function is implemented with | |
1064 | the @code{tmpnam} function in the system libraries.") | |
1bbd0b84 | 1065 | #define FUNC_NAME s_scm_tmpnam |
9ee5fce4 MD |
1066 | { |
1067 | char name[L_tmpnam]; | |
1068 | SCM_SYSCALL (tmpnam (name);); | |
1069 | return scm_makfrom0str (name); | |
1070 | } | |
1bbd0b84 | 1071 | #undef FUNC_NAME; |
0f2d19dd | 1072 | |
1bbd0b84 | 1073 | #endif |
1cc91f1b | 1074 | |
a1ec6916 | 1075 | SCM_DEFINE (scm_utime, "utime", 1, 2, 0, |
1bbd0b84 | 1076 | (SCM pathname, SCM actime, SCM modtime), |
4079f87e GB |
1077 | "@code{utime} sets the access and modification times for |
1078 | the file named by @var{path}. If @var{actime} or @var{modtime} | |
1079 | is not supplied, then the current time is used. | |
1080 | @var{actime} and @var{modtime} | |
1081 | must be integer time values as returned by the @code{current-time} | |
1082 | procedure. | |
1083 | ||
1084 | E.g., | |
1085 | ||
1086 | @smalllisp | |
1087 | (utime \"foo\" (- (current-time) 3600)) | |
1088 | @end smalllisp | |
1089 | ||
1090 | will set the access time to one hour in the past and the modification | |
1091 | time to the current time.") | |
1bbd0b84 | 1092 | #define FUNC_NAME s_scm_utime |
0f2d19dd JB |
1093 | { |
1094 | int rv; | |
1095 | struct utimbuf utm_tmp; | |
1096 | ||
1bbd0b84 | 1097 | SCM_VALIDATE_ROSTRING(1,pathname); |
89958ad0 | 1098 | SCM_COERCE_SUBSTR (pathname); |
0f2d19dd JB |
1099 | if (SCM_UNBNDP (actime)) |
1100 | SCM_SYSCALL (time (&utm_tmp.actime)); | |
1101 | else | |
1bbd0b84 | 1102 | utm_tmp.actime = SCM_NUM2ULONG (2,actime); |
0f2d19dd JB |
1103 | |
1104 | if (SCM_UNBNDP (modtime)) | |
1105 | SCM_SYSCALL (time (&utm_tmp.modtime)); | |
1106 | else | |
1bbd0b84 | 1107 | utm_tmp.modtime = SCM_NUM2ULONG (3,modtime); |
0f2d19dd | 1108 | |
89958ad0 | 1109 | SCM_SYSCALL (rv = utime (SCM_ROCHARS (pathname), &utm_tmp)); |
02b754d3 | 1110 | if (rv != 0) |
1bbd0b84 | 1111 | SCM_SYSERROR; |
02b754d3 | 1112 | return SCM_UNSPECIFIED; |
0f2d19dd | 1113 | } |
1bbd0b84 | 1114 | #undef FUNC_NAME |
0f2d19dd | 1115 | |
a1ec6916 | 1116 | SCM_DEFINE (scm_access, "access?", 2, 0, 0, |
1bbd0b84 | 1117 | (SCM path, SCM how), |
4079f87e GB |
1118 | "Returns @code{#t} if @var{path} corresponds to an existing |
1119 | file and the current process | |
1120 | has the type of access specified by @var{how}, otherwise | |
1121 | @code{#f}. | |
1122 | @var{how} should be specified | |
1123 | using the values of the variables listed below. Multiple values can | |
1124 | be combined using a bitwise or, in which case @code{#t} will only | |
1125 | be returned if all accesses are granted. | |
1126 | ||
1127 | Permissions are checked using the real id of the current process, | |
1128 | not the effective id, although it's the effective id which determines | |
1129 | whether the access would actually be granted. | |
1130 | ||
1131 | @defvar R_OK | |
1132 | test for read permission. | |
1133 | @end defvar | |
1134 | @defvar W_OK | |
1135 | test for write permission. | |
1136 | @end defvar | |
1137 | @defvar X_OK | |
1138 | test for execute permission. | |
1139 | @end defvar | |
1140 | @defvar F_OK | |
1141 | test for existence of the file. | |
1142 | @end defvar") | |
1bbd0b84 | 1143 | #define FUNC_NAME s_scm_access |
0f2d19dd JB |
1144 | { |
1145 | int rv; | |
1146 | ||
1bbd0b84 | 1147 | SCM_VALIDATE_ROSTRING(1,path); |
0f2d19dd JB |
1148 | if (SCM_SUBSTRP (path)) |
1149 | path = scm_makfromstr (SCM_ROCHARS (path), SCM_ROLENGTH (path), 0); | |
47c6b75e | 1150 | SCM_VALIDATE_INUM(2,how); |
0f2d19dd | 1151 | rv = access (SCM_ROCHARS (path), SCM_INUM (how)); |
156dcb09 | 1152 | return SCM_NEGATE_BOOL(rv); |
0f2d19dd | 1153 | } |
1bbd0b84 | 1154 | #undef FUNC_NAME |
0f2d19dd | 1155 | |
a1ec6916 | 1156 | SCM_DEFINE (scm_getpid, "getpid", 0, 0, 0, |
1bbd0b84 | 1157 | (), |
4079f87e | 1158 | "Returns an integer representing the current process ID.") |
1bbd0b84 | 1159 | #define FUNC_NAME s_scm_getpid |
0f2d19dd JB |
1160 | { |
1161 | return SCM_MAKINUM ((unsigned long) getpid ()); | |
1162 | } | |
1bbd0b84 | 1163 | #undef FUNC_NAME |
0f2d19dd | 1164 | |
a1ec6916 | 1165 | SCM_DEFINE (scm_putenv, "putenv", 1, 0, 0, |
1bbd0b84 | 1166 | (SCM str), |
4079f87e GB |
1167 | "Modifies the environment of the current process, which is |
1168 | also the default environment inherited by child processes. | |
1169 | ||
1170 | If @var{string} is of the form @code{NAME=VALUE} then it will be written | |
1171 | directly into the environment, replacing any existing environment string | |
1172 | with | |
1173 | name matching @code{NAME}. If @var{string} does not contain an equal | |
1174 | sign, then any existing string with name matching @var{string} will | |
1175 | be removed. | |
1176 | ||
1177 | The return value is unspecified.") | |
1bbd0b84 | 1178 | #define FUNC_NAME s_scm_putenv |
0f2d19dd | 1179 | { |
f93ddd39 | 1180 | int rv; |
19468eff | 1181 | char *ptr; |
f93ddd39 | 1182 | |
1bbd0b84 | 1183 | SCM_VALIDATE_ROSTRING(1,str); |
19468eff GH |
1184 | /* must make a new copy to be left in the environment, safe from gc. */ |
1185 | ptr = malloc (SCM_LENGTH (str) + 1); | |
1186 | if (ptr == NULL) | |
1bbd0b84 | 1187 | SCM_MEMORY_ERROR; |
89958ad0 JB |
1188 | strncpy (ptr, SCM_ROCHARS (str), SCM_LENGTH (str)); |
1189 | ptr[SCM_LENGTH(str)] = 0; | |
19468eff | 1190 | rv = putenv (ptr); |
f93ddd39 | 1191 | if (rv < 0) |
1bbd0b84 | 1192 | SCM_SYSERROR; |
f93ddd39 | 1193 | return SCM_UNSPECIFIED; |
0f2d19dd | 1194 | } |
1bbd0b84 | 1195 | #undef FUNC_NAME |
0f2d19dd | 1196 | |
a1ec6916 | 1197 | SCM_DEFINE (scm_setlocale, "setlocale", 1, 1, 0, |
1bbd0b84 | 1198 | (SCM category, SCM locale), |
4079f87e GB |
1199 | "If @var{locale} is omitted, returns the current value of the specified |
1200 | locale category | |
1201 | as a system-dependent string. | |
1202 | @var{category} should be specified using the values @code{LC_COLLATE}, | |
1203 | @code{LC_ALL} etc. | |
1204 | ||
1205 | Otherwise the specified locale category is set to | |
1206 | the string @var{locale} | |
1207 | and the new value is returned as a system-dependent string. If @var{locale} | |
1208 | is an empty string, the locale will be set using envirionment variables.") | |
1bbd0b84 | 1209 | #define FUNC_NAME s_scm_setlocale |
0f2d19dd JB |
1210 | { |
1211 | #ifdef HAVE_SETLOCALE | |
1212 | char *clocale; | |
1213 | char *rv; | |
1214 | ||
47c6b75e | 1215 | SCM_VALIDATE_INUM(1,category); |
0f2d19dd JB |
1216 | if (SCM_UNBNDP (locale)) |
1217 | { | |
1218 | clocale = NULL; | |
1219 | } | |
1220 | else | |
1221 | { | |
1bbd0b84 | 1222 | SCM_VALIDATE_ROSTRING(2,locale); |
89958ad0 JB |
1223 | SCM_COERCE_SUBSTR (locale); |
1224 | clocale = SCM_ROCHARS (locale); | |
0f2d19dd JB |
1225 | } |
1226 | ||
1227 | rv = setlocale (SCM_INUM (category), clocale); | |
02b754d3 | 1228 | if (rv == NULL) |
1bbd0b84 | 1229 | SCM_SYSERROR; |
02b754d3 | 1230 | return scm_makfrom0str (rv); |
0f2d19dd | 1231 | #else |
1bbd0b84 | 1232 | SCM_SYSMISSING; |
02b754d3 | 1233 | /* not reached. */ |
0f2d19dd JB |
1234 | return SCM_BOOL_F; |
1235 | #endif | |
1236 | } | |
1bbd0b84 | 1237 | #undef FUNC_NAME |
0f2d19dd | 1238 | |
a1ec6916 | 1239 | SCM_DEFINE (scm_mknod, "mknod", 4, 0, 0, |
1bbd0b84 | 1240 | (SCM path, SCM type, SCM perms, SCM dev), |
4079f87e GB |
1241 | "Creates a new special file, such as a file corresponding to a device. |
1242 | @var{path} specifies the name of the file. @var{type} should | |
1243 | be one of the following symbols: | |
1244 | regular, directory, symlink, block-special, char-special, | |
1245 | fifo, or socket. @var{perms} (an integer) specifies the file permissions. | |
1246 | @var{dev} (an integer) specifies which device the special file refers | |
1247 | to. Its exact interpretation depends on the kind of special file | |
1248 | being created. | |
1249 | ||
1250 | E.g., | |
1251 | @example | |
1252 | (mknod "/dev/fd0" 'block-special #o660 (+ (* 2 256) 2)) | |
1253 | @end example | |
1254 | ||
1255 | The return value is unspecified.") | |
1bbd0b84 | 1256 | #define FUNC_NAME s_scm_mknod |
0f2d19dd JB |
1257 | { |
1258 | #ifdef HAVE_MKNOD | |
1259 | int val; | |
19468eff | 1260 | char *p; |
82a76bdf | 1261 | int ctype = 0; |
19468eff | 1262 | |
1bbd0b84 GB |
1263 | SCM_VALIDATE_ROSTRING(1,path); |
1264 | SCM_VALIDATE_SYMBOL(2,type); | |
47c6b75e GB |
1265 | SCM_VALIDATE_INUM(3,perms); |
1266 | SCM_VALIDATE_INUM(4,dev); | |
89958ad0 | 1267 | SCM_COERCE_SUBSTR (path); |
19468eff GH |
1268 | |
1269 | p = SCM_CHARS (type); | |
1270 | if (strcmp (p, "regular") == 0) | |
1271 | ctype = S_IFREG; | |
1272 | else if (strcmp (p, "directory") == 0) | |
1273 | ctype = S_IFDIR; | |
1274 | else if (strcmp (p, "symlink") == 0) | |
1275 | ctype = S_IFLNK; | |
1276 | else if (strcmp (p, "block-special") == 0) | |
1277 | ctype = S_IFBLK; | |
1278 | else if (strcmp (p, "char-special") == 0) | |
1279 | ctype = S_IFCHR; | |
1280 | else if (strcmp (p, "fifo") == 0) | |
1281 | ctype = S_IFIFO; | |
1282 | else if (strcmp (p, "socket") == 0) | |
1283 | ctype = S_IFSOCK; | |
1284 | else | |
1bbd0b84 | 1285 | SCM_OUT_OF_RANGE (2,type); |
19468eff | 1286 | |
19468eff GH |
1287 | SCM_SYSCALL (val = mknod(SCM_ROCHARS(path), ctype | SCM_INUM (perms), |
1288 | SCM_INUM (dev))); | |
02b754d3 | 1289 | if (val != 0) |
1bbd0b84 | 1290 | SCM_SYSERROR; |
02b754d3 | 1291 | return SCM_UNSPECIFIED; |
0f2d19dd | 1292 | #else |
1bbd0b84 | 1293 | SCM_SYSMISSING; |
02b754d3 | 1294 | /* not reached. */ |
0f2d19dd JB |
1295 | return SCM_BOOL_F; |
1296 | #endif | |
1297 | } | |
1bbd0b84 | 1298 | #undef FUNC_NAME |
0f2d19dd JB |
1299 | |
1300 | ||
a1ec6916 | 1301 | SCM_DEFINE (scm_nice, "nice", 1, 0, 0, |
1bbd0b84 | 1302 | (SCM incr), |
4079f87e GB |
1303 | "Increment the priority of the current process by @var{incr}. A higher |
1304 | priority value means that the process runs less often. | |
1305 | The return value is unspecified.") | |
1bbd0b84 | 1306 | #define FUNC_NAME s_scm_nice |
0f2d19dd JB |
1307 | { |
1308 | #ifdef HAVE_NICE | |
47c6b75e | 1309 | SCM_VALIDATE_INUM(1,incr); |
02b754d3 | 1310 | if (nice(SCM_INUM(incr)) != 0) |
1bbd0b84 | 1311 | SCM_SYSERROR; |
02b754d3 | 1312 | return SCM_UNSPECIFIED; |
0f2d19dd | 1313 | #else |
1bbd0b84 | 1314 | SCM_SYSMISSING; |
02b754d3 GH |
1315 | /* not reached. */ |
1316 | return SCM_BOOL_F; | |
0f2d19dd JB |
1317 | #endif |
1318 | } | |
1bbd0b84 | 1319 | #undef FUNC_NAME |
0f2d19dd JB |
1320 | |
1321 | ||
a1ec6916 | 1322 | SCM_DEFINE (scm_sync, "sync", 0, 0, 0, |
1bbd0b84 | 1323 | (), |
4079f87e GB |
1324 | "Flush the operating system disk buffers. |
1325 | The return value is unspecified.") | |
1bbd0b84 | 1326 | #define FUNC_NAME s_scm_sync |
0f2d19dd JB |
1327 | { |
1328 | #ifdef HAVE_SYNC | |
1329 | sync(); | |
52859adf | 1330 | #else |
1bbd0b84 | 1331 | SCM_SYSMISSING; |
02b754d3 | 1332 | /* not reached. */ |
52859adf | 1333 | #endif |
6afcd3b2 | 1334 | return SCM_UNSPECIFIED; |
0f2d19dd | 1335 | } |
1bbd0b84 | 1336 | #undef FUNC_NAME |
0f2d19dd | 1337 | |
0f2d19dd JB |
1338 | void |
1339 | scm_init_posix () | |
0f2d19dd JB |
1340 | { |
1341 | scm_add_feature ("posix"); | |
1342 | #ifdef HAVE_GETEUID | |
1343 | scm_add_feature ("EIDs"); | |
1344 | #endif | |
1345 | #ifdef WAIT_ANY | |
1346 | scm_sysintern ("WAIT_ANY", SCM_MAKINUM (WAIT_ANY)); | |
1347 | #endif | |
1348 | #ifdef WAIT_MYPGRP | |
1349 | scm_sysintern ("WAIT_MYPGRP", SCM_MAKINUM (WAIT_MYPGRP)); | |
1350 | #endif | |
1351 | #ifdef WNOHANG | |
1352 | scm_sysintern ("WNOHANG", SCM_MAKINUM (WNOHANG)); | |
1353 | #endif | |
1354 | #ifdef WUNTRACED | |
1355 | scm_sysintern ("WUNTRACED", SCM_MAKINUM (WUNTRACED)); | |
1356 | #endif | |
1357 | ||
0f2d19dd | 1358 | /* access() symbols. */ |
bab0f4e5 JB |
1359 | scm_sysintern ("R_OK", SCM_MAKINUM (R_OK)); |
1360 | scm_sysintern ("W_OK", SCM_MAKINUM (W_OK)); | |
1361 | scm_sysintern ("X_OK", SCM_MAKINUM (X_OK)); | |
1362 | scm_sysintern ("F_OK", SCM_MAKINUM (F_OK)); | |
0f2d19dd JB |
1363 | |
1364 | #ifdef LC_COLLATE | |
1365 | scm_sysintern ("LC_COLLATE", SCM_MAKINUM (LC_COLLATE)); | |
1366 | #endif | |
1367 | #ifdef LC_CTYPE | |
1368 | scm_sysintern ("LC_CTYPE", SCM_MAKINUM (LC_CTYPE)); | |
1369 | #endif | |
1370 | #ifdef LC_MONETARY | |
1371 | scm_sysintern ("LC_MONETARY", SCM_MAKINUM (LC_MONETARY)); | |
1372 | #endif | |
1373 | #ifdef LC_NUMERIC | |
1374 | scm_sysintern ("LC_NUMERIC", SCM_MAKINUM (LC_NUMERIC)); | |
1375 | #endif | |
1376 | #ifdef LC_TIME | |
1377 | scm_sysintern ("LC_TIME", SCM_MAKINUM (LC_TIME)); | |
1378 | #endif | |
1379 | #ifdef LC_MESSAGES | |
1380 | scm_sysintern ("LC_MESSAGES", SCM_MAKINUM (LC_MESSAGES)); | |
1381 | #endif | |
1382 | #ifdef LC_ALL | |
1383 | scm_sysintern ("LC_ALL", SCM_MAKINUM (LC_ALL)); | |
1384 | #endif | |
67ec3667 | 1385 | #include "cpp_sig_symbols.c" |
0f2d19dd JB |
1386 | #include "posix.x" |
1387 | } |