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