Fixed weak alist vectors by having them use weak hash tables instead.
[bpt/guile.git] / libguile / posix.c
1 /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2003, 2004, 2005, 2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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/dynwind.h"
32 #include "libguile/fports.h"
33 #include "libguile/scmsigs.h"
34 #include "libguile/feature.h"
35 #include "libguile/strings.h"
36 #include "libguile/srfi-13.h"
37 #include "libguile/srfi-14.h"
38 #include "libguile/vectors.h"
39 #include "libguile/lang.h"
40
41 #include "libguile/validate.h"
42 #include "libguile/posix.h"
43 #include "libguile/i18n.h"
44 #include "libguile/threads.h"
45 \f
46
47 #ifdef HAVE_STRING_H
48 #include <string.h>
49 #endif
50 #ifdef TIME_WITH_SYS_TIME
51 # include <sys/time.h>
52 # include <time.h>
53 #else
54 # if HAVE_SYS_TIME_H
55 # include <sys/time.h>
56 # else
57 # include <time.h>
58 # endif
59 #endif
60
61 #ifdef HAVE_UNISTD_H
62 #include <unistd.h>
63 #else
64 #ifndef ttyname
65 extern char *ttyname();
66 #endif
67 #endif
68
69 #ifdef LIBC_H_WITH_UNISTD_H
70 #include <libc.h>
71 #endif
72
73 #include <sys/types.h>
74 #include <sys/stat.h>
75 #include <fcntl.h>
76
77 #ifdef HAVE_PWD_H
78 #include <pwd.h>
79 #endif
80 #ifdef HAVE_IO_H
81 #include <io.h>
82 #endif
83 #ifdef HAVE_WINSOCK2_H
84 #include <winsock2.h>
85 #endif
86
87 #ifdef __MINGW32__
88 /* Some defines for Windows here. */
89 # include <process.h>
90 # define pipe(fd) _pipe (fd, 256, O_BINARY)
91 #endif /* __MINGW32__ */
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 #ifdef HAVE_GRP_H
108 #include <grp.h>
109 #endif
110 #ifdef HAVE_SYS_UTSNAME_H
111 #include <sys/utsname.h>
112 #endif
113
114 #ifdef HAVE_SETLOCALE
115 #include <locale.h>
116 #endif
117
118 #if HAVE_CRYPT_H
119 # include <crypt.h>
120 #endif
121
122 #ifdef HAVE_NETDB_H
123 #include <netdb.h> /* for MAXHOSTNAMELEN on Solaris */
124 #endif
125
126 #ifdef HAVE_SYS_PARAM_H
127 #include <sys/param.h> /* for MAXHOSTNAMELEN */
128 #endif
129
130 #if HAVE_SYS_RESOURCE_H
131 # include <sys/resource.h>
132 #endif
133
134 #if HAVE_SYS_FILE_H
135 # include <sys/file.h>
136 #endif
137
138 #if HAVE_CRT_EXTERNS_H
139 #include <crt_externs.h> /* for Darwin _NSGetEnviron */
140 #endif
141
142 /* Some Unix systems don't define these. CPP hair is dangerous, but
143 this seems safe enough... */
144 #ifndef R_OK
145 #define R_OK 4
146 #endif
147
148 #ifndef W_OK
149 #define W_OK 2
150 #endif
151
152 #ifndef X_OK
153 #define X_OK 1
154 #endif
155
156 #ifndef F_OK
157 #define F_OK 0
158 #endif
159
160 /* On NextStep, <utime.h> doesn't define struct utime, unless we
161 #define _POSIX_SOURCE before #including it. I think this is less
162 of a kludge than defining struct utimbuf ourselves. */
163 #ifdef UTIMBUF_NEEDS_POSIX
164 #define _POSIX_SOURCE
165 #endif
166
167 #ifdef HAVE_SYS_UTIME_H
168 #include <sys/utime.h>
169 #endif
170
171 #ifdef HAVE_UTIME_H
172 #include <utime.h>
173 #endif
174
175 /* Please don't add any more #includes or #defines here. The hack
176 above means that _POSIX_SOURCE may be #defined, which will
177 encourage header files to do strange things.
178
179 FIXME: Maybe should undef _POSIX_SOURCE after it's done its job.
180
181 FIXME: Probably should do all the includes first, then all the fallback
182 declarations and defines, in case things are not in the header we
183 imagine. */
184
185
186
187
188 /* On Apple Darwin in a shared library there's no "environ" to access
189 directly, instead the address of that variable must be obtained with
190 _NSGetEnviron(). */
191 #if HAVE__NSGETENVIRON && defined (PIC)
192 #define environ (*_NSGetEnviron())
193 #endif
194
195 \f
196
197 /* Two often used patterns
198 */
199
200 #define WITH_STRING(str,cstr,code) \
201 do { \
202 char *cstr = scm_to_locale_string (str); \
203 code; \
204 free (cstr); \
205 } while (0)
206
207 #define STRING_SYSCALL(str,cstr,code) \
208 do { \
209 int eno; \
210 char *cstr = scm_to_locale_string (str); \
211 SCM_SYSCALL (code); \
212 eno = errno; free (cstr); errno = eno; \
213 } while (0)
214
215
216 \f
217 SCM_SYMBOL (sym_read_pipe, "read pipe");
218 SCM_SYMBOL (sym_write_pipe, "write pipe");
219
220 SCM_DEFINE (scm_pipe, "pipe", 0, 0, 0,
221 (),
222 "Return a newly created pipe: a pair of ports which are linked\n"
223 "together on the local machine. The @emph{car} is the input\n"
224 "port and the @emph{cdr} is the output port. Data written (and\n"
225 "flushed) to the output port can be read from the input port.\n"
226 "Pipes are commonly used for communication with a newly forked\n"
227 "child process. The need to flush the output port can be\n"
228 "avoided by making it unbuffered using @code{setvbuf}.\n"
229 "\n"
230 "Writes occur atomically provided the size of the data in bytes\n"
231 "is not greater than the value of @code{PIPE_BUF}. Note that\n"
232 "the output port is likely to block if too much data (typically\n"
233 "equal to @code{PIPE_BUF}) has been written but not yet read\n"
234 "from the input port.")
235 #define FUNC_NAME s_scm_pipe
236 {
237 int fd[2], rv;
238 SCM p_rd, p_wt;
239
240 rv = pipe (fd);
241 if (rv)
242 SCM_SYSERROR;
243
244 p_rd = scm_fdes_to_port (fd[0], "r", sym_read_pipe);
245 p_wt = scm_fdes_to_port (fd[1], "w", sym_write_pipe);
246 return scm_cons (p_rd, p_wt);
247 }
248 #undef FUNC_NAME
249
250
251 #ifdef HAVE_GETGROUPS
252 SCM_DEFINE (scm_getgroups, "getgroups", 0, 0, 0,
253 (),
254 "Return a vector of integers representing the current\n"
255 "supplementary group IDs.")
256 #define FUNC_NAME s_scm_getgroups
257 {
258 SCM result;
259 int ngroups;
260 size_t size;
261 GETGROUPS_T *groups;
262
263 ngroups = getgroups (0, NULL);
264 if (ngroups <= 0)
265 SCM_SYSERROR;
266
267 size = ngroups * sizeof (GETGROUPS_T);
268 groups = scm_malloc (size);
269 getgroups (ngroups, groups);
270
271 result = scm_c_make_vector (ngroups, SCM_BOOL_F);
272 while (--ngroups >= 0)
273 SCM_SIMPLE_VECTOR_SET (result, ngroups, scm_from_ulong (groups[ngroups]));
274
275 free (groups);
276 return result;
277 }
278 #undef FUNC_NAME
279 #endif
280
281 #ifdef HAVE_SETGROUPS
282 SCM_DEFINE (scm_setgroups, "setgroups", 1, 0, 0,
283 (SCM group_vec),
284 "Set the current set of supplementary group IDs to the integers\n"
285 "in the given vector @var{vec}. The return value is\n"
286 "unspecified.\n"
287 "\n"
288 "Generally only the superuser can set the process group IDs.")
289 #define FUNC_NAME s_scm_setgroups
290 {
291 size_t ngroups;
292 size_t size;
293 size_t i;
294 int result;
295 int save_errno;
296 GETGROUPS_T *groups;
297
298 SCM_VALIDATE_VECTOR (SCM_ARG1, group_vec);
299
300 ngroups = SCM_SIMPLE_VECTOR_LENGTH (group_vec);
301
302 /* validate before allocating, so we don't have to worry about leaks */
303 for (i = 0; i < ngroups; i++)
304 {
305 unsigned long ulong_gid;
306 GETGROUPS_T gid;
307 SCM_VALIDATE_ULONG_COPY (1, SCM_SIMPLE_VECTOR_REF (group_vec, i),
308 ulong_gid);
309 gid = ulong_gid;
310 if (gid != ulong_gid)
311 SCM_OUT_OF_RANGE (1, SCM_SIMPLE_VECTOR_REF (group_vec, i));
312 }
313
314 size = ngroups * sizeof (GETGROUPS_T);
315 if (size / sizeof (GETGROUPS_T) != ngroups)
316 SCM_OUT_OF_RANGE (SCM_ARG1, scm_from_int (ngroups));
317 groups = scm_malloc (size);
318 for(i = 0; i < ngroups; i++)
319 groups [i] = SCM_NUM2ULONG (1, SCM_SIMPLE_VECTOR_REF (group_vec, i));
320
321 result = setgroups (ngroups, groups);
322 save_errno = errno; /* don't let free() touch errno */
323 free (groups);
324 errno = save_errno;
325 if (result < 0)
326 SCM_SYSERROR;
327 return SCM_UNSPECIFIED;
328 }
329 #undef FUNC_NAME
330 #endif
331
332 #ifdef HAVE_GETPWENT
333 SCM_DEFINE (scm_getpwuid, "getpw", 0, 1, 0,
334 (SCM user),
335 "Look up an entry in the user database. @var{obj} can be an integer,\n"
336 "a string, or omitted, giving the behaviour of getpwuid, getpwnam\n"
337 "or getpwent respectively.")
338 #define FUNC_NAME s_scm_getpwuid
339 {
340 struct passwd *entry;
341
342 SCM result = scm_c_make_vector (7, SCM_UNSPECIFIED);
343 if (SCM_UNBNDP (user) || scm_is_false (user))
344 {
345 SCM_SYSCALL (entry = getpwent ());
346 if (! entry)
347 {
348 return SCM_BOOL_F;
349 }
350 }
351 else if (scm_is_integer (user))
352 {
353 entry = getpwuid (scm_to_int (user));
354 }
355 else
356 {
357 WITH_STRING (user, c_user,
358 entry = getpwnam (c_user));
359 }
360 if (!entry)
361 SCM_MISC_ERROR ("entry not found", SCM_EOL);
362
363 SCM_SIMPLE_VECTOR_SET(result, 0, scm_from_locale_string (entry->pw_name));
364 SCM_SIMPLE_VECTOR_SET(result, 1, scm_from_locale_string (entry->pw_passwd));
365 SCM_SIMPLE_VECTOR_SET(result, 2, scm_from_ulong (entry->pw_uid));
366 SCM_SIMPLE_VECTOR_SET(result, 3, scm_from_ulong (entry->pw_gid));
367 SCM_SIMPLE_VECTOR_SET(result, 4, scm_from_locale_string (entry->pw_gecos));
368 if (!entry->pw_dir)
369 SCM_SIMPLE_VECTOR_SET(result, 5, scm_from_locale_string (""));
370 else
371 SCM_SIMPLE_VECTOR_SET(result, 5, scm_from_locale_string (entry->pw_dir));
372 if (!entry->pw_shell)
373 SCM_SIMPLE_VECTOR_SET(result, 6, scm_from_locale_string (""));
374 else
375 SCM_SIMPLE_VECTOR_SET(result, 6, scm_from_locale_string (entry->pw_shell));
376 return result;
377 }
378 #undef FUNC_NAME
379 #endif /* HAVE_GETPWENT */
380
381
382 #ifdef HAVE_SETPWENT
383 SCM_DEFINE (scm_setpwent, "setpw", 0, 1, 0,
384 (SCM arg),
385 "If called with a true argument, initialize or reset the password data\n"
386 "stream. Otherwise, close the stream. The @code{setpwent} and\n"
387 "@code{endpwent} procedures are implemented on top of this.")
388 #define FUNC_NAME s_scm_setpwent
389 {
390 if (SCM_UNBNDP (arg) || scm_is_false (arg))
391 endpwent ();
392 else
393 setpwent ();
394 return SCM_UNSPECIFIED;
395 }
396 #undef FUNC_NAME
397 #endif
398
399
400 #ifdef HAVE_GETGRENT
401 /* Combines getgrgid and getgrnam. */
402 SCM_DEFINE (scm_getgrgid, "getgr", 0, 1, 0,
403 (SCM name),
404 "Look up an entry in the group database. @var{obj} can be an integer,\n"
405 "a string, or omitted, giving the behaviour of getgrgid, getgrnam\n"
406 "or getgrent respectively.")
407 #define FUNC_NAME s_scm_getgrgid
408 {
409 struct group *entry;
410 SCM result = scm_c_make_vector (4, SCM_UNSPECIFIED);
411
412 if (SCM_UNBNDP (name) || scm_is_false (name))
413 {
414 SCM_SYSCALL (entry = getgrent ());
415 if (! entry)
416 {
417 return SCM_BOOL_F;
418 }
419 }
420 else if (scm_is_integer (name))
421 SCM_SYSCALL (entry = getgrgid (scm_to_int (name)));
422 else
423 STRING_SYSCALL (name, c_name,
424 entry = getgrnam (c_name));
425 if (!entry)
426 SCM_SYSERROR;
427
428 SCM_SIMPLE_VECTOR_SET(result, 0, scm_from_locale_string (entry->gr_name));
429 SCM_SIMPLE_VECTOR_SET(result, 1, scm_from_locale_string (entry->gr_passwd));
430 SCM_SIMPLE_VECTOR_SET(result, 2, scm_from_ulong (entry->gr_gid));
431 SCM_SIMPLE_VECTOR_SET(result, 3, scm_makfromstrs (-1, entry->gr_mem));
432 return result;
433 }
434 #undef FUNC_NAME
435
436
437
438 SCM_DEFINE (scm_setgrent, "setgr", 0, 1, 0,
439 (SCM arg),
440 "If called with a true argument, initialize or reset the group data\n"
441 "stream. Otherwise, close the stream. The @code{setgrent} and\n"
442 "@code{endgrent} procedures are implemented on top of this.")
443 #define FUNC_NAME s_scm_setgrent
444 {
445 if (SCM_UNBNDP (arg) || scm_is_false (arg))
446 endgrent ();
447 else
448 setgrent ();
449 return SCM_UNSPECIFIED;
450 }
451 #undef FUNC_NAME
452 #endif /* HAVE_GETGRENT */
453
454
455 SCM_DEFINE (scm_kill, "kill", 2, 0, 0,
456 (SCM pid, SCM sig),
457 "Sends a signal to the specified process or group of processes.\n\n"
458 "@var{pid} specifies the processes to which the signal is sent:\n\n"
459 "@table @r\n"
460 "@item @var{pid} greater than 0\n"
461 "The process whose identifier is @var{pid}.\n"
462 "@item @var{pid} equal to 0\n"
463 "All processes in the current process group.\n"
464 "@item @var{pid} less than -1\n"
465 "The process group whose identifier is -@var{pid}\n"
466 "@item @var{pid} equal to -1\n"
467 "If the process is privileged, all processes except for some special\n"
468 "system processes. Otherwise, all processes with the current effective\n"
469 "user ID.\n"
470 "@end table\n\n"
471 "@var{sig} should be specified using a variable corresponding to\n"
472 "the Unix symbolic name, e.g.,\n\n"
473 "@defvar SIGHUP\n"
474 "Hang-up signal.\n"
475 "@end defvar\n\n"
476 "@defvar SIGINT\n"
477 "Interrupt signal.\n"
478 "@end defvar")
479 #define FUNC_NAME s_scm_kill
480 {
481 /* Signal values are interned in scm_init_posix(). */
482 #ifdef HAVE_KILL
483 if (kill (scm_to_int (pid), scm_to_int (sig)) != 0)
484 #else
485 if (scm_to_int (pid) == getpid ())
486 if (raise (scm_to_int (sig)) != 0)
487 #endif
488 SCM_SYSERROR;
489 return SCM_UNSPECIFIED;
490 }
491 #undef FUNC_NAME
492
493 #ifdef HAVE_WAITPID
494 SCM_DEFINE (scm_waitpid, "waitpid", 1, 1, 0,
495 (SCM pid, SCM options),
496 "This procedure collects status information from a child process which\n"
497 "has terminated or (optionally) stopped. Normally it will\n"
498 "suspend the calling process until this can be done. If more than one\n"
499 "child process is eligible then one will be chosen by the operating system.\n\n"
500 "The value of @var{pid} determines the behaviour:\n\n"
501 "@table @r\n"
502 "@item @var{pid} greater than 0\n"
503 "Request status information from the specified child process.\n"
504 "@item @var{pid} equal to -1 or WAIT_ANY\n"
505 "Request status information for any child process.\n"
506 "@item @var{pid} equal to 0 or WAIT_MYPGRP\n"
507 "Request status information for any child process in the current process\n"
508 "group.\n"
509 "@item @var{pid} less than -1\n"
510 "Request status information for any child process whose process group ID\n"
511 "is -@var{PID}.\n"
512 "@end table\n\n"
513 "The @var{options} argument, if supplied, should be the bitwise OR of the\n"
514 "values of zero or more of the following variables:\n\n"
515 "@defvar WNOHANG\n"
516 "Return immediately even if there are no child processes to be collected.\n"
517 "@end defvar\n\n"
518 "@defvar WUNTRACED\n"
519 "Report status information for stopped processes as well as terminated\n"
520 "processes.\n"
521 "@end defvar\n\n"
522 "The return value is a pair containing:\n\n"
523 "@enumerate\n"
524 "@item\n"
525 "The process ID of the child process, or 0 if @code{WNOHANG} was\n"
526 "specified and no process was collected.\n"
527 "@item\n"
528 "The integer status value.\n"
529 "@end enumerate")
530 #define FUNC_NAME s_scm_waitpid
531 {
532 int i;
533 int status;
534 int ioptions;
535 if (SCM_UNBNDP (options))
536 ioptions = 0;
537 else
538 {
539 /* Flags are interned in scm_init_posix. */
540 ioptions = scm_to_int (options);
541 }
542 SCM_SYSCALL (i = waitpid (scm_to_int (pid), &status, ioptions));
543 if (i == -1)
544 SCM_SYSERROR;
545 return scm_cons (scm_from_int (i), scm_from_int (status));
546 }
547 #undef FUNC_NAME
548 #endif /* HAVE_WAITPID */
549
550 #ifndef __MINGW32__
551 SCM_DEFINE (scm_status_exit_val, "status:exit-val", 1, 0, 0,
552 (SCM status),
553 "Return the exit status value, as would be set if a process\n"
554 "ended normally through a call to @code{exit} or @code{_exit},\n"
555 "if any, otherwise @code{#f}.")
556 #define FUNC_NAME s_scm_status_exit_val
557 {
558 int lstatus;
559
560 /* On Ultrix, the WIF... macros assume their argument is an lvalue;
561 go figure. */
562 lstatus = scm_to_int (status);
563 if (WIFEXITED (lstatus))
564 return (scm_from_int (WEXITSTATUS (lstatus)));
565 else
566 return SCM_BOOL_F;
567 }
568 #undef FUNC_NAME
569
570 SCM_DEFINE (scm_status_term_sig, "status:term-sig", 1, 0, 0,
571 (SCM status),
572 "Return the signal number which terminated the process, if any,\n"
573 "otherwise @code{#f}.")
574 #define FUNC_NAME s_scm_status_term_sig
575 {
576 int lstatus;
577
578 lstatus = scm_to_int (status);
579 if (WIFSIGNALED (lstatus))
580 return scm_from_int (WTERMSIG (lstatus));
581 else
582 return SCM_BOOL_F;
583 }
584 #undef FUNC_NAME
585
586 SCM_DEFINE (scm_status_stop_sig, "status:stop-sig", 1, 0, 0,
587 (SCM status),
588 "Return the signal number which stopped the process, if any,\n"
589 "otherwise @code{#f}.")
590 #define FUNC_NAME s_scm_status_stop_sig
591 {
592 int lstatus;
593
594 lstatus = scm_to_int (status);
595 if (WIFSTOPPED (lstatus))
596 return scm_from_int (WSTOPSIG (lstatus));
597 else
598 return SCM_BOOL_F;
599 }
600 #undef FUNC_NAME
601 #endif /* __MINGW32__ */
602
603 #ifdef HAVE_GETPPID
604 SCM_DEFINE (scm_getppid, "getppid", 0, 0, 0,
605 (),
606 "Return an integer representing the process ID of the parent\n"
607 "process.")
608 #define FUNC_NAME s_scm_getppid
609 {
610 return scm_from_int (getppid ());
611 }
612 #undef FUNC_NAME
613 #endif /* HAVE_GETPPID */
614
615
616 #ifndef __MINGW32__
617 SCM_DEFINE (scm_getuid, "getuid", 0, 0, 0,
618 (),
619 "Return an integer representing the current real user ID.")
620 #define FUNC_NAME s_scm_getuid
621 {
622 return scm_from_int (getuid ());
623 }
624 #undef FUNC_NAME
625
626
627
628 SCM_DEFINE (scm_getgid, "getgid", 0, 0, 0,
629 (),
630 "Return an integer representing the current real group ID.")
631 #define FUNC_NAME s_scm_getgid
632 {
633 return scm_from_int (getgid ());
634 }
635 #undef FUNC_NAME
636
637
638
639 SCM_DEFINE (scm_geteuid, "geteuid", 0, 0, 0,
640 (),
641 "Return an integer representing the current effective user ID.\n"
642 "If the system does not support effective IDs, then the real ID\n"
643 "is returned. @code{(provided? 'EIDs)} reports whether the\n"
644 "system supports effective IDs.")
645 #define FUNC_NAME s_scm_geteuid
646 {
647 #ifdef HAVE_GETEUID
648 return scm_from_int (geteuid ());
649 #else
650 return scm_from_int (getuid ());
651 #endif
652 }
653 #undef FUNC_NAME
654
655
656 SCM_DEFINE (scm_getegid, "getegid", 0, 0, 0,
657 (),
658 "Return an integer representing the current effective group ID.\n"
659 "If the system does not support effective IDs, then the real ID\n"
660 "is returned. @code{(provided? 'EIDs)} reports whether the\n"
661 "system supports effective IDs.")
662 #define FUNC_NAME s_scm_getegid
663 {
664 #ifdef HAVE_GETEUID
665 return scm_from_int (getegid ());
666 #else
667 return scm_from_int (getgid ());
668 #endif
669 }
670 #undef FUNC_NAME
671
672
673 SCM_DEFINE (scm_setuid, "setuid", 1, 0, 0,
674 (SCM id),
675 "Sets both the real and effective user IDs to the integer @var{id}, provided\n"
676 "the process has appropriate privileges.\n"
677 "The return value is unspecified.")
678 #define FUNC_NAME s_scm_setuid
679 {
680 if (setuid (scm_to_int (id)) != 0)
681 SCM_SYSERROR;
682 return SCM_UNSPECIFIED;
683 }
684 #undef FUNC_NAME
685
686 SCM_DEFINE (scm_setgid, "setgid", 1, 0, 0,
687 (SCM id),
688 "Sets both the real and effective group IDs to the integer @var{id}, provided\n"
689 "the process has appropriate privileges.\n"
690 "The return value is unspecified.")
691 #define FUNC_NAME s_scm_setgid
692 {
693 if (setgid (scm_to_int (id)) != 0)
694 SCM_SYSERROR;
695 return SCM_UNSPECIFIED;
696 }
697 #undef FUNC_NAME
698
699 SCM_DEFINE (scm_seteuid, "seteuid", 1, 0, 0,
700 (SCM id),
701 "Sets the effective user ID to the integer @var{id}, provided the process\n"
702 "has appropriate privileges. If effective IDs are not supported, the\n"
703 "real ID is set instead -- @code{(provided? 'EIDs)} reports whether the\n"
704 "system supports effective IDs.\n"
705 "The return value is unspecified.")
706 #define FUNC_NAME s_scm_seteuid
707 {
708 int rv;
709
710 #ifdef HAVE_SETEUID
711 rv = seteuid (scm_to_int (id));
712 #else
713 rv = setuid (scm_to_int (id));
714 #endif
715 if (rv != 0)
716 SCM_SYSERROR;
717 return SCM_UNSPECIFIED;
718 }
719 #undef FUNC_NAME
720 #endif /* __MINGW32__ */
721
722
723 #ifdef HAVE_SETEGID
724 SCM_DEFINE (scm_setegid, "setegid", 1, 0, 0,
725 (SCM id),
726 "Sets the effective group ID to the integer @var{id}, provided the process\n"
727 "has appropriate privileges. If effective IDs are not supported, the\n"
728 "real ID is set instead -- @code{(provided? 'EIDs)} reports whether the\n"
729 "system supports effective IDs.\n"
730 "The return value is unspecified.")
731 #define FUNC_NAME s_scm_setegid
732 {
733 int rv;
734
735 #ifdef HAVE_SETEUID
736 rv = setegid (scm_to_int (id));
737 #else
738 rv = setgid (scm_to_int (id));
739 #endif
740 if (rv != 0)
741 SCM_SYSERROR;
742 return SCM_UNSPECIFIED;
743
744 }
745 #undef FUNC_NAME
746 #endif
747
748
749 #ifdef HAVE_GETPGRP
750 SCM_DEFINE (scm_getpgrp, "getpgrp", 0, 0, 0,
751 (),
752 "Return an integer representing the current process group ID.\n"
753 "This is the POSIX definition, not BSD.")
754 #define FUNC_NAME s_scm_getpgrp
755 {
756 int (*fn)();
757 fn = (int (*) ()) getpgrp;
758 return scm_from_int (fn (0));
759 }
760 #undef FUNC_NAME
761 #endif /* HAVE_GETPGRP */
762
763
764 #ifdef HAVE_SETPGID
765 SCM_DEFINE (scm_setpgid, "setpgid", 2, 0, 0,
766 (SCM pid, SCM pgid),
767 "Move the process @var{pid} into the process group @var{pgid}. @var{pid} or\n"
768 "@var{pgid} must be integers: they can be zero to indicate the ID of the\n"
769 "current process.\n"
770 "Fails on systems that do not support job control.\n"
771 "The return value is unspecified.")
772 #define FUNC_NAME s_scm_setpgid
773 {
774 /* FIXME(?): may be known as setpgrp. */
775 if (setpgid (scm_to_int (pid), scm_to_int (pgid)) != 0)
776 SCM_SYSERROR;
777 return SCM_UNSPECIFIED;
778 }
779 #undef FUNC_NAME
780 #endif /* HAVE_SETPGID */
781
782 #ifdef HAVE_SETSID
783 SCM_DEFINE (scm_setsid, "setsid", 0, 0, 0,
784 (),
785 "Creates a new session. The current process becomes the session leader\n"
786 "and is put in a new process group. The process will be detached\n"
787 "from its controlling terminal if it has one.\n"
788 "The return value is an integer representing the new process group ID.")
789 #define FUNC_NAME s_scm_setsid
790 {
791 pid_t sid = setsid ();
792 if (sid == -1)
793 SCM_SYSERROR;
794 return SCM_UNSPECIFIED;
795 }
796 #undef FUNC_NAME
797 #endif /* HAVE_SETSID */
798
799
800 /* ttyname returns its result in a single static buffer, hence
801 scm_i_misc_mutex for thread safety. In glibc 2.3.2 two threads
802 continuously calling ttyname will otherwise get an overwrite quite
803 easily.
804
805 ttyname_r (when available) could be used instead of scm_i_misc_mutex, but
806 there's probably little to be gained in either speed or parallelism. */
807
808 #ifdef HAVE_TTYNAME
809 SCM_DEFINE (scm_ttyname, "ttyname", 1, 0, 0,
810 (SCM port),
811 "Return a string with the name of the serial terminal device\n"
812 "underlying @var{port}.")
813 #define FUNC_NAME s_scm_ttyname
814 {
815 char *result;
816 int fd, err;
817 SCM ret;
818
819 port = SCM_COERCE_OUTPORT (port);
820 SCM_VALIDATE_OPPORT (1, port);
821 if (!SCM_FPORTP (port))
822 return SCM_BOOL_F;
823 fd = SCM_FPORT_FDES (port);
824
825 scm_i_scm_pthread_mutex_lock (&scm_i_misc_mutex);
826 SCM_SYSCALL (result = ttyname (fd));
827 err = errno;
828 ret = scm_from_locale_string (result);
829 scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
830
831 if (!result)
832 {
833 errno = err;
834 SCM_SYSERROR;
835 }
836 return ret;
837 }
838 #undef FUNC_NAME
839 #endif /* HAVE_TTYNAME */
840
841
842 /* For thread safety "buf" is used instead of NULL for the ctermid static
843 buffer. Actually it's unlikely the controlling terminal will change
844 during program execution, and indeed on glibc (2.3.2) it's always just
845 "/dev/tty", but L_ctermid on the stack is easy and fast and guarantees
846 safety everywhere. */
847 #ifdef HAVE_CTERMID
848 SCM_DEFINE (scm_ctermid, "ctermid", 0, 0, 0,
849 (),
850 "Return a string containing the file name of the controlling\n"
851 "terminal for the current process.")
852 #define FUNC_NAME s_scm_ctermid
853 {
854 char buf[L_ctermid];
855 char *result = ctermid (buf);
856 if (*result == '\0')
857 SCM_SYSERROR;
858 return scm_from_locale_string (result);
859 }
860 #undef FUNC_NAME
861 #endif /* HAVE_CTERMID */
862
863 #ifdef HAVE_TCGETPGRP
864 SCM_DEFINE (scm_tcgetpgrp, "tcgetpgrp", 1, 0, 0,
865 (SCM port),
866 "Return the process group ID of the foreground process group\n"
867 "associated with the terminal open on the file descriptor\n"
868 "underlying @var{port}.\n"
869 "\n"
870 "If there is no foreground process group, the return value is a\n"
871 "number greater than 1 that does not match the process group ID\n"
872 "of any existing process group. This can happen if all of the\n"
873 "processes in the job that was formerly the foreground job have\n"
874 "terminated, and no other job has yet been moved into the\n"
875 "foreground.")
876 #define FUNC_NAME s_scm_tcgetpgrp
877 {
878 int fd;
879 pid_t pgid;
880
881 port = SCM_COERCE_OUTPORT (port);
882
883 SCM_VALIDATE_OPFPORT (1, port);
884 fd = SCM_FPORT_FDES (port);
885 if ((pgid = tcgetpgrp (fd)) == -1)
886 SCM_SYSERROR;
887 return scm_from_int (pgid);
888 }
889 #undef FUNC_NAME
890 #endif /* HAVE_TCGETPGRP */
891
892 #ifdef HAVE_TCSETPGRP
893 SCM_DEFINE (scm_tcsetpgrp, "tcsetpgrp", 2, 0, 0,
894 (SCM port, SCM pgid),
895 "Set the foreground process group ID for the terminal used by the file\n"
896 "descriptor underlying @var{port} to the integer @var{pgid}.\n"
897 "The calling process\n"
898 "must be a member of the same session as @var{pgid} and must have the same\n"
899 "controlling terminal. The return value is unspecified.")
900 #define FUNC_NAME s_scm_tcsetpgrp
901 {
902 int fd;
903
904 port = SCM_COERCE_OUTPORT (port);
905
906 SCM_VALIDATE_OPFPORT (1, port);
907 fd = SCM_FPORT_FDES (port);
908 if (tcsetpgrp (fd, scm_to_int (pgid)) == -1)
909 SCM_SYSERROR;
910 return SCM_UNSPECIFIED;
911 }
912 #undef FUNC_NAME
913 #endif /* HAVE_TCSETPGRP */
914
915 static void
916 free_string_pointers (void *data)
917 {
918 scm_i_free_string_pointers ((char **)data);
919 }
920
921 SCM_DEFINE (scm_execl, "execl", 1, 0, 1,
922 (SCM filename, SCM args),
923 "Executes the file named by @var{path} as a new process image.\n"
924 "The remaining arguments are supplied to the process; from a C program\n"
925 "they are accessible as the @code{argv} argument to @code{main}.\n"
926 "Conventionally the first @var{arg} is the same as @var{path}.\n"
927 "All arguments must be strings.\n\n"
928 "If @var{arg} is missing, @var{path} is executed with a null\n"
929 "argument list, which may have system-dependent side-effects.\n\n"
930 "This procedure is currently implemented using the @code{execv} system\n"
931 "call, but we call it @code{execl} because of its Scheme calling interface.")
932 #define FUNC_NAME s_scm_execl
933 {
934 char *exec_file;
935 char **exec_argv;
936
937 scm_dynwind_begin (0);
938
939 exec_file = scm_to_locale_string (filename);
940 scm_dynwind_free (exec_file);
941
942 exec_argv = scm_i_allocate_string_pointers (args);
943 scm_dynwind_unwind_handler (free_string_pointers, exec_argv,
944 SCM_F_WIND_EXPLICITLY);
945
946 execv (exec_file, exec_argv);
947 SCM_SYSERROR;
948
949 /* not reached. */
950 scm_dynwind_end ();
951 return SCM_BOOL_F;
952 }
953 #undef FUNC_NAME
954
955 SCM_DEFINE (scm_execlp, "execlp", 1, 0, 1,
956 (SCM filename, SCM args),
957 "Similar to @code{execl}, however if\n"
958 "@var{filename} does not contain a slash\n"
959 "then the file to execute will be located by searching the\n"
960 "directories listed in the @code{PATH} environment variable.\n\n"
961 "This procedure is currently implemented using the @code{execvp} system\n"
962 "call, but we call it @code{execlp} because of its Scheme calling interface.")
963 #define FUNC_NAME s_scm_execlp
964 {
965 char *exec_file;
966 char **exec_argv;
967
968 scm_dynwind_begin (0);
969
970 exec_file = scm_to_locale_string (filename);
971 scm_dynwind_free (exec_file);
972
973 exec_argv = scm_i_allocate_string_pointers (args);
974 scm_dynwind_unwind_handler (free_string_pointers, exec_argv,
975 SCM_F_WIND_EXPLICITLY);
976
977 execvp (exec_file, exec_argv);
978 SCM_SYSERROR;
979
980 /* not reached. */
981 scm_dynwind_end ();
982 return SCM_BOOL_F;
983 }
984 #undef FUNC_NAME
985
986
987 /* OPTIMIZE-ME: scm_execle doesn't need malloced copies of the environment
988 list strings the way environ_list_to_c gives. */
989
990 SCM_DEFINE (scm_execle, "execle", 2, 0, 1,
991 (SCM filename, SCM env, SCM args),
992 "Similar to @code{execl}, but the environment of the new process is\n"
993 "specified by @var{env}, which must be a list of strings as returned by the\n"
994 "@code{environ} procedure.\n\n"
995 "This procedure is currently implemented using the @code{execve} system\n"
996 "call, but we call it @code{execle} because of its Scheme calling interface.")
997 #define FUNC_NAME s_scm_execle
998 {
999 char **exec_argv;
1000 char **exec_env;
1001 char *exec_file;
1002
1003 scm_dynwind_begin (0);
1004
1005 exec_file = scm_to_locale_string (filename);
1006 scm_dynwind_free (exec_file);
1007
1008 exec_argv = scm_i_allocate_string_pointers (args);
1009 scm_dynwind_unwind_handler (free_string_pointers, exec_argv,
1010 SCM_F_WIND_EXPLICITLY);
1011
1012 exec_env = scm_i_allocate_string_pointers (env);
1013 scm_dynwind_unwind_handler (free_string_pointers, exec_env,
1014 SCM_F_WIND_EXPLICITLY);
1015
1016 execve (exec_file, exec_argv, exec_env);
1017 SCM_SYSERROR;
1018
1019 /* not reached. */
1020 scm_dynwind_end ();
1021 return SCM_BOOL_F;
1022 }
1023 #undef FUNC_NAME
1024
1025 #ifdef HAVE_FORK
1026 SCM_DEFINE (scm_fork, "primitive-fork", 0, 0, 0,
1027 (),
1028 "Creates a new \"child\" process by duplicating the current \"parent\" process.\n"
1029 "In the child the return value is 0. In the parent the return value is\n"
1030 "the integer process ID of the child.\n\n"
1031 "This procedure has been renamed from @code{fork} to avoid a naming conflict\n"
1032 "with the scsh fork.")
1033 #define FUNC_NAME s_scm_fork
1034 {
1035 int pid;
1036 pid = fork ();
1037 if (pid == -1)
1038 SCM_SYSERROR;
1039 return scm_from_int (pid);
1040 }
1041 #undef FUNC_NAME
1042 #endif /* HAVE_FORK */
1043
1044 #ifdef __MINGW32__
1045 # include "win32-uname.h"
1046 #endif
1047
1048 #if defined (HAVE_UNAME) || defined (__MINGW32__)
1049 SCM_DEFINE (scm_uname, "uname", 0, 0, 0,
1050 (),
1051 "Return an object with some information about the computer\n"
1052 "system the program is running on.")
1053 #define FUNC_NAME s_scm_uname
1054 {
1055 struct utsname buf;
1056 SCM result = scm_c_make_vector (5, SCM_UNSPECIFIED);
1057 if (uname (&buf) < 0)
1058 SCM_SYSERROR;
1059 SCM_SIMPLE_VECTOR_SET(result, 0, scm_from_locale_string (buf.sysname));
1060 SCM_SIMPLE_VECTOR_SET(result, 1, scm_from_locale_string (buf.nodename));
1061 SCM_SIMPLE_VECTOR_SET(result, 2, scm_from_locale_string (buf.release));
1062 SCM_SIMPLE_VECTOR_SET(result, 3, scm_from_locale_string (buf.version));
1063 SCM_SIMPLE_VECTOR_SET(result, 4, scm_from_locale_string (buf.machine));
1064 /*
1065 a linux special?
1066 SCM_SIMPLE_VECTOR_SET(result, 5, scm_from_locale_string (buf.domainname));
1067 */
1068 return result;
1069 }
1070 #undef FUNC_NAME
1071 #endif /* HAVE_UNAME */
1072
1073 SCM_DEFINE (scm_environ, "environ", 0, 1, 0,
1074 (SCM env),
1075 "If @var{env} is omitted, return the current environment (in the\n"
1076 "Unix sense) as a list of strings. Otherwise set the current\n"
1077 "environment, which is also the default environment for child\n"
1078 "processes, to the supplied list of strings. Each member of\n"
1079 "@var{env} should be of the form @code{NAME=VALUE} and values of\n"
1080 "@code{NAME} should not be duplicated. If @var{env} is supplied\n"
1081 "then the return value is unspecified.")
1082 #define FUNC_NAME s_scm_environ
1083 {
1084 if (SCM_UNBNDP (env))
1085 return scm_makfromstrs (-1, environ);
1086 else
1087 {
1088 char **new_environ;
1089
1090 new_environ = scm_i_allocate_string_pointers (env);
1091 /* Free the old environment, except when called for the first
1092 * time.
1093 */
1094 {
1095 static int first = 1;
1096 if (!first)
1097 scm_i_free_string_pointers (environ);
1098 first = 0;
1099 }
1100 environ = new_environ;
1101 return SCM_UNSPECIFIED;
1102 }
1103 }
1104 #undef FUNC_NAME
1105
1106 #ifdef L_tmpnam
1107
1108 SCM_DEFINE (scm_tmpnam, "tmpnam", 0, 0, 0,
1109 (),
1110 "Return a name in the file system that does not match any\n"
1111 "existing file. However there is no guarantee that another\n"
1112 "process will not create the file after @code{tmpnam} is called.\n"
1113 "Care should be taken if opening the file, e.g., use the\n"
1114 "@code{O_EXCL} open flag or use @code{mkstemp!} instead.")
1115 #define FUNC_NAME s_scm_tmpnam
1116 {
1117 char name[L_tmpnam];
1118 char *rv;
1119
1120 SCM_SYSCALL (rv = tmpnam (name));
1121 if (rv == NULL)
1122 /* not SCM_SYSERROR since errno probably not set. */
1123 SCM_MISC_ERROR ("tmpnam failed", SCM_EOL);
1124 return scm_from_locale_string (name);
1125 }
1126 #undef FUNC_NAME
1127
1128 #endif
1129
1130 #ifndef HAVE_MKSTEMP
1131 extern int mkstemp (char *);
1132 #endif
1133
1134 SCM_DEFINE (scm_mkstemp, "mkstemp!", 1, 0, 0,
1135 (SCM tmpl),
1136 "Create a new unique file in the file system and return a new\n"
1137 "buffered port open for reading and writing to the file.\n"
1138 "\n"
1139 "@var{tmpl} is a string specifying where the file should be\n"
1140 "created: it must end with @samp{XXXXXX} and those @samp{X}s\n"
1141 "will be changed in the string to return the name of the file.\n"
1142 "(@code{port-filename} on the port also gives the name.)\n"
1143 "\n"
1144 "POSIX doesn't specify the permissions mode of the file, on GNU\n"
1145 "and most systems it's @code{#o600}. An application can use\n"
1146 "@code{chmod} to relax that if desired. For example\n"
1147 "@code{#o666} less @code{umask}, which is usual for ordinary\n"
1148 "file creation,\n"
1149 "\n"
1150 "@example\n"
1151 "(let ((port (mkstemp! (string-copy \"/tmp/myfile-XXXXXX\"))))\n"
1152 " (chmod port (logand #o666 (lognot (umask))))\n"
1153 " ...)\n"
1154 "@end example")
1155 #define FUNC_NAME s_scm_mkstemp
1156 {
1157 char *c_tmpl;
1158 int rv;
1159
1160 scm_dynwind_begin (0);
1161
1162 c_tmpl = scm_to_locale_string (tmpl);
1163 scm_dynwind_free (c_tmpl);
1164
1165 SCM_SYSCALL (rv = mkstemp (c_tmpl));
1166 if (rv == -1)
1167 SCM_SYSERROR;
1168
1169 scm_substring_move_x (scm_from_locale_string (c_tmpl),
1170 SCM_INUM0, scm_string_length (tmpl),
1171 tmpl, SCM_INUM0);
1172
1173 scm_dynwind_end ();
1174 return scm_fdes_to_port (rv, "w+", tmpl);
1175 }
1176 #undef FUNC_NAME
1177
1178 SCM_DEFINE (scm_utime, "utime", 1, 2, 0,
1179 (SCM pathname, SCM actime, SCM modtime),
1180 "@code{utime} sets the access and modification times for the\n"
1181 "file named by @var{path}. If @var{actime} or @var{modtime} is\n"
1182 "not supplied, then the current time is used. @var{actime} and\n"
1183 "@var{modtime} must be integer time values as returned by the\n"
1184 "@code{current-time} procedure.\n"
1185 "@lisp\n"
1186 "(utime \"foo\" (- (current-time) 3600))\n"
1187 "@end lisp\n"
1188 "will set the access time to one hour in the past and the\n"
1189 "modification time to the current time.")
1190 #define FUNC_NAME s_scm_utime
1191 {
1192 int rv;
1193 struct utimbuf utm_tmp;
1194
1195 if (SCM_UNBNDP (actime))
1196 SCM_SYSCALL (time (&utm_tmp.actime));
1197 else
1198 utm_tmp.actime = SCM_NUM2ULONG (2, actime);
1199
1200 if (SCM_UNBNDP (modtime))
1201 SCM_SYSCALL (time (&utm_tmp.modtime));
1202 else
1203 utm_tmp.modtime = SCM_NUM2ULONG (3, modtime);
1204
1205 STRING_SYSCALL (pathname, c_pathname,
1206 rv = utime (c_pathname, &utm_tmp));
1207 if (rv != 0)
1208 SCM_SYSERROR;
1209 return SCM_UNSPECIFIED;
1210 }
1211 #undef FUNC_NAME
1212
1213 SCM_DEFINE (scm_access, "access?", 2, 0, 0,
1214 (SCM path, SCM how),
1215 "Test accessibility of a file under the real UID and GID of the\n"
1216 "calling process. The return is @code{#t} if @var{path} exists\n"
1217 "and the permissions requested by @var{how} are all allowed, or\n"
1218 "@code{#f} if not.\n"
1219 "\n"
1220 "@var{how} is an integer which is one of the following values,\n"
1221 "or a bitwise-OR (@code{logior}) of multiple values.\n"
1222 "\n"
1223 "@defvar R_OK\n"
1224 "Test for read permission.\n"
1225 "@end defvar\n"
1226 "@defvar W_OK\n"
1227 "Test for write permission.\n"
1228 "@end defvar\n"
1229 "@defvar X_OK\n"
1230 "Test for execute permission.\n"
1231 "@end defvar\n"
1232 "@defvar F_OK\n"
1233 "Test for existence of the file. This is implied by each of the\n"
1234 "other tests, so there's no need to combine it with them.\n"
1235 "@end defvar\n"
1236 "\n"
1237 "It's important to note that @code{access?} does not simply\n"
1238 "indicate what will happen on attempting to read or write a\n"
1239 "file. In normal circumstances it does, but in a set-UID or\n"
1240 "set-GID program it doesn't because @code{access?} tests the\n"
1241 "real ID, whereas an open or execute attempt uses the effective\n"
1242 "ID.\n"
1243 "\n"
1244 "A program which will never run set-UID/GID can ignore the\n"
1245 "difference between real and effective IDs, but for maximum\n"
1246 "generality, especially in library functions, it's best not to\n"
1247 "use @code{access?} to predict the result of an open or execute,\n"
1248 "instead simply attempt that and catch any exception.\n"
1249 "\n"
1250 "The main use for @code{access?} is to let a set-UID/GID program\n"
1251 "determine what the invoking user would have been allowed to do,\n"
1252 "without the greater (or perhaps lesser) privileges afforded by\n"
1253 "the effective ID. For more on this, see ``Testing File\n"
1254 "Access'' in The GNU C Library Reference Manual.")
1255 #define FUNC_NAME s_scm_access
1256 {
1257 int rv;
1258
1259 WITH_STRING (path, c_path,
1260 rv = access (c_path, scm_to_int (how)));
1261 return scm_from_bool (!rv);
1262 }
1263 #undef FUNC_NAME
1264
1265 SCM_DEFINE (scm_getpid, "getpid", 0, 0, 0,
1266 (),
1267 "Return an integer representing the current process ID.")
1268 #define FUNC_NAME s_scm_getpid
1269 {
1270 return scm_from_ulong (getpid ());
1271 }
1272 #undef FUNC_NAME
1273
1274 SCM_DEFINE (scm_putenv, "putenv", 1, 0, 0,
1275 (SCM str),
1276 "Modifies the environment of the current process, which is\n"
1277 "also the default environment inherited by child processes.\n\n"
1278 "If @var{string} is of the form @code{NAME=VALUE} then it will be written\n"
1279 "directly into the environment, replacing any existing environment string\n"
1280 "with\n"
1281 "name matching @code{NAME}. If @var{string} does not contain an equal\n"
1282 "sign, then any existing string with name matching @var{string} will\n"
1283 "be removed.\n\n"
1284 "The return value is unspecified.")
1285 #define FUNC_NAME s_scm_putenv
1286 {
1287 int rv;
1288 char *c_str = scm_to_locale_string (str);
1289 #ifdef __MINGW32__
1290 size_t len = strlen (c_str);
1291 #endif
1292
1293 if (strchr (c_str, '=') == NULL)
1294 {
1295 #ifdef HAVE_UNSETENV
1296 /* No '=' in argument means we should remove the variable from
1297 the environment. Not all putenvs understand this (for instance
1298 FreeBSD 4.8 doesn't). To be safe, we do it explicitely using
1299 unsetenv. */
1300 unsetenv (c_str);
1301 free (c_str);
1302 #else
1303 /* On e.g. Win32 hosts putenv() called with 'name=' removes the
1304 environment variable 'name'. */
1305 int e;
1306 char *ptr = scm_malloc (len + 2);
1307 strcpy (ptr, c_str);
1308 strcpy (ptr+len, "=");
1309 rv = putenv (ptr);
1310 e = errno; free (ptr); free (c_str); errno = e;
1311 if (rv < 0)
1312 SCM_SYSERROR;
1313 #endif /* !HAVE_UNSETENV */
1314 }
1315 else
1316 {
1317 #ifdef __MINGW32__
1318 /* If str is "FOO=", ie. attempting to set an empty string, then
1319 we need to see if it's been successful. On MINGW, "FOO="
1320 means remove FOO from the environment. As a workaround, we
1321 set "FOO= ", ie. a space, and then modify the string returned
1322 by getenv. It's not enough just to modify the string we set,
1323 because MINGW putenv copies it. */
1324
1325 if (c_str[len-1] == '=')
1326 {
1327 char *ptr = scm_malloc (len+2);
1328 strcpy (ptr, c_str);
1329 strcpy (ptr+len, " ");
1330 rv = putenv (ptr);
1331 if (rv < 0)
1332 {
1333 int eno = errno;
1334 free (c_str);
1335 errno = eno;
1336 SCM_SYSERROR;
1337 }
1338 /* truncate to just the name */
1339 c_str[len-1] = '\0';
1340 ptr = getenv (c_str);
1341 if (ptr)
1342 ptr[0] = '\0';
1343 return SCM_UNSPECIFIED;
1344 }
1345 #endif /* __MINGW32__ */
1346
1347 /* Leave c_str in the environment. */
1348
1349 rv = putenv (c_str);
1350 if (rv < 0)
1351 SCM_SYSERROR;
1352 }
1353 return SCM_UNSPECIFIED;
1354 }
1355 #undef FUNC_NAME
1356
1357 #ifdef HAVE_SETLOCALE
1358 SCM_DEFINE (scm_setlocale, "setlocale", 1, 1, 0,
1359 (SCM category, SCM locale),
1360 "If @var{locale} is omitted, return the current value of the\n"
1361 "specified locale category as a system-dependent string.\n"
1362 "@var{category} should be specified using the values\n"
1363 "@code{LC_COLLATE}, @code{LC_ALL} etc.\n"
1364 "\n"
1365 "Otherwise the specified locale category is set to the string\n"
1366 "@var{locale} and the new value is returned as a\n"
1367 "system-dependent string. If @var{locale} is an empty string,\n"
1368 "the locale will be set using environment variables.")
1369 #define FUNC_NAME s_scm_setlocale
1370 {
1371 char *clocale;
1372 char *rv;
1373
1374 scm_dynwind_begin (0);
1375
1376 if (SCM_UNBNDP (locale))
1377 {
1378 clocale = NULL;
1379 }
1380 else
1381 {
1382 clocale = scm_to_locale_string (locale);
1383 scm_dynwind_free (clocale);
1384 }
1385
1386 rv = setlocale (scm_i_to_lc_category (category, 1), clocale);
1387 if (rv == NULL)
1388 {
1389 /* POSIX and C99 don't say anything about setlocale setting errno, so
1390 force a sensible value here. glibc leaves ENOENT, which would be
1391 fine, but it's not a documented feature. */
1392 errno = EINVAL;
1393 SCM_SYSERROR;
1394 }
1395
1396 /* Recompute the standard SRFI-14 character sets in a locale-dependent
1397 (actually charset-dependent) way. */
1398 scm_srfi_14_compute_char_sets ();
1399
1400 scm_dynwind_end ();
1401 return scm_from_locale_string (rv);
1402 }
1403 #undef FUNC_NAME
1404 #endif /* HAVE_SETLOCALE */
1405
1406 #ifdef HAVE_MKNOD
1407 SCM_DEFINE (scm_mknod, "mknod", 4, 0, 0,
1408 (SCM path, SCM type, SCM perms, SCM dev),
1409 "Creates a new special file, such as a file corresponding to a device.\n"
1410 "@var{path} specifies the name of the file. @var{type} should\n"
1411 "be one of the following symbols:\n"
1412 "regular, directory, symlink, block-special, char-special,\n"
1413 "fifo, or socket. @var{perms} (an integer) specifies the file permissions.\n"
1414 "@var{dev} (an integer) specifies which device the special file refers\n"
1415 "to. Its exact interpretation depends on the kind of special file\n"
1416 "being created.\n\n"
1417 "E.g.,\n"
1418 "@lisp\n"
1419 "(mknod \"/dev/fd0\" 'block-special #o660 (+ (* 2 256) 2))\n"
1420 "@end lisp\n\n"
1421 "The return value is unspecified.")
1422 #define FUNC_NAME s_scm_mknod
1423 {
1424 int val;
1425 const char *p;
1426 int ctype = 0;
1427
1428 SCM_VALIDATE_STRING (1, path);
1429 SCM_VALIDATE_SYMBOL (2, type);
1430
1431 p = scm_i_symbol_chars (type);
1432 if (strcmp (p, "regular") == 0)
1433 ctype = S_IFREG;
1434 else if (strcmp (p, "directory") == 0)
1435 ctype = S_IFDIR;
1436 #ifdef S_IFLNK
1437 /* systems without symlinks probably don't have S_IFLNK defined */
1438 else if (strcmp (p, "symlink") == 0)
1439 ctype = S_IFLNK;
1440 #endif
1441 else if (strcmp (p, "block-special") == 0)
1442 ctype = S_IFBLK;
1443 else if (strcmp (p, "char-special") == 0)
1444 ctype = S_IFCHR;
1445 else if (strcmp (p, "fifo") == 0)
1446 ctype = S_IFIFO;
1447 #ifdef S_IFSOCK
1448 else if (strcmp (p, "socket") == 0)
1449 ctype = S_IFSOCK;
1450 #endif
1451 else
1452 SCM_OUT_OF_RANGE (2, type);
1453
1454 STRING_SYSCALL (path, c_path,
1455 val = mknod (c_path,
1456 ctype | scm_to_int (perms),
1457 scm_to_int (dev)));
1458 if (val != 0)
1459 SCM_SYSERROR;
1460 return SCM_UNSPECIFIED;
1461 }
1462 #undef FUNC_NAME
1463 #endif /* HAVE_MKNOD */
1464
1465 #ifdef HAVE_NICE
1466 SCM_DEFINE (scm_nice, "nice", 1, 0, 0,
1467 (SCM incr),
1468 "Increment the priority of the current process by @var{incr}. A higher\n"
1469 "priority value means that the process runs less often.\n"
1470 "The return value is unspecified.")
1471 #define FUNC_NAME s_scm_nice
1472 {
1473 /* nice() returns "prio-NZERO" on success or -1 on error, but -1 can arise
1474 from "prio-NZERO", so an error must be detected from errno changed */
1475 errno = 0;
1476 nice (scm_to_int (incr));
1477 if (errno != 0)
1478 SCM_SYSERROR;
1479 return SCM_UNSPECIFIED;
1480 }
1481 #undef FUNC_NAME
1482 #endif /* HAVE_NICE */
1483
1484 #ifdef HAVE_SYNC
1485 SCM_DEFINE (scm_sync, "sync", 0, 0, 0,
1486 (),
1487 "Flush the operating system disk buffers.\n"
1488 "The return value is unspecified.")
1489 #define FUNC_NAME s_scm_sync
1490 {
1491 sync();
1492 return SCM_UNSPECIFIED;
1493 }
1494 #undef FUNC_NAME
1495 #endif /* HAVE_SYNC */
1496
1497
1498 /* crypt() returns a pointer to a static buffer, so we use scm_i_misc_mutex
1499 to avoid another thread overwriting it. A test program running crypt
1500 continuously in two threads can be quickly seen tripping this problem.
1501 crypt() is pretty slow normally, so a mutex shouldn't add much overhead.
1502
1503 glibc has a thread-safe crypt_r, but (in version 2.3.2) it runs a lot
1504 slower (about 5x) than plain crypt if you pass an uninitialized data
1505 block each time. Presumably there's some one-time setups. The best way
1506 to use crypt_r for parallel execution in multiple threads would probably
1507 be to maintain a little pool of initialized crypt_data structures, take
1508 one and use it, then return it to the pool. That pool could be garbage
1509 collected so it didn't add permanently to memory use if only a few crypt
1510 calls are made. But we expect crypt will be used rarely, and even more
1511 rarely will there be any desire for lots of parallel execution on
1512 multiple cpus. So for now we don't bother with anything fancy, just
1513 ensure it works. */
1514
1515 #if HAVE_CRYPT
1516 SCM_DEFINE (scm_crypt, "crypt", 2, 0, 0,
1517 (SCM key, SCM salt),
1518 "Encrypt @var{key} using @var{salt} as the salt value to the\n"
1519 "crypt(3) library call.")
1520 #define FUNC_NAME s_scm_crypt
1521 {
1522 SCM ret;
1523 char *c_key, *c_salt;
1524
1525 scm_dynwind_begin (0);
1526 scm_i_dynwind_pthread_mutex_lock (&scm_i_misc_mutex);
1527
1528 c_key = scm_to_locale_string (key);
1529 scm_dynwind_free (c_key);
1530 c_salt = scm_to_locale_string (salt);
1531 scm_dynwind_free (c_salt);
1532
1533 ret = scm_from_locale_string (crypt (c_key, c_salt));
1534
1535 scm_dynwind_end ();
1536 return ret;
1537 }
1538 #undef FUNC_NAME
1539 #endif /* HAVE_CRYPT */
1540
1541 #if HAVE_CHROOT
1542 SCM_DEFINE (scm_chroot, "chroot", 1, 0, 0,
1543 (SCM path),
1544 "Change the root directory to that specified in @var{path}.\n"
1545 "This directory will be used for path names beginning with\n"
1546 "@file{/}. The root directory is inherited by all children\n"
1547 "of the current process. Only the superuser may change the\n"
1548 "root directory.")
1549 #define FUNC_NAME s_scm_chroot
1550 {
1551 int rv;
1552
1553 WITH_STRING (path, c_path,
1554 rv = chroot (c_path));
1555 if (rv == -1)
1556 SCM_SYSERROR;
1557 return SCM_UNSPECIFIED;
1558 }
1559 #undef FUNC_NAME
1560 #endif /* HAVE_CHROOT */
1561
1562
1563 #ifdef __MINGW32__
1564 /* Wrapper function to supplying `getlogin()' under Windows. */
1565 static char * getlogin (void)
1566 {
1567 static char user[256];
1568 static unsigned long len = 256;
1569
1570 if (!GetUserName (user, &len))
1571 return NULL;
1572 return user;
1573 }
1574 #endif /* __MINGW32__ */
1575
1576
1577 #if defined (HAVE_GETLOGIN) || defined (__MINGW32__)
1578 SCM_DEFINE (scm_getlogin, "getlogin", 0, 0, 0,
1579 (void),
1580 "Return a string containing the name of the user logged in on\n"
1581 "the controlling terminal of the process, or @code{#f} if this\n"
1582 "information cannot be obtained.")
1583 #define FUNC_NAME s_scm_getlogin
1584 {
1585 char * p;
1586
1587 p = getlogin ();
1588 if (!p || !*p)
1589 return SCM_BOOL_F;
1590 return scm_from_locale_string (p);
1591 }
1592 #undef FUNC_NAME
1593 #endif /* HAVE_GETLOGIN */
1594
1595 #if HAVE_CUSERID
1596 SCM_DEFINE (scm_cuserid, "cuserid", 0, 0, 0,
1597 (void),
1598 "Return a string containing a user name associated with the\n"
1599 "effective user id of the process. Return @code{#f} if this\n"
1600 "information cannot be obtained.")
1601 #define FUNC_NAME s_scm_cuserid
1602 {
1603 char buf[L_cuserid];
1604 char * p;
1605
1606 p = cuserid (buf);
1607 if (!p || !*p)
1608 return SCM_BOOL_F;
1609 return scm_from_locale_string (p);
1610 }
1611 #undef FUNC_NAME
1612 #endif /* HAVE_CUSERID */
1613
1614 #if HAVE_GETPRIORITY
1615 SCM_DEFINE (scm_getpriority, "getpriority", 2, 0, 0,
1616 (SCM which, SCM who),
1617 "Return the scheduling priority of the process, process group\n"
1618 "or user, as indicated by @var{which} and @var{who}. @var{which}\n"
1619 "is one of the variables @code{PRIO_PROCESS}, @code{PRIO_PGRP}\n"
1620 "or @code{PRIO_USER}, and @var{who} is interpreted relative to\n"
1621 "@var{which} (a process identifier for @code{PRIO_PROCESS},\n"
1622 "process group identifier for @code{PRIO_PGRP}, and a user\n"
1623 "identifier for @code{PRIO_USER}. A zero value of @var{who}\n"
1624 "denotes the current process, process group, or user. Return\n"
1625 "the highest priority (lowest numerical value) of any of the\n"
1626 "specified processes.")
1627 #define FUNC_NAME s_scm_getpriority
1628 {
1629 int cwhich, cwho, ret;
1630
1631 cwhich = scm_to_int (which);
1632 cwho = scm_to_int (who);
1633
1634 /* We have to clear errno and examine it later, because -1 is a
1635 legal return value for getpriority(). */
1636 errno = 0;
1637 ret = getpriority (cwhich, cwho);
1638 if (errno != 0)
1639 SCM_SYSERROR;
1640 return scm_from_int (ret);
1641 }
1642 #undef FUNC_NAME
1643 #endif /* HAVE_GETPRIORITY */
1644
1645 #if HAVE_SETPRIORITY
1646 SCM_DEFINE (scm_setpriority, "setpriority", 3, 0, 0,
1647 (SCM which, SCM who, SCM prio),
1648 "Set the scheduling priority of the process, process group\n"
1649 "or user, as indicated by @var{which} and @var{who}. @var{which}\n"
1650 "is one of the variables @code{PRIO_PROCESS}, @code{PRIO_PGRP}\n"
1651 "or @code{PRIO_USER}, and @var{who} is interpreted relative to\n"
1652 "@var{which} (a process identifier for @code{PRIO_PROCESS},\n"
1653 "process group identifier for @code{PRIO_PGRP}, and a user\n"
1654 "identifier for @code{PRIO_USER}. A zero value of @var{who}\n"
1655 "denotes the current process, process group, or user.\n"
1656 "@var{prio} is a value in the range -20 and 20, the default\n"
1657 "priority is 0; lower priorities cause more favorable\n"
1658 "scheduling. Sets the priority of all of the specified\n"
1659 "processes. Only the super-user may lower priorities.\n"
1660 "The return value is not specified.")
1661 #define FUNC_NAME s_scm_setpriority
1662 {
1663 int cwhich, cwho, cprio;
1664
1665 cwhich = scm_to_int (which);
1666 cwho = scm_to_int (who);
1667 cprio = scm_to_int (prio);
1668
1669 if (setpriority (cwhich, cwho, cprio) == -1)
1670 SCM_SYSERROR;
1671 return SCM_UNSPECIFIED;
1672 }
1673 #undef FUNC_NAME
1674 #endif /* HAVE_SETPRIORITY */
1675
1676 #if HAVE_GETPASS
1677 SCM_DEFINE (scm_getpass, "getpass", 1, 0, 0,
1678 (SCM prompt),
1679 "Display @var{prompt} to the standard error output and read\n"
1680 "a password from @file{/dev/tty}. If this file is not\n"
1681 "accessible, it reads from standard input. The password may be\n"
1682 "up to 127 characters in length. Additional characters and the\n"
1683 "terminating newline character are discarded. While reading\n"
1684 "the password, echoing and the generation of signals by special\n"
1685 "characters is disabled.")
1686 #define FUNC_NAME s_scm_getpass
1687 {
1688 char * p;
1689 SCM passwd;
1690
1691 SCM_VALIDATE_STRING (1, prompt);
1692
1693 WITH_STRING (prompt, c_prompt,
1694 p = getpass(c_prompt));
1695 passwd = scm_from_locale_string (p);
1696
1697 /* Clear out the password in the static buffer. */
1698 memset (p, 0, strlen (p));
1699
1700 return passwd;
1701 }
1702 #undef FUNC_NAME
1703 #endif /* HAVE_GETPASS */
1704
1705 /* Wrapper function for flock() support under M$-Windows. */
1706 #ifdef __MINGW32__
1707 # include <io.h>
1708 # include <sys/locking.h>
1709 # include <errno.h>
1710 # ifndef _LK_UNLCK
1711 /* Current MinGW package fails to define this. *sigh* */
1712 # define _LK_UNLCK 0
1713 # endif
1714 # define LOCK_EX 1
1715 # define LOCK_UN 2
1716 # define LOCK_SH 4
1717 # define LOCK_NB 8
1718
1719 static int flock (int fd, int operation)
1720 {
1721 long pos, len;
1722 int ret, err;
1723
1724 /* Disable invalid arguments. */
1725 if (((operation & (LOCK_EX | LOCK_SH)) == (LOCK_EX | LOCK_SH)) ||
1726 ((operation & (LOCK_EX | LOCK_UN)) == (LOCK_EX | LOCK_UN)) ||
1727 ((operation & (LOCK_SH | LOCK_UN)) == (LOCK_SH | LOCK_UN)))
1728 {
1729 errno = EINVAL;
1730 return -1;
1731 }
1732
1733 /* Determine mode of operation and discard unsupported ones. */
1734 if (operation == (LOCK_NB | LOCK_EX))
1735 operation = _LK_NBLCK;
1736 else if (operation & LOCK_UN)
1737 operation = _LK_UNLCK;
1738 else if (operation == LOCK_EX)
1739 operation = _LK_LOCK;
1740 else
1741 {
1742 errno = EINVAL;
1743 return -1;
1744 }
1745
1746 /* Save current file pointer and seek to beginning. */
1747 if ((pos = lseek (fd, 0, SEEK_CUR)) == -1 || (len = filelength (fd)) == -1)
1748 return -1;
1749 lseek (fd, 0L, SEEK_SET);
1750
1751 /* Deadlock if necessary. */
1752 do
1753 {
1754 ret = _locking (fd, operation, len);
1755 }
1756 while (ret == -1 && errno == EDEADLOCK);
1757
1758 /* Produce meaningful error message. */
1759 if (errno == EACCES && operation == _LK_NBLCK)
1760 err = EDEADLOCK;
1761 else
1762 err = errno;
1763
1764 /* Return to saved file position pointer. */
1765 lseek (fd, pos, SEEK_SET);
1766 errno = err;
1767 return ret;
1768 }
1769 #endif /* __MINGW32__ */
1770
1771 #if HAVE_FLOCK || defined (__MINGW32__)
1772 SCM_DEFINE (scm_flock, "flock", 2, 0, 0,
1773 (SCM file, SCM operation),
1774 "Apply or remove an advisory lock on an open file.\n"
1775 "@var{operation} specifies the action to be done:\n"
1776 "\n"
1777 "@defvar LOCK_SH\n"
1778 "Shared lock. More than one process may hold a shared lock\n"
1779 "for a given file at a given time.\n"
1780 "@end defvar\n"
1781 "@defvar LOCK_EX\n"
1782 "Exclusive lock. Only one process may hold an exclusive lock\n"
1783 "for a given file at a given time.\n"
1784 "@end defvar\n"
1785 "@defvar LOCK_UN\n"
1786 "Unlock the file.\n"
1787 "@end defvar\n"
1788 "@defvar LOCK_NB\n"
1789 "Don't block when locking. This is combined with one of the\n"
1790 "other operations using @code{logior}. If @code{flock} would\n"
1791 "block an @code{EWOULDBLOCK} error is thrown.\n"
1792 "@end defvar\n"
1793 "\n"
1794 "The return value is not specified. @var{file} may be an open\n"
1795 "file descriptor or an open file descriptor port.\n"
1796 "\n"
1797 "Note that @code{flock} does not lock files across NFS.")
1798 #define FUNC_NAME s_scm_flock
1799 {
1800 int fdes;
1801
1802 if (scm_is_integer (file))
1803 fdes = scm_to_int (file);
1804 else
1805 {
1806 SCM_VALIDATE_OPFPORT (2, file);
1807
1808 fdes = SCM_FPORT_FDES (file);
1809 }
1810 if (flock (fdes, scm_to_int (operation)) == -1)
1811 SCM_SYSERROR;
1812 return SCM_UNSPECIFIED;
1813 }
1814 #undef FUNC_NAME
1815 #endif /* HAVE_FLOCK */
1816
1817 #if HAVE_SETHOSTNAME
1818 SCM_DEFINE (scm_sethostname, "sethostname", 1, 0, 0,
1819 (SCM name),
1820 "Set the host name of the current processor to @var{name}. May\n"
1821 "only be used by the superuser. The return value is not\n"
1822 "specified.")
1823 #define FUNC_NAME s_scm_sethostname
1824 {
1825 int rv;
1826
1827 WITH_STRING (name, c_name,
1828 rv = sethostname (c_name, strlen(c_name)));
1829 if (rv == -1)
1830 SCM_SYSERROR;
1831 return SCM_UNSPECIFIED;
1832 }
1833 #undef FUNC_NAME
1834 #endif /* HAVE_SETHOSTNAME */
1835
1836
1837 #if HAVE_GETHOSTNAME
1838 SCM_DEFINE (scm_gethostname, "gethostname", 0, 0, 0,
1839 (void),
1840 "Return the host name of the current processor.")
1841 #define FUNC_NAME s_scm_gethostname
1842 {
1843 #ifdef MAXHOSTNAMELEN
1844
1845 /* Various systems define MAXHOSTNAMELEN (including Solaris in fact).
1846 * On GNU/Linux this doesn't include the terminating '\0', hence "+ 1". */
1847 const int len = MAXHOSTNAMELEN + 1;
1848 char *const p = scm_malloc (len);
1849 const int res = gethostname (p, len);
1850
1851 scm_dynwind_begin (0);
1852 scm_dynwind_unwind_handler (free, p, 0);
1853
1854 #else
1855
1856 /* Default 256 is for Solaris, under Linux ENAMETOOLONG is returned if not
1857 * large enough. SUSv2 specifies 255 maximum too, apparently. */
1858 int len = 256;
1859 int res;
1860 char *p;
1861
1862 # if HAVE_SYSCONF && defined (_SC_HOST_NAME_MAX)
1863
1864 /* POSIX specifies the HOST_NAME_MAX system parameter for the max size,
1865 * which may reflect a particular kernel configuration.
1866 * Must watch out for this existing but giving -1, as happens for instance
1867 * in gnu/linux glibc 2.3.2. */
1868 {
1869 const long int n = sysconf (_SC_HOST_NAME_MAX);
1870 if (n != -1L)
1871 len = n;
1872 }
1873
1874 # endif
1875
1876 p = scm_malloc (len);
1877
1878 scm_dynwind_begin (0);
1879 scm_dynwind_unwind_handler (free, p, 0);
1880
1881 res = gethostname (p, len);
1882 while (res == -1 && errno == ENAMETOOLONG)
1883 {
1884 len *= 2;
1885
1886 /* scm_realloc may throw an exception. */
1887 p = scm_realloc (p, len);
1888 res = gethostname (p, len);
1889 }
1890
1891 #endif
1892
1893 if (res == -1)
1894 {
1895 const int save_errno = errno;
1896
1897 /* No guile exceptions can occur before we have freed p's memory. */
1898 scm_dynwind_end ();
1899 free (p);
1900
1901 errno = save_errno;
1902 SCM_SYSERROR;
1903 }
1904 else
1905 {
1906 /* scm_from_locale_string may throw an exception. */
1907 const SCM name = scm_from_locale_string (p);
1908
1909 /* No guile exceptions can occur before we have freed p's memory. */
1910 scm_dynwind_end ();
1911 free (p);
1912
1913 return name;
1914 }
1915 }
1916 #undef FUNC_NAME
1917 #endif /* HAVE_GETHOSTNAME */
1918
1919
1920 void
1921 scm_init_posix ()
1922 {
1923 scm_add_feature ("posix");
1924 #ifdef HAVE_GETEUID
1925 scm_add_feature ("EIDs");
1926 #endif
1927 #ifdef WAIT_ANY
1928 scm_c_define ("WAIT_ANY", scm_from_int (WAIT_ANY));
1929 #endif
1930 #ifdef WAIT_MYPGRP
1931 scm_c_define ("WAIT_MYPGRP", scm_from_int (WAIT_MYPGRP));
1932 #endif
1933 #ifdef WNOHANG
1934 scm_c_define ("WNOHANG", scm_from_int (WNOHANG));
1935 #endif
1936 #ifdef WUNTRACED
1937 scm_c_define ("WUNTRACED", scm_from_int (WUNTRACED));
1938 #endif
1939
1940 /* access() symbols. */
1941 scm_c_define ("R_OK", scm_from_int (R_OK));
1942 scm_c_define ("W_OK", scm_from_int (W_OK));
1943 scm_c_define ("X_OK", scm_from_int (X_OK));
1944 scm_c_define ("F_OK", scm_from_int (F_OK));
1945
1946 #ifdef LC_COLLATE
1947 scm_c_define ("LC_COLLATE", scm_from_int (LC_COLLATE));
1948 #endif
1949 #ifdef LC_CTYPE
1950 scm_c_define ("LC_CTYPE", scm_from_int (LC_CTYPE));
1951 #endif
1952 #ifdef LC_MONETARY
1953 scm_c_define ("LC_MONETARY", scm_from_int (LC_MONETARY));
1954 #endif
1955 #ifdef LC_NUMERIC
1956 scm_c_define ("LC_NUMERIC", scm_from_int (LC_NUMERIC));
1957 #endif
1958 #ifdef LC_TIME
1959 scm_c_define ("LC_TIME", scm_from_int (LC_TIME));
1960 #endif
1961 #ifdef LC_MESSAGES
1962 scm_c_define ("LC_MESSAGES", scm_from_int (LC_MESSAGES));
1963 #endif
1964 #ifdef LC_ALL
1965 scm_c_define ("LC_ALL", scm_from_int (LC_ALL));
1966 #endif
1967 #ifdef LC_PAPER
1968 scm_c_define ("LC_PAPER", scm_from_int (LC_PAPER));
1969 #endif
1970 #ifdef LC_NAME
1971 scm_c_define ("LC_NAME", scm_from_int (LC_NAME));
1972 #endif
1973 #ifdef LC_ADDRESS
1974 scm_c_define ("LC_ADDRESS", scm_from_int (LC_ADDRESS));
1975 #endif
1976 #ifdef LC_TELEPHONE
1977 scm_c_define ("LC_TELEPHONE", scm_from_int (LC_TELEPHONE));
1978 #endif
1979 #ifdef LC_MEASUREMENT
1980 scm_c_define ("LC_MEASUREMENT", scm_from_int (LC_MEASUREMENT));
1981 #endif
1982 #ifdef LC_IDENTIFICATION
1983 scm_c_define ("LC_IDENTIFICATION", scm_from_int (LC_IDENTIFICATION));
1984 #endif
1985 #ifdef PIPE_BUF
1986 scm_c_define ("PIPE_BUF", scm_from_long (PIPE_BUF));
1987 #endif
1988
1989 #ifdef PRIO_PROCESS
1990 scm_c_define ("PRIO_PROCESS", scm_from_int (PRIO_PROCESS));
1991 #endif
1992 #ifdef PRIO_PGRP
1993 scm_c_define ("PRIO_PGRP", scm_from_int (PRIO_PGRP));
1994 #endif
1995 #ifdef PRIO_USER
1996 scm_c_define ("PRIO_USER", scm_from_int (PRIO_USER));
1997 #endif
1998
1999 #ifdef LOCK_SH
2000 scm_c_define ("LOCK_SH", scm_from_int (LOCK_SH));
2001 #endif
2002 #ifdef LOCK_EX
2003 scm_c_define ("LOCK_EX", scm_from_int (LOCK_EX));
2004 #endif
2005 #ifdef LOCK_UN
2006 scm_c_define ("LOCK_UN", scm_from_int (LOCK_UN));
2007 #endif
2008 #ifdef LOCK_NB
2009 scm_c_define ("LOCK_NB", scm_from_int (LOCK_NB));
2010 #endif
2011
2012 #include "libguile/cpp_sig_symbols.c"
2013 #include "libguile/posix.x"
2014 }
2015
2016 /*
2017 Local Variables:
2018 c-file-style: "gnu"
2019 End:
2020 */