1 /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful, but
9 * 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.
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
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/values.h"
40 #include "libguile/lang.h"
42 #include "libguile/validate.h"
43 #include "libguile/posix.h"
44 #include "libguile/gettext.h"
45 #include "libguile/threads.h"
51 #ifdef TIME_WITH_SYS_TIME
52 # include <sys/time.h>
56 # include <sys/time.h>
66 extern char *ttyname();
70 #ifdef LIBC_H_WITH_UNISTD_H
74 #include <sys/types.h>
84 #ifdef HAVE_WINSOCK2_H
89 /* Some defines for Windows here. */
91 # define pipe(fd) _pipe (fd, 256, O_BINARY)
92 #endif /* __MINGW32__ */
95 # include <sys/wait.h>
98 # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
101 # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
109 #ifdef HAVE_SYS_UTSNAME_H
110 #include <sys/utsname.h>
113 #ifdef HAVE_SETLOCALE
117 #if (defined HAVE_NEWLOCALE) && (defined HAVE_STRCOLL_L)
118 # define USE_GNU_LOCALE_API
121 #if (defined USE_GNU_LOCALE_API) && (defined HAVE_XLOCALE_H)
122 # include <xlocale.h>
130 #include <netdb.h> /* for MAXHOSTNAMELEN on Solaris */
133 #ifdef HAVE_SYS_PARAM_H
134 #include <sys/param.h> /* for MAXHOSTNAMELEN */
137 #if HAVE_SYS_RESOURCE_H
138 # include <sys/resource.h>
141 #include <sys/file.h> /* from Gnulib */
143 /* Some Unix systems don't define these. CPP hair is dangerous, but
144 this seems safe enough... */
161 /* No prototype for this on Solaris 10. The man page says it's in
162 <unistd.h> ... but it lies. */
163 #if ! HAVE_DECL_SETHOSTNAME
164 int sethostname (char *name
, size_t namelen
);
167 /* On NextStep, <utime.h> doesn't define struct utime, unless we
168 #define _POSIX_SOURCE before #including it. I think this is less
169 of a kludge than defining struct utimbuf ourselves. */
170 #ifdef UTIMBUF_NEEDS_POSIX
171 #define _POSIX_SOURCE
174 #ifdef HAVE_SYS_UTIME_H
175 #include <sys/utime.h>
182 /* Please don't add any more #includes or #defines here. The hack
183 above means that _POSIX_SOURCE may be #defined, which will
184 encourage header files to do strange things.
186 FIXME: Maybe should undef _POSIX_SOURCE after it's done its job.
188 FIXME: Probably should do all the includes first, then all the fallback
189 declarations and defines, in case things are not in the header we
197 /* Two often used patterns
200 #define WITH_STRING(str,cstr,code) \
202 char *cstr = scm_to_locale_string (str); \
207 #define STRING_SYSCALL(str,cstr,code) \
210 char *cstr = scm_to_locale_string (str); \
211 SCM_SYSCALL (code); \
212 eno = errno; free (cstr); errno = eno; \
217 SCM_SYMBOL (sym_read_pipe
, "read pipe");
218 SCM_SYMBOL (sym_write_pipe
, "write pipe");
220 SCM_DEFINE (scm_pipe
, "pipe", 0, 0, 0,
222 "Return a newly created pipe: a pair of ports which are linked\n"
223 "together on the local machine. The @emph{car} is the input\n"
224 "port and the @emph{cdr} is the output port. Data written (and\n"
225 "flushed) to the output port can be read from the input port.\n"
226 "Pipes are commonly used for communication with a newly forked\n"
227 "child process. The need to flush the output port can be\n"
228 "avoided by making it unbuffered using @code{setvbuf}.\n"
230 "Writes occur atomically provided the size of the data in bytes\n"
231 "is not greater than the value of @code{PIPE_BUF}. Note that\n"
232 "the output port is likely to block if too much data (typically\n"
233 "equal to @code{PIPE_BUF}) has been written but not yet read\n"
234 "from the input port.")
235 #define FUNC_NAME s_scm_pipe
244 p_rd
= scm_fdes_to_port (fd
[0], "r", sym_read_pipe
);
245 p_wt
= scm_fdes_to_port (fd
[1], "w", sym_write_pipe
);
246 return scm_cons (p_rd
, p_wt
);
251 #ifdef HAVE_GETGROUPS
252 SCM_DEFINE (scm_getgroups
, "getgroups", 0, 0, 0,
254 "Return a vector of integers representing the current\n"
255 "supplementary group IDs.")
256 #define FUNC_NAME s_scm_getgroups
263 ngroups
= getgroups (0, NULL
);
267 size
= ngroups
* sizeof (GETGROUPS_T
);
268 groups
= scm_malloc (size
);
269 ngroups
= getgroups (ngroups
, groups
);
271 result
= scm_c_make_vector (ngroups
, SCM_BOOL_F
);
272 while (--ngroups
>= 0)
273 SCM_SIMPLE_VECTOR_SET (result
, ngroups
, scm_from_ulong (groups
[ngroups
]));
281 #ifdef HAVE_SETGROUPS
282 SCM_DEFINE (scm_setgroups
, "setgroups", 1, 0, 0,
284 "Set the current set of supplementary group IDs to the integers\n"
285 "in the given vector @var{vec}. The return value is\n"
288 "Generally only the superuser can set the process group IDs.")
289 #define FUNC_NAME s_scm_setgroups
298 SCM_VALIDATE_VECTOR (SCM_ARG1
, group_vec
);
300 ngroups
= SCM_SIMPLE_VECTOR_LENGTH (group_vec
);
302 /* validate before allocating, so we don't have to worry about leaks */
303 for (i
= 0; i
< ngroups
; i
++)
305 unsigned long ulong_gid
;
307 SCM_VALIDATE_ULONG_COPY (1, SCM_SIMPLE_VECTOR_REF (group_vec
, i
),
310 if (gid
!= ulong_gid
)
311 SCM_OUT_OF_RANGE (1, SCM_SIMPLE_VECTOR_REF (group_vec
, i
));
314 size
= ngroups
* sizeof (GETGROUPS_T
);
315 if (size
/ sizeof (GETGROUPS_T
) != ngroups
)
316 SCM_OUT_OF_RANGE (SCM_ARG1
, scm_from_int (ngroups
));
317 groups
= scm_malloc (size
);
318 for(i
= 0; i
< ngroups
; i
++)
319 groups
[i
] = SCM_NUM2ULONG (1, SCM_SIMPLE_VECTOR_REF (group_vec
, i
));
321 result
= setgroups (ngroups
, groups
);
322 save_errno
= errno
; /* don't let free() touch errno */
327 return SCM_UNSPECIFIED
;
333 SCM_DEFINE (scm_getpwuid
, "getpw", 0, 1, 0,
335 "Look up an entry in the user database. @var{obj} can be an integer,\n"
336 "a string, or omitted, giving the behaviour of getpwuid, getpwnam\n"
337 "or getpwent respectively.")
338 #define FUNC_NAME s_scm_getpwuid
340 struct passwd
*entry
;
342 SCM result
= scm_c_make_vector (7, SCM_UNSPECIFIED
);
343 if (SCM_UNBNDP (user
) || scm_is_false (user
))
345 SCM_SYSCALL (entry
= getpwent ());
351 else if (scm_is_integer (user
))
353 entry
= getpwuid (scm_to_int (user
));
357 WITH_STRING (user
, c_user
,
358 entry
= getpwnam (c_user
));
361 SCM_MISC_ERROR ("entry not found", SCM_EOL
);
363 SCM_SIMPLE_VECTOR_SET(result
, 0, scm_from_locale_string (entry
->pw_name
));
364 SCM_SIMPLE_VECTOR_SET(result
, 1, scm_from_locale_string (entry
->pw_passwd
));
365 SCM_SIMPLE_VECTOR_SET(result
, 2, scm_from_ulong (entry
->pw_uid
));
366 SCM_SIMPLE_VECTOR_SET(result
, 3, scm_from_ulong (entry
->pw_gid
));
367 SCM_SIMPLE_VECTOR_SET(result
, 4, scm_from_locale_string (entry
->pw_gecos
));
369 SCM_SIMPLE_VECTOR_SET(result
, 5, scm_from_locale_string (""));
371 SCM_SIMPLE_VECTOR_SET(result
, 5, scm_from_locale_string (entry
->pw_dir
));
372 if (!entry
->pw_shell
)
373 SCM_SIMPLE_VECTOR_SET(result
, 6, scm_from_locale_string (""));
375 SCM_SIMPLE_VECTOR_SET(result
, 6, scm_from_locale_string (entry
->pw_shell
));
379 #endif /* HAVE_GETPWENT */
383 SCM_DEFINE (scm_setpwent
, "setpw", 0, 1, 0,
385 "If called with a true argument, initialize or reset the password data\n"
386 "stream. Otherwise, close the stream. The @code{setpwent} and\n"
387 "@code{endpwent} procedures are implemented on top of this.")
388 #define FUNC_NAME s_scm_setpwent
390 if (SCM_UNBNDP (arg
) || scm_is_false (arg
))
394 return SCM_UNSPECIFIED
;
401 /* Combines getgrgid and getgrnam. */
402 SCM_DEFINE (scm_getgrgid
, "getgr", 0, 1, 0,
404 "Look up an entry in the group database. @var{obj} can be an integer,\n"
405 "a string, or omitted, giving the behaviour of getgrgid, getgrnam\n"
406 "or getgrent respectively.")
407 #define FUNC_NAME s_scm_getgrgid
410 SCM result
= scm_c_make_vector (4, SCM_UNSPECIFIED
);
412 if (SCM_UNBNDP (name
) || scm_is_false (name
))
414 SCM_SYSCALL (entry
= getgrent ());
420 else if (scm_is_integer (name
))
421 SCM_SYSCALL (entry
= getgrgid (scm_to_int (name
)));
423 STRING_SYSCALL (name
, c_name
,
424 entry
= getgrnam (c_name
));
428 SCM_SIMPLE_VECTOR_SET(result
, 0, scm_from_locale_string (entry
->gr_name
));
429 SCM_SIMPLE_VECTOR_SET(result
, 1, scm_from_locale_string (entry
->gr_passwd
));
430 SCM_SIMPLE_VECTOR_SET(result
, 2, scm_from_ulong (entry
->gr_gid
));
431 SCM_SIMPLE_VECTOR_SET(result
, 3, scm_makfromstrs (-1, entry
->gr_mem
));
438 SCM_DEFINE (scm_setgrent
, "setgr", 0, 1, 0,
440 "If called with a true argument, initialize or reset the group data\n"
441 "stream. Otherwise, close the stream. The @code{setgrent} and\n"
442 "@code{endgrent} procedures are implemented on top of this.")
443 #define FUNC_NAME s_scm_setgrent
445 if (SCM_UNBNDP (arg
) || scm_is_false (arg
))
449 return SCM_UNSPECIFIED
;
452 #endif /* HAVE_GETGRENT */
455 #ifdef HAVE_GETRLIMIT
457 SCM_SYMBOL (sym_as
, "as");
460 SCM_SYMBOL (sym_core
, "core");
463 SCM_SYMBOL (sym_cpu
, "cpu");
466 SCM_SYMBOL (sym_data
, "data");
469 SCM_SYMBOL (sym_fsize
, "fsize");
471 #ifdef RLIMIT_MEMLOCK
472 SCM_SYMBOL (sym_memlock
, "memlock");
474 #ifdef RLIMIT_MSGQUEUE
475 SCM_SYMBOL (sym_msgqueue
, "msgqueue");
478 SCM_SYMBOL (sym_nice
, "nice");
481 SCM_SYMBOL (sym_nofile
, "nofile");
484 SCM_SYMBOL (sym_nproc
, "nproc");
487 SCM_SYMBOL (sym_rss
, "rss");
490 SCM_SYMBOL (sym_rtprio
, "rtprio");
493 SCM_SYMBOL (sym_rttime
, "rttime");
495 #ifdef RLIMIT_SIGPENDING
496 SCM_SYMBOL (sym_sigpending
, "sigpending");
499 SCM_SYMBOL (sym_stack
, "stack");
503 scm_to_resource (SCM s
, const char *func
, int pos
)
505 if (scm_is_number (s
))
506 return scm_to_int (s
);
508 SCM_ASSERT_TYPE (scm_is_symbol (s
), s
, pos
, func
, "symbol");
530 #ifdef RLIMIT_MEMLOCK
531 if (s
== sym_memlock
)
532 return RLIMIT_MEMLOCK
;
534 #ifdef RLIMIT_MSGQUEUE
535 if (s
== sym_msgqueue
)
536 return RLIMIT_MSGQUEUE
;
544 return RLIMIT_NOFILE
;
556 return RLIMIT_RTPRIO
;
560 return RLIMIT_RTPRIO
;
562 #ifdef RLIMIT_SIGPENDING
563 if (s
== sym_sigpending
)
564 return RLIMIT_SIGPENDING
;
571 scm_misc_error (func
, "invalid rlimit resource ~A", scm_list_1 (s
));
575 SCM_DEFINE (scm_getrlimit
, "getrlimit", 1, 0, 0,
577 "Get a resource limit for this process. @var{resource} identifies the resource,\n"
578 "either as an integer or as a symbol. For example, @code{(getrlimit 'stack)}\n"
579 "gets the limits associated with @code{RLIMIT_STACK}.\n\n"
580 "@code{getrlimit} returns two values, the soft and the hard limit. If no\n"
581 "limit is set for the resource in question, the returned limit will be @code{#f}.")
582 #define FUNC_NAME s_scm_getrlimit
585 struct rlimit lim
= { 0, 0 };
587 iresource
= scm_to_resource (resource
, FUNC_NAME
, 1);
589 if (getrlimit (iresource
, &lim
) != 0)
590 scm_syserror (FUNC_NAME
);
592 return scm_values (scm_list_2 ((lim
.rlim_cur
== RLIM_INFINITY
) ? SCM_BOOL_F
593 : scm_from_long (lim
.rlim_cur
),
594 (lim
.rlim_max
== RLIM_INFINITY
) ? SCM_BOOL_F
595 : scm_from_long (lim
.rlim_max
)));
600 #ifdef HAVE_SETRLIMIT
601 SCM_DEFINE (scm_setrlimit
, "setrlimit", 3, 0, 0,
602 (SCM resource
, SCM soft
, SCM hard
),
603 "Set a resource limit for this process. @var{resource} identifies the resource,\n"
604 "either as an integer or as a symbol. @var{soft} and @var{hard} should be integers,\n"
605 "or @code{#f} to indicate no limit (i.e., @code{RLIM_INFINITY}).\n\n"
606 "For example, @code{(setrlimit 'stack 150000 300000)} sets the @code{RLIMIT_STACK}\n"
607 "limit to 150 kilobytes, with a hard limit of 300 kB.")
608 #define FUNC_NAME s_scm_setrlimit
611 struct rlimit lim
= { 0, 0 };
613 iresource
= scm_to_resource (resource
, FUNC_NAME
, 1);
615 lim
.rlim_cur
= (soft
== SCM_BOOL_F
) ? RLIM_INFINITY
: scm_to_long (soft
);
616 lim
.rlim_max
= (hard
== SCM_BOOL_F
) ? RLIM_INFINITY
: scm_to_long (hard
);
618 if (setrlimit (iresource
, &lim
) != 0)
619 scm_syserror (FUNC_NAME
);
621 return SCM_UNSPECIFIED
;
624 #endif /* HAVE_SETRLIMIT */
625 #endif /* HAVE_GETRLIMIT */
628 SCM_DEFINE (scm_kill
, "kill", 2, 0, 0,
630 "Sends a signal to the specified process or group of processes.\n\n"
631 "@var{pid} specifies the processes to which the signal is sent:\n\n"
633 "@item @var{pid} greater than 0\n"
634 "The process whose identifier is @var{pid}.\n"
635 "@item @var{pid} equal to 0\n"
636 "All processes in the current process group.\n"
637 "@item @var{pid} less than -1\n"
638 "The process group whose identifier is -@var{pid}\n"
639 "@item @var{pid} equal to -1\n"
640 "If the process is privileged, all processes except for some special\n"
641 "system processes. Otherwise, all processes with the current effective\n"
644 "@var{sig} should be specified using a variable corresponding to\n"
645 "the Unix symbolic name, e.g.,\n\n"
650 "Interrupt signal.\n"
652 #define FUNC_NAME s_scm_kill
654 /* Signal values are interned in scm_init_posix(). */
656 if (kill (scm_to_int (pid
), scm_to_int (sig
)) != 0)
659 /* Mingw has raise(), but not kill(). (Other raw DOS environments might
660 be similar.) Use raise() when the requested pid is our own process,
662 if (scm_to_int (pid
) == getpid ())
664 if (raise (scm_to_int (sig
)) != 0)
676 return SCM_UNSPECIFIED
;
681 SCM_DEFINE (scm_waitpid
, "waitpid", 1, 1, 0,
682 (SCM pid
, SCM options
),
683 "This procedure collects status information from a child process which\n"
684 "has terminated or (optionally) stopped. Normally it will\n"
685 "suspend the calling process until this can be done. If more than one\n"
686 "child process is eligible then one will be chosen by the operating system.\n\n"
687 "The value of @var{pid} determines the behaviour:\n\n"
689 "@item @var{pid} greater than 0\n"
690 "Request status information from the specified child process.\n"
691 "@item @var{pid} equal to -1 or WAIT_ANY\n"
692 "Request status information for any child process.\n"
693 "@item @var{pid} equal to 0 or WAIT_MYPGRP\n"
694 "Request status information for any child process in the current process\n"
696 "@item @var{pid} less than -1\n"
697 "Request status information for any child process whose process group ID\n"
700 "The @var{options} argument, if supplied, should be the bitwise OR of the\n"
701 "values of zero or more of the following variables:\n\n"
703 "Return immediately even if there are no child processes to be collected.\n"
705 "@defvar WUNTRACED\n"
706 "Report status information for stopped processes as well as terminated\n"
709 "The return value is a pair containing:\n\n"
712 "The process ID of the child process, or 0 if @code{WNOHANG} was\n"
713 "specified and no process was collected.\n"
715 "The integer status value.\n"
717 #define FUNC_NAME s_scm_waitpid
722 if (SCM_UNBNDP (options
))
726 /* Flags are interned in scm_init_posix. */
727 ioptions
= scm_to_int (options
);
729 SCM_SYSCALL (i
= waitpid (scm_to_int (pid
), &status
, ioptions
));
732 return scm_cons (scm_from_int (i
), scm_from_int (status
));
735 #endif /* HAVE_WAITPID */
738 SCM_DEFINE (scm_status_exit_val
, "status:exit-val", 1, 0, 0,
740 "Return the exit status value, as would be set if a process\n"
741 "ended normally through a call to @code{exit} or @code{_exit},\n"
742 "if any, otherwise @code{#f}.")
743 #define FUNC_NAME s_scm_status_exit_val
747 /* On Ultrix, the WIF... macros assume their argument is an lvalue;
749 lstatus
= scm_to_int (status
);
750 if (WIFEXITED (lstatus
))
751 return (scm_from_int (WEXITSTATUS (lstatus
)));
757 SCM_DEFINE (scm_status_term_sig
, "status:term-sig", 1, 0, 0,
759 "Return the signal number which terminated the process, if any,\n"
760 "otherwise @code{#f}.")
761 #define FUNC_NAME s_scm_status_term_sig
765 lstatus
= scm_to_int (status
);
766 if (WIFSIGNALED (lstatus
))
767 return scm_from_int (WTERMSIG (lstatus
));
773 SCM_DEFINE (scm_status_stop_sig
, "status:stop-sig", 1, 0, 0,
775 "Return the signal number which stopped the process, if any,\n"
776 "otherwise @code{#f}.")
777 #define FUNC_NAME s_scm_status_stop_sig
781 lstatus
= scm_to_int (status
);
782 if (WIFSTOPPED (lstatus
))
783 return scm_from_int (WSTOPSIG (lstatus
));
788 #endif /* __MINGW32__ */
791 SCM_DEFINE (scm_getppid
, "getppid", 0, 0, 0,
793 "Return an integer representing the process ID of the parent\n"
795 #define FUNC_NAME s_scm_getppid
797 return scm_from_int (getppid ());
800 #endif /* HAVE_GETPPID */
804 SCM_DEFINE (scm_getuid
, "getuid", 0, 0, 0,
806 "Return an integer representing the current real user ID.")
807 #define FUNC_NAME s_scm_getuid
809 return scm_from_int (getuid ());
815 SCM_DEFINE (scm_getgid
, "getgid", 0, 0, 0,
817 "Return an integer representing the current real group ID.")
818 #define FUNC_NAME s_scm_getgid
820 return scm_from_int (getgid ());
826 SCM_DEFINE (scm_geteuid
, "geteuid", 0, 0, 0,
828 "Return an integer representing the current effective user ID.\n"
829 "If the system does not support effective IDs, then the real ID\n"
830 "is returned. @code{(provided? 'EIDs)} reports whether the\n"
831 "system supports effective IDs.")
832 #define FUNC_NAME s_scm_geteuid
835 return scm_from_int (geteuid ());
837 return scm_from_int (getuid ());
843 SCM_DEFINE (scm_getegid
, "getegid", 0, 0, 0,
845 "Return an integer representing the current effective group ID.\n"
846 "If the system does not support effective IDs, then the real ID\n"
847 "is returned. @code{(provided? 'EIDs)} reports whether the\n"
848 "system supports effective IDs.")
849 #define FUNC_NAME s_scm_getegid
852 return scm_from_int (getegid ());
854 return scm_from_int (getgid ());
860 SCM_DEFINE (scm_setuid
, "setuid", 1, 0, 0,
862 "Sets both the real and effective user IDs to the integer @var{id}, provided\n"
863 "the process has appropriate privileges.\n"
864 "The return value is unspecified.")
865 #define FUNC_NAME s_scm_setuid
867 if (setuid (scm_to_int (id
)) != 0)
869 return SCM_UNSPECIFIED
;
873 SCM_DEFINE (scm_setgid
, "setgid", 1, 0, 0,
875 "Sets both the real and effective group IDs to the integer @var{id}, provided\n"
876 "the process has appropriate privileges.\n"
877 "The return value is unspecified.")
878 #define FUNC_NAME s_scm_setgid
880 if (setgid (scm_to_int (id
)) != 0)
882 return SCM_UNSPECIFIED
;
886 SCM_DEFINE (scm_seteuid
, "seteuid", 1, 0, 0,
888 "Sets the effective user ID to the integer @var{id}, provided the process\n"
889 "has appropriate privileges. If effective IDs are not supported, the\n"
890 "real ID is set instead -- @code{(provided? 'EIDs)} reports whether the\n"
891 "system supports effective IDs.\n"
892 "The return value is unspecified.")
893 #define FUNC_NAME s_scm_seteuid
898 rv
= seteuid (scm_to_int (id
));
900 rv
= setuid (scm_to_int (id
));
904 return SCM_UNSPECIFIED
;
907 #endif /* __MINGW32__ */
911 SCM_DEFINE (scm_setegid
, "setegid", 1, 0, 0,
913 "Sets the effective group ID to the integer @var{id}, provided the process\n"
914 "has appropriate privileges. If effective IDs are not supported, the\n"
915 "real ID is set instead -- @code{(provided? 'EIDs)} reports whether the\n"
916 "system supports effective IDs.\n"
917 "The return value is unspecified.")
918 #define FUNC_NAME s_scm_setegid
923 rv
= setegid (scm_to_int (id
));
925 rv
= setgid (scm_to_int (id
));
929 return SCM_UNSPECIFIED
;
937 SCM_DEFINE (scm_getpgrp
, "getpgrp", 0, 0, 0,
939 "Return an integer representing the current process group ID.\n"
940 "This is the POSIX definition, not BSD.")
941 #define FUNC_NAME s_scm_getpgrp
944 fn
= (int (*) ()) getpgrp
;
945 return scm_from_int (fn (0));
948 #endif /* HAVE_GETPGRP */
952 SCM_DEFINE (scm_setpgid
, "setpgid", 2, 0, 0,
954 "Move the process @var{pid} into the process group @var{pgid}. @var{pid} or\n"
955 "@var{pgid} must be integers: they can be zero to indicate the ID of the\n"
957 "Fails on systems that do not support job control.\n"
958 "The return value is unspecified.")
959 #define FUNC_NAME s_scm_setpgid
961 /* FIXME(?): may be known as setpgrp. */
962 if (setpgid (scm_to_int (pid
), scm_to_int (pgid
)) != 0)
964 return SCM_UNSPECIFIED
;
967 #endif /* HAVE_SETPGID */
970 SCM_DEFINE (scm_setsid
, "setsid", 0, 0, 0,
972 "Creates a new session. The current process becomes the session leader\n"
973 "and is put in a new process group. The process will be detached\n"
974 "from its controlling terminal if it has one.\n"
975 "The return value is an integer representing the new process group ID.")
976 #define FUNC_NAME s_scm_setsid
978 pid_t sid
= setsid ();
981 return SCM_UNSPECIFIED
;
984 #endif /* HAVE_SETSID */
987 /* ttyname returns its result in a single static buffer, hence
988 scm_i_misc_mutex for thread safety. In glibc 2.3.2 two threads
989 continuously calling ttyname will otherwise get an overwrite quite
992 ttyname_r (when available) could be used instead of scm_i_misc_mutex, but
993 there's probably little to be gained in either speed or parallelism. */
996 SCM_DEFINE (scm_ttyname
, "ttyname", 1, 0, 0,
998 "Return a string with the name of the serial terminal device\n"
999 "underlying @var{port}.")
1000 #define FUNC_NAME s_scm_ttyname
1004 SCM ret
= SCM_BOOL_F
;
1006 port
= SCM_COERCE_OUTPORT (port
);
1007 SCM_VALIDATE_OPPORT (1, port
);
1008 if (!SCM_FPORTP (port
))
1010 fd
= SCM_FPORT_FDES (port
);
1012 scm_i_scm_pthread_mutex_lock (&scm_i_misc_mutex
);
1014 SCM_SYSCALL (result
= ttyname (fd
));
1017 result
= strdup (result
);
1019 scm_i_pthread_mutex_unlock (&scm_i_misc_mutex
);
1027 ret
= scm_take_locale_string (result
);
1032 #endif /* HAVE_TTYNAME */
1035 /* For thread safety "buf" is used instead of NULL for the ctermid static
1036 buffer. Actually it's unlikely the controlling terminal will change
1037 during program execution, and indeed on glibc (2.3.2) it's always just
1038 "/dev/tty", but L_ctermid on the stack is easy and fast and guarantees
1039 safety everywhere. */
1041 SCM_DEFINE (scm_ctermid
, "ctermid", 0, 0, 0,
1043 "Return a string containing the file name of the controlling\n"
1044 "terminal for the current process.")
1045 #define FUNC_NAME s_scm_ctermid
1047 char buf
[L_ctermid
];
1048 char *result
= ctermid (buf
);
1049 if (*result
== '\0')
1051 return scm_from_locale_string (result
);
1054 #endif /* HAVE_CTERMID */
1056 #ifdef HAVE_TCGETPGRP
1057 SCM_DEFINE (scm_tcgetpgrp
, "tcgetpgrp", 1, 0, 0,
1059 "Return the process group ID of the foreground process group\n"
1060 "associated with the terminal open on the file descriptor\n"
1061 "underlying @var{port}.\n"
1063 "If there is no foreground process group, the return value is a\n"
1064 "number greater than 1 that does not match the process group ID\n"
1065 "of any existing process group. This can happen if all of the\n"
1066 "processes in the job that was formerly the foreground job have\n"
1067 "terminated, and no other job has yet been moved into the\n"
1069 #define FUNC_NAME s_scm_tcgetpgrp
1074 port
= SCM_COERCE_OUTPORT (port
);
1076 SCM_VALIDATE_OPFPORT (1, port
);
1077 fd
= SCM_FPORT_FDES (port
);
1078 if ((pgid
= tcgetpgrp (fd
)) == -1)
1080 return scm_from_int (pgid
);
1083 #endif /* HAVE_TCGETPGRP */
1085 #ifdef HAVE_TCSETPGRP
1086 SCM_DEFINE (scm_tcsetpgrp
, "tcsetpgrp", 2, 0, 0,
1087 (SCM port
, SCM pgid
),
1088 "Set the foreground process group ID for the terminal used by the file\n"
1089 "descriptor underlying @var{port} to the integer @var{pgid}.\n"
1090 "The calling process\n"
1091 "must be a member of the same session as @var{pgid} and must have the same\n"
1092 "controlling terminal. The return value is unspecified.")
1093 #define FUNC_NAME s_scm_tcsetpgrp
1097 port
= SCM_COERCE_OUTPORT (port
);
1099 SCM_VALIDATE_OPFPORT (1, port
);
1100 fd
= SCM_FPORT_FDES (port
);
1101 if (tcsetpgrp (fd
, scm_to_int (pgid
)) == -1)
1103 return SCM_UNSPECIFIED
;
1106 #endif /* HAVE_TCSETPGRP */
1108 SCM_DEFINE (scm_execl
, "execl", 1, 0, 1,
1109 (SCM filename
, SCM args
),
1110 "Executes the file named by @var{path} as a new process image.\n"
1111 "The remaining arguments are supplied to the process; from a C program\n"
1112 "they are accessible as the @code{argv} argument to @code{main}.\n"
1113 "Conventionally the first @var{arg} is the same as @var{path}.\n"
1114 "All arguments must be strings.\n\n"
1115 "If @var{arg} is missing, @var{path} is executed with a null\n"
1116 "argument list, which may have system-dependent side-effects.\n\n"
1117 "This procedure is currently implemented using the @code{execv} system\n"
1118 "call, but we call it @code{execl} because of its Scheme calling interface.")
1119 #define FUNC_NAME s_scm_execl
1124 scm_dynwind_begin (0);
1126 exec_file
= scm_to_locale_string (filename
);
1127 scm_dynwind_free (exec_file
);
1129 exec_argv
= scm_i_allocate_string_pointers (args
);
1133 /* extra "const" in mingw formals, provokes warning from gcc */
1134 (const char * const *)
1145 SCM_DEFINE (scm_execlp
, "execlp", 1, 0, 1,
1146 (SCM filename
, SCM args
),
1147 "Similar to @code{execl}, however if\n"
1148 "@var{filename} does not contain a slash\n"
1149 "then the file to execute will be located by searching the\n"
1150 "directories listed in the @code{PATH} environment variable.\n\n"
1151 "This procedure is currently implemented using the @code{execvp} system\n"
1152 "call, but we call it @code{execlp} because of its Scheme calling interface.")
1153 #define FUNC_NAME s_scm_execlp
1158 scm_dynwind_begin (0);
1160 exec_file
= scm_to_locale_string (filename
);
1161 scm_dynwind_free (exec_file
);
1163 exec_argv
= scm_i_allocate_string_pointers (args
);
1167 /* extra "const" in mingw formals, provokes warning from gcc */
1168 (const char * const *)
1180 /* OPTIMIZE-ME: scm_execle doesn't need malloced copies of the environment
1181 list strings the way environ_list_to_c gives. */
1183 SCM_DEFINE (scm_execle
, "execle", 2, 0, 1,
1184 (SCM filename
, SCM env
, SCM args
),
1185 "Similar to @code{execl}, but the environment of the new process is\n"
1186 "specified by @var{env}, which must be a list of strings as returned by the\n"
1187 "@code{environ} procedure.\n\n"
1188 "This procedure is currently implemented using the @code{execve} system\n"
1189 "call, but we call it @code{execle} because of its Scheme calling interface.")
1190 #define FUNC_NAME s_scm_execle
1196 scm_dynwind_begin (0);
1198 exec_file
= scm_to_locale_string (filename
);
1199 scm_dynwind_free (exec_file
);
1201 exec_argv
= scm_i_allocate_string_pointers (args
);
1202 exec_env
= scm_i_allocate_string_pointers (env
);
1206 /* extra "const" in mingw formals, provokes warning from gcc */
1207 (const char * const *)
1211 /* extra "const" in mingw formals, provokes warning from gcc */
1212 (const char * const *)
1224 SCM_DEFINE (scm_fork
, "primitive-fork", 0, 0, 0,
1226 "Creates a new \"child\" process by duplicating the current \"parent\" process.\n"
1227 "In the child the return value is 0. In the parent the return value is\n"
1228 "the integer process ID of the child.\n\n"
1229 "This procedure has been renamed from @code{fork} to avoid a naming conflict\n"
1230 "with the scsh fork.")
1231 #define FUNC_NAME s_scm_fork
1237 return scm_from_int (pid
);
1240 #endif /* HAVE_FORK */
1243 # include "win32-uname.h"
1246 #if defined (HAVE_UNAME) || defined (__MINGW32__)
1247 SCM_DEFINE (scm_uname
, "uname", 0, 0, 0,
1249 "Return an object with some information about the computer\n"
1250 "system the program is running on.")
1251 #define FUNC_NAME s_scm_uname
1254 SCM result
= scm_c_make_vector (5, SCM_UNSPECIFIED
);
1255 if (uname (&buf
) < 0)
1257 SCM_SIMPLE_VECTOR_SET(result
, 0, scm_from_locale_string (buf
.sysname
));
1258 SCM_SIMPLE_VECTOR_SET(result
, 1, scm_from_locale_string (buf
.nodename
));
1259 SCM_SIMPLE_VECTOR_SET(result
, 2, scm_from_locale_string (buf
.release
));
1260 SCM_SIMPLE_VECTOR_SET(result
, 3, scm_from_locale_string (buf
.version
));
1261 SCM_SIMPLE_VECTOR_SET(result
, 4, scm_from_locale_string (buf
.machine
));
1264 SCM_SIMPLE_VECTOR_SET(result, 5, scm_from_locale_string (buf.domainname));
1269 #endif /* HAVE_UNAME */
1271 SCM_DEFINE (scm_environ
, "environ", 0, 1, 0,
1273 "If @var{env} is omitted, return the current environment (in the\n"
1274 "Unix sense) as a list of strings. Otherwise set the current\n"
1275 "environment, which is also the default environment for child\n"
1276 "processes, to the supplied list of strings. Each member of\n"
1277 "@var{env} should be of the form @code{NAME=VALUE} and values of\n"
1278 "@code{NAME} should not be duplicated. If @var{env} is supplied\n"
1279 "then the return value is unspecified.")
1280 #define FUNC_NAME s_scm_environ
1282 if (SCM_UNBNDP (env
))
1283 return scm_makfromstrs (-1, environ
);
1286 environ
= scm_i_allocate_string_pointers (env
);
1287 return SCM_UNSPECIFIED
;
1294 SCM_DEFINE (scm_tmpnam
, "tmpnam", 0, 0, 0,
1296 "Return a name in the file system that does not match any\n"
1297 "existing file. However there is no guarantee that another\n"
1298 "process will not create the file after @code{tmpnam} is called.\n"
1299 "Care should be taken if opening the file, e.g., use the\n"
1300 "@code{O_EXCL} open flag or use @code{mkstemp!} instead.")
1301 #define FUNC_NAME s_scm_tmpnam
1303 char name
[L_tmpnam
];
1306 SCM_SYSCALL (rv
= tmpnam (name
));
1308 /* not SCM_SYSERROR since errno probably not set. */
1309 SCM_MISC_ERROR ("tmpnam failed", SCM_EOL
);
1310 return scm_from_locale_string (name
);
1316 #ifndef HAVE_MKSTEMP
1317 extern int mkstemp (char *);
1320 SCM_DEFINE (scm_mkstemp
, "mkstemp!", 1, 0, 0,
1322 "Create a new unique file in the file system and return a new\n"
1323 "buffered port open for reading and writing to the file.\n"
1325 "@var{tmpl} is a string specifying where the file should be\n"
1326 "created: it must end with @samp{XXXXXX} and those @samp{X}s\n"
1327 "will be changed in the string to return the name of the file.\n"
1328 "(@code{port-filename} on the port also gives the name.)\n"
1330 "POSIX doesn't specify the permissions mode of the file, on GNU\n"
1331 "and most systems it's @code{#o600}. An application can use\n"
1332 "@code{chmod} to relax that if desired. For example\n"
1333 "@code{#o666} less @code{umask}, which is usual for ordinary\n"
1337 "(let ((port (mkstemp! (string-copy \"/tmp/myfile-XXXXXX\"))))\n"
1338 " (chmod port (logand #o666 (lognot (umask))))\n"
1341 #define FUNC_NAME s_scm_mkstemp
1346 scm_dynwind_begin (0);
1348 c_tmpl
= scm_to_locale_string (tmpl
);
1349 scm_dynwind_free (c_tmpl
);
1351 SCM_SYSCALL (rv
= mkstemp (c_tmpl
));
1355 scm_substring_move_x (scm_from_locale_string (c_tmpl
),
1356 SCM_INUM0
, scm_string_length (tmpl
),
1360 return scm_fdes_to_port (rv
, "w+", tmpl
);
1364 SCM_DEFINE (scm_utime
, "utime", 1, 2, 0,
1365 (SCM pathname
, SCM actime
, SCM modtime
),
1366 "@code{utime} sets the access and modification times for the\n"
1367 "file named by @var{path}. If @var{actime} or @var{modtime} is\n"
1368 "not supplied, then the current time is used. @var{actime} and\n"
1369 "@var{modtime} must be integer time values as returned by the\n"
1370 "@code{current-time} procedure.\n"
1372 "(utime \"foo\" (- (current-time) 3600))\n"
1374 "will set the access time to one hour in the past and the\n"
1375 "modification time to the current time.")
1376 #define FUNC_NAME s_scm_utime
1379 struct utimbuf utm_tmp
;
1381 if (SCM_UNBNDP (actime
))
1382 SCM_SYSCALL (time (&utm_tmp
.actime
));
1384 utm_tmp
.actime
= SCM_NUM2ULONG (2, actime
);
1386 if (SCM_UNBNDP (modtime
))
1387 SCM_SYSCALL (time (&utm_tmp
.modtime
));
1389 utm_tmp
.modtime
= SCM_NUM2ULONG (3, modtime
);
1391 STRING_SYSCALL (pathname
, c_pathname
,
1392 rv
= utime (c_pathname
, &utm_tmp
));
1395 return SCM_UNSPECIFIED
;
1399 SCM_DEFINE (scm_access
, "access?", 2, 0, 0,
1400 (SCM path
, SCM how
),
1401 "Test accessibility of a file under the real UID and GID of the\n"
1402 "calling process. The return is @code{#t} if @var{path} exists\n"
1403 "and the permissions requested by @var{how} are all allowed, or\n"
1404 "@code{#f} if not.\n"
1406 "@var{how} is an integer which is one of the following values,\n"
1407 "or a bitwise-OR (@code{logior}) of multiple values.\n"
1410 "Test for read permission.\n"
1413 "Test for write permission.\n"
1416 "Test for execute permission.\n"
1419 "Test for existence of the file. This is implied by each of the\n"
1420 "other tests, so there's no need to combine it with them.\n"
1423 "It's important to note that @code{access?} does not simply\n"
1424 "indicate what will happen on attempting to read or write a\n"
1425 "file. In normal circumstances it does, but in a set-UID or\n"
1426 "set-GID program it doesn't because @code{access?} tests the\n"
1427 "real ID, whereas an open or execute attempt uses the effective\n"
1430 "A program which will never run set-UID/GID can ignore the\n"
1431 "difference between real and effective IDs, but for maximum\n"
1432 "generality, especially in library functions, it's best not to\n"
1433 "use @code{access?} to predict the result of an open or execute,\n"
1434 "instead simply attempt that and catch any exception.\n"
1436 "The main use for @code{access?} is to let a set-UID/GID program\n"
1437 "determine what the invoking user would have been allowed to do,\n"
1438 "without the greater (or perhaps lesser) privileges afforded by\n"
1439 "the effective ID. For more on this, see ``Testing File\n"
1440 "Access'' in The GNU C Library Reference Manual.")
1441 #define FUNC_NAME s_scm_access
1445 WITH_STRING (path
, c_path
,
1446 rv
= access (c_path
, scm_to_int (how
)));
1447 return scm_from_bool (!rv
);
1451 SCM_DEFINE (scm_getpid
, "getpid", 0, 0, 0,
1453 "Return an integer representing the current process ID.")
1454 #define FUNC_NAME s_scm_getpid
1456 return scm_from_ulong (getpid ());
1460 SCM_DEFINE (scm_putenv
, "putenv", 1, 0, 0,
1462 "Modifies the environment of the current process, which is\n"
1463 "also the default environment inherited by child processes.\n\n"
1464 "If @var{string} is of the form @code{NAME=VALUE} then it will be written\n"
1465 "directly into the environment, replacing any existing environment string\n"
1467 "name matching @code{NAME}. If @var{string} does not contain an equal\n"
1468 "sign, then any existing string with name matching @var{string} will\n"
1470 "The return value is unspecified.")
1471 #define FUNC_NAME s_scm_putenv
1474 char *c_str
= scm_to_locale_string (str
);
1476 /* Leave C_STR in the environment. */
1478 /* Gnulib's `putenv' module honors the semantics described above. */
1479 rv
= putenv (c_str
);
1483 return SCM_UNSPECIFIED
;
1487 /* This mutex is used to serialize invocations of `setlocale ()' on non-GNU
1488 systems (i.e., systems where a reentrant locale API is not available). It
1489 is also acquired before calls to `nl_langinfo ()'. See `i18n.c' for
1491 scm_i_pthread_mutex_t scm_i_locale_mutex
= SCM_I_PTHREAD_MUTEX_INITIALIZER
;
1493 #ifdef HAVE_SETLOCALE
1495 SCM_DEFINE (scm_setlocale
, "setlocale", 1, 1, 0,
1496 (SCM category
, SCM locale
),
1497 "If @var{locale} is omitted, return the current value of the\n"
1498 "specified locale category as a system-dependent string.\n"
1499 "@var{category} should be specified using the values\n"
1500 "@code{LC_COLLATE}, @code{LC_ALL} etc.\n"
1502 "Otherwise the specified locale category is set to the string\n"
1503 "@var{locale} and the new value is returned as a\n"
1504 "system-dependent string. If @var{locale} is an empty string,\n"
1505 "the locale will be set using environment variables.\n"
1507 "When the locale is changed, the character encoding of the new\n"
1508 "locale (UTF-8, ISO-8859-1, etc.) is used for the current\n"
1509 "input, output, and error ports\n")
1510 #define FUNC_NAME s_scm_setlocale
1517 scm_dynwind_begin (0);
1519 if (SCM_UNBNDP (locale
))
1525 clocale
= scm_to_locale_string (locale
);
1526 scm_dynwind_free (clocale
);
1529 c_category
= scm_i_to_lc_category (category
, 1);
1531 scm_i_pthread_mutex_lock (&scm_i_locale_mutex
);
1532 rv
= setlocale (c_category
, clocale
);
1533 scm_i_pthread_mutex_unlock (&scm_i_locale_mutex
);
1537 /* POSIX and C99 don't say anything about setlocale setting errno, so
1538 force a sensible value here. glibc leaves ENOENT, which would be
1539 fine, but it's not a documented feature. */
1544 enc
= locale_charset ();
1545 /* Set the default encoding for new ports. */
1546 scm_i_set_port_encoding_x (SCM_BOOL_F
, enc
);
1547 /* Set the encoding for the stdio ports. */
1548 scm_i_set_port_encoding_x (scm_current_input_port (), enc
);
1549 scm_i_set_port_encoding_x (scm_current_output_port (), enc
);
1550 scm_i_set_port_encoding_x (scm_current_error_port (), enc
);
1553 return scm_from_locale_string (rv
);
1556 #endif /* HAVE_SETLOCALE */
1559 SCM_DEFINE (scm_mknod
, "mknod", 4, 0, 0,
1560 (SCM path
, SCM type
, SCM perms
, SCM dev
),
1561 "Creates a new special file, such as a file corresponding to a device.\n"
1562 "@var{path} specifies the name of the file. @var{type} should\n"
1563 "be one of the following symbols:\n"
1564 "regular, directory, symlink, block-special, char-special,\n"
1565 "fifo, or socket. @var{perms} (an integer) specifies the file permissions.\n"
1566 "@var{dev} (an integer) specifies which device the special file refers\n"
1567 "to. Its exact interpretation depends on the kind of special file\n"
1568 "being created.\n\n"
1571 "(mknod \"/dev/fd0\" 'block-special #o660 (+ (* 2 256) 2))\n"
1573 "The return value is unspecified.")
1574 #define FUNC_NAME s_scm_mknod
1580 SCM_VALIDATE_STRING (1, path
);
1581 SCM_VALIDATE_SYMBOL (2, type
);
1583 p
= scm_i_symbol_chars (type
);
1584 if (strcmp (p
, "regular") == 0)
1586 else if (strcmp (p
, "directory") == 0)
1589 /* systems without symlinks probably don't have S_IFLNK defined */
1590 else if (strcmp (p
, "symlink") == 0)
1593 else if (strcmp (p
, "block-special") == 0)
1595 else if (strcmp (p
, "char-special") == 0)
1597 else if (strcmp (p
, "fifo") == 0)
1600 else if (strcmp (p
, "socket") == 0)
1604 SCM_OUT_OF_RANGE (2, type
);
1606 STRING_SYSCALL (path
, c_path
,
1607 val
= mknod (c_path
,
1608 ctype
| scm_to_int (perms
),
1612 return SCM_UNSPECIFIED
;
1615 #endif /* HAVE_MKNOD */
1618 SCM_DEFINE (scm_nice
, "nice", 1, 0, 0,
1620 "Increment the priority of the current process by @var{incr}. A higher\n"
1621 "priority value means that the process runs less often.\n"
1622 "The return value is unspecified.")
1623 #define FUNC_NAME s_scm_nice
1627 /* nice() returns "prio-NZERO" on success or -1 on error, but -1 can arise
1628 from "prio-NZERO", so an error must be detected from errno changed */
1630 nice_value
= nice (scm_to_int (incr
));
1634 return SCM_UNSPECIFIED
;
1637 #endif /* HAVE_NICE */
1640 SCM_DEFINE (scm_sync
, "sync", 0, 0, 0,
1642 "Flush the operating system disk buffers.\n"
1643 "The return value is unspecified.")
1644 #define FUNC_NAME s_scm_sync
1647 return SCM_UNSPECIFIED
;
1650 #endif /* HAVE_SYNC */
1653 /* crypt() returns a pointer to a static buffer, so we use scm_i_misc_mutex
1654 to avoid another thread overwriting it. A test program running crypt
1655 continuously in two threads can be quickly seen tripping this problem.
1656 crypt() is pretty slow normally, so a mutex shouldn't add much overhead.
1658 glibc has a thread-safe crypt_r, but (in version 2.3.2) it runs a lot
1659 slower (about 5x) than plain crypt if you pass an uninitialized data
1660 block each time. Presumably there's some one-time setups. The best way
1661 to use crypt_r for parallel execution in multiple threads would probably
1662 be to maintain a little pool of initialized crypt_data structures, take
1663 one and use it, then return it to the pool. That pool could be garbage
1664 collected so it didn't add permanently to memory use if only a few crypt
1665 calls are made. But we expect crypt will be used rarely, and even more
1666 rarely will there be any desire for lots of parallel execution on
1667 multiple cpus. So for now we don't bother with anything fancy, just
1671 SCM_DEFINE (scm_crypt
, "crypt", 2, 0, 0,
1672 (SCM key
, SCM salt
),
1673 "Encrypt @var{key} using @var{salt} as the salt value to the\n"
1674 "crypt(3) library call.")
1675 #define FUNC_NAME s_scm_crypt
1678 char *c_key
, *c_salt
, *c_ret
;
1680 scm_dynwind_begin (0);
1681 scm_i_dynwind_pthread_mutex_lock (&scm_i_misc_mutex
);
1683 c_key
= scm_to_locale_string (key
);
1684 scm_dynwind_free (c_key
);
1685 c_salt
= scm_to_locale_string (salt
);
1686 scm_dynwind_free (c_salt
);
1688 /* The Linux crypt(3) man page says crypt will return NULL and set errno
1689 on error. (Eg. ENOSYS if legal restrictions mean it cannot be
1691 c_ret
= crypt (c_key
, c_salt
);
1695 ret
= scm_from_locale_string (c_ret
);
1700 #endif /* HAVE_CRYPT */
1703 SCM_DEFINE (scm_chroot
, "chroot", 1, 0, 0,
1705 "Change the root directory to that specified in @var{path}.\n"
1706 "This directory will be used for path names beginning with\n"
1707 "@file{/}. The root directory is inherited by all children\n"
1708 "of the current process. Only the superuser may change the\n"
1710 #define FUNC_NAME s_scm_chroot
1714 WITH_STRING (path
, c_path
,
1715 rv
= chroot (c_path
));
1718 return SCM_UNSPECIFIED
;
1721 #endif /* HAVE_CHROOT */
1725 /* Wrapper function to supplying `getlogin()' under Windows. */
1726 static char * getlogin (void)
1728 static char user
[256];
1729 static unsigned long len
= 256;
1731 if (!GetUserName (user
, &len
))
1735 #endif /* __MINGW32__ */
1738 #if defined (HAVE_GETLOGIN) || defined (__MINGW32__)
1739 SCM_DEFINE (scm_getlogin
, "getlogin", 0, 0, 0,
1741 "Return a string containing the name of the user logged in on\n"
1742 "the controlling terminal of the process, or @code{#f} if this\n"
1743 "information cannot be obtained.")
1744 #define FUNC_NAME s_scm_getlogin
1751 return scm_from_locale_string (p
);
1754 #endif /* HAVE_GETLOGIN */
1758 # if !HAVE_DECL_CUSERID
1759 extern char *cuserid (char *);
1762 SCM_DEFINE (scm_cuserid
, "cuserid", 0, 0, 0,
1764 "Return a string containing a user name associated with the\n"
1765 "effective user id of the process. Return @code{#f} if this\n"
1766 "information cannot be obtained.")
1767 #define FUNC_NAME s_scm_cuserid
1769 char buf
[L_cuserid
];
1775 return scm_from_locale_string (p
);
1778 #endif /* HAVE_CUSERID */
1780 #if HAVE_GETPRIORITY
1781 SCM_DEFINE (scm_getpriority
, "getpriority", 2, 0, 0,
1782 (SCM which
, SCM who
),
1783 "Return the scheduling priority of the process, process group\n"
1784 "or user, as indicated by @var{which} and @var{who}. @var{which}\n"
1785 "is one of the variables @code{PRIO_PROCESS}, @code{PRIO_PGRP}\n"
1786 "or @code{PRIO_USER}, and @var{who} is interpreted relative to\n"
1787 "@var{which} (a process identifier for @code{PRIO_PROCESS},\n"
1788 "process group identifier for @code{PRIO_PGRP}, and a user\n"
1789 "identifier for @code{PRIO_USER}. A zero value of @var{who}\n"
1790 "denotes the current process, process group, or user. Return\n"
1791 "the highest priority (lowest numerical value) of any of the\n"
1792 "specified processes.")
1793 #define FUNC_NAME s_scm_getpriority
1795 int cwhich
, cwho
, ret
;
1797 cwhich
= scm_to_int (which
);
1798 cwho
= scm_to_int (who
);
1800 /* We have to clear errno and examine it later, because -1 is a
1801 legal return value for getpriority(). */
1803 ret
= getpriority (cwhich
, cwho
);
1806 return scm_from_int (ret
);
1809 #endif /* HAVE_GETPRIORITY */
1811 #if HAVE_SETPRIORITY
1812 SCM_DEFINE (scm_setpriority
, "setpriority", 3, 0, 0,
1813 (SCM which
, SCM who
, SCM prio
),
1814 "Set the scheduling priority of the process, process group\n"
1815 "or user, as indicated by @var{which} and @var{who}. @var{which}\n"
1816 "is one of the variables @code{PRIO_PROCESS}, @code{PRIO_PGRP}\n"
1817 "or @code{PRIO_USER}, and @var{who} is interpreted relative to\n"
1818 "@var{which} (a process identifier for @code{PRIO_PROCESS},\n"
1819 "process group identifier for @code{PRIO_PGRP}, and a user\n"
1820 "identifier for @code{PRIO_USER}. A zero value of @var{who}\n"
1821 "denotes the current process, process group, or user.\n"
1822 "@var{prio} is a value in the range -20 and 20, the default\n"
1823 "priority is 0; lower priorities cause more favorable\n"
1824 "scheduling. Sets the priority of all of the specified\n"
1825 "processes. Only the super-user may lower priorities.\n"
1826 "The return value is not specified.")
1827 #define FUNC_NAME s_scm_setpriority
1829 int cwhich
, cwho
, cprio
;
1831 cwhich
= scm_to_int (which
);
1832 cwho
= scm_to_int (who
);
1833 cprio
= scm_to_int (prio
);
1835 if (setpriority (cwhich
, cwho
, cprio
) == -1)
1837 return SCM_UNSPECIFIED
;
1840 #endif /* HAVE_SETPRIORITY */
1843 SCM_DEFINE (scm_getpass
, "getpass", 1, 0, 0,
1845 "Display @var{prompt} to the standard error output and read\n"
1846 "a password from @file{/dev/tty}. If this file is not\n"
1847 "accessible, it reads from standard input. The password may be\n"
1848 "up to 127 characters in length. Additional characters and the\n"
1849 "terminating newline character are discarded. While reading\n"
1850 "the password, echoing and the generation of signals by special\n"
1851 "characters is disabled.")
1852 #define FUNC_NAME s_scm_getpass
1857 SCM_VALIDATE_STRING (1, prompt
);
1859 WITH_STRING (prompt
, c_prompt
,
1860 p
= getpass(c_prompt
));
1861 passwd
= scm_from_locale_string (p
);
1863 /* Clear out the password in the static buffer. */
1864 memset (p
, 0, strlen (p
));
1869 #endif /* HAVE_GETPASS */
1871 SCM_DEFINE (scm_flock
, "flock", 2, 0, 0,
1872 (SCM file
, SCM operation
),
1873 "Apply or remove an advisory lock on an open file.\n"
1874 "@var{operation} specifies the action to be done:\n"
1877 "Shared lock. More than one process may hold a shared lock\n"
1878 "for a given file at a given time.\n"
1881 "Exclusive lock. Only one process may hold an exclusive lock\n"
1882 "for a given file at a given time.\n"
1885 "Unlock the file.\n"
1888 "Don't block when locking. This is combined with one of the\n"
1889 "other operations using @code{logior}. If @code{flock} would\n"
1890 "block an @code{EWOULDBLOCK} error is thrown.\n"
1893 "The return value is not specified. @var{file} may be an open\n"
1894 "file descriptor or an open file descriptor port.\n"
1896 "Note that @code{flock} does not lock files across NFS.")
1897 #define FUNC_NAME s_scm_flock
1901 if (scm_is_integer (file
))
1902 fdes
= scm_to_int (file
);
1905 SCM_VALIDATE_OPFPORT (2, file
);
1907 fdes
= SCM_FPORT_FDES (file
);
1909 if (flock (fdes
, scm_to_int (operation
)) == -1)
1911 return SCM_UNSPECIFIED
;
1915 #if HAVE_SETHOSTNAME
1916 SCM_DEFINE (scm_sethostname
, "sethostname", 1, 0, 0,
1918 "Set the host name of the current processor to @var{name}. May\n"
1919 "only be used by the superuser. The return value is not\n"
1921 #define FUNC_NAME s_scm_sethostname
1925 WITH_STRING (name
, c_name
,
1926 rv
= sethostname (c_name
, strlen(c_name
)));
1929 return SCM_UNSPECIFIED
;
1932 #endif /* HAVE_SETHOSTNAME */
1935 #if HAVE_GETHOSTNAME
1936 SCM_DEFINE (scm_gethostname
, "gethostname", 0, 0, 0,
1938 "Return the host name of the current processor.")
1939 #define FUNC_NAME s_scm_gethostname
1941 #ifdef MAXHOSTNAMELEN
1943 /* Various systems define MAXHOSTNAMELEN (including Solaris in fact).
1944 * On GNU/Linux this doesn't include the terminating '\0', hence "+ 1". */
1945 const int len
= MAXHOSTNAMELEN
+ 1;
1946 char *const p
= scm_malloc (len
);
1947 const int res
= gethostname (p
, len
);
1949 scm_dynwind_begin (0);
1950 scm_dynwind_unwind_handler (free
, p
, 0);
1954 /* Default 256 is for Solaris, under Linux ENAMETOOLONG is returned if not
1955 * large enough. SUSv2 specifies 255 maximum too, apparently. */
1960 # if HAVE_SYSCONF && defined (_SC_HOST_NAME_MAX)
1962 /* POSIX specifies the HOST_NAME_MAX system parameter for the max size,
1963 * which may reflect a particular kernel configuration.
1964 * Must watch out for this existing but giving -1, as happens for instance
1965 * in gnu/linux glibc 2.3.2. */
1967 const long int n
= sysconf (_SC_HOST_NAME_MAX
);
1974 p
= scm_malloc (len
);
1976 scm_dynwind_begin (0);
1977 scm_dynwind_unwind_handler (free
, p
, 0);
1979 res
= gethostname (p
, len
);
1980 while (res
== -1 && errno
== ENAMETOOLONG
)
1984 /* scm_realloc may throw an exception. */
1985 p
= scm_realloc (p
, len
);
1986 res
= gethostname (p
, len
);
1993 const int save_errno
= errno
;
1995 /* No guile exceptions can occur before we have freed p's memory. */
2004 /* scm_from_locale_string may throw an exception. */
2005 const SCM name
= scm_from_locale_string (p
);
2007 /* No guile exceptions can occur before we have freed p's memory. */
2015 #endif /* HAVE_GETHOSTNAME */
2021 scm_add_feature ("posix");
2023 scm_add_feature ("EIDs");
2026 scm_c_define ("WAIT_ANY", scm_from_int (WAIT_ANY
));
2029 scm_c_define ("WAIT_MYPGRP", scm_from_int (WAIT_MYPGRP
));
2032 scm_c_define ("WNOHANG", scm_from_int (WNOHANG
));
2035 scm_c_define ("WUNTRACED", scm_from_int (WUNTRACED
));
2038 /* access() symbols. */
2039 scm_c_define ("R_OK", scm_from_int (R_OK
));
2040 scm_c_define ("W_OK", scm_from_int (W_OK
));
2041 scm_c_define ("X_OK", scm_from_int (X_OK
));
2042 scm_c_define ("F_OK", scm_from_int (F_OK
));
2045 scm_c_define ("LC_COLLATE", scm_from_int (LC_COLLATE
));
2048 scm_c_define ("LC_CTYPE", scm_from_int (LC_CTYPE
));
2051 scm_c_define ("LC_MONETARY", scm_from_int (LC_MONETARY
));
2054 scm_c_define ("LC_NUMERIC", scm_from_int (LC_NUMERIC
));
2057 scm_c_define ("LC_TIME", scm_from_int (LC_TIME
));
2060 scm_c_define ("LC_MESSAGES", scm_from_int (LC_MESSAGES
));
2063 scm_c_define ("LC_ALL", scm_from_int (LC_ALL
));
2066 scm_c_define ("LC_PAPER", scm_from_int (LC_PAPER
));
2069 scm_c_define ("LC_NAME", scm_from_int (LC_NAME
));
2072 scm_c_define ("LC_ADDRESS", scm_from_int (LC_ADDRESS
));
2075 scm_c_define ("LC_TELEPHONE", scm_from_int (LC_TELEPHONE
));
2077 #ifdef LC_MEASUREMENT
2078 scm_c_define ("LC_MEASUREMENT", scm_from_int (LC_MEASUREMENT
));
2080 #ifdef LC_IDENTIFICATION
2081 scm_c_define ("LC_IDENTIFICATION", scm_from_int (LC_IDENTIFICATION
));
2084 scm_c_define ("PIPE_BUF", scm_from_long (PIPE_BUF
));
2088 scm_c_define ("PRIO_PROCESS", scm_from_int (PRIO_PROCESS
));
2091 scm_c_define ("PRIO_PGRP", scm_from_int (PRIO_PGRP
));
2094 scm_c_define ("PRIO_USER", scm_from_int (PRIO_USER
));
2098 scm_c_define ("LOCK_SH", scm_from_int (LOCK_SH
));
2101 scm_c_define ("LOCK_EX", scm_from_int (LOCK_EX
));
2104 scm_c_define ("LOCK_UN", scm_from_int (LOCK_UN
));
2107 scm_c_define ("LOCK_NB", scm_from_int (LOCK_NB
));
2110 #include "libguile/cpp_sig_symbols.c"
2111 #include "libguile/posix.x"