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