1 /* Copyright (C) 1995-2000 Free Software Foundation, Inc.
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
8 * This program 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
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
42 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
53 #include "scm_validate.h"
60 #ifdef TIME_WITH_SYS_TIME
61 # include <sys/time.h>
65 # include <sys/time.h>
75 extern char *ttyname();
79 #ifdef LIBC_H_WITH_UNISTD_H
83 #include <sys/types.h>
90 # include <sys/wait.h>
93 # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
96 # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
101 extern FILE *popen ();
102 extern char ** environ
;
105 #include <sys/utsname.h>
109 # define NAMLEN(dirent) strlen((dirent)->d_name)
111 # define dirent direct
112 # define NAMLEN(dirent) (dirent)->d_namlen
114 # include <sys/ndir.h>
117 # include <sys/dir.h>
124 #ifdef HAVE_SETLOCALE
128 /* Some Unix systems don't define these. CPP hair is dangerous, but
129 this seems safe enough... */
146 /* On NextStep, <utime.h> doesn't define struct utime, unless we
147 #define _POSIX_SOURCE before #including it. I think this is less
148 of a kludge than defining struct utimbuf ourselves. */
149 #ifdef UTIMBUF_NEEDS_POSIX
150 #define _POSIX_SOURCE
153 #ifdef HAVE_SYS_UTIME_H
154 #include <sys/utime.h>
161 /* Please don't add any more #includes or #defines here. The hack
162 above means that _POSIX_SOURCE may be #defined, which will
163 encourage header files to do strange things. */
166 SCM_SYMBOL (sym_read_pipe
, "read pipe");
167 SCM_SYMBOL (sym_write_pipe
, "write pipe");
169 SCM_DEFINE (scm_pipe
, "pipe", 0, 0, 0,
171 "Creates a pipe which can be used for communication. The return value
172 is a pair in which the CAR contains an input port and the CDR an
173 output port. Data written to the output port can be read from the
174 input port. Note that both ports are buffered so it may be necessary
175 to flush the output port before data will actually be sent across the pipe.
176 Alternatively a buffer can be added to the port using @code{setvbuf}
178 #define FUNC_NAME s_scm_pipe
187 p_rd
= scm_fdes_to_port (fd
[0], "r", sym_read_pipe
);
188 p_wt
= scm_fdes_to_port (fd
[1], "w", sym_write_pipe
);
189 return scm_cons (p_rd
, p_wt
);
194 #ifdef HAVE_GETGROUPS
195 SCM_DEFINE (scm_getgroups
, "getgroups", 0, 0, 0,
197 "Returns a vector of integers representing the current supplimentary group IDs.")
198 #define FUNC_NAME s_scm_getgroups
201 int ngroups
= getgroups (0, NULL
);
210 groups
= SCM_MUST_MALLOC_TYPE_NUM(GETGROUPS_T
,ngroups
);
211 val
= getgroups(ngroups
, groups
);
215 scm_must_free((char *)groups
);
219 SCM_SETCHARS(grps
, groups
); /* set up grps as a GC protect */
220 SCM_SETLENGTH(grps
, 0L + ngroups
* sizeof(GETGROUPS_T
), scm_tc7_string
);
221 ans
= scm_make_vector (SCM_MAKINUM(ngroups
), SCM_UNDEFINED
);
222 while (--ngroups
>= 0) SCM_VELTS(ans
)[ngroups
] = SCM_MAKINUM(groups
[ngroups
]);
223 SCM_SETCHARS(grps
, groups
); /* to make sure grps stays around. */
232 SCM_DEFINE (scm_getpwuid
, "getpw", 0, 1, 0,
234 "Look up an entry in the user database. @var{obj} can be an integer,
235 a string, or omitted, giving the behaviour of getpwuid, getpwnam
236 or getpwent respectively.")
237 #define FUNC_NAME s_scm_getpwuid
240 struct passwd
*entry
;
243 result
= scm_make_vector (SCM_MAKINUM (7), SCM_UNSPECIFIED
);
244 ve
= SCM_VELTS (result
);
245 if (SCM_UNBNDP (user
) || SCM_FALSEP (user
))
247 SCM_SYSCALL (entry
= getpwent ());
253 else if (SCM_INUMP (user
))
255 entry
= getpwuid (SCM_INUM (user
));
259 SCM_VALIDATE_ROSTRING (1,user
);
260 if (SCM_SUBSTRP (user
))
261 user
= scm_makfromstr (SCM_ROCHARS (user
), SCM_ROLENGTH (user
), 0);
262 entry
= getpwnam (SCM_ROCHARS (user
));
265 SCM_MISC_ERROR ("entry not found", SCM_EOL
);
267 ve
[0] = scm_makfrom0str (entry
->pw_name
);
268 ve
[1] = scm_makfrom0str (entry
->pw_passwd
);
269 ve
[2] = scm_ulong2num ((unsigned long) entry
->pw_uid
);
270 ve
[3] = scm_ulong2num ((unsigned long) entry
->pw_gid
);
271 ve
[4] = scm_makfrom0str (entry
->pw_gecos
);
273 ve
[5] = scm_makfrom0str ("");
275 ve
[5] = scm_makfrom0str (entry
->pw_dir
);
276 if (!entry
->pw_shell
)
277 ve
[6] = scm_makfrom0str ("");
279 ve
[6] = scm_makfrom0str (entry
->pw_shell
);
286 SCM_DEFINE (scm_setpwent
, "setpw", 0, 1, 0,
288 "If called with a true argument, initialize or reset the password data
289 stream. Otherwise, close the stream. The @code{setpwent} and
290 @code{endpwent} procedures are implemented on top of this.")
291 #define FUNC_NAME s_scm_setpwent
293 if (SCM_UNBNDP (arg
) || SCM_FALSEP (arg
))
297 return SCM_UNSPECIFIED
;
304 /* Combines getgrgid and getgrnam. */
305 SCM_DEFINE (scm_getgrgid
, "getgr", 0, 1, 0,
307 "Look up an entry in the group database. @var{obj} can be an integer,
308 a string, or omitted, giving the behaviour of getgrgid, getgrnam
309 or getgrent respectively.")
310 #define FUNC_NAME s_scm_getgrgid
315 result
= scm_make_vector (SCM_MAKINUM (4), SCM_UNSPECIFIED
);
316 ve
= SCM_VELTS (result
);
317 if (SCM_UNBNDP (name
) || (name
== SCM_BOOL_F
))
319 SCM_SYSCALL (entry
= getgrent ());
325 else if (SCM_INUMP (name
))
326 SCM_SYSCALL (entry
= getgrgid (SCM_INUM (name
)));
329 SCM_VALIDATE_ROSTRING (1,name
);
330 SCM_COERCE_SUBSTR (name
);
331 SCM_SYSCALL (entry
= getgrnam (SCM_ROCHARS (name
)));
336 ve
[0] = scm_makfrom0str (entry
->gr_name
);
337 ve
[1] = scm_makfrom0str (entry
->gr_passwd
);
338 ve
[2] = scm_ulong2num ((unsigned long) entry
->gr_gid
);
339 ve
[3] = scm_makfromstrs (-1, entry
->gr_mem
);
346 SCM_DEFINE (scm_setgrent
, "setgr", 0, 1, 0,
348 "If called with a true argument, initialize or reset the group data
349 stream. Otherwise, close the stream. The @code{setgrent} and
350 @code{endgrent} procedures are implemented on top of this.")
351 #define FUNC_NAME s_scm_setgrent
353 if (SCM_UNBNDP (arg
) || SCM_FALSEP (arg
))
357 return SCM_UNSPECIFIED
;
363 SCM_DEFINE (scm_kill
, "kill", 2, 0, 0,
365 "Sends a signal to the specified process or group of processes.
367 @var{pid} specifies the processes to which the signal is sent:
370 @item @var{pid} greater than 0
371 The process whose identifier is @var{pid}.
372 @item @var{pid} equal to 0
373 All processes in the current process group.
374 @item @var{pid} less than -1
375 The process group whose identifier is -@var{pid}
376 @item @var{pid} equal to -1
377 If the process is privileged, all processes except for some special
378 system processes. Otherwise, all processes with the current effective
382 @var{sig} should be specified using a variable corresponding to
383 the Unix symbolic name, e.g.,
392 #define FUNC_NAME s_scm_kill
394 SCM_VALIDATE_INUM (1,pid
);
395 SCM_VALIDATE_INUM (2,sig
);
396 /* Signal values are interned in scm_init_posix(). */
397 if (kill ((int) SCM_INUM (pid
), (int) SCM_INUM (sig
)) != 0)
399 return SCM_UNSPECIFIED
;
404 SCM_DEFINE (scm_waitpid
, "waitpid", 1, 1, 0,
405 (SCM pid
, SCM options
),
406 "This procedure collects status information from a child process which
407 has terminated or (optionally) stopped. Normally it will
408 suspend the calling process until this can be done. If more than one
409 child process is eligible then one will be chosen by the operating system.
411 The value of @var{pid} determines the behaviour:
414 @item @var{pid} greater than 0
415 Request status information from the specified child process.
416 @item @var{pid} equal to -1 or WAIT_ANY
417 Request status information for any child process.
418 @item @var{pid} equal to 0 or WAIT_MYPGRP
419 Request status information for any child process in the current process
421 @item @var{pid} less than -1
422 Request status information for any child process whose process group ID
426 The @var{options} argument, if supplied, should be the bitwise OR of the
427 values of zero or more of the following variables:
430 Return immediately even if there are no child processes to be collected.
434 Report status information for stopped processes as well as terminated
438 The return value is a pair containing:
442 The process ID of the child process, or 0 if @code{WNOHANG} was
443 specified and no process was collected.
445 The integer status value.
447 #define FUNC_NAME s_scm_waitpid
452 SCM_VALIDATE_INUM (1,pid
);
453 if (SCM_UNBNDP (options
))
457 SCM_VALIDATE_INUM (2,options
);
458 /* Flags are interned in scm_init_posix. */
459 ioptions
= SCM_INUM (options
);
461 SCM_SYSCALL (i
= waitpid (SCM_INUM (pid
), &status
, ioptions
));
464 return scm_cons (SCM_MAKINUM (0L + i
), SCM_MAKINUM (0L + status
));
467 #endif /* HAVE_WAITPID */
469 SCM_DEFINE (scm_status_exit_val
, "status:exit-val", 1, 0, 0,
471 "Returns the exit status value, as would be
472 set if a process ended normally through a
473 call to @code{exit} or @code{_exit}, if any, otherwise @code{#f}.")
474 #define FUNC_NAME s_scm_status_exit_val
478 SCM_VALIDATE_INUM (1,status
);
480 /* On Ultrix, the WIF... macros assume their argument is an lvalue;
481 go figure. SCM_INUM does not yield an lvalue. */
482 lstatus
= SCM_INUM (status
);
483 if (WIFEXITED (lstatus
))
484 return (SCM_MAKINUM (WEXITSTATUS (lstatus
)));
490 SCM_DEFINE (scm_status_term_sig
, "status:term-sig", 1, 0, 0,
492 "Returns the signal number which terminated the
493 process, if any, otherwise @code{#f}.")
494 #define FUNC_NAME s_scm_status_term_sig
498 SCM_VALIDATE_INUM (1,status
);
500 lstatus
= SCM_INUM (status
);
501 if (WIFSIGNALED (lstatus
))
502 return SCM_MAKINUM (WTERMSIG (lstatus
));
508 SCM_DEFINE (scm_status_stop_sig
, "status:stop-sig", 1, 0, 0,
510 "Returns the signal number which stopped the
511 process, if any, otherwise @code{#f}.")
512 #define FUNC_NAME s_scm_status_stop_sig
516 SCM_VALIDATE_INUM (1,status
);
518 lstatus
= SCM_INUM (status
);
519 if (WIFSTOPPED (lstatus
))
520 return SCM_MAKINUM (WSTOPSIG (lstatus
));
526 SCM_DEFINE (scm_getppid
, "getppid", 0, 0, 0,
528 "Returns an integer representing the process ID of the parent process.")
529 #define FUNC_NAME s_scm_getppid
531 return SCM_MAKINUM (0L + getppid ());
537 SCM_DEFINE (scm_getuid
, "getuid", 0, 0, 0,
539 "Returns an integer representing the current real user ID.")
540 #define FUNC_NAME s_scm_getuid
542 return SCM_MAKINUM (0L + getuid ());
548 SCM_DEFINE (scm_getgid
, "getgid", 0, 0, 0,
550 "Returns an integer representing the current real group ID.")
551 #define FUNC_NAME s_scm_getgid
553 return SCM_MAKINUM (0L + getgid ());
559 SCM_DEFINE (scm_geteuid
, "geteuid", 0, 0, 0,
561 "Returns an integer representing the current effective user ID.
562 If the system does not support effective IDs, then the real ID
563 is returned. @code{(feature? 'EIDs)} reports whether the system
564 supports effective IDs.")
565 #define FUNC_NAME s_scm_geteuid
568 return SCM_MAKINUM (0L + geteuid ());
570 return SCM_MAKINUM (0L + getuid ());
577 SCM_DEFINE (scm_getegid
, "getegid", 0, 0, 0,
579 "Returns an integer representing the current effective group ID.
580 If the system does not support effective IDs, then the real ID
581 is returned. @code{(feature? 'EIDs)} reports whether the system
582 supports effective IDs.")
583 #define FUNC_NAME s_scm_getegid
586 return SCM_MAKINUM (0L + getegid ());
588 return SCM_MAKINUM (0L + getgid ());
594 SCM_DEFINE (scm_setuid
, "setuid", 1, 0, 0,
596 "Sets both the real and effective user IDs to the integer @var{id}, provided
597 the process has appropriate privileges.
598 The return value is unspecified.")
599 #define FUNC_NAME s_scm_setuid
601 SCM_VALIDATE_INUM (1,id
);
602 if (setuid (SCM_INUM (id
)) != 0)
604 return SCM_UNSPECIFIED
;
608 SCM_DEFINE (scm_setgid
, "setgid", 1, 0, 0,
610 "Sets both the real and effective group IDs to the integer @var{id}, provided
611 the process has appropriate privileges.
612 The return value is unspecified.")
613 #define FUNC_NAME s_scm_setgid
615 SCM_VALIDATE_INUM (1,id
);
616 if (setgid (SCM_INUM (id
)) != 0)
618 return SCM_UNSPECIFIED
;
622 SCM_DEFINE (scm_seteuid
, "seteuid", 1, 0, 0,
624 "Sets the effective user ID to the integer @var{id}, provided the process
625 has appropriate privileges. If effective IDs are not supported, the
626 real ID is set instead -- @code{(feature? 'EIDs)} reports whether the
627 system supports effective IDs.
628 The return value is unspecified.")
629 #define FUNC_NAME s_scm_seteuid
633 SCM_VALIDATE_INUM (1,id
);
635 rv
= seteuid (SCM_INUM (id
));
637 rv
= setuid (SCM_INUM (id
));
641 return SCM_UNSPECIFIED
;
646 SCM_DEFINE (scm_setegid
, "setegid", 1, 0, 0,
648 "Sets the effective group ID to the integer @var{id}, provided the process
649 has appropriate privileges. If effective IDs are not supported, the
650 real ID is set instead -- @code{(feature? 'EIDs)} reports whether the
651 system supports effective IDs.
652 The return value is unspecified.")
653 #define FUNC_NAME s_scm_setegid
657 SCM_VALIDATE_INUM (1,id
);
659 rv
= setegid (SCM_INUM (id
));
661 rv
= setgid (SCM_INUM (id
));
665 return SCM_UNSPECIFIED
;
671 SCM_DEFINE (scm_getpgrp
, "getpgrp", 0, 0, 0,
673 "Returns an integer representing the current process group ID.
674 This is the POSIX definition, not BSD.")
675 #define FUNC_NAME s_scm_getpgrp
678 fn
= (int (*) ()) getpgrp
;
679 return SCM_MAKINUM (fn (0));
684 SCM_DEFINE (scm_setpgid
, "setpgid", 2, 0, 0,
686 "Move the process @var{pid} into the process group @var{pgid}. @var{pid} or
687 @var{pgid} must be integers: they can be zero to indicate the ID of the
689 Fails on systems that do not support job control.
690 The return value is unspecified.")
691 #define FUNC_NAME s_scm_setpgid
693 SCM_VALIDATE_INUM (1,pid
);
694 SCM_VALIDATE_INUM (2,pgid
);
695 /* FIXME(?): may be known as setpgrp. */
696 if (setpgid (SCM_INUM (pid
), SCM_INUM (pgid
)) != 0)
698 return SCM_UNSPECIFIED
;
701 #endif /* HAVE_SETPGID */
704 SCM_DEFINE (scm_setsid
, "setsid", 0, 0, 0,
706 "Creates a new session. The current process becomes the session leader
707 and is put in a new process group. The process will be detached
708 from its controlling terminal if it has one.
709 The return value is an integer representing the new process group ID.")
710 #define FUNC_NAME s_scm_setsid
712 pid_t sid
= setsid ();
715 return SCM_UNSPECIFIED
;
718 #endif /* HAVE_SETSID */
720 SCM_DEFINE (scm_ttyname
, "ttyname", 1, 0, 0,
722 "Returns a string with the name of the serial terminal device underlying
724 #define FUNC_NAME s_scm_ttyname
729 port
= SCM_COERCE_OUTPORT (port
);
730 SCM_VALIDATE_OPPORT (1,port
);
731 if (scm_tc16_fport
!= SCM_TYP16 (port
))
733 fd
= SCM_FPORT_FDES (port
);
734 SCM_SYSCALL (ans
= ttyname (fd
));
737 /* ans could be overwritten by another call to ttyname */
738 return (scm_makfrom0str (ans
));
743 SCM_DEFINE (scm_ctermid
, "ctermid", 0, 0, 0,
745 "Returns a string containing the file name of the controlling terminal
746 for the current process.")
747 #define FUNC_NAME s_scm_ctermid
749 char *result
= ctermid (NULL
);
752 return scm_makfrom0str (result
);
755 #endif /* HAVE_CTERMID */
757 #ifdef HAVE_TCGETPGRP
758 SCM_DEFINE (scm_tcgetpgrp
, "tcgetpgrp", 1, 0, 0,
760 "Returns the process group ID of the foreground
761 process group associated with the terminal open on the file descriptor
762 underlying @var{port}.
764 If there is no foreground process group, the return value is a
765 number greater than 1 that does not match the process group ID
766 of any existing process group. This can happen if all of the
767 processes in the job that was formerly the foreground job have
768 terminated, and no other job has yet been moved into the
770 #define FUNC_NAME s_scm_tcgetpgrp
775 port
= SCM_COERCE_OUTPORT (port
);
777 SCM_VALIDATE_OPFPORT (1,port
);
778 fd
= SCM_FPORT_FDES (port
);
779 if ((pgid
= tcgetpgrp (fd
)) == -1)
781 return SCM_MAKINUM (pgid
);
784 #endif /* HAVE_TCGETPGRP */
786 #ifdef HAVE_TCSETPGRP
787 SCM_DEFINE (scm_tcsetpgrp
, "tcsetpgrp", 2, 0, 0,
788 (SCM port
, SCM pgid
),
789 "Set the foreground process group ID for the terminal used by the file
790 descriptor underlying @var{port} to the integer @var{pgid}.
792 must be a member of the same session as @var{pgid} and must have the same
793 controlling terminal. The return value is unspecified.")
794 #define FUNC_NAME s_scm_tcsetpgrp
798 port
= SCM_COERCE_OUTPORT (port
);
800 SCM_VALIDATE_OPFPORT (1,port
);
801 SCM_VALIDATE_INUM (2,pgid
);
802 fd
= SCM_FPORT_FDES (port
);
803 if (tcsetpgrp (fd
, SCM_INUM (pgid
)) == -1)
805 return SCM_UNSPECIFIED
;
808 #endif /* HAVE_TCSETPGRP */
810 /* Copy exec args from an SCM vector into a new C array. */
813 scm_convert_exec_args (SCM args
, int pos
, const char *subr
)
819 SCM_ASSERT (SCM_NULLP (args
)
820 || (SCM_CONSP (args
)),
822 num_args
= scm_ilength (args
);
824 scm_must_malloc ((num_args
+ 1) * sizeof (char *), subr
);
825 for (i
= 0; SCM_NNULLP (args
); args
= SCM_CDR (args
), ++i
)
830 SCM_ASSERT (SCM_ROSTRINGP (SCM_CAR (args
)),
831 SCM_CAR (args
), SCM_ARGn
, subr
);
832 len
= 1 + SCM_ROLENGTH (SCM_CAR (args
));
833 dst
= (char *) scm_must_malloc ((long) len
, subr
);
834 src
= SCM_ROCHARS (SCM_CAR (args
));
843 SCM_DEFINE (scm_execl
, "execl", 1, 0, 1,
844 (SCM filename
, SCM args
),
845 "Executes the file named by @var{path} as a new process image.
846 The remaining arguments are supplied to the process; from a C program
847 they are accessable as the @code{argv} argument to @code{main}.
848 Conventionally the first @var{arg} is the same as @var{path}.
849 All arguments must be strings.
851 If @var{arg} is missing, @var{path} is executed with a null
852 argument list, which may have system-dependent side-effects.
854 This procedure is currently implemented using the @code{execv} system
855 call, but we call it @code{execl} because of its Scheme calling interface.")
856 #define FUNC_NAME s_scm_execl
859 SCM_VALIDATE_ROSTRING (1,filename
);
860 SCM_COERCE_SUBSTR (filename
);
861 execargv
= scm_convert_exec_args (args
, SCM_ARG2
, FUNC_NAME
);
862 execv (SCM_ROCHARS (filename
), execargv
);
869 SCM_DEFINE (scm_execlp
, "execlp", 1, 0, 1,
870 (SCM filename
, SCM args
),
871 "Similar to @code{execl}, however if
872 @var{filename} does not contain a slash
873 then the file to execute will be located by searching the
874 directories listed in the @code{PATH} environment variable.
876 This procedure is currently implemented using the @code{execlv} system
877 call, but we call it @code{execlp} because of its Scheme calling interface.")
878 #define FUNC_NAME s_scm_execlp
881 SCM_VALIDATE_ROSTRING (1,filename
);
882 SCM_COERCE_SUBSTR (filename
);
883 execargv
= scm_convert_exec_args (args
, SCM_ARG2
, FUNC_NAME
);
884 execvp (SCM_ROCHARS (filename
), execargv
);
892 environ_list_to_c (SCM envlist
, int arg
, const char *proc
)
898 SCM_ASSERT (SCM_NULLP (envlist
) || SCM_CONSP (envlist
),
900 num_strings
= scm_ilength (envlist
);
901 result
= (char **) malloc ((num_strings
+ 1) * sizeof (char *));
903 scm_memory_error (proc
);
904 while (SCM_NNULLP (envlist
))
909 SCM_ASSERT (SCM_ROSTRINGP (SCM_CAR (envlist
)),
911 len
= 1 + SCM_ROLENGTH (SCM_CAR (envlist
));
912 result
[i
] = malloc ((long) len
);
913 if (result
[i
] == NULL
)
914 scm_memory_error (proc
);
915 src
= SCM_ROCHARS (SCM_CAR (envlist
));
917 result
[i
][len
] = src
[len
];
918 envlist
= SCM_CDR (envlist
);
925 SCM_DEFINE (scm_execle
, "execle", 2, 0, 1,
926 (SCM filename
, SCM env
, SCM args
),
927 "Similar to @code{execl}, but the environment of the new process is
928 specified by @var{env}, which must be a list of strings as returned by the
929 @code{environ} procedure.
931 This procedure is currently implemented using the @code{execve} system
932 call, but we call it @code{execle} because of its Scheme calling interface.")
933 #define FUNC_NAME s_scm_execle
938 SCM_VALIDATE_ROSTRING (1,filename
);
939 SCM_COERCE_SUBSTR (filename
);
941 execargv
= scm_convert_exec_args (args
, SCM_ARG1
, FUNC_NAME
);
942 exec_env
= environ_list_to_c (env
, SCM_ARG2
, FUNC_NAME
);
943 execve (SCM_ROCHARS (filename
), execargv
, exec_env
);
950 SCM_DEFINE (scm_fork
, "primitive-fork", 0, 0, 0,
952 "Creates a new \"child\" process by duplicating the current \"parent\" process.
953 In the child the return value is 0. In the parent the return value is
954 the integer process ID of the child.
956 This procedure has been renamed from @code{fork} to avoid a naming conflict
957 with the scsh fork.")
958 #define FUNC_NAME s_scm_fork
964 return SCM_MAKINUM (0L+pid
);
969 SCM_DEFINE (scm_uname
, "uname", 0, 0, 0,
971 "Returns an object with some information about the computer system the
972 program is running on.")
973 #define FUNC_NAME s_scm_uname
976 SCM ans
= scm_make_vector (SCM_MAKINUM(5), SCM_UNSPECIFIED
);
977 SCM
*ve
= SCM_VELTS (ans
);
978 if (uname (&buf
) < 0)
980 ve
[0] = scm_makfrom0str (buf
.sysname
);
981 ve
[1] = scm_makfrom0str (buf
.nodename
);
982 ve
[2] = scm_makfrom0str (buf
.release
);
983 ve
[3] = scm_makfrom0str (buf
.version
);
984 ve
[4] = scm_makfrom0str (buf
.machine
);
987 ve[5] = scm_makfrom0str (buf.domainname);
992 #endif /* HAVE_UNAME */
994 SCM_DEFINE (scm_environ
, "environ", 0, 1, 0,
996 "If @var{env} is omitted, returns the current environment as a list of strings.
997 Otherwise it sets the current environment, which is also the
998 default environment for child processes, to the supplied list of strings.
999 Each member of @var{env} should be of the form
1000 @code{NAME=VALUE} and values of @code{NAME} should not be duplicated.
1001 If @var{env} is supplied then the return value is unspecified.")
1002 #define FUNC_NAME s_scm_environ
1004 if (SCM_UNBNDP (env
))
1005 return scm_makfromstrs (-1, environ
);
1010 new_environ
= environ_list_to_c (env
, SCM_ARG1
, FUNC_NAME
);
1011 /* Free the old environment, except when called for the first
1016 static int first
= 1;
1019 for (ep
= environ
; *ep
!= NULL
; ep
++)
1021 free ((char *) environ
);
1025 environ
= new_environ
;
1026 return SCM_UNSPECIFIED
;
1033 SCM_DEFINE (scm_tmpnam
, "tmpnam", 0, 0, 0,
1035 "Create a new file in the file system with a unique name. The return
1036 value is the name of the new file. This function is implemented with
1037 the @code{tmpnam} function in the system libraries.")
1038 #define FUNC_NAME s_scm_tmpnam
1040 char name
[L_tmpnam
];
1041 SCM_SYSCALL (tmpnam (name
););
1042 return scm_makfrom0str (name
);
1048 SCM_DEFINE (scm_utime
, "utime", 1, 2, 0,
1049 (SCM pathname
, SCM actime
, SCM modtime
),
1050 "@code{utime} sets the access and modification times for
1051 the file named by @var{path}. If @var{actime} or @var{modtime}
1052 is not supplied, then the current time is used.
1053 @var{actime} and @var{modtime}
1054 must be integer time values as returned by the @code{current-time}
1060 (utime \"foo\" (- (current-time) 3600))
1063 will set the access time to one hour in the past and the modification
1064 time to the current time.")
1065 #define FUNC_NAME s_scm_utime
1068 struct utimbuf utm_tmp
;
1070 SCM_VALIDATE_ROSTRING (1,pathname
);
1071 SCM_COERCE_SUBSTR (pathname
);
1072 if (SCM_UNBNDP (actime
))
1073 SCM_SYSCALL (time (&utm_tmp
.actime
));
1075 utm_tmp
.actime
= SCM_NUM2ULONG (2,actime
);
1077 if (SCM_UNBNDP (modtime
))
1078 SCM_SYSCALL (time (&utm_tmp
.modtime
));
1080 utm_tmp
.modtime
= SCM_NUM2ULONG (3,modtime
);
1082 SCM_SYSCALL (rv
= utime (SCM_ROCHARS (pathname
), &utm_tmp
));
1085 return SCM_UNSPECIFIED
;
1089 SCM_DEFINE (scm_access
, "access?", 2, 0, 0,
1090 (SCM path
, SCM how
),
1091 "Returns @code{#t} if @var{path} corresponds to an existing
1092 file and the current process
1093 has the type of access specified by @var{how}, otherwise
1095 @var{how} should be specified
1096 using the values of the variables listed below. Multiple values can
1097 be combined using a bitwise or, in which case @code{#t} will only
1098 be returned if all accesses are granted.
1100 Permissions are checked using the real id of the current process,
1101 not the effective id, although it's the effective id which determines
1102 whether the access would actually be granted.
1105 test for read permission.
1108 test for write permission.
1111 test for execute permission.
1114 test for existence of the file.
1116 #define FUNC_NAME s_scm_access
1120 SCM_VALIDATE_ROSTRING (1,path
);
1121 if (SCM_SUBSTRP (path
))
1122 path
= scm_makfromstr (SCM_ROCHARS (path
), SCM_ROLENGTH (path
), 0);
1123 SCM_VALIDATE_INUM (2,how
);
1124 rv
= access (SCM_ROCHARS (path
), SCM_INUM (how
));
1125 return SCM_NEGATE_BOOL(rv
);
1129 SCM_DEFINE (scm_getpid
, "getpid", 0, 0, 0,
1131 "Returns an integer representing the current process ID.")
1132 #define FUNC_NAME s_scm_getpid
1134 return SCM_MAKINUM ((unsigned long) getpid ());
1138 SCM_DEFINE (scm_putenv
, "putenv", 1, 0, 0,
1140 "Modifies the environment of the current process, which is
1141 also the default environment inherited by child processes.
1143 If @var{string} is of the form @code{NAME=VALUE} then it will be written
1144 directly into the environment, replacing any existing environment string
1146 name matching @code{NAME}. If @var{string} does not contain an equal
1147 sign, then any existing string with name matching @var{string} will
1150 The return value is unspecified.")
1151 #define FUNC_NAME s_scm_putenv
1156 SCM_VALIDATE_ROSTRING (1,str
);
1157 /* must make a new copy to be left in the environment, safe from gc. */
1158 ptr
= malloc (SCM_LENGTH (str
) + 1);
1161 strncpy (ptr
, SCM_ROCHARS (str
), SCM_LENGTH (str
));
1162 ptr
[SCM_LENGTH(str
)] = 0;
1166 return SCM_UNSPECIFIED
;
1170 #ifdef HAVE_SETLOCALE
1171 SCM_DEFINE (scm_setlocale
, "setlocale", 1, 1, 0,
1172 (SCM category
, SCM locale
),
1173 "If @var{locale} is omitted, returns the current value of the specified
1175 as a system-dependent string.
1176 @var{category} should be specified using the values @code{LC_COLLATE},
1179 Otherwise the specified locale category is set to
1180 the string @var{locale}
1181 and the new value is returned as a system-dependent string. If @var{locale}
1182 is an empty string, the locale will be set using envirionment variables.")
1183 #define FUNC_NAME s_scm_setlocale
1188 SCM_VALIDATE_INUM (1,category
);
1189 if (SCM_UNBNDP (locale
))
1195 SCM_VALIDATE_ROSTRING (2,locale
);
1196 SCM_COERCE_SUBSTR (locale
);
1197 clocale
= SCM_ROCHARS (locale
);
1200 rv
= setlocale (SCM_INUM (category
), clocale
);
1203 return scm_makfrom0str (rv
);
1206 #endif /* HAVE_SETLOCALE */
1209 SCM_DEFINE (scm_mknod
, "mknod", 4, 0, 0,
1210 (SCM path
, SCM type
, SCM perms
, SCM dev
),
1211 "Creates a new special file, such as a file corresponding to a device.
1212 @var{path} specifies the name of the file. @var{type} should
1213 be one of the following symbols:
1214 regular, directory, symlink, block-special, char-special,
1215 fifo, or socket. @var{perms} (an integer) specifies the file permissions.
1216 @var{dev} (an integer) specifies which device the special file refers
1217 to. Its exact interpretation depends on the kind of special file
1222 (mknod "/dev
/fd0
" 'block-special #o660 (+ (* 2 256) 2))
1225 The return value is unspecified.")
1226 #define FUNC_NAME s_scm_mknod
1232 SCM_VALIDATE_ROSTRING (1,path
);
1233 SCM_VALIDATE_SYMBOL (2,type
);
1234 SCM_VALIDATE_INUM (3,perms
);
1235 SCM_VALIDATE_INUM (4,dev
);
1236 SCM_COERCE_SUBSTR (path
);
1238 p
= SCM_CHARS (type
);
1239 if (strcmp (p
, "regular") == 0)
1241 else if (strcmp (p
, "directory") == 0)
1243 else if (strcmp (p
, "symlink") == 0)
1245 else if (strcmp (p
, "block-special") == 0)
1247 else if (strcmp (p
, "char-special") == 0)
1249 else if (strcmp (p
, "fifo") == 0)
1251 else if (strcmp (p
, "socket") == 0)
1254 SCM_OUT_OF_RANGE (2,type
);
1256 SCM_SYSCALL (val
= mknod(SCM_ROCHARS(path
), ctype
| SCM_INUM (perms
),
1260 return SCM_UNSPECIFIED
;
1263 #endif /* HAVE_MKNOD */
1266 SCM_DEFINE (scm_nice
, "nice", 1, 0, 0,
1268 "Increment the priority of the current process by @var{incr}. A higher
1269 priority value means that the process runs less often.
1270 The return value is unspecified.")
1271 #define FUNC_NAME s_scm_nice
1273 SCM_VALIDATE_INUM (1,incr
);
1274 if (nice(SCM_INUM(incr
)) != 0)
1276 return SCM_UNSPECIFIED
;
1279 #endif /* HAVE_NICE */
1282 SCM_DEFINE (scm_sync
, "sync", 0, 0, 0,
1284 "Flush the operating system disk buffers.
1285 The return value is unspecified.")
1286 #define FUNC_NAME s_scm_sync
1289 return SCM_UNSPECIFIED
;
1292 #endif /* HAVE_SYNC */
1297 scm_add_feature ("posix");
1299 scm_add_feature ("EIDs");
1302 scm_sysintern ("WAIT_ANY", SCM_MAKINUM (WAIT_ANY
));
1305 scm_sysintern ("WAIT_MYPGRP", SCM_MAKINUM (WAIT_MYPGRP
));
1308 scm_sysintern ("WNOHANG", SCM_MAKINUM (WNOHANG
));
1311 scm_sysintern ("WUNTRACED", SCM_MAKINUM (WUNTRACED
));
1314 /* access() symbols. */
1315 scm_sysintern ("R_OK", SCM_MAKINUM (R_OK
));
1316 scm_sysintern ("W_OK", SCM_MAKINUM (W_OK
));
1317 scm_sysintern ("X_OK", SCM_MAKINUM (X_OK
));
1318 scm_sysintern ("F_OK", SCM_MAKINUM (F_OK
));
1321 scm_sysintern ("LC_COLLATE", SCM_MAKINUM (LC_COLLATE
));
1324 scm_sysintern ("LC_CTYPE", SCM_MAKINUM (LC_CTYPE
));
1327 scm_sysintern ("LC_MONETARY", SCM_MAKINUM (LC_MONETARY
));
1330 scm_sysintern ("LC_NUMERIC", SCM_MAKINUM (LC_NUMERIC
));
1333 scm_sysintern ("LC_TIME", SCM_MAKINUM (LC_TIME
));
1336 scm_sysintern ("LC_MESSAGES", SCM_MAKINUM (LC_MESSAGES
));
1339 scm_sysintern ("LC_ALL", SCM_MAKINUM (LC_ALL
));
1341 #include "cpp_sig_symbols.c"