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