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