* Removed lots of deprecated stuff.
[bpt/guile.git] / libguile / posix.c
1 /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001 Free Software Foundation, Inc.
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
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
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.
40 * If you do not wish that, delete this exception notice. */
41
42
43 \f
44
45 /* Make GNU/Linux libc declare everything it has. */
46 #define _GNU_SOURCE
47
48 #include <stdio.h>
49 #include <errno.h>
50
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"
60 \f
61
62 #ifdef HAVE_STRING_H
63 #include <string.h>
64 #endif
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>
78 #else
79 #ifndef ttyname
80 extern char *ttyname();
81 #endif
82 #endif
83
84 #ifdef LIBC_H_WITH_UNISTD_H
85 #include <libc.h>
86 #endif
87
88 #include <sys/types.h>
89 #include <sys/stat.h>
90 #include <fcntl.h>
91
92 #ifdef HAVE_PWD_H
93 #include <pwd.h>
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. */
104 # include <process.h>
105 # define pipe(fd) _pipe (fd, 256, O_BINARY)
106 #endif /* __MINGW32__ */
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
120 extern char ** environ;
121
122 #ifdef HAVE_GRP_H
123 #include <grp.h>
124 #endif
125 #ifdef HAVE_SYS_UTSNAME_H
126 #include <sys/utsname.h>
127 #endif
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
146 #ifdef HAVE_SETLOCALE
147 #include <locale.h>
148 #endif
149
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
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
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
199 \f
200 SCM_SYMBOL (sym_read_pipe, "read pipe");
201 SCM_SYMBOL (sym_write_pipe, "write pipe");
202
203 SCM_DEFINE (scm_pipe, "pipe", 0, 0, 0,
204 (),
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.")
218 #define FUNC_NAME s_scm_pipe
219 {
220 int fd[2], rv;
221 SCM p_rd, p_wt;
222
223 rv = pipe (fd);
224 if (rv)
225 SCM_SYSERROR;
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);
229 return scm_cons (p_rd, p_wt);
230 }
231 #undef FUNC_NAME
232
233
234 #ifdef HAVE_GETGROUPS
235 SCM_DEFINE (scm_getgroups, "getgroups", 0, 0, 0,
236 (),
237 "Return a vector of integers representing the current\n"
238 "supplimentary group IDs.")
239 #define FUNC_NAME s_scm_getgroups
240 {
241 SCM ans;
242 int ngroups;
243 size_t size;
244 GETGROUPS_T *groups;
245
246 ngroups = getgroups (0, NULL);
247 if (ngroups <= 0)
248 SCM_SYSERROR;
249
250 size = ngroups * sizeof (GETGROUPS_T);
251 groups = scm_must_malloc (size, FUNC_NAME);
252 getgroups (ngroups, groups);
253
254 ans = scm_c_make_vector (ngroups, SCM_UNDEFINED);
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;
262 }
263 #undef FUNC_NAME
264 #endif
265
266 #ifdef HAVE_GETPWENT
267 SCM_DEFINE (scm_getpwuid, "getpw", 0, 1, 0,
268 (SCM user),
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.")
272 #define FUNC_NAME s_scm_getpwuid
273 {
274 SCM result;
275 struct passwd *entry;
276 SCM *ve;
277
278 result = scm_c_make_vector (7, SCM_UNSPECIFIED);
279 ve = SCM_VELTS (result);
280 if (SCM_UNBNDP (user) || SCM_FALSEP (user))
281 {
282 SCM_SYSCALL (entry = getpwent ());
283 if (! entry)
284 {
285 return SCM_BOOL_F;
286 }
287 }
288 else if (SCM_INUMP (user))
289 {
290 entry = getpwuid (SCM_INUM (user));
291 }
292 else
293 {
294 SCM_VALIDATE_STRING (1, user);
295 entry = getpwnam (SCM_STRING_CHARS (user));
296 }
297 if (!entry)
298 SCM_MISC_ERROR ("entry not found", SCM_EOL);
299
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);
313 return result;
314 }
315 #undef FUNC_NAME
316 #endif /* HAVE_GETPWENT */
317
318
319 #ifdef HAVE_SETPWENT
320 SCM_DEFINE (scm_setpwent, "setpw", 0, 1, 0,
321 (SCM arg),
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.")
325 #define FUNC_NAME s_scm_setpwent
326 {
327 if (SCM_UNBNDP (arg) || SCM_FALSEP (arg))
328 endpwent ();
329 else
330 setpwent ();
331 return SCM_UNSPECIFIED;
332 }
333 #undef FUNC_NAME
334 #endif
335
336
337 #ifdef HAVE_GETGRENT
338 /* Combines getgrgid and getgrnam. */
339 SCM_DEFINE (scm_getgrgid, "getgr", 0, 1, 0,
340 (SCM name),
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.")
344 #define FUNC_NAME s_scm_getgrgid
345 {
346 SCM result;
347 struct group *entry;
348 SCM *ve;
349 result = scm_c_make_vector (4, SCM_UNSPECIFIED);
350 ve = SCM_VELTS (result);
351 if (SCM_UNBNDP (name) || SCM_FALSEP (name))
352 {
353 SCM_SYSCALL (entry = getgrent ());
354 if (! entry)
355 {
356 return SCM_BOOL_F;
357 }
358 }
359 else if (SCM_INUMP (name))
360 SCM_SYSCALL (entry = getgrgid (SCM_INUM (name)));
361 else
362 {
363 SCM_VALIDATE_STRING (1, name);
364 SCM_SYSCALL (entry = getgrnam (SCM_STRING_CHARS (name)));
365 }
366 if (!entry)
367 SCM_SYSERROR;
368
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);
373 return result;
374 }
375 #undef FUNC_NAME
376
377
378
379 SCM_DEFINE (scm_setgrent, "setgr", 0, 1, 0,
380 (SCM arg),
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.")
384 #define FUNC_NAME s_scm_setgrent
385 {
386 if (SCM_UNBNDP (arg) || SCM_FALSEP (arg))
387 endgrent ();
388 else
389 setgrent ();
390 return SCM_UNSPECIFIED;
391 }
392 #undef FUNC_NAME
393 #endif /* HAVE_GETGRENT */
394
395
396 SCM_DEFINE (scm_kill, "kill", 2, 0, 0,
397 (SCM pid, SCM sig),
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")
420 #define FUNC_NAME s_scm_kill
421 {
422 SCM_VALIDATE_INUM (1,pid);
423 SCM_VALIDATE_INUM (2,sig);
424 /* Signal values are interned in scm_init_posix(). */
425 #ifdef HAVE_KILL
426 if (kill ((int) SCM_INUM (pid), (int) SCM_INUM (sig)) != 0)
427 #else
428 if ((int) SCM_INUM (pid) == getpid ())
429 if (raise ((int) SCM_INUM (sig)) != 0)
430 #endif
431 SCM_SYSERROR;
432 return SCM_UNSPECIFIED;
433 }
434 #undef FUNC_NAME
435
436 #ifdef HAVE_WAITPID
437 SCM_DEFINE (scm_waitpid, "waitpid", 1, 1, 0,
438 (SCM pid, SCM options),
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")
473 #define FUNC_NAME s_scm_waitpid
474 {
475 int i;
476 int status;
477 int ioptions;
478 SCM_VALIDATE_INUM (1,pid);
479 if (SCM_UNBNDP (options))
480 ioptions = 0;
481 else
482 {
483 SCM_VALIDATE_INUM (2,options);
484 /* Flags are interned in scm_init_posix. */
485 ioptions = SCM_INUM (options);
486 }
487 SCM_SYSCALL (i = waitpid (SCM_INUM (pid), &status, ioptions));
488 if (i == -1)
489 SCM_SYSERROR;
490 return scm_cons (SCM_MAKINUM (0L + i), SCM_MAKINUM (0L + status));
491 }
492 #undef FUNC_NAME
493 #endif /* HAVE_WAITPID */
494
495 #ifndef __MINGW32__
496 SCM_DEFINE (scm_status_exit_val, "status:exit-val", 1, 0, 0,
497 (SCM status),
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}.")
501 #define FUNC_NAME s_scm_status_exit_val
502 {
503 int lstatus;
504
505 SCM_VALIDATE_INUM (1,status);
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)));
512 else
513 return SCM_BOOL_F;
514 }
515 #undef FUNC_NAME
516
517 SCM_DEFINE (scm_status_term_sig, "status:term-sig", 1, 0, 0,
518 (SCM status),
519 "Return the signal number which terminated the process, if any,\n"
520 "otherwise @code{#f}.")
521 #define FUNC_NAME s_scm_status_term_sig
522 {
523 int lstatus;
524
525 SCM_VALIDATE_INUM (1,status);
526
527 lstatus = SCM_INUM (status);
528 if (WIFSIGNALED (lstatus))
529 return SCM_MAKINUM (WTERMSIG (lstatus));
530 else
531 return SCM_BOOL_F;
532 }
533 #undef FUNC_NAME
534
535 SCM_DEFINE (scm_status_stop_sig, "status:stop-sig", 1, 0, 0,
536 (SCM status),
537 "Return the signal number which stopped the process, if any,\n"
538 "otherwise @code{#f}.")
539 #define FUNC_NAME s_scm_status_stop_sig
540 {
541 int lstatus;
542
543 SCM_VALIDATE_INUM (1,status);
544
545 lstatus = SCM_INUM (status);
546 if (WIFSTOPPED (lstatus))
547 return SCM_MAKINUM (WSTOPSIG (lstatus));
548 else
549 return SCM_BOOL_F;
550 }
551 #undef FUNC_NAME
552 #endif /* __MINGW32__ */
553
554 #ifdef HAVE_GETPPID
555 SCM_DEFINE (scm_getppid, "getppid", 0, 0, 0,
556 (),
557 "Return an integer representing the process ID of the parent\n"
558 "process.")
559 #define FUNC_NAME s_scm_getppid
560 {
561 return SCM_MAKINUM (0L + getppid ());
562 }
563 #undef FUNC_NAME
564 #endif /* HAVE_GETPPID */
565
566
567 #ifndef __MINGW32__
568 SCM_DEFINE (scm_getuid, "getuid", 0, 0, 0,
569 (),
570 "Return an integer representing the current real user ID.")
571 #define FUNC_NAME s_scm_getuid
572 {
573 return SCM_MAKINUM (0L + getuid ());
574 }
575 #undef FUNC_NAME
576
577
578
579 SCM_DEFINE (scm_getgid, "getgid", 0, 0, 0,
580 (),
581 "Return an integer representing the current real group ID.")
582 #define FUNC_NAME s_scm_getgid
583 {
584 return SCM_MAKINUM (0L + getgid ());
585 }
586 #undef FUNC_NAME
587
588
589
590 SCM_DEFINE (scm_geteuid, "geteuid", 0, 0, 0,
591 (),
592 "Return an integer representing the current effective user ID.\n"
593 "If the system does not support effective IDs, then the real ID\n"
594 "is returned. @code{(feature? 'EIDs)} reports whether the\n"
595 "system supports effective IDs.")
596 #define FUNC_NAME s_scm_geteuid
597 {
598 #ifdef HAVE_GETEUID
599 return SCM_MAKINUM (0L + geteuid ());
600 #else
601 return SCM_MAKINUM (0L + getuid ());
602 #endif
603 }
604 #undef FUNC_NAME
605
606
607 SCM_DEFINE (scm_getegid, "getegid", 0, 0, 0,
608 (),
609 "Return an integer representing the current effective group ID.\n"
610 "If the system does not support effective IDs, then the real ID\n"
611 "is returned. @code{(feature? 'EIDs)} reports whether the\n"
612 "system supports effective IDs.")
613 #define FUNC_NAME s_scm_getegid
614 {
615 #ifdef HAVE_GETEUID
616 return SCM_MAKINUM (0L + getegid ());
617 #else
618 return SCM_MAKINUM (0L + getgid ());
619 #endif
620 }
621 #undef FUNC_NAME
622
623
624 SCM_DEFINE (scm_setuid, "setuid", 1, 0, 0,
625 (SCM id),
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.")
629 #define FUNC_NAME s_scm_setuid
630 {
631 SCM_VALIDATE_INUM (1,id);
632 if (setuid (SCM_INUM (id)) != 0)
633 SCM_SYSERROR;
634 return SCM_UNSPECIFIED;
635 }
636 #undef FUNC_NAME
637
638 SCM_DEFINE (scm_setgid, "setgid", 1, 0, 0,
639 (SCM id),
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.")
643 #define FUNC_NAME s_scm_setgid
644 {
645 SCM_VALIDATE_INUM (1,id);
646 if (setgid (SCM_INUM (id)) != 0)
647 SCM_SYSERROR;
648 return SCM_UNSPECIFIED;
649 }
650 #undef FUNC_NAME
651
652 SCM_DEFINE (scm_seteuid, "seteuid", 1, 0, 0,
653 (SCM id),
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.")
659 #define FUNC_NAME s_scm_seteuid
660 {
661 int rv;
662
663 SCM_VALIDATE_INUM (1,id);
664 #ifdef HAVE_SETEUID
665 rv = seteuid (SCM_INUM (id));
666 #else
667 rv = setuid (SCM_INUM (id));
668 #endif
669 if (rv != 0)
670 SCM_SYSERROR;
671 return SCM_UNSPECIFIED;
672 }
673 #undef FUNC_NAME
674 #endif /* __MINGW32__ */
675
676
677 #ifdef HAVE_SETEGID
678 SCM_DEFINE (scm_setegid, "setegid", 1, 0, 0,
679 (SCM id),
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.")
685 #define FUNC_NAME s_scm_setegid
686 {
687 int rv;
688
689 SCM_VALIDATE_INUM (1,id);
690 #ifdef HAVE_SETEUID
691 rv = setegid (SCM_INUM (id));
692 #else
693 rv = setgid (SCM_INUM (id));
694 #endif
695 if (rv != 0)
696 SCM_SYSERROR;
697 return SCM_UNSPECIFIED;
698
699 }
700 #undef FUNC_NAME
701 #endif
702
703
704 #ifdef HAVE_GETPGRP
705 SCM_DEFINE (scm_getpgrp, "getpgrp", 0, 0, 0,
706 (),
707 "Return an integer representing the current process group ID.\n"
708 "This is the POSIX definition, not BSD.")
709 #define FUNC_NAME s_scm_getpgrp
710 {
711 int (*fn)();
712 fn = (int (*) ()) getpgrp;
713 return SCM_MAKINUM (fn (0));
714 }
715 #undef FUNC_NAME
716 #endif /* HAVE_GETPGRP */
717
718
719 #ifdef HAVE_SETPGID
720 SCM_DEFINE (scm_setpgid, "setpgid", 2, 0, 0,
721 (SCM pid, SCM pgid),
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.")
727 #define FUNC_NAME s_scm_setpgid
728 {
729 SCM_VALIDATE_INUM (1,pid);
730 SCM_VALIDATE_INUM (2,pgid);
731 /* FIXME(?): may be known as setpgrp. */
732 if (setpgid (SCM_INUM (pid), SCM_INUM (pgid)) != 0)
733 SCM_SYSERROR;
734 return SCM_UNSPECIFIED;
735 }
736 #undef FUNC_NAME
737 #endif /* HAVE_SETPGID */
738
739 #ifdef HAVE_SETSID
740 SCM_DEFINE (scm_setsid, "setsid", 0, 0, 0,
741 (),
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.")
746 #define FUNC_NAME s_scm_setsid
747 {
748 pid_t sid = setsid ();
749 if (sid == -1)
750 SCM_SYSERROR;
751 return SCM_UNSPECIFIED;
752 }
753 #undef FUNC_NAME
754 #endif /* HAVE_SETSID */
755
756 #ifdef HAVE_TTYNAME
757 SCM_DEFINE (scm_ttyname, "ttyname", 1, 0, 0,
758 (SCM port),
759 "Return a string with the name of the serial terminal device\n"
760 "underlying @var{port}.")
761 #define FUNC_NAME s_scm_ttyname
762 {
763 char *ans;
764 int fd;
765
766 port = SCM_COERCE_OUTPORT (port);
767 SCM_VALIDATE_OPPORT (1,port);
768 if (!SCM_FPORTP (port))
769 return SCM_BOOL_F;
770 fd = SCM_FPORT_FDES (port);
771 SCM_SYSCALL (ans = ttyname (fd));
772 if (!ans)
773 SCM_SYSERROR;
774 /* ans could be overwritten by another call to ttyname */
775 return (scm_makfrom0str (ans));
776 }
777 #undef FUNC_NAME
778 #endif /* HAVE_TTYNAME */
779
780 #ifdef HAVE_CTERMID
781 SCM_DEFINE (scm_ctermid, "ctermid", 0, 0, 0,
782 (),
783 "Return a string containing the file name of the controlling\n"
784 "terminal for the current process.")
785 #define FUNC_NAME s_scm_ctermid
786 {
787 char *result = ctermid (NULL);
788 if (*result == '\0')
789 SCM_SYSERROR;
790 return scm_makfrom0str (result);
791 }
792 #undef FUNC_NAME
793 #endif /* HAVE_CTERMID */
794
795 #ifdef HAVE_TCGETPGRP
796 SCM_DEFINE (scm_tcgetpgrp, "tcgetpgrp", 1, 0, 0,
797 (SCM port),
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"
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.")
808 #define FUNC_NAME s_scm_tcgetpgrp
809 {
810 int fd;
811 pid_t pgid;
812
813 port = SCM_COERCE_OUTPORT (port);
814
815 SCM_VALIDATE_OPFPORT (1,port);
816 fd = SCM_FPORT_FDES (port);
817 if ((pgid = tcgetpgrp (fd)) == -1)
818 SCM_SYSERROR;
819 return SCM_MAKINUM (pgid);
820 }
821 #undef FUNC_NAME
822 #endif /* HAVE_TCGETPGRP */
823
824 #ifdef HAVE_TCSETPGRP
825 SCM_DEFINE (scm_tcsetpgrp, "tcsetpgrp", 2, 0, 0,
826 (SCM port, SCM pgid),
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.")
832 #define FUNC_NAME s_scm_tcsetpgrp
833 {
834 int fd;
835
836 port = SCM_COERCE_OUTPORT (port);
837
838 SCM_VALIDATE_OPFPORT (1,port);
839 SCM_VALIDATE_INUM (2,pgid);
840 fd = SCM_FPORT_FDES (port);
841 if (tcsetpgrp (fd, SCM_INUM (pgid)) == -1)
842 SCM_SYSERROR;
843 return SCM_UNSPECIFIED;
844 }
845 #undef FUNC_NAME
846 #endif /* HAVE_TCSETPGRP */
847
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 */
851
852 static char **
853 scm_convert_exec_args (SCM args, int argn, const char *subr)
854 {
855 char **argv;
856 int argc;
857 int i;
858
859 argc = scm_ilength (args);
860 SCM_ASSERT (argc >= 0, args, argn, subr);
861 argv = (char **) scm_must_malloc ((argc + 1) * sizeof (char *), subr);
862 for (i = 0; !SCM_NULLP (args); args = SCM_CDR (args), ++i)
863 {
864 SCM arg = SCM_CAR (args);
865 size_t len;
866 char *dst;
867 char *src;
868
869 SCM_ASSERT (SCM_STRINGP (arg), args, argn, subr);
870 len = SCM_STRING_LENGTH (arg);
871 src = SCM_STRING_CHARS (arg);
872 dst = (char *) scm_must_malloc (len + 1, subr);
873 memcpy (dst, src, len);
874 dst[len] = 0;
875 argv[i] = dst;
876 }
877 argv[i] = 0;
878 return argv;
879 }
880
881 SCM_DEFINE (scm_execl, "execl", 1, 0, 1,
882 (SCM filename, SCM args),
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.")
892 #define FUNC_NAME s_scm_execl
893 {
894 char **execargv;
895 SCM_VALIDATE_STRING (1, filename);
896 execargv = scm_convert_exec_args (args, SCM_ARG2, FUNC_NAME);
897 execv (SCM_STRING_CHARS (filename), execargv);
898 SCM_SYSERROR;
899 /* not reached. */
900 return SCM_BOOL_F;
901 }
902 #undef FUNC_NAME
903
904 SCM_DEFINE (scm_execlp, "execlp", 1, 0, 1,
905 (SCM filename, SCM args),
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"
910 "This procedure is currently implemented using the @code{execvp} system\n"
911 "call, but we call it @code{execlp} because of its Scheme calling interface.")
912 #define FUNC_NAME s_scm_execlp
913 {
914 char **execargv;
915 SCM_VALIDATE_STRING (1, filename);
916 execargv = scm_convert_exec_args (args, SCM_ARG2, FUNC_NAME);
917 execvp (SCM_STRING_CHARS (filename), execargv);
918 SCM_SYSERROR;
919 /* not reached. */
920 return SCM_BOOL_F;
921 }
922 #undef FUNC_NAME
923
924 static char **
925 environ_list_to_c (SCM envlist, int arg, const char *proc)
926 {
927 int num_strings;
928 char **result;
929 int i;
930
931 num_strings = scm_ilength (envlist);
932 SCM_ASSERT (num_strings >= 0, envlist, arg, proc);
933 result = (char **) malloc ((num_strings + 1) * sizeof (char *));
934 if (result == NULL)
935 scm_memory_error (proc);
936 for (i = 0; !SCM_NULLP (envlist); ++i, envlist = SCM_CDR (envlist))
937 {
938 SCM str = SCM_CAR (envlist);
939 int len;
940 char *src;
941
942 SCM_ASSERT (SCM_STRINGP (str), envlist, arg, proc);
943 len = SCM_STRING_LENGTH (str);
944 src = SCM_STRING_CHARS (str);
945 result[i] = malloc (len + 1);
946 if (result[i] == NULL)
947 scm_memory_error (proc);
948 memcpy (result[i], src, len);
949 result[i][len] = 0;
950 }
951 result[i] = 0;
952 return result;
953 }
954
955 SCM_DEFINE (scm_execle, "execle", 2, 0, 1,
956 (SCM filename, SCM env, SCM args),
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.")
962 #define FUNC_NAME s_scm_execle
963 {
964 char **execargv;
965 char **exec_env;
966
967 SCM_VALIDATE_STRING (1, filename);
968
969 execargv = scm_convert_exec_args (args, SCM_ARG1, FUNC_NAME);
970 exec_env = environ_list_to_c (env, SCM_ARG2, FUNC_NAME);
971 execve (SCM_STRING_CHARS (filename), execargv, exec_env);
972 SCM_SYSERROR;
973 /* not reached. */
974 return SCM_BOOL_F;
975 }
976 #undef FUNC_NAME
977
978 #ifdef HAVE_FORK
979 SCM_DEFINE (scm_fork, "primitive-fork", 0, 0, 0,
980 (),
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.")
986 #define FUNC_NAME s_scm_fork
987 {
988 int pid;
989 pid = fork ();
990 if (pid == -1)
991 SCM_SYSERROR;
992 return SCM_MAKINUM (0L+pid);
993 }
994 #undef FUNC_NAME
995 #endif /* HAVE_FORK */
996
997 #ifdef HAVE_UNAME
998 SCM_DEFINE (scm_uname, "uname", 0, 0, 0,
999 (),
1000 "Return an object with some information about the computer\n"
1001 "system the program is running on.")
1002 #define FUNC_NAME s_scm_uname
1003 {
1004 struct utsname buf;
1005 SCM ans = scm_c_make_vector (5, SCM_UNSPECIFIED);
1006 SCM *ve = SCM_VELTS (ans);
1007 if (uname (&buf) < 0)
1008 SCM_SYSERROR;
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 /*
1015 a linux special?
1016 ve[5] = scm_makfrom0str (buf.domainname);
1017 */
1018 return ans;
1019 }
1020 #undef FUNC_NAME
1021 #endif /* HAVE_UNAME */
1022
1023 SCM_DEFINE (scm_environ, "environ", 0, 1, 0,
1024 (SCM env),
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.")
1032 #define FUNC_NAME s_scm_environ
1033 {
1034 if (SCM_UNBNDP (env))
1035 return scm_makfromstrs (-1, environ);
1036 else
1037 {
1038 char **new_environ;
1039
1040 new_environ = environ_list_to_c (env, SCM_ARG1, FUNC_NAME);
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++)
1050 free (*ep);
1051 free ((char *) environ);
1052 }
1053 first = 0;
1054 }
1055 environ = new_environ;
1056 return SCM_UNSPECIFIED;
1057 }
1058 }
1059 #undef FUNC_NAME
1060
1061 #ifdef L_tmpnam
1062
1063 SCM_DEFINE (scm_tmpnam, "tmpnam", 0, 0, 0,
1064 (),
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.")
1070 #define FUNC_NAME s_scm_tmpnam
1071 {
1072 char name[L_tmpnam];
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);
1079 return scm_makfrom0str (name);
1080 }
1081 #undef FUNC_NAME
1082
1083 #endif
1084
1085 SCM_DEFINE (scm_mkstemp, "mkstemp!", 1, 0, 0,
1086 (SCM tmpl),
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.")
1092 #define FUNC_NAME s_scm_mkstemp
1093 {
1094 char *c_tmpl;
1095 int rv;
1096
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
1105 SCM_DEFINE (scm_utime, "utime", 1, 2, 0,
1106 (SCM pathname, SCM actime, SCM modtime),
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"
1113 "(utime \"foo\" (- (current-time) 3600))\n"
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.")
1117 #define FUNC_NAME s_scm_utime
1118 {
1119 int rv;
1120 struct utimbuf utm_tmp;
1121
1122 SCM_VALIDATE_STRING (1, pathname);
1123 if (SCM_UNBNDP (actime))
1124 SCM_SYSCALL (time (&utm_tmp.actime));
1125 else
1126 utm_tmp.actime = SCM_NUM2ULONG (2, actime);
1127
1128 if (SCM_UNBNDP (modtime))
1129 SCM_SYSCALL (time (&utm_tmp.modtime));
1130 else
1131 utm_tmp.modtime = SCM_NUM2ULONG (3, modtime);
1132
1133 SCM_SYSCALL (rv = utime (SCM_STRING_CHARS (pathname), &utm_tmp));
1134 if (rv != 0)
1135 SCM_SYSERROR;
1136 return SCM_UNSPECIFIED;
1137 }
1138 #undef FUNC_NAME
1139
1140 SCM_DEFINE (scm_access, "access?", 2, 0, 0,
1141 (SCM path, SCM how),
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"
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")
1165 #define FUNC_NAME s_scm_access
1166 {
1167 int rv;
1168
1169 SCM_VALIDATE_STRING (1, path);
1170 SCM_VALIDATE_INUM (2, how);
1171 rv = access (SCM_STRING_CHARS (path), SCM_INUM (how));
1172 return SCM_NEGATE_BOOL(rv);
1173 }
1174 #undef FUNC_NAME
1175
1176 SCM_DEFINE (scm_getpid, "getpid", 0, 0, 0,
1177 (),
1178 "Return an integer representing the current process ID.")
1179 #define FUNC_NAME s_scm_getpid
1180 {
1181 return SCM_MAKINUM ((unsigned long) getpid ());
1182 }
1183 #undef FUNC_NAME
1184
1185 SCM_DEFINE (scm_putenv, "putenv", 1, 0, 0,
1186 (SCM str),
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.")
1196 #define FUNC_NAME s_scm_putenv
1197 {
1198 int rv;
1199 char *ptr;
1200
1201 SCM_VALIDATE_STRING (1, str);
1202 /* must make a new copy to be left in the environment, safe from gc. */
1203 ptr = malloc (SCM_STRING_LENGTH (str) + 1);
1204 if (ptr == NULL)
1205 SCM_MEMORY_ERROR;
1206 strncpy (ptr, SCM_STRING_CHARS (str), SCM_STRING_LENGTH (str));
1207 ptr[SCM_STRING_LENGTH (str)] = 0;
1208 rv = putenv (ptr);
1209 if (rv < 0)
1210 SCM_SYSERROR;
1211 return SCM_UNSPECIFIED;
1212 }
1213 #undef FUNC_NAME
1214
1215 #ifdef HAVE_SETLOCALE
1216 SCM_DEFINE (scm_setlocale, "setlocale", 1, 1, 0,
1217 (SCM category, SCM locale),
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.")
1227 #define FUNC_NAME s_scm_setlocale
1228 {
1229 char *clocale;
1230 char *rv;
1231
1232 SCM_VALIDATE_INUM (1,category);
1233 if (SCM_UNBNDP (locale))
1234 {
1235 clocale = NULL;
1236 }
1237 else
1238 {
1239 SCM_VALIDATE_STRING (2, locale);
1240 clocale = SCM_STRING_CHARS (locale);
1241 }
1242
1243 rv = setlocale (SCM_INUM (category), clocale);
1244 if (rv == NULL)
1245 SCM_SYSERROR;
1246 return scm_makfrom0str (rv);
1247 }
1248 #undef FUNC_NAME
1249 #endif /* HAVE_SETLOCALE */
1250
1251 #ifdef HAVE_MKNOD
1252 SCM_DEFINE (scm_mknod, "mknod", 4, 0, 0,
1253 (SCM path, SCM type, SCM perms, SCM dev),
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"
1263 "@lisp\n"
1264 "(mknod \"/dev/fd0\" 'block-special #o660 (+ (* 2 256) 2))\n"
1265 "@end lisp\n\n"
1266 "The return value is unspecified.")
1267 #define FUNC_NAME s_scm_mknod
1268 {
1269 int val;
1270 char *p;
1271 int ctype = 0;
1272
1273 SCM_VALIDATE_STRING (1, path);
1274 SCM_VALIDATE_SYMBOL (2,type);
1275 SCM_VALIDATE_INUM (3,perms);
1276 SCM_VALIDATE_INUM (4,dev);
1277
1278 p = SCM_SYMBOL_CHARS (type);
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;
1291 #ifdef S_IFSOCK
1292 else if (strcmp (p, "socket") == 0)
1293 ctype = S_IFSOCK;
1294 #endif
1295 else
1296 SCM_OUT_OF_RANGE (2,type);
1297
1298 SCM_SYSCALL (val = mknod (SCM_STRING_CHARS (path), ctype | SCM_INUM (perms),
1299 SCM_INUM (dev)));
1300 if (val != 0)
1301 SCM_SYSERROR;
1302 return SCM_UNSPECIFIED;
1303 }
1304 #undef FUNC_NAME
1305 #endif /* HAVE_MKNOD */
1306
1307 #ifdef HAVE_NICE
1308 SCM_DEFINE (scm_nice, "nice", 1, 0, 0,
1309 (SCM incr),
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.")
1313 #define FUNC_NAME s_scm_nice
1314 {
1315 SCM_VALIDATE_INUM (1,incr);
1316 if (nice(SCM_INUM(incr)) != 0)
1317 SCM_SYSERROR;
1318 return SCM_UNSPECIFIED;
1319 }
1320 #undef FUNC_NAME
1321 #endif /* HAVE_NICE */
1322
1323 #ifdef HAVE_SYNC
1324 SCM_DEFINE (scm_sync, "sync", 0, 0, 0,
1325 (),
1326 "Flush the operating system disk buffers.\n"
1327 "The return value is unspecified.")
1328 #define FUNC_NAME s_scm_sync
1329 {
1330 sync();
1331 return SCM_UNSPECIFIED;
1332 }
1333 #undef FUNC_NAME
1334 #endif /* HAVE_SYNC */
1335
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);
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);
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
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
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);
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);
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 {
1577 /* 256 is for Solaris, under Linux ENAMETOOLONG is returned if not
1578 large enough. */
1579 int len = 256, res;
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
1602 void
1603 scm_init_posix ()
1604 {
1605 scm_add_feature ("posix");
1606 #ifdef HAVE_GETEUID
1607 scm_add_feature ("EIDs");
1608 #endif
1609 #ifdef WAIT_ANY
1610 scm_c_define ("WAIT_ANY", SCM_MAKINUM (WAIT_ANY));
1611 #endif
1612 #ifdef WAIT_MYPGRP
1613 scm_c_define ("WAIT_MYPGRP", SCM_MAKINUM (WAIT_MYPGRP));
1614 #endif
1615 #ifdef WNOHANG
1616 scm_c_define ("WNOHANG", SCM_MAKINUM (WNOHANG));
1617 #endif
1618 #ifdef WUNTRACED
1619 scm_c_define ("WUNTRACED", SCM_MAKINUM (WUNTRACED));
1620 #endif
1621
1622 /* access() symbols. */
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));
1627
1628 #ifdef LC_COLLATE
1629 scm_c_define ("LC_COLLATE", SCM_MAKINUM (LC_COLLATE));
1630 #endif
1631 #ifdef LC_CTYPE
1632 scm_c_define ("LC_CTYPE", SCM_MAKINUM (LC_CTYPE));
1633 #endif
1634 #ifdef LC_MONETARY
1635 scm_c_define ("LC_MONETARY", SCM_MAKINUM (LC_MONETARY));
1636 #endif
1637 #ifdef LC_NUMERIC
1638 scm_c_define ("LC_NUMERIC", SCM_MAKINUM (LC_NUMERIC));
1639 #endif
1640 #ifdef LC_TIME
1641 scm_c_define ("LC_TIME", SCM_MAKINUM (LC_TIME));
1642 #endif
1643 #ifdef LC_MESSAGES
1644 scm_c_define ("LC_MESSAGES", SCM_MAKINUM (LC_MESSAGES));
1645 #endif
1646 #ifdef LC_ALL
1647 scm_c_define ("LC_ALL", SCM_MAKINUM (LC_ALL));
1648 #endif
1649 #ifdef PIPE_BUF
1650 scm_c_define ("PIPE_BUF", scm_long2num (PIPE_BUF));
1651 #endif
1652
1653 #ifdef PRIO_PROCESS
1654 scm_c_define ("PRIO_PROCESS", SCM_MAKINUM (PRIO_PROCESS));
1655 #endif
1656 #ifdef PRIO_PGRP
1657 scm_c_define ("PRIO_PGRP", SCM_MAKINUM (PRIO_PGRP));
1658 #endif
1659 #ifdef PRIO_USER
1660 scm_c_define ("PRIO_USER", SCM_MAKINUM (PRIO_USER));
1661 #endif
1662
1663 #ifdef LOCK_SH
1664 scm_c_define ("LOCK_SH", SCM_MAKINUM (LOCK_SH));
1665 #endif
1666 #ifdef LOCK_EX
1667 scm_c_define ("LOCK_EX", SCM_MAKINUM (LOCK_EX));
1668 #endif
1669 #ifdef LOCK_UN
1670 scm_c_define ("LOCK_UN", SCM_MAKINUM (LOCK_UN));
1671 #endif
1672 #ifdef LOCK_NB
1673 scm_c_define ("LOCK_NB", SCM_MAKINUM (LOCK_NB));
1674 #endif
1675
1676 #include "libguile/cpp_sig_symbols.c"
1677 #ifndef SCM_MAGIC_SNARFER
1678 #include "libguile/posix.x"
1679 #endif
1680 }
1681
1682 /*
1683 Local Variables:
1684 c-file-style: "gnu"
1685 End:
1686 */