Commit | Line | Data |
---|---|---|
e6e2e95a | 1 | /* Copyright (C) 1995, 96, 97, 98, 99, 2000, 2001 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 | ||
8b50fe8e MV |
47 | /* Make GNU/Linux libc declare everything it has. */ |
48 | #define _GNU_SOURCE | |
49 | ||
0f2d19dd | 50 | #include <stdio.h> |
e6e2e95a MD |
51 | #include <errno.h> |
52 | ||
a0599745 MD |
53 | #include "libguile/_scm.h" |
54 | #include "libguile/fports.h" | |
55 | #include "libguile/scmsigs.h" | |
56 | #include "libguile/feature.h" | |
57 | #include "libguile/strings.h" | |
58 | #include "libguile/vectors.h" | |
59 | ||
60 | #include "libguile/validate.h" | |
61 | #include "libguile/posix.h" | |
0f2d19dd JB |
62 | \f |
63 | ||
02b754d3 GH |
64 | #ifdef HAVE_STRING_H |
65 | #include <string.h> | |
66 | #endif | |
0f2d19dd JB |
67 | #ifdef TIME_WITH_SYS_TIME |
68 | # include <sys/time.h> | |
69 | # include <time.h> | |
70 | #else | |
71 | # if HAVE_SYS_TIME_H | |
72 | # include <sys/time.h> | |
73 | # else | |
74 | # include <time.h> | |
75 | # endif | |
76 | #endif | |
77 | ||
78 | #ifdef HAVE_UNISTD_H | |
79 | #include <unistd.h> | |
95b88819 GH |
80 | #else |
81 | #ifndef ttyname | |
82 | extern char *ttyname(); | |
83 | #endif | |
0f2d19dd JB |
84 | #endif |
85 | ||
3594582b | 86 | #ifdef LIBC_H_WITH_UNISTD_H |
bab0f4e5 JB |
87 | #include <libc.h> |
88 | #endif | |
89 | ||
8cc71382 | 90 | #include <sys/types.h> |
0f2d19dd JB |
91 | #include <sys/stat.h> |
92 | #include <fcntl.h> | |
93 | ||
94 | #include <pwd.h> | |
95 | ||
96 | #if HAVE_SYS_WAIT_H | |
97 | # include <sys/wait.h> | |
98 | #endif | |
99 | #ifndef WEXITSTATUS | |
100 | # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8) | |
101 | #endif | |
102 | #ifndef WIFEXITED | |
103 | # define WIFEXITED(stat_val) (((stat_val) & 255) == 0) | |
104 | #endif | |
105 | ||
106 | #include <signal.h> | |
107 | ||
0f2d19dd JB |
108 | extern char ** environ; |
109 | ||
110 | #include <grp.h> | |
111 | #include <sys/utsname.h> | |
112 | ||
113 | #if HAVE_DIRENT_H | |
114 | # include <dirent.h> | |
115 | # define NAMLEN(dirent) strlen((dirent)->d_name) | |
116 | #else | |
117 | # define dirent direct | |
118 | # define NAMLEN(dirent) (dirent)->d_namlen | |
119 | # if HAVE_SYS_NDIR_H | |
120 | # include <sys/ndir.h> | |
121 | # endif | |
122 | # if HAVE_SYS_DIR_H | |
123 | # include <sys/dir.h> | |
124 | # endif | |
125 | # if HAVE_NDIR_H | |
126 | # include <ndir.h> | |
127 | # endif | |
128 | #endif | |
129 | ||
0f2d19dd JB |
130 | #ifdef HAVE_SETLOCALE |
131 | #include <locale.h> | |
132 | #endif | |
133 | ||
94e6d793 MG |
134 | #if HAVE_LIBCRYPT && HAVE_CRYPT_H |
135 | # include <crypt.h> | |
136 | #endif | |
137 | ||
138 | #if HAVE_SYS_RESOURCE_H | |
139 | # include <sys/resource.h> | |
140 | #endif | |
141 | ||
142 | #if HAVE_SYS_FILE_H | |
143 | # include <sys/file.h> | |
144 | #endif | |
145 | ||
bab0f4e5 JB |
146 | /* Some Unix systems don't define these. CPP hair is dangerous, but |
147 | this seems safe enough... */ | |
148 | #ifndef R_OK | |
149 | #define R_OK 4 | |
150 | #endif | |
151 | ||
152 | #ifndef W_OK | |
153 | #define W_OK 2 | |
154 | #endif | |
155 | ||
156 | #ifndef X_OK | |
157 | #define X_OK 1 | |
158 | #endif | |
159 | ||
160 | #ifndef F_OK | |
161 | #define F_OK 0 | |
162 | #endif | |
398609a5 JB |
163 | |
164 | /* On NextStep, <utime.h> doesn't define struct utime, unless we | |
165 | #define _POSIX_SOURCE before #including it. I think this is less | |
166 | of a kludge than defining struct utimbuf ourselves. */ | |
167 | #ifdef UTIMBUF_NEEDS_POSIX | |
168 | #define _POSIX_SOURCE | |
169 | #endif | |
170 | ||
171 | #ifdef HAVE_SYS_UTIME_H | |
172 | #include <sys/utime.h> | |
173 | #endif | |
174 | ||
175 | #ifdef HAVE_UTIME_H | |
176 | #include <utime.h> | |
177 | #endif | |
178 | ||
179 | /* Please don't add any more #includes or #defines here. The hack | |
180 | above means that _POSIX_SOURCE may be #defined, which will | |
181 | encourage header files to do strange things. */ | |
182 | ||
0f2d19dd | 183 | \f |
bc45012d JB |
184 | SCM_SYMBOL (sym_read_pipe, "read pipe"); |
185 | SCM_SYMBOL (sym_write_pipe, "write pipe"); | |
0f2d19dd | 186 | |
a1ec6916 | 187 | SCM_DEFINE (scm_pipe, "pipe", 0, 0, 0, |
1bbd0b84 | 188 | (), |
ae1b098b GH |
189 | "Returns a newly created pipe: a pair of ports which are linked\n" |
190 | "together on the local machine. The CAR is the input port and\n" | |
191 | "the CDR is the output port. Data written (and flushed) to the\n" | |
192 | "output port can be read from the input port.\n" | |
193 | "Pipes are commonly used for communication with a newly\n" | |
bd9e24b3 GH |
194 | "forked child process. The need to flush the output port\n" |
195 | "can be avoided by making it unbuffered using @code{setvbuf}.\n\n" | |
196 | "Writes occur atomically provided the size of the data in\n" | |
197 | "bytes is not greater than the value of @code{PIPE_BUF}\n" | |
198 | "Note that the output port is likely to block if too much data\n" | |
199 | "(typically equal to @code{PIPE_BUF}) has been written but not\n" | |
200 | "yet read from the input port\n" | |
ae1b098b | 201 | ) |
1bbd0b84 | 202 | #define FUNC_NAME s_scm_pipe |
0f2d19dd JB |
203 | { |
204 | int fd[2], rv; | |
0f2d19dd | 205 | SCM p_rd, p_wt; |
02b754d3 | 206 | |
0f2d19dd JB |
207 | rv = pipe (fd); |
208 | if (rv) | |
1bbd0b84 | 209 | SCM_SYSERROR; |
ee149d03 JB |
210 | |
211 | p_rd = scm_fdes_to_port (fd[0], "r", sym_read_pipe); | |
212 | p_wt = scm_fdes_to_port (fd[1], "w", sym_write_pipe); | |
0f2d19dd JB |
213 | return scm_cons (p_rd, p_wt); |
214 | } | |
1bbd0b84 | 215 | #undef FUNC_NAME |
0f2d19dd JB |
216 | |
217 | ||
0e958795 | 218 | #ifdef HAVE_GETGROUPS |
a1ec6916 | 219 | SCM_DEFINE (scm_getgroups, "getgroups", 0, 0, 0, |
1bbd0b84 | 220 | (), |
b380b885 | 221 | "Returns a vector of integers representing the current supplimentary group IDs.") |
1bbd0b84 | 222 | #define FUNC_NAME s_scm_getgroups |
0f2d19dd | 223 | { |
66460dfb DH |
224 | SCM ans; |
225 | int ngroups; | |
226 | scm_sizet size; | |
227 | GETGROUPS_T *groups; | |
228 | ||
229 | ngroups = getgroups (0, NULL); | |
230 | if (ngroups <= 0) | |
1bbd0b84 | 231 | SCM_SYSERROR; |
66460dfb DH |
232 | |
233 | size = ngroups * sizeof (GETGROUPS_T); | |
234 | groups = scm_must_malloc (size, FUNC_NAME); | |
235 | getgroups (ngroups, groups); | |
236 | ||
00ffa0e7 | 237 | ans = scm_c_make_vector (ngroups, SCM_UNDEFINED); |
66460dfb DH |
238 | while (--ngroups >= 0) |
239 | SCM_VELTS (ans) [ngroups] = SCM_MAKINUM (groups [ngroups]); | |
240 | ||
241 | scm_must_free (groups); | |
242 | scm_done_free (size); | |
243 | ||
244 | return ans; | |
1bbd0b84 GB |
245 | } |
246 | #undef FUNC_NAME | |
0e958795 | 247 | #endif |
0f2d19dd JB |
248 | |
249 | ||
a1ec6916 | 250 | SCM_DEFINE (scm_getpwuid, "getpw", 0, 1, 0, |
1bbd0b84 | 251 | (SCM user), |
b380b885 MD |
252 | "Look up an entry in the user database. @var{obj} can be an integer,\n" |
253 | "a string, or omitted, giving the behaviour of getpwuid, getpwnam\n" | |
254 | "or getpwent respectively.") | |
1bbd0b84 | 255 | #define FUNC_NAME s_scm_getpwuid |
0f2d19dd JB |
256 | { |
257 | SCM result; | |
258 | struct passwd *entry; | |
259 | SCM *ve; | |
260 | ||
00ffa0e7 | 261 | result = scm_c_make_vector (7, SCM_UNSPECIFIED); |
0f2d19dd JB |
262 | ve = SCM_VELTS (result); |
263 | if (SCM_UNBNDP (user) || SCM_FALSEP (user)) | |
264 | { | |
0f2d19dd | 265 | SCM_SYSCALL (entry = getpwent ()); |
3d781d49 JB |
266 | if (! entry) |
267 | { | |
3d781d49 JB |
268 | return SCM_BOOL_F; |
269 | } | |
0f2d19dd JB |
270 | } |
271 | else if (SCM_INUMP (user)) | |
272 | { | |
0f2d19dd JB |
273 | entry = getpwuid (SCM_INUM (user)); |
274 | } | |
275 | else | |
276 | { | |
a6d9e5ab DH |
277 | SCM_VALIDATE_STRING (1, user); |
278 | SCM_STRING_COERCE_0TERMINATION_X (user); | |
279 | entry = getpwnam (SCM_STRING_CHARS (user)); | |
0f2d19dd JB |
280 | } |
281 | if (!entry) | |
1bbd0b84 | 282 | SCM_MISC_ERROR ("entry not found", SCM_EOL); |
02b754d3 | 283 | |
0f2d19dd JB |
284 | ve[0] = scm_makfrom0str (entry->pw_name); |
285 | ve[1] = scm_makfrom0str (entry->pw_passwd); | |
286 | ve[2] = scm_ulong2num ((unsigned long) entry->pw_uid); | |
287 | ve[3] = scm_ulong2num ((unsigned long) entry->pw_gid); | |
288 | ve[4] = scm_makfrom0str (entry->pw_gecos); | |
289 | if (!entry->pw_dir) | |
290 | ve[5] = scm_makfrom0str (""); | |
291 | else | |
292 | ve[5] = scm_makfrom0str (entry->pw_dir); | |
293 | if (!entry->pw_shell) | |
294 | ve[6] = scm_makfrom0str (""); | |
295 | else | |
296 | ve[6] = scm_makfrom0str (entry->pw_shell); | |
0f2d19dd JB |
297 | return result; |
298 | } | |
1bbd0b84 | 299 | #undef FUNC_NAME |
0f2d19dd JB |
300 | |
301 | ||
0e958795 | 302 | #ifdef HAVE_SETPWENT |
a1ec6916 | 303 | SCM_DEFINE (scm_setpwent, "setpw", 0, 1, 0, |
1bbd0b84 | 304 | (SCM arg), |
b380b885 MD |
305 | "If called with a true argument, initialize or reset the password data\n" |
306 | "stream. Otherwise, close the stream. The @code{setpwent} and\n" | |
307 | "@code{endpwent} procedures are implemented on top of this.") | |
1bbd0b84 | 308 | #define FUNC_NAME s_scm_setpwent |
0f2d19dd JB |
309 | { |
310 | if (SCM_UNBNDP (arg) || SCM_FALSEP (arg)) | |
311 | endpwent (); | |
312 | else | |
313 | setpwent (); | |
314 | return SCM_UNSPECIFIED; | |
315 | } | |
1bbd0b84 | 316 | #undef FUNC_NAME |
0e958795 | 317 | #endif |
0f2d19dd JB |
318 | |
319 | ||
320 | ||
321 | /* Combines getgrgid and getgrnam. */ | |
a1ec6916 | 322 | SCM_DEFINE (scm_getgrgid, "getgr", 0, 1, 0, |
1bbd0b84 | 323 | (SCM name), |
b380b885 MD |
324 | "Look up an entry in the group database. @var{obj} can be an integer,\n" |
325 | "a string, or omitted, giving the behaviour of getgrgid, getgrnam\n" | |
326 | "or getgrent respectively.") | |
1bbd0b84 | 327 | #define FUNC_NAME s_scm_getgrgid |
0f2d19dd JB |
328 | { |
329 | SCM result; | |
330 | struct group *entry; | |
331 | SCM *ve; | |
00ffa0e7 | 332 | result = scm_c_make_vector (4, SCM_UNSPECIFIED); |
0f2d19dd | 333 | ve = SCM_VELTS (result); |
4260a7fc | 334 | if (SCM_UNBNDP (name) || SCM_FALSEP (name)) |
3d781d49 JB |
335 | { |
336 | SCM_SYSCALL (entry = getgrent ()); | |
337 | if (! entry) | |
338 | { | |
3d781d49 JB |
339 | return SCM_BOOL_F; |
340 | } | |
341 | } | |
0f2d19dd JB |
342 | else if (SCM_INUMP (name)) |
343 | SCM_SYSCALL (entry = getgrgid (SCM_INUM (name))); | |
344 | else | |
345 | { | |
a6d9e5ab DH |
346 | SCM_VALIDATE_STRING (1, name); |
347 | SCM_STRING_COERCE_0TERMINATION_X (name); | |
348 | SCM_SYSCALL (entry = getgrnam (SCM_STRING_CHARS (name))); | |
0f2d19dd JB |
349 | } |
350 | if (!entry) | |
1bbd0b84 | 351 | SCM_SYSERROR; |
02b754d3 | 352 | |
0f2d19dd JB |
353 | ve[0] = scm_makfrom0str (entry->gr_name); |
354 | ve[1] = scm_makfrom0str (entry->gr_passwd); | |
355 | ve[2] = scm_ulong2num ((unsigned long) entry->gr_gid); | |
356 | ve[3] = scm_makfromstrs (-1, entry->gr_mem); | |
0f2d19dd JB |
357 | return result; |
358 | } | |
1bbd0b84 | 359 | #undef FUNC_NAME |
0f2d19dd JB |
360 | |
361 | ||
362 | ||
a1ec6916 | 363 | SCM_DEFINE (scm_setgrent, "setgr", 0, 1, 0, |
1bbd0b84 | 364 | (SCM arg), |
b380b885 MD |
365 | "If called with a true argument, initialize or reset the group data\n" |
366 | "stream. Otherwise, close the stream. The @code{setgrent} and\n" | |
367 | "@code{endgrent} procedures are implemented on top of this.") | |
1bbd0b84 | 368 | #define FUNC_NAME s_scm_setgrent |
0f2d19dd JB |
369 | { |
370 | if (SCM_UNBNDP (arg) || SCM_FALSEP (arg)) | |
371 | endgrent (); | |
372 | else | |
373 | setgrent (); | |
374 | return SCM_UNSPECIFIED; | |
375 | } | |
1bbd0b84 | 376 | #undef FUNC_NAME |
0f2d19dd JB |
377 | |
378 | ||
379 | ||
a1ec6916 | 380 | SCM_DEFINE (scm_kill, "kill", 2, 0, 0, |
1bbd0b84 | 381 | (SCM pid, SCM sig), |
b380b885 MD |
382 | "Sends a signal to the specified process or group of processes.\n\n" |
383 | "@var{pid} specifies the processes to which the signal is sent:\n\n" | |
384 | "@table @r\n" | |
385 | "@item @var{pid} greater than 0\n" | |
386 | "The process whose identifier is @var{pid}.\n" | |
387 | "@item @var{pid} equal to 0\n" | |
388 | "All processes in the current process group.\n" | |
389 | "@item @var{pid} less than -1\n" | |
390 | "The process group whose identifier is -@var{pid}\n" | |
391 | "@item @var{pid} equal to -1\n" | |
392 | "If the process is privileged, all processes except for some special\n" | |
393 | "system processes. Otherwise, all processes with the current effective\n" | |
394 | "user ID.\n" | |
395 | "@end table\n\n" | |
396 | "@var{sig} should be specified using a variable corresponding to\n" | |
397 | "the Unix symbolic name, e.g.,\n\n" | |
398 | "@defvar SIGHUP\n" | |
399 | "Hang-up signal.\n" | |
400 | "@end defvar\n\n" | |
401 | "@defvar SIGINT\n" | |
402 | "Interrupt signal.\n" | |
403 | "@end defvar") | |
1bbd0b84 | 404 | #define FUNC_NAME s_scm_kill |
0f2d19dd | 405 | { |
3b3b36dd GB |
406 | SCM_VALIDATE_INUM (1,pid); |
407 | SCM_VALIDATE_INUM (2,sig); | |
0f2d19dd | 408 | /* Signal values are interned in scm_init_posix(). */ |
02b754d3 | 409 | if (kill ((int) SCM_INUM (pid), (int) SCM_INUM (sig)) != 0) |
1bbd0b84 | 410 | SCM_SYSERROR; |
02b754d3 | 411 | return SCM_UNSPECIFIED; |
0f2d19dd | 412 | } |
1bbd0b84 | 413 | #undef FUNC_NAME |
0f2d19dd | 414 | |
d00ae47e | 415 | #ifdef HAVE_WAITPID |
a1ec6916 | 416 | SCM_DEFINE (scm_waitpid, "waitpid", 1, 1, 0, |
1bbd0b84 | 417 | (SCM pid, SCM options), |
b380b885 MD |
418 | "This procedure collects status information from a child process which\n" |
419 | "has terminated or (optionally) stopped. Normally it will\n" | |
420 | "suspend the calling process until this can be done. If more than one\n" | |
421 | "child process is eligible then one will be chosen by the operating system.\n\n" | |
422 | "The value of @var{pid} determines the behaviour:\n\n" | |
423 | "@table @r\n" | |
424 | "@item @var{pid} greater than 0\n" | |
425 | "Request status information from the specified child process.\n" | |
426 | "@item @var{pid} equal to -1 or WAIT_ANY\n" | |
427 | "Request status information for any child process.\n" | |
428 | "@item @var{pid} equal to 0 or WAIT_MYPGRP\n" | |
429 | "Request status information for any child process in the current process\n" | |
430 | "group.\n" | |
431 | "@item @var{pid} less than -1\n" | |
432 | "Request status information for any child process whose process group ID\n" | |
433 | "is -@var{PID}.\n" | |
434 | "@end table\n\n" | |
435 | "The @var{options} argument, if supplied, should be the bitwise OR of the\n" | |
436 | "values of zero or more of the following variables:\n\n" | |
437 | "@defvar WNOHANG\n" | |
438 | "Return immediately even if there are no child processes to be collected.\n" | |
439 | "@end defvar\n\n" | |
440 | "@defvar WUNTRACED\n" | |
441 | "Report status information for stopped processes as well as terminated\n" | |
442 | "processes.\n" | |
443 | "@end defvar\n\n" | |
444 | "The return value is a pair containing:\n\n" | |
445 | "@enumerate\n" | |
446 | "@item\n" | |
447 | "The process ID of the child process, or 0 if @code{WNOHANG} was\n" | |
448 | "specified and no process was collected.\n" | |
449 | "@item\n" | |
450 | "The integer status value.\n" | |
451 | "@end enumerate") | |
1bbd0b84 | 452 | #define FUNC_NAME s_scm_waitpid |
0f2d19dd JB |
453 | { |
454 | int i; | |
455 | int status; | |
456 | int ioptions; | |
3b3b36dd | 457 | SCM_VALIDATE_INUM (1,pid); |
0f2d19dd JB |
458 | if (SCM_UNBNDP (options)) |
459 | ioptions = 0; | |
460 | else | |
461 | { | |
3b3b36dd | 462 | SCM_VALIDATE_INUM (2,options); |
0f2d19dd JB |
463 | /* Flags are interned in scm_init_posix. */ |
464 | ioptions = SCM_INUM (options); | |
465 | } | |
466 | SCM_SYSCALL (i = waitpid (SCM_INUM (pid), &status, ioptions)); | |
02b754d3 | 467 | if (i == -1) |
1bbd0b84 | 468 | SCM_SYSERROR; |
02b754d3 | 469 | return scm_cons (SCM_MAKINUM (0L + i), SCM_MAKINUM (0L + status)); |
0f2d19dd | 470 | } |
1bbd0b84 | 471 | #undef FUNC_NAME |
d00ae47e | 472 | #endif /* HAVE_WAITPID */ |
0f2d19dd | 473 | |
a1ec6916 | 474 | SCM_DEFINE (scm_status_exit_val, "status:exit-val", 1, 0, 0, |
1bbd0b84 | 475 | (SCM status), |
b380b885 MD |
476 | "Returns the exit status value, as would be\n" |
477 | "set if a process ended normally through a\n" | |
478 | "call to @code{exit} or @code{_exit}, if any, otherwise @code{#f}.") | |
1bbd0b84 | 479 | #define FUNC_NAME s_scm_status_exit_val |
67ec3667 | 480 | { |
e67dc2be JB |
481 | int lstatus; |
482 | ||
3b3b36dd | 483 | SCM_VALIDATE_INUM (1,status); |
e67dc2be JB |
484 | |
485 | /* On Ultrix, the WIF... macros assume their argument is an lvalue; | |
486 | go figure. SCM_INUM does not yield an lvalue. */ | |
487 | lstatus = SCM_INUM (status); | |
488 | if (WIFEXITED (lstatus)) | |
489 | return (SCM_MAKINUM (WEXITSTATUS (lstatus))); | |
67ec3667 GH |
490 | else |
491 | return SCM_BOOL_F; | |
492 | } | |
1bbd0b84 | 493 | #undef FUNC_NAME |
e67dc2be | 494 | |
a1ec6916 | 495 | SCM_DEFINE (scm_status_term_sig, "status:term-sig", 1, 0, 0, |
1bbd0b84 | 496 | (SCM status), |
b380b885 MD |
497 | "Returns the signal number which terminated the\n" |
498 | "process, if any, otherwise @code{#f}.") | |
1bbd0b84 | 499 | #define FUNC_NAME s_scm_status_term_sig |
67ec3667 | 500 | { |
e67dc2be JB |
501 | int lstatus; |
502 | ||
3b3b36dd | 503 | SCM_VALIDATE_INUM (1,status); |
e67dc2be JB |
504 | |
505 | lstatus = SCM_INUM (status); | |
506 | if (WIFSIGNALED (lstatus)) | |
507 | return SCM_MAKINUM (WTERMSIG (lstatus)); | |
67ec3667 GH |
508 | else |
509 | return SCM_BOOL_F; | |
510 | } | |
1bbd0b84 | 511 | #undef FUNC_NAME |
0f2d19dd | 512 | |
a1ec6916 | 513 | SCM_DEFINE (scm_status_stop_sig, "status:stop-sig", 1, 0, 0, |
1bbd0b84 | 514 | (SCM status), |
b380b885 MD |
515 | "Returns the signal number which stopped the\n" |
516 | "process, if any, otherwise @code{#f}.") | |
1bbd0b84 | 517 | #define FUNC_NAME s_scm_status_stop_sig |
67ec3667 | 518 | { |
e67dc2be JB |
519 | int lstatus; |
520 | ||
3b3b36dd | 521 | SCM_VALIDATE_INUM (1,status); |
e67dc2be JB |
522 | |
523 | lstatus = SCM_INUM (status); | |
524 | if (WIFSTOPPED (lstatus)) | |
525 | return SCM_MAKINUM (WSTOPSIG (lstatus)); | |
67ec3667 GH |
526 | else |
527 | return SCM_BOOL_F; | |
528 | } | |
1bbd0b84 | 529 | #undef FUNC_NAME |
0f2d19dd | 530 | |
a1ec6916 | 531 | SCM_DEFINE (scm_getppid, "getppid", 0, 0, 0, |
1bbd0b84 | 532 | (), |
b380b885 | 533 | "Returns an integer representing the process ID of the parent process.") |
1bbd0b84 | 534 | #define FUNC_NAME s_scm_getppid |
0f2d19dd JB |
535 | { |
536 | return SCM_MAKINUM (0L + getppid ()); | |
537 | } | |
1bbd0b84 | 538 | #undef FUNC_NAME |
0f2d19dd JB |
539 | |
540 | ||
541 | ||
a1ec6916 | 542 | SCM_DEFINE (scm_getuid, "getuid", 0, 0, 0, |
1bbd0b84 | 543 | (), |
b380b885 | 544 | "Returns an integer representing the current real user ID.") |
1bbd0b84 | 545 | #define FUNC_NAME s_scm_getuid |
0f2d19dd JB |
546 | { |
547 | return SCM_MAKINUM (0L + getuid ()); | |
548 | } | |
1bbd0b84 | 549 | #undef FUNC_NAME |
0f2d19dd JB |
550 | |
551 | ||
552 | ||
a1ec6916 | 553 | SCM_DEFINE (scm_getgid, "getgid", 0, 0, 0, |
1bbd0b84 | 554 | (), |
b380b885 | 555 | "Returns an integer representing the current real group ID.") |
1bbd0b84 | 556 | #define FUNC_NAME s_scm_getgid |
0f2d19dd JB |
557 | { |
558 | return SCM_MAKINUM (0L + getgid ()); | |
559 | } | |
1bbd0b84 | 560 | #undef FUNC_NAME |
0f2d19dd JB |
561 | |
562 | ||
563 | ||
a1ec6916 | 564 | SCM_DEFINE (scm_geteuid, "geteuid", 0, 0, 0, |
1bbd0b84 | 565 | (), |
b380b885 MD |
566 | "Returns an integer representing the current effective user ID.\n" |
567 | "If the system does not support effective IDs, then the real ID\n" | |
568 | "is returned. @code{(feature? 'EIDs)} reports whether the system\n" | |
569 | "supports effective IDs.") | |
1bbd0b84 | 570 | #define FUNC_NAME s_scm_geteuid |
0f2d19dd JB |
571 | { |
572 | #ifdef HAVE_GETEUID | |
573 | return SCM_MAKINUM (0L + geteuid ()); | |
574 | #else | |
575 | return SCM_MAKINUM (0L + getuid ()); | |
576 | #endif | |
577 | } | |
1bbd0b84 | 578 | #undef FUNC_NAME |
0f2d19dd JB |
579 | |
580 | ||
581 | ||
a1ec6916 | 582 | SCM_DEFINE (scm_getegid, "getegid", 0, 0, 0, |
1bbd0b84 | 583 | (), |
b380b885 MD |
584 | "Returns an integer representing the current effective group ID.\n" |
585 | "If the system does not support effective IDs, then the real ID\n" | |
586 | "is returned. @code{(feature? 'EIDs)} reports whether the system\n" | |
587 | "supports effective IDs.") | |
1bbd0b84 | 588 | #define FUNC_NAME s_scm_getegid |
0f2d19dd JB |
589 | { |
590 | #ifdef HAVE_GETEUID | |
591 | return SCM_MAKINUM (0L + getegid ()); | |
592 | #else | |
593 | return SCM_MAKINUM (0L + getgid ()); | |
594 | #endif | |
595 | } | |
1bbd0b84 | 596 | #undef FUNC_NAME |
0f2d19dd JB |
597 | |
598 | ||
a1ec6916 | 599 | SCM_DEFINE (scm_setuid, "setuid", 1, 0, 0, |
1bbd0b84 | 600 | (SCM id), |
b380b885 MD |
601 | "Sets both the real and effective user IDs to the integer @var{id}, provided\n" |
602 | "the process has appropriate privileges.\n" | |
603 | "The return value is unspecified.") | |
1bbd0b84 | 604 | #define FUNC_NAME s_scm_setuid |
0f2d19dd | 605 | { |
3b3b36dd | 606 | SCM_VALIDATE_INUM (1,id); |
02b754d3 | 607 | if (setuid (SCM_INUM (id)) != 0) |
1bbd0b84 | 608 | SCM_SYSERROR; |
02b754d3 | 609 | return SCM_UNSPECIFIED; |
0f2d19dd | 610 | } |
1bbd0b84 | 611 | #undef FUNC_NAME |
0f2d19dd | 612 | |
a1ec6916 | 613 | SCM_DEFINE (scm_setgid, "setgid", 1, 0, 0, |
1bbd0b84 | 614 | (SCM id), |
b380b885 MD |
615 | "Sets both the real and effective group IDs to the integer @var{id}, provided\n" |
616 | "the process has appropriate privileges.\n" | |
617 | "The return value is unspecified.") | |
1bbd0b84 | 618 | #define FUNC_NAME s_scm_setgid |
0f2d19dd | 619 | { |
3b3b36dd | 620 | SCM_VALIDATE_INUM (1,id); |
02b754d3 | 621 | if (setgid (SCM_INUM (id)) != 0) |
1bbd0b84 | 622 | SCM_SYSERROR; |
02b754d3 | 623 | return SCM_UNSPECIFIED; |
0f2d19dd | 624 | } |
1bbd0b84 | 625 | #undef FUNC_NAME |
0f2d19dd | 626 | |
a1ec6916 | 627 | SCM_DEFINE (scm_seteuid, "seteuid", 1, 0, 0, |
1bbd0b84 | 628 | (SCM id), |
b380b885 MD |
629 | "Sets the effective user ID to the integer @var{id}, provided the process\n" |
630 | "has appropriate privileges. If effective IDs are not supported, the\n" | |
631 | "real ID is set instead -- @code{(feature? 'EIDs)} reports whether the\n" | |
632 | "system supports effective IDs.\n" | |
633 | "The return value is unspecified.") | |
1bbd0b84 | 634 | #define FUNC_NAME s_scm_seteuid |
0f2d19dd | 635 | { |
02b754d3 GH |
636 | int rv; |
637 | ||
3b3b36dd | 638 | SCM_VALIDATE_INUM (1,id); |
0f2d19dd | 639 | #ifdef HAVE_SETEUID |
02b754d3 | 640 | rv = seteuid (SCM_INUM (id)); |
0f2d19dd | 641 | #else |
02b754d3 | 642 | rv = setuid (SCM_INUM (id)); |
0f2d19dd | 643 | #endif |
02b754d3 | 644 | if (rv != 0) |
1bbd0b84 | 645 | SCM_SYSERROR; |
02b754d3 | 646 | return SCM_UNSPECIFIED; |
0f2d19dd | 647 | } |
1bbd0b84 | 648 | #undef FUNC_NAME |
0f2d19dd | 649 | |
0e958795 | 650 | #ifdef HAVE_SETEGID |
a1ec6916 | 651 | SCM_DEFINE (scm_setegid, "setegid", 1, 0, 0, |
1bbd0b84 | 652 | (SCM id), |
b380b885 MD |
653 | "Sets the effective group ID to the integer @var{id}, provided the process\n" |
654 | "has appropriate privileges. If effective IDs are not supported, the\n" | |
655 | "real ID is set instead -- @code{(feature? 'EIDs)} reports whether the\n" | |
656 | "system supports effective IDs.\n" | |
657 | "The return value is unspecified.") | |
1bbd0b84 | 658 | #define FUNC_NAME s_scm_setegid |
0f2d19dd | 659 | { |
02b754d3 GH |
660 | int rv; |
661 | ||
3b3b36dd | 662 | SCM_VALIDATE_INUM (1,id); |
0f2d19dd | 663 | #ifdef HAVE_SETEUID |
02b754d3 | 664 | rv = setegid (SCM_INUM (id)); |
0f2d19dd | 665 | #else |
02b754d3 | 666 | rv = setgid (SCM_INUM (id)); |
0f2d19dd | 667 | #endif |
02b754d3 | 668 | if (rv != 0) |
1bbd0b84 | 669 | SCM_SYSERROR; |
02b754d3 GH |
670 | return SCM_UNSPECIFIED; |
671 | ||
0f2d19dd | 672 | } |
1bbd0b84 | 673 | #undef FUNC_NAME |
0e958795 | 674 | #endif |
0f2d19dd | 675 | |
a1ec6916 | 676 | SCM_DEFINE (scm_getpgrp, "getpgrp", 0, 0, 0, |
1bbd0b84 | 677 | (), |
b380b885 MD |
678 | "Returns an integer representing the current process group ID.\n" |
679 | "This is the POSIX definition, not BSD.") | |
1bbd0b84 | 680 | #define FUNC_NAME s_scm_getpgrp |
0f2d19dd JB |
681 | { |
682 | int (*fn)(); | |
4625e44f | 683 | fn = (int (*) ()) getpgrp; |
0f2d19dd JB |
684 | return SCM_MAKINUM (fn (0)); |
685 | } | |
1bbd0b84 | 686 | #undef FUNC_NAME |
0f2d19dd | 687 | |
f25f761d | 688 | #ifdef HAVE_SETPGID |
a1ec6916 | 689 | SCM_DEFINE (scm_setpgid, "setpgid", 2, 0, 0, |
1bbd0b84 | 690 | (SCM pid, SCM pgid), |
b380b885 MD |
691 | "Move the process @var{pid} into the process group @var{pgid}. @var{pid} or\n" |
692 | "@var{pgid} must be integers: they can be zero to indicate the ID of the\n" | |
693 | "current process.\n" | |
694 | "Fails on systems that do not support job control.\n" | |
695 | "The return value is unspecified.") | |
1bbd0b84 | 696 | #define FUNC_NAME s_scm_setpgid |
0f2d19dd | 697 | { |
3b3b36dd GB |
698 | SCM_VALIDATE_INUM (1,pid); |
699 | SCM_VALIDATE_INUM (2,pgid); | |
02b754d3 GH |
700 | /* FIXME(?): may be known as setpgrp. */ |
701 | if (setpgid (SCM_INUM (pid), SCM_INUM (pgid)) != 0) | |
1bbd0b84 | 702 | SCM_SYSERROR; |
02b754d3 | 703 | return SCM_UNSPECIFIED; |
0f2d19dd | 704 | } |
1bbd0b84 | 705 | #undef FUNC_NAME |
f25f761d | 706 | #endif /* HAVE_SETPGID */ |
0f2d19dd | 707 | |
f25f761d | 708 | #ifdef HAVE_SETSID |
a1ec6916 | 709 | SCM_DEFINE (scm_setsid, "setsid", 0, 0, 0, |
1bbd0b84 | 710 | (), |
b380b885 MD |
711 | "Creates a new session. The current process becomes the session leader\n" |
712 | "and is put in a new process group. The process will be detached\n" | |
713 | "from its controlling terminal if it has one.\n" | |
714 | "The return value is an integer representing the new process group ID.") | |
1bbd0b84 | 715 | #define FUNC_NAME s_scm_setsid |
0f2d19dd JB |
716 | { |
717 | pid_t sid = setsid (); | |
02b754d3 | 718 | if (sid == -1) |
1bbd0b84 | 719 | SCM_SYSERROR; |
02b754d3 | 720 | return SCM_UNSPECIFIED; |
0f2d19dd | 721 | } |
1bbd0b84 | 722 | #undef FUNC_NAME |
f25f761d | 723 | #endif /* HAVE_SETSID */ |
0f2d19dd | 724 | |
a1ec6916 | 725 | SCM_DEFINE (scm_ttyname, "ttyname", 1, 0, 0, |
1bbd0b84 | 726 | (SCM port), |
b380b885 MD |
727 | "Returns a string with the name of the serial terminal device underlying\n" |
728 | "@var{port}.") | |
1bbd0b84 | 729 | #define FUNC_NAME s_scm_ttyname |
0f2d19dd JB |
730 | { |
731 | char *ans; | |
732 | int fd; | |
78446828 MV |
733 | |
734 | port = SCM_COERCE_OUTPORT (port); | |
3b3b36dd | 735 | SCM_VALIDATE_OPPORT (1,port); |
a98bddfd | 736 | if (!SCM_FPORTP (port)) |
0f2d19dd | 737 | return SCM_BOOL_F; |
ee149d03 | 738 | fd = SCM_FPORT_FDES (port); |
02b754d3 GH |
739 | SCM_SYSCALL (ans = ttyname (fd)); |
740 | if (!ans) | |
1bbd0b84 | 741 | SCM_SYSERROR; |
0f2d19dd | 742 | /* ans could be overwritten by another call to ttyname */ |
02b754d3 | 743 | return (scm_makfrom0str (ans)); |
0f2d19dd | 744 | } |
1bbd0b84 | 745 | #undef FUNC_NAME |
0f2d19dd | 746 | |
f25f761d | 747 | #ifdef HAVE_CTERMID |
a1ec6916 | 748 | SCM_DEFINE (scm_ctermid, "ctermid", 0, 0, 0, |
1bbd0b84 | 749 | (), |
b380b885 MD |
750 | "Returns a string containing the file name of the controlling terminal\n" |
751 | "for the current process.") | |
1bbd0b84 | 752 | #define FUNC_NAME s_scm_ctermid |
0f2d19dd JB |
753 | { |
754 | char *result = ctermid (NULL); | |
02b754d3 | 755 | if (*result == '\0') |
1bbd0b84 | 756 | SCM_SYSERROR; |
02b754d3 | 757 | return scm_makfrom0str (result); |
0f2d19dd | 758 | } |
1bbd0b84 | 759 | #undef FUNC_NAME |
f25f761d | 760 | #endif /* HAVE_CTERMID */ |
0f2d19dd | 761 | |
f25f761d | 762 | #ifdef HAVE_TCGETPGRP |
a1ec6916 | 763 | SCM_DEFINE (scm_tcgetpgrp, "tcgetpgrp", 1, 0, 0, |
1bbd0b84 | 764 | (SCM port), |
b380b885 MD |
765 | "Returns the process group ID of the foreground\n" |
766 | "process group associated with the terminal open on the file descriptor\n" | |
767 | "underlying @var{port}.\n\n" | |
768 | "If there is no foreground process group, the return value is a\n" | |
769 | "number greater than 1 that does not match the process group ID\n" | |
770 | "of any existing process group. This can happen if all of the\n" | |
771 | "processes in the job that was formerly the foreground job have\n" | |
772 | "terminated, and no other job has yet been moved into the\n" | |
773 | "foreground.") | |
1bbd0b84 | 774 | #define FUNC_NAME s_scm_tcgetpgrp |
0f2d19dd JB |
775 | { |
776 | int fd; | |
777 | pid_t pgid; | |
78446828 MV |
778 | |
779 | port = SCM_COERCE_OUTPORT (port); | |
780 | ||
3b3b36dd | 781 | SCM_VALIDATE_OPFPORT (1,port); |
ee149d03 JB |
782 | fd = SCM_FPORT_FDES (port); |
783 | if ((pgid = tcgetpgrp (fd)) == -1) | |
1bbd0b84 | 784 | SCM_SYSERROR; |
02b754d3 | 785 | return SCM_MAKINUM (pgid); |
1bbd0b84 GB |
786 | } |
787 | #undef FUNC_NAME | |
f25f761d | 788 | #endif /* HAVE_TCGETPGRP */ |
0f2d19dd | 789 | |
f25f761d | 790 | #ifdef HAVE_TCSETPGRP |
a1ec6916 | 791 | SCM_DEFINE (scm_tcsetpgrp, "tcsetpgrp", 2, 0, 0, |
1bbd0b84 | 792 | (SCM port, SCM pgid), |
b380b885 MD |
793 | "Set the foreground process group ID for the terminal used by the file\n" |
794 | "descriptor underlying @var{port} to the integer @var{pgid}.\n" | |
795 | "The calling process\n" | |
796 | "must be a member of the same session as @var{pgid} and must have the same\n" | |
797 | "controlling terminal. The return value is unspecified.") | |
1bbd0b84 | 798 | #define FUNC_NAME s_scm_tcsetpgrp |
0f2d19dd JB |
799 | { |
800 | int fd; | |
78446828 MV |
801 | |
802 | port = SCM_COERCE_OUTPORT (port); | |
803 | ||
3b3b36dd GB |
804 | SCM_VALIDATE_OPFPORT (1,port); |
805 | SCM_VALIDATE_INUM (2,pgid); | |
ee149d03 JB |
806 | fd = SCM_FPORT_FDES (port); |
807 | if (tcsetpgrp (fd, SCM_INUM (pgid)) == -1) | |
1bbd0b84 | 808 | SCM_SYSERROR; |
02b754d3 | 809 | return SCM_UNSPECIFIED; |
1bbd0b84 GB |
810 | } |
811 | #undef FUNC_NAME | |
f25f761d | 812 | #endif /* HAVE_TCSETPGRP */ |
0f2d19dd | 813 | |
a6d9e5ab DH |
814 | /* Create a new C argv array from a scheme list of strings. */ |
815 | /* Dirk:FIXME:: A quite similar function is implemented in dynl.c */ | |
816 | /* Dirk:FIXME:: In case of assertion errors, we get memory leaks */ | |
1cc91f1b | 817 | |
0f2d19dd | 818 | static char ** |
a6d9e5ab | 819 | scm_convert_exec_args (SCM args, int argn, const char *subr) |
0f2d19dd | 820 | { |
a6d9e5ab DH |
821 | char **argv; |
822 | int argc; | |
0f2d19dd | 823 | int i; |
6afcd3b2 | 824 | |
a6d9e5ab DH |
825 | argc = scm_ilength (args); |
826 | SCM_ASSERT (argc >= 0, args, argn, subr); | |
827 | argv = (char **) scm_must_malloc ((argc + 1) * sizeof (char *), subr); | |
af45e3b0 | 828 | for (i = 0; !SCM_NULLP (args); args = SCM_CDR (args), ++i) |
0f2d19dd | 829 | { |
a6d9e5ab | 830 | SCM arg = SCM_CAR (args); |
0f2d19dd JB |
831 | scm_sizet len; |
832 | char *dst; | |
833 | char *src; | |
a6d9e5ab DH |
834 | |
835 | SCM_ASSERT (SCM_STRINGP (arg), args, argn, subr); | |
836 | len = SCM_STRING_LENGTH (arg); | |
34f0f2b8 | 837 | src = SCM_STRING_CHARS (arg); |
a6d9e5ab DH |
838 | dst = (char *) scm_must_malloc (len + 1, subr); |
839 | memcpy (dst, src, len); | |
840 | dst[len] = 0; | |
841 | argv[i] = dst; | |
0f2d19dd | 842 | } |
a6d9e5ab DH |
843 | argv[i] = 0; |
844 | return argv; | |
0f2d19dd JB |
845 | } |
846 | ||
a1ec6916 | 847 | SCM_DEFINE (scm_execl, "execl", 1, 0, 1, |
1bbd0b84 | 848 | (SCM filename, SCM args), |
b380b885 MD |
849 | "Executes the file named by @var{path} as a new process image.\n" |
850 | "The remaining arguments are supplied to the process; from a C program\n" | |
851 | "they are accessable as the @code{argv} argument to @code{main}.\n" | |
852 | "Conventionally the first @var{arg} is the same as @var{path}.\n" | |
853 | "All arguments must be strings. \n\n" | |
854 | "If @var{arg} is missing, @var{path} is executed with a null\n" | |
855 | "argument list, which may have system-dependent side-effects.\n\n" | |
856 | "This procedure is currently implemented using the @code{execv} system\n" | |
857 | "call, but we call it @code{execl} because of its Scheme calling interface.") | |
1bbd0b84 | 858 | #define FUNC_NAME s_scm_execl |
0f2d19dd JB |
859 | { |
860 | char **execargv; | |
a6d9e5ab DH |
861 | SCM_VALIDATE_STRING (1, filename); |
862 | SCM_STRING_COERCE_0TERMINATION_X (filename); | |
1bbd0b84 | 863 | execargv = scm_convert_exec_args (args, SCM_ARG2, FUNC_NAME); |
a6d9e5ab | 864 | execv (SCM_STRING_CHARS (filename), execargv); |
1bbd0b84 | 865 | SCM_SYSERROR; |
02b754d3 GH |
866 | /* not reached. */ |
867 | return SCM_BOOL_F; | |
0f2d19dd | 868 | } |
1bbd0b84 | 869 | #undef FUNC_NAME |
0f2d19dd | 870 | |
a1ec6916 | 871 | SCM_DEFINE (scm_execlp, "execlp", 1, 0, 1, |
1bbd0b84 | 872 | (SCM filename, SCM args), |
b380b885 MD |
873 | "Similar to @code{execl}, however if\n" |
874 | "@var{filename} does not contain a slash\n" | |
875 | "then the file to execute will be located by searching the\n" | |
876 | "directories listed in the @code{PATH} environment variable.\n\n" | |
6bcd01fc | 877 | "This procedure is currently implemented using the @code{execvp} system\n" |
b380b885 | 878 | "call, but we call it @code{execlp} because of its Scheme calling interface.") |
1bbd0b84 | 879 | #define FUNC_NAME s_scm_execlp |
0f2d19dd JB |
880 | { |
881 | char **execargv; | |
a6d9e5ab DH |
882 | SCM_VALIDATE_STRING (1, filename); |
883 | SCM_STRING_COERCE_0TERMINATION_X (filename); | |
1bbd0b84 | 884 | execargv = scm_convert_exec_args (args, SCM_ARG2, FUNC_NAME); |
a6d9e5ab | 885 | execvp (SCM_STRING_CHARS (filename), execargv); |
1bbd0b84 | 886 | SCM_SYSERROR; |
02b754d3 GH |
887 | /* not reached. */ |
888 | return SCM_BOOL_F; | |
0f2d19dd | 889 | } |
1bbd0b84 | 890 | #undef FUNC_NAME |
0f2d19dd | 891 | |
6afcd3b2 | 892 | static char ** |
3eeba8d4 | 893 | environ_list_to_c (SCM envlist, int arg, const char *proc) |
6afcd3b2 GH |
894 | { |
895 | int num_strings; | |
896 | char **result; | |
a6d9e5ab | 897 | int i; |
6afcd3b2 | 898 | |
6afcd3b2 | 899 | num_strings = scm_ilength (envlist); |
a6d9e5ab | 900 | SCM_ASSERT (num_strings >= 0, envlist, arg, proc); |
6afcd3b2 GH |
901 | result = (char **) malloc ((num_strings + 1) * sizeof (char *)); |
902 | if (result == NULL) | |
903 | scm_memory_error (proc); | |
a6d9e5ab | 904 | for (i = 0; !SCM_NULLP (envlist); ++i, envlist = SCM_CDR (envlist)) |
6afcd3b2 | 905 | { |
a6d9e5ab | 906 | SCM str = SCM_CAR (envlist); |
6afcd3b2 GH |
907 | int len; |
908 | char *src; | |
909 | ||
a6d9e5ab DH |
910 | SCM_ASSERT (SCM_STRINGP (str), envlist, arg, proc); |
911 | len = SCM_STRING_LENGTH (str); | |
34f0f2b8 | 912 | src = SCM_STRING_CHARS (str); |
a6d9e5ab | 913 | result[i] = malloc (len + 1); |
6afcd3b2 GH |
914 | if (result[i] == NULL) |
915 | scm_memory_error (proc); | |
a6d9e5ab DH |
916 | memcpy (result[i], src, len); |
917 | result[i][len] = 0; | |
6afcd3b2 GH |
918 | } |
919 | result[i] = 0; | |
6afcd3b2 GH |
920 | return result; |
921 | } | |
922 | ||
a1ec6916 | 923 | SCM_DEFINE (scm_execle, "execle", 2, 0, 1, |
1bbd0b84 | 924 | (SCM filename, SCM env, SCM args), |
b380b885 MD |
925 | "Similar to @code{execl}, but the environment of the new process is\n" |
926 | "specified by @var{env}, which must be a list of strings as returned by the\n" | |
927 | "@code{environ} procedure.\n\n" | |
928 | "This procedure is currently implemented using the @code{execve} system\n" | |
929 | "call, but we call it @code{execle} because of its Scheme calling interface.") | |
1bbd0b84 | 930 | #define FUNC_NAME s_scm_execle |
6afcd3b2 GH |
931 | { |
932 | char **execargv; | |
933 | char **exec_env; | |
934 | ||
a6d9e5ab DH |
935 | SCM_VALIDATE_STRING (1, filename); |
936 | SCM_STRING_COERCE_0TERMINATION_X (filename); | |
6afcd3b2 | 937 | |
1bbd0b84 GB |
938 | execargv = scm_convert_exec_args (args, SCM_ARG1, FUNC_NAME); |
939 | exec_env = environ_list_to_c (env, SCM_ARG2, FUNC_NAME); | |
a6d9e5ab | 940 | execve (SCM_STRING_CHARS (filename), execargv, exec_env); |
1bbd0b84 | 941 | SCM_SYSERROR; |
6afcd3b2 GH |
942 | /* not reached. */ |
943 | return SCM_BOOL_F; | |
944 | } | |
1bbd0b84 | 945 | #undef FUNC_NAME |
6afcd3b2 | 946 | |
a1ec6916 | 947 | SCM_DEFINE (scm_fork, "primitive-fork", 0, 0, 0, |
1bbd0b84 | 948 | (), |
b380b885 MD |
949 | "Creates a new \"child\" process by duplicating the current \"parent\" process.\n" |
950 | "In the child the return value is 0. In the parent the return value is\n" | |
951 | "the integer process ID of the child.\n\n" | |
952 | "This procedure has been renamed from @code{fork} to avoid a naming conflict\n" | |
953 | "with the scsh fork.") | |
1bbd0b84 | 954 | #define FUNC_NAME s_scm_fork |
0f2d19dd | 955 | { |
bab0f4e5 | 956 | int pid; |
0f2d19dd JB |
957 | pid = fork (); |
958 | if (pid == -1) | |
1bbd0b84 | 959 | SCM_SYSERROR; |
02b754d3 | 960 | return SCM_MAKINUM (0L+pid); |
0f2d19dd | 961 | } |
1bbd0b84 | 962 | #undef FUNC_NAME |
0f2d19dd | 963 | |
f25f761d | 964 | #ifdef HAVE_UNAME |
a1ec6916 | 965 | SCM_DEFINE (scm_uname, "uname", 0, 0, 0, |
1bbd0b84 | 966 | (), |
b380b885 MD |
967 | "Returns an object with some information about the computer system the\n" |
968 | "program is running on.") | |
1bbd0b84 | 969 | #define FUNC_NAME s_scm_uname |
0f2d19dd | 970 | { |
0f2d19dd | 971 | struct utsname buf; |
00ffa0e7 | 972 | SCM ans = scm_c_make_vector (5, SCM_UNSPECIFIED); |
0f2d19dd | 973 | SCM *ve = SCM_VELTS (ans); |
e1a191a8 | 974 | if (uname (&buf) < 0) |
1bbd0b84 | 975 | SCM_SYSERROR; |
0f2d19dd JB |
976 | ve[0] = scm_makfrom0str (buf.sysname); |
977 | ve[1] = scm_makfrom0str (buf.nodename); | |
978 | ve[2] = scm_makfrom0str (buf.release); | |
979 | ve[3] = scm_makfrom0str (buf.version); | |
980 | ve[4] = scm_makfrom0str (buf.machine); | |
981 | /* | |
02b754d3 | 982 | a linux special? |
0f2d19dd JB |
983 | ve[5] = scm_makfrom0str (buf.domainname); |
984 | */ | |
985 | return ans; | |
0f2d19dd | 986 | } |
1bbd0b84 | 987 | #undef FUNC_NAME |
f25f761d | 988 | #endif /* HAVE_UNAME */ |
0f2d19dd | 989 | |
a1ec6916 | 990 | SCM_DEFINE (scm_environ, "environ", 0, 1, 0, |
1bbd0b84 | 991 | (SCM env), |
b380b885 MD |
992 | "If @var{env} is omitted, returns the current environment as a list of strings.\n" |
993 | "Otherwise it sets the current environment, which is also the\n" | |
994 | "default environment for child processes, to the supplied list of strings.\n" | |
995 | "Each member of @var{env} should be of the form\n" | |
996 | "@code{NAME=VALUE} and values of @code{NAME} should not be duplicated.\n" | |
997 | "If @var{env} is supplied then the return value is unspecified.") | |
1bbd0b84 | 998 | #define FUNC_NAME s_scm_environ |
0f2d19dd JB |
999 | { |
1000 | if (SCM_UNBNDP (env)) | |
1001 | return scm_makfromstrs (-1, environ); | |
1002 | else | |
1003 | { | |
0f2d19dd | 1004 | char **new_environ; |
6afcd3b2 | 1005 | |
1bbd0b84 | 1006 | new_environ = environ_list_to_c (env, SCM_ARG1, FUNC_NAME); |
0f2d19dd JB |
1007 | /* Free the old environment, except when called for the first |
1008 | * time. | |
1009 | */ | |
1010 | { | |
1011 | char **ep; | |
1012 | static int first = 1; | |
1013 | if (!first) | |
1014 | { | |
1015 | for (ep = environ; *ep != NULL; ep++) | |
19468eff GH |
1016 | free (*ep); |
1017 | free ((char *) environ); | |
0f2d19dd JB |
1018 | } |
1019 | first = 0; | |
1020 | } | |
1021 | environ = new_environ; | |
1022 | return SCM_UNSPECIFIED; | |
1023 | } | |
1024 | } | |
1bbd0b84 | 1025 | #undef FUNC_NAME |
0f2d19dd | 1026 | |
9ee5fce4 MD |
1027 | #ifdef L_tmpnam |
1028 | ||
a1ec6916 | 1029 | SCM_DEFINE (scm_tmpnam, "tmpnam", 0, 0, 0, |
1bbd0b84 | 1030 | (), |
6d163216 GH |
1031 | "tmpnam returns a name in the file system that does not match\n" |
1032 | "any existing file. However there is no guarantee that\n" | |
1033 | "another process will not create the file after tmpnam\n" | |
1034 | "is called. Care should be taken if opening the file,\n" | |
1035 | "e.g., use the O_EXCL open flag or use @code{mkstemp!} instead.") | |
1bbd0b84 | 1036 | #define FUNC_NAME s_scm_tmpnam |
9ee5fce4 MD |
1037 | { |
1038 | char name[L_tmpnam]; | |
6d163216 GH |
1039 | char *rv; |
1040 | ||
1041 | SCM_SYSCALL (rv = tmpnam (name)); | |
1042 | if (rv == NULL) | |
1043 | /* not SCM_SYSERROR since errno probably not set. */ | |
1044 | SCM_MISC_ERROR ("tmpnam failed", SCM_EOL); | |
9ee5fce4 MD |
1045 | return scm_makfrom0str (name); |
1046 | } | |
0f981281 | 1047 | #undef FUNC_NAME |
0f2d19dd | 1048 | |
1bbd0b84 | 1049 | #endif |
1cc91f1b | 1050 | |
6d163216 GH |
1051 | SCM_DEFINE (scm_mkstemp, "mkstemp!", 1, 0, 0, |
1052 | (SCM tmpl), | |
1053 | "mkstemp creates a new unique file in the file system and\n" | |
1054 | "returns a new buffered port open for reading and writing to\n" | |
1055 | "the file. @var{tmpl} is a string specifying where the\n" | |
1056 | "file should be created: it must end with @code{XXXXXX}\n" | |
1057 | "and will be changed in place to return the name of the\n" | |
1058 | "temporary file.\n") | |
1059 | #define FUNC_NAME s_scm_mkstemp | |
1060 | { | |
1061 | char *c_tmpl; | |
1062 | int rv; | |
1063 | ||
1064 | SCM_STRING_COERCE_0TERMINATION_X (tmpl); | |
1065 | SCM_VALIDATE_STRING_COPY (1, tmpl, c_tmpl); | |
1066 | SCM_SYSCALL (rv = mkstemp (c_tmpl)); | |
1067 | if (rv == -1) | |
1068 | SCM_SYSERROR; | |
1069 | return scm_fdes_to_port (rv, "w+", tmpl); | |
1070 | } | |
1071 | #undef FUNC_NAME | |
1072 | ||
a1ec6916 | 1073 | SCM_DEFINE (scm_utime, "utime", 1, 2, 0, |
1bbd0b84 | 1074 | (SCM pathname, SCM actime, SCM modtime), |
b380b885 MD |
1075 | "@code{utime} sets the access and modification times for\n" |
1076 | "the file named by @var{path}. If @var{actime} or @var{modtime}\n" | |
1077 | "is not supplied, then the current time is used.\n" | |
1078 | "@var{actime} and @var{modtime}\n" | |
1079 | "must be integer time values as returned by the @code{current-time}\n" | |
1080 | "procedure.\n\n" | |
1081 | "E.g.,\n\n" | |
1082 | "@smalllisp\n" | |
1083 | "(utime \"foo\" (- (current-time) 3600))\n" | |
1084 | "@end smalllisp\n\n" | |
1085 | "will set the access time to one hour in the past and the modification\n" | |
1086 | "time to the current time.") | |
1bbd0b84 | 1087 | #define FUNC_NAME s_scm_utime |
0f2d19dd JB |
1088 | { |
1089 | int rv; | |
1090 | struct utimbuf utm_tmp; | |
1091 | ||
a6d9e5ab DH |
1092 | SCM_VALIDATE_STRING (1, pathname); |
1093 | SCM_STRING_COERCE_0TERMINATION_X (pathname); | |
0f2d19dd JB |
1094 | if (SCM_UNBNDP (actime)) |
1095 | SCM_SYSCALL (time (&utm_tmp.actime)); | |
1096 | else | |
1bbd0b84 | 1097 | utm_tmp.actime = SCM_NUM2ULONG (2,actime); |
0f2d19dd JB |
1098 | |
1099 | if (SCM_UNBNDP (modtime)) | |
1100 | SCM_SYSCALL (time (&utm_tmp.modtime)); | |
1101 | else | |
1bbd0b84 | 1102 | utm_tmp.modtime = SCM_NUM2ULONG (3,modtime); |
0f2d19dd | 1103 | |
a6d9e5ab | 1104 | SCM_SYSCALL (rv = utime (SCM_STRING_CHARS (pathname), &utm_tmp)); |
02b754d3 | 1105 | if (rv != 0) |
1bbd0b84 | 1106 | SCM_SYSERROR; |
02b754d3 | 1107 | return SCM_UNSPECIFIED; |
0f2d19dd | 1108 | } |
1bbd0b84 | 1109 | #undef FUNC_NAME |
0f2d19dd | 1110 | |
a1ec6916 | 1111 | SCM_DEFINE (scm_access, "access?", 2, 0, 0, |
1bbd0b84 | 1112 | (SCM path, SCM how), |
b380b885 MD |
1113 | "Returns @code{#t} if @var{path} corresponds to an existing\n" |
1114 | "file and the current process\n" | |
1115 | "has the type of access specified by @var{how}, otherwise \n" | |
1116 | "@code{#f}.\n" | |
1117 | "@var{how} should be specified\n" | |
1118 | "using the values of the variables listed below. Multiple values can\n" | |
1119 | "be combined using a bitwise or, in which case @code{#t} will only\n" | |
1120 | "be returned if all accesses are granted.\n\n" | |
1121 | "Permissions are checked using the real id of the current process,\n" | |
1122 | "not the effective id, although it's the effective id which determines\n" | |
1123 | "whether the access would actually be granted.\n\n" | |
1124 | "@defvar R_OK\n" | |
1125 | "test for read permission.\n" | |
1126 | "@end defvar\n" | |
1127 | "@defvar W_OK\n" | |
1128 | "test for write permission.\n" | |
1129 | "@end defvar\n" | |
1130 | "@defvar X_OK\n" | |
1131 | "test for execute permission.\n" | |
1132 | "@end defvar\n" | |
1133 | "@defvar F_OK\n" | |
1134 | "test for existence of the file.\n" | |
1135 | "@end defvar") | |
1bbd0b84 | 1136 | #define FUNC_NAME s_scm_access |
0f2d19dd JB |
1137 | { |
1138 | int rv; | |
1139 | ||
a6d9e5ab DH |
1140 | SCM_VALIDATE_STRING (1, path); |
1141 | SCM_STRING_COERCE_0TERMINATION_X (path); | |
1142 | SCM_VALIDATE_INUM (2, how); | |
1143 | rv = access (SCM_STRING_CHARS (path), SCM_INUM (how)); | |
156dcb09 | 1144 | return SCM_NEGATE_BOOL(rv); |
0f2d19dd | 1145 | } |
1bbd0b84 | 1146 | #undef FUNC_NAME |
0f2d19dd | 1147 | |
a1ec6916 | 1148 | SCM_DEFINE (scm_getpid, "getpid", 0, 0, 0, |
1bbd0b84 | 1149 | (), |
b380b885 | 1150 | "Returns an integer representing the current process ID.") |
1bbd0b84 | 1151 | #define FUNC_NAME s_scm_getpid |
0f2d19dd JB |
1152 | { |
1153 | return SCM_MAKINUM ((unsigned long) getpid ()); | |
1154 | } | |
1bbd0b84 | 1155 | #undef FUNC_NAME |
0f2d19dd | 1156 | |
a1ec6916 | 1157 | SCM_DEFINE (scm_putenv, "putenv", 1, 0, 0, |
1bbd0b84 | 1158 | (SCM str), |
b380b885 MD |
1159 | "Modifies the environment of the current process, which is\n" |
1160 | "also the default environment inherited by child processes.\n\n" | |
1161 | "If @var{string} is of the form @code{NAME=VALUE} then it will be written\n" | |
1162 | "directly into the environment, replacing any existing environment string\n" | |
1163 | "with\n" | |
1164 | "name matching @code{NAME}. If @var{string} does not contain an equal\n" | |
1165 | "sign, then any existing string with name matching @var{string} will\n" | |
1166 | "be removed.\n\n" | |
1167 | "The return value is unspecified.") | |
1bbd0b84 | 1168 | #define FUNC_NAME s_scm_putenv |
0f2d19dd | 1169 | { |
f93ddd39 | 1170 | int rv; |
19468eff | 1171 | char *ptr; |
f93ddd39 | 1172 | |
9fd38a3d | 1173 | SCM_VALIDATE_STRING (1, str); |
19468eff | 1174 | /* must make a new copy to be left in the environment, safe from gc. */ |
9fd38a3d | 1175 | ptr = malloc (SCM_STRING_LENGTH (str) + 1); |
19468eff | 1176 | if (ptr == NULL) |
1bbd0b84 | 1177 | SCM_MEMORY_ERROR; |
34f0f2b8 | 1178 | strncpy (ptr, SCM_STRING_CHARS (str), SCM_STRING_LENGTH (str)); |
9fd38a3d | 1179 | ptr[SCM_STRING_LENGTH (str)] = 0; |
19468eff | 1180 | rv = putenv (ptr); |
f93ddd39 | 1181 | if (rv < 0) |
1bbd0b84 | 1182 | SCM_SYSERROR; |
f93ddd39 | 1183 | return SCM_UNSPECIFIED; |
0f2d19dd | 1184 | } |
1bbd0b84 | 1185 | #undef FUNC_NAME |
0f2d19dd | 1186 | |
f25f761d | 1187 | #ifdef HAVE_SETLOCALE |
a1ec6916 | 1188 | SCM_DEFINE (scm_setlocale, "setlocale", 1, 1, 0, |
1bbd0b84 | 1189 | (SCM category, SCM locale), |
b380b885 MD |
1190 | "If @var{locale} is omitted, returns the current value of the specified\n" |
1191 | "locale category \n" | |
1192 | "as a system-dependent string.\n" | |
1193 | "@var{category} should be specified using the values @code{LC_COLLATE},\n" | |
1194 | "@code{LC_ALL} etc.\n\n" | |
1195 | "Otherwise the specified locale category is set to\n" | |
1196 | "the string @var{locale}\n" | |
1197 | "and the new value is returned as a system-dependent string. If @var{locale}\n" | |
1198 | "is an empty string, the locale will be set using envirionment variables.") | |
1bbd0b84 | 1199 | #define FUNC_NAME s_scm_setlocale |
0f2d19dd | 1200 | { |
0f2d19dd JB |
1201 | char *clocale; |
1202 | char *rv; | |
1203 | ||
3b3b36dd | 1204 | SCM_VALIDATE_INUM (1,category); |
0f2d19dd JB |
1205 | if (SCM_UNBNDP (locale)) |
1206 | { | |
1207 | clocale = NULL; | |
1208 | } | |
1209 | else | |
1210 | { | |
a6d9e5ab DH |
1211 | SCM_VALIDATE_STRING (2, locale); |
1212 | SCM_STRING_COERCE_0TERMINATION_X (locale); | |
1213 | clocale = SCM_STRING_CHARS (locale); | |
0f2d19dd JB |
1214 | } |
1215 | ||
1216 | rv = setlocale (SCM_INUM (category), clocale); | |
02b754d3 | 1217 | if (rv == NULL) |
1bbd0b84 | 1218 | SCM_SYSERROR; |
02b754d3 | 1219 | return scm_makfrom0str (rv); |
0f2d19dd | 1220 | } |
1bbd0b84 | 1221 | #undef FUNC_NAME |
f25f761d | 1222 | #endif /* HAVE_SETLOCALE */ |
0f2d19dd | 1223 | |
f25f761d | 1224 | #ifdef HAVE_MKNOD |
a1ec6916 | 1225 | SCM_DEFINE (scm_mknod, "mknod", 4, 0, 0, |
1bbd0b84 | 1226 | (SCM path, SCM type, SCM perms, SCM dev), |
b380b885 MD |
1227 | "Creates a new special file, such as a file corresponding to a device.\n" |
1228 | "@var{path} specifies the name of the file. @var{type} should\n" | |
1229 | "be one of the following symbols:\n" | |
1230 | "regular, directory, symlink, block-special, char-special,\n" | |
1231 | "fifo, or socket. @var{perms} (an integer) specifies the file permissions.\n" | |
1232 | "@var{dev} (an integer) specifies which device the special file refers\n" | |
1233 | "to. Its exact interpretation depends on the kind of special file\n" | |
1234 | "being created.\n\n" | |
1235 | "E.g.,\n" | |
1236 | "@example\n" | |
09831f94 NJ |
1237 | "(mknod \"/dev/fd0\" 'block-special #o660 (+ (* 2 256) 2))\n" |
1238 | "@end example\n\n" | |
a3c8b9fc | 1239 | "The return value is unspecified.") |
1bbd0b84 | 1240 | #define FUNC_NAME s_scm_mknod |
0f2d19dd | 1241 | { |
0f2d19dd | 1242 | int val; |
19468eff | 1243 | char *p; |
82a76bdf | 1244 | int ctype = 0; |
19468eff | 1245 | |
a6d9e5ab | 1246 | SCM_VALIDATE_STRING (1, path); |
3b3b36dd GB |
1247 | SCM_VALIDATE_SYMBOL (2,type); |
1248 | SCM_VALIDATE_INUM (3,perms); | |
1249 | SCM_VALIDATE_INUM (4,dev); | |
a6d9e5ab | 1250 | SCM_STRING_COERCE_0TERMINATION_X (path); |
19468eff | 1251 | |
86c991c2 | 1252 | p = SCM_SYMBOL_CHARS (type); |
19468eff GH |
1253 | if (strcmp (p, "regular") == 0) |
1254 | ctype = S_IFREG; | |
1255 | else if (strcmp (p, "directory") == 0) | |
1256 | ctype = S_IFDIR; | |
1257 | else if (strcmp (p, "symlink") == 0) | |
1258 | ctype = S_IFLNK; | |
1259 | else if (strcmp (p, "block-special") == 0) | |
1260 | ctype = S_IFBLK; | |
1261 | else if (strcmp (p, "char-special") == 0) | |
1262 | ctype = S_IFCHR; | |
1263 | else if (strcmp (p, "fifo") == 0) | |
1264 | ctype = S_IFIFO; | |
e655d034 | 1265 | #ifdef S_IFSOCK |
19468eff GH |
1266 | else if (strcmp (p, "socket") == 0) |
1267 | ctype = S_IFSOCK; | |
e655d034 | 1268 | #endif |
19468eff | 1269 | else |
1bbd0b84 | 1270 | SCM_OUT_OF_RANGE (2,type); |
19468eff | 1271 | |
a6d9e5ab DH |
1272 | SCM_SYSCALL (val = mknod (SCM_STRING_CHARS (path), ctype | SCM_INUM (perms), |
1273 | SCM_INUM (dev))); | |
02b754d3 | 1274 | if (val != 0) |
1bbd0b84 | 1275 | SCM_SYSERROR; |
02b754d3 | 1276 | return SCM_UNSPECIFIED; |
0f2d19dd | 1277 | } |
1bbd0b84 | 1278 | #undef FUNC_NAME |
f25f761d | 1279 | #endif /* HAVE_MKNOD */ |
0f2d19dd | 1280 | |
f25f761d | 1281 | #ifdef HAVE_NICE |
a1ec6916 | 1282 | SCM_DEFINE (scm_nice, "nice", 1, 0, 0, |
1bbd0b84 | 1283 | (SCM incr), |
b380b885 MD |
1284 | "Increment the priority of the current process by @var{incr}. A higher\n" |
1285 | "priority value means that the process runs less often.\n" | |
1286 | "The return value is unspecified.") | |
1bbd0b84 | 1287 | #define FUNC_NAME s_scm_nice |
0f2d19dd | 1288 | { |
3b3b36dd | 1289 | SCM_VALIDATE_INUM (1,incr); |
02b754d3 | 1290 | if (nice(SCM_INUM(incr)) != 0) |
1bbd0b84 | 1291 | SCM_SYSERROR; |
02b754d3 | 1292 | return SCM_UNSPECIFIED; |
0f2d19dd | 1293 | } |
1bbd0b84 | 1294 | #undef FUNC_NAME |
f25f761d | 1295 | #endif /* HAVE_NICE */ |
0f2d19dd | 1296 | |
f25f761d | 1297 | #ifdef HAVE_SYNC |
a1ec6916 | 1298 | SCM_DEFINE (scm_sync, "sync", 0, 0, 0, |
1bbd0b84 | 1299 | (), |
b380b885 MD |
1300 | "Flush the operating system disk buffers.\n" |
1301 | "The return value is unspecified.") | |
1bbd0b84 | 1302 | #define FUNC_NAME s_scm_sync |
0f2d19dd | 1303 | { |
0f2d19dd | 1304 | sync(); |
127ec750 | 1305 | return SCM_UNSPECIFIED; |
0f2d19dd | 1306 | } |
1bbd0b84 | 1307 | #undef FUNC_NAME |
f25f761d | 1308 | #endif /* HAVE_SYNC */ |
0f2d19dd | 1309 | |
94e6d793 MG |
1310 | #if HAVE_LIBCRYPT && HAVE_CRYPT_H |
1311 | SCM_DEFINE (scm_crypt, "crypt", 2, 0, 0, | |
1312 | (SCM key, SCM salt), | |
1313 | "Encrypt @var{key} using @var{salt} as the salt value to the\n" | |
1314 | "crypt(3) library call\n") | |
1315 | #define FUNC_NAME s_scm_crypt | |
1316 | { | |
1317 | char * p; | |
1318 | ||
1319 | SCM_VALIDATE_STRING (1, key); | |
1320 | SCM_VALIDATE_STRING (2, salt); | |
1321 | SCM_STRING_COERCE_0TERMINATION_X (key); | |
1322 | SCM_STRING_COERCE_0TERMINATION_X (salt); | |
1323 | ||
1324 | p = crypt (SCM_STRING_CHARS (key), SCM_STRING_CHARS (salt)); | |
1325 | return scm_makfrom0str (p); | |
1326 | } | |
1327 | #undef FUNC_NAME | |
1328 | #endif /* HAVE_LIBCRYPT && HAVE_CRYPT_H */ | |
1329 | ||
1330 | #if HAVE_CHROOT | |
1331 | SCM_DEFINE (scm_chroot, "chroot", 1, 0, 0, | |
1332 | (SCM path), | |
1333 | "Change the root directory to that specified in @var{path}.\n" | |
1334 | "This directory will be used for path names beginning with\n" | |
1335 | "@file{/}. The root directory is inherited by all children\n" | |
1336 | "of the current process. Only the superuser may change the\n" | |
1337 | "root directory.") | |
1338 | #define FUNC_NAME s_scm_chroot | |
1339 | { | |
1340 | SCM_VALIDATE_STRING (1, path); | |
1341 | SCM_STRING_COERCE_0TERMINATION_X (path); | |
1342 | ||
1343 | if (chroot (SCM_STRING_CHARS (path)) == -1) | |
1344 | SCM_SYSERROR; | |
1345 | return SCM_UNSPECIFIED; | |
1346 | } | |
1347 | #undef FUNC_NAME | |
1348 | #endif /* HAVE_CHROOT */ | |
1349 | ||
1350 | #if HAVE_GETLOGIN | |
1351 | SCM_DEFINE (scm_getlogin, "getlogin", 0, 0, 0, | |
1352 | (void), | |
1353 | "Return a string containing the name of the user logged in on\n" | |
1354 | "the controlling terminal of the process, or @code{#f} if this\n" | |
1355 | "information cannot be obtained.") | |
1356 | #define FUNC_NAME s_scm_getlogin | |
1357 | { | |
1358 | char * p; | |
1359 | ||
1360 | p = getlogin (); | |
1361 | if (!p || !*p) | |
1362 | return SCM_BOOL_F; | |
1363 | return scm_makfrom0str (p); | |
1364 | } | |
1365 | #undef FUNC_NAME | |
1366 | #endif /* HAVE_GETLOGIN */ | |
1367 | ||
1368 | #if HAVE_CUSERID | |
1369 | SCM_DEFINE (scm_cuserid, "cuserid", 0, 0, 0, | |
1370 | (void), | |
1371 | "Return a string containing a user name associated with the\n" | |
1372 | "effective user id of the process. Return @code{#f} if this\n" | |
1373 | "information cannot be obtained.") | |
1374 | #define FUNC_NAME s_scm_cuserid | |
1375 | { | |
1376 | char * p; | |
1377 | ||
1378 | p = cuserid (NULL); | |
1379 | if (!p || !*p) | |
1380 | return SCM_BOOL_F; | |
1381 | return scm_makfrom0str (p); | |
1382 | } | |
1383 | #undef FUNC_NAME | |
1384 | #endif /* HAVE_CUSERID */ | |
1385 | ||
1386 | #if HAVE_GETPRIORITY | |
1387 | SCM_DEFINE (scm_getpriority, "getpriority", 2, 0, 0, | |
1388 | (SCM which, SCM who), | |
1389 | "Return the scheduling priority of the process, process group\n" | |
1390 | "or user, as indicated by @var{which} and @var{who}. @var{which}\n" | |
1391 | "is one of the variables @code{PRIO_PROCESS}, @code{PRIO_PGRP}\n" | |
1392 | "or @code{PRIO_USER}, and @var{who} is interpreted relative to\n" | |
1393 | "@var{which} (a process identifier for @code{PRIO_PROCESS},\n" | |
1394 | "process group identifier for @code{PRIO_PGRP}, and a user\n" | |
1395 | "identifier for @code{PRIO_USER}. A zero value of @var{who}\n" | |
1396 | "denotes the current process, process group, or user. Return\n" | |
1397 | "the highest priority (lowest numerical value) of any of the\n" | |
1398 | "specified processes.") | |
1399 | #define FUNC_NAME s_scm_getpriority | |
1400 | { | |
1401 | int cwhich, cwho, ret; | |
1402 | ||
1403 | SCM_VALIDATE_INUM_COPY (1, which, cwhich); | |
1404 | SCM_VALIDATE_INUM_COPY (2, who, cwho); | |
1405 | ||
1406 | /* We have to clear errno and examine it later, because -1 is a | |
1407 | legal return value for getpriority(). */ | |
1408 | errno = 0; | |
1409 | ret = getpriority (cwhich, cwho); | |
1410 | if (errno != 0) | |
1411 | SCM_SYSERROR; | |
1412 | return SCM_MAKINUM (ret); | |
1413 | } | |
1414 | #undef FUNC_NAME | |
1415 | #endif /* HAVE_GETPRIORITY */ | |
1416 | ||
1417 | #if HAVE_SETPRIORITY | |
1418 | SCM_DEFINE (scm_setpriority, "setpriority", 3, 0, 0, | |
1419 | (SCM which, SCM who, SCM prio), | |
1420 | "Set the scheduling priority of the process, process group\n" | |
1421 | "or user, as indicated by @var{which} and @var{who}. @var{which}\n" | |
1422 | "is one of the variables @code{PRIO_PROCESS}, @code{PRIO_PGRP}\n" | |
1423 | "or @code{PRIO_USER}, and @var{who} is interpreted relative to\n" | |
1424 | "@var{which} (a process identifier for @code{PRIO_PROCESS},\n" | |
1425 | "process group identifier for @code{PRIO_PGRP}, and a user\n" | |
1426 | "identifier for @code{PRIO_USER}. A zero value of @var{who}\n" | |
1427 | "denotes the current process, process group, or user.\n" | |
1428 | "@var{prio} is a value in the range -20 and 20, the default\n" | |
1429 | "priority is 0; lower priorities cause more favorable\n" | |
1430 | "scheduling. Sets the priority of all of the specified\n" | |
1431 | "processes. Only the super-user may lower priorities.\n" | |
1432 | "The return value is not specified.") | |
1433 | #define FUNC_NAME s_scm_setpriority | |
1434 | { | |
1435 | int cwhich, cwho, cprio; | |
1436 | ||
1437 | SCM_VALIDATE_INUM_COPY (1, which, cwhich); | |
1438 | SCM_VALIDATE_INUM_COPY (2, who, cwho); | |
1439 | SCM_VALIDATE_INUM_COPY (3, prio, cprio); | |
1440 | ||
1441 | if (setpriority (cwhich, cwho, cprio) == -1) | |
1442 | SCM_SYSERROR; | |
1443 | return SCM_UNSPECIFIED; | |
1444 | } | |
1445 | #undef FUNC_NAME | |
1446 | #endif /* HAVE_SETPRIORITY */ | |
1447 | ||
1448 | #if HAVE_GETPASS | |
1449 | SCM_DEFINE (scm_getpass, "getpass", 1, 0, 0, | |
1450 | (SCM prompt), | |
1451 | "Display @var{prompt} to the standard error output and read\n" | |
1452 | "a password from @file{/dev/tty}. If this file is not\n" | |
1453 | "accessible, it reads from standard input. The password may be\n" | |
1454 | "up to 127 characters in length. Additional characters and the\n" | |
1455 | "terminating newline character are discarded. While reading\n" | |
1456 | "the password, echoing and the generation of signals by special\n" | |
1457 | "characters is disabled.") | |
1458 | #define FUNC_NAME s_scm_getpass | |
1459 | { | |
1460 | char * p; | |
1461 | SCM passwd; | |
1462 | ||
1463 | SCM_VALIDATE_STRING (1, prompt); | |
1464 | SCM_STRING_COERCE_0TERMINATION_X (prompt); | |
1465 | ||
1466 | p = getpass(SCM_STRING_CHARS (prompt)); | |
1467 | passwd = scm_makfrom0str (p); | |
1468 | ||
1469 | /* Clear out the password in the static buffer. */ | |
1470 | memset (p, 0, strlen (p)); | |
1471 | ||
1472 | return passwd; | |
1473 | } | |
1474 | #undef FUNC_NAME | |
1475 | #endif /* HAVE_GETPASS */ | |
1476 | ||
1477 | #if HAVE_FLOCK | |
1478 | SCM_DEFINE (scm_flock, "flock", 2, 0, 0, | |
1479 | (SCM file, SCM operation), | |
1480 | "Apply or remove an advisory lock on an open file.\n" | |
1481 | "@var{operation} specifies the action to be done:\n" | |
1482 | "@table @code\n" | |
1483 | "@item LOCK_SH\n" | |
1484 | "Shared lock. More than one process may hold a shared lock\n" | |
1485 | "for a given file at a given time.\n" | |
1486 | "@item LOCK_EX\n" | |
1487 | "Exclusive lock. Only one process may hold an exclusive lock\n" | |
1488 | "for a given file at a given time.\n" | |
1489 | "@item LOCK_UN\n" | |
1490 | "Unlock the file.\n" | |
1491 | "@item LOCK_NB\n" | |
1492 | "Don't block when locking. May be specified by bitwise OR'ing\n" | |
1493 | "it to one of the other operations.\n" | |
1494 | "@end table\n" | |
1495 | "The return value is not specified. @var{file} may be an open\n" | |
1496 | "file descriptor or an open file descriptior port.") | |
1497 | #define FUNC_NAME s_scm_flock | |
1498 | { | |
1499 | int coperation, fdes; | |
1500 | ||
1501 | if (SCM_INUMP (file)) | |
1502 | fdes = SCM_INUM (file); | |
1503 | else | |
1504 | { | |
1505 | SCM_VALIDATE_OPFPORT (2, file); | |
1506 | ||
1507 | fdes = SCM_FPORT_FDES (file); | |
1508 | } | |
1509 | SCM_VALIDATE_INUM_COPY (2, operation, coperation); | |
1510 | if (flock (fdes, coperation) == -1) | |
1511 | SCM_SYSERROR; | |
1512 | return SCM_UNSPECIFIED; | |
1513 | } | |
1514 | #undef FUNC_NAME | |
1515 | #endif /* HAVE_FLOCK */ | |
1516 | ||
1517 | #if HAVE_SETHOSTNAME | |
1518 | SCM_DEFINE (scm_sethostname, "sethostname", 1, 0, 0, | |
1519 | (SCM name), | |
1520 | "Set the host name of the current processor to @var{name}. May\n" | |
1521 | "only be used by the superuser. The return value is not\n" | |
1522 | "specified.") | |
1523 | #define FUNC_NAME s_scm_sethostname | |
1524 | { | |
1525 | SCM_VALIDATE_STRING (1, name); | |
1526 | SCM_STRING_COERCE_0TERMINATION_X (name); | |
1527 | ||
1528 | if (sethostname (SCM_STRING_CHARS (name), SCM_STRING_LENGTH (name)) == -1) | |
1529 | SCM_SYSERROR; | |
1530 | return SCM_UNSPECIFIED; | |
1531 | } | |
1532 | #undef FUNC_NAME | |
1533 | #endif /* HAVE_SETHOSTNAME */ | |
1534 | ||
1535 | #if HAVE_GETHOSTNAME | |
1536 | SCM_DEFINE (scm_gethostname, "gethostname", 0, 0, 0, | |
1537 | (void), | |
1538 | "Return the host name of the current processor.") | |
1539 | #define FUNC_NAME s_scm_gethostname | |
1540 | { | |
a0f9c651 MG |
1541 | /* 256 is for Solaris, under Linux ENAMETOOLONG is returned if not |
1542 | large enough. */ | |
1543 | int len = 256, res; | |
94e6d793 MG |
1544 | char *p = scm_must_malloc (len, "gethostname"); |
1545 | SCM name; | |
1546 | ||
1547 | res = gethostname (p, len); | |
1548 | while (res == -1 && errno == ENAMETOOLONG) | |
1549 | { | |
1550 | p = scm_must_realloc (p, len, len * 2, "gethostname"); | |
1551 | len *= 2; | |
1552 | res = gethostname (p, len); | |
1553 | } | |
1554 | if (res == -1) | |
1555 | { | |
1556 | scm_must_free (p); | |
1557 | SCM_SYSERROR; | |
1558 | } | |
1559 | name = scm_makfrom0str (p); | |
1560 | scm_must_free (p); | |
1561 | return name; | |
1562 | } | |
1563 | #undef FUNC_NAME | |
1564 | #endif /* HAVE_GETHOSTNAME */ | |
1565 | ||
0f2d19dd JB |
1566 | void |
1567 | scm_init_posix () | |
0f2d19dd JB |
1568 | { |
1569 | scm_add_feature ("posix"); | |
1570 | #ifdef HAVE_GETEUID | |
1571 | scm_add_feature ("EIDs"); | |
1572 | #endif | |
1573 | #ifdef WAIT_ANY | |
1574 | scm_sysintern ("WAIT_ANY", SCM_MAKINUM (WAIT_ANY)); | |
1575 | #endif | |
1576 | #ifdef WAIT_MYPGRP | |
1577 | scm_sysintern ("WAIT_MYPGRP", SCM_MAKINUM (WAIT_MYPGRP)); | |
1578 | #endif | |
1579 | #ifdef WNOHANG | |
1580 | scm_sysintern ("WNOHANG", SCM_MAKINUM (WNOHANG)); | |
1581 | #endif | |
1582 | #ifdef WUNTRACED | |
1583 | scm_sysintern ("WUNTRACED", SCM_MAKINUM (WUNTRACED)); | |
1584 | #endif | |
1585 | ||
0f2d19dd | 1586 | /* access() symbols. */ |
bab0f4e5 JB |
1587 | scm_sysintern ("R_OK", SCM_MAKINUM (R_OK)); |
1588 | scm_sysintern ("W_OK", SCM_MAKINUM (W_OK)); | |
1589 | scm_sysintern ("X_OK", SCM_MAKINUM (X_OK)); | |
1590 | scm_sysintern ("F_OK", SCM_MAKINUM (F_OK)); | |
0f2d19dd JB |
1591 | |
1592 | #ifdef LC_COLLATE | |
1593 | scm_sysintern ("LC_COLLATE", SCM_MAKINUM (LC_COLLATE)); | |
1594 | #endif | |
1595 | #ifdef LC_CTYPE | |
1596 | scm_sysintern ("LC_CTYPE", SCM_MAKINUM (LC_CTYPE)); | |
1597 | #endif | |
1598 | #ifdef LC_MONETARY | |
1599 | scm_sysintern ("LC_MONETARY", SCM_MAKINUM (LC_MONETARY)); | |
1600 | #endif | |
1601 | #ifdef LC_NUMERIC | |
1602 | scm_sysintern ("LC_NUMERIC", SCM_MAKINUM (LC_NUMERIC)); | |
1603 | #endif | |
1604 | #ifdef LC_TIME | |
1605 | scm_sysintern ("LC_TIME", SCM_MAKINUM (LC_TIME)); | |
1606 | #endif | |
1607 | #ifdef LC_MESSAGES | |
1608 | scm_sysintern ("LC_MESSAGES", SCM_MAKINUM (LC_MESSAGES)); | |
1609 | #endif | |
1610 | #ifdef LC_ALL | |
1611 | scm_sysintern ("LC_ALL", SCM_MAKINUM (LC_ALL)); | |
1612 | #endif | |
bd9e24b3 GH |
1613 | #ifdef PIPE_BUF |
1614 | scm_sysintern ("PIPE_BUF", scm_long2num (PIPE_BUF)); | |
1615 | #endif | |
1616 | ||
94e6d793 MG |
1617 | #ifdef PRIO_PROCESS |
1618 | scm_sysintern ("PRIO_PROCESS", SCM_MAKINUM (PRIO_PROCESS)); | |
1619 | #endif | |
1620 | #ifdef PRIO_PGRP | |
1621 | scm_sysintern ("PRIO_PGRP", SCM_MAKINUM (PRIO_PGRP)); | |
1622 | #endif | |
1623 | #ifdef PRIO_USER | |
1624 | scm_sysintern ("PRIO_USER", SCM_MAKINUM (PRIO_USER)); | |
1625 | #endif | |
1626 | ||
1627 | #ifdef LOCK_SH | |
1628 | scm_sysintern ("LOCK_SH", SCM_MAKINUM (LOCK_SH)); | |
1629 | #endif | |
1630 | #ifdef LOCK_EX | |
1631 | scm_sysintern ("LOCK_EX", SCM_MAKINUM (LOCK_EX)); | |
1632 | #endif | |
1633 | #ifdef LOCK_UN | |
1634 | scm_sysintern ("LOCK_UN", SCM_MAKINUM (LOCK_UN)); | |
1635 | #endif | |
1636 | #ifdef LOCK_NB | |
1637 | scm_sysintern ("LOCK_NB", SCM_MAKINUM (LOCK_NB)); | |
1638 | #endif | |
1639 | ||
a0599745 | 1640 | #include "libguile/cpp_sig_symbols.c" |
8dc9439f | 1641 | #ifndef SCM_MAGIC_SNARFER |
a0599745 | 1642 | #include "libguile/posix.x" |
8dc9439f | 1643 | #endif |
0f2d19dd | 1644 | } |
89e00824 ML |
1645 | |
1646 | /* | |
1647 | Local Variables: | |
1648 | c-file-style: "gnu" | |
1649 | End: | |
1650 | */ |