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