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