1 /* Copyright (C) 1995, 96, 97, 98, 99, 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 */
48 #include "libguile/_scm.h"
49 #include "libguile/fports.h"
50 #include "libguile/scmsigs.h"
51 #include "libguile/feature.h"
52 #include "libguile/strings.h"
53 #include "libguile/vectors.h"
55 #include "libguile/validate.h"
56 #include "libguile/posix.h"
62 #ifdef TIME_WITH_SYS_TIME
63 # include <sys/time.h>
67 # include <sys/time.h>
77 extern char *ttyname();
81 #ifdef LIBC_H_WITH_UNISTD_H
85 #include <sys/types.h>
92 # include <sys/wait.h>
95 # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
98 # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
103 extern FILE *popen ();
104 extern char ** environ
;
107 #include <sys/utsname.h>
111 # define NAMLEN(dirent) strlen((dirent)->d_name)
113 # define dirent direct
114 # define NAMLEN(dirent) (dirent)->d_namlen
116 # include <sys/ndir.h>
119 # include <sys/dir.h>
126 #ifdef HAVE_SETLOCALE
130 /* Some Unix systems don't define these. CPP hair is dangerous, but
131 this seems safe enough... */
148 /* On NextStep, <utime.h> doesn't define struct utime, unless we
149 #define _POSIX_SOURCE before #including it. I think this is less
150 of a kludge than defining struct utimbuf ourselves. */
151 #ifdef UTIMBUF_NEEDS_POSIX
152 #define _POSIX_SOURCE
155 #ifdef HAVE_SYS_UTIME_H
156 #include <sys/utime.h>
163 /* Please don't add any more #includes or #defines here. The hack
164 above means that _POSIX_SOURCE may be #defined, which will
165 encourage header files to do strange things. */
168 SCM_SYMBOL (sym_read_pipe
, "read pipe");
169 SCM_SYMBOL (sym_write_pipe
, "write pipe");
171 SCM_DEFINE (scm_pipe
, "pipe", 0, 0, 0,
173 "Returns a newly created pipe: a pair of ports which are linked\n"
174 "together on the local machine. The CAR is the input port and\n"
175 "the CDR is the output port. Data written (and flushed) to the\n"
176 "output port can be read from the input port.\n"
177 "Pipes are commonly used for communication with a newly\n"
178 "forked child process. The need to flush the output port\n"
179 "can be avoided by making it unbuffered using @code{setvbuf}.\n\n"
180 "Writes occur atomically provided the size of the data in\n"
181 "bytes is not greater than the value of @code{PIPE_BUF}\n"
182 "Note that the output port is likely to block if too much data\n"
183 "(typically equal to @code{PIPE_BUF}) has been written but not\n"
184 "yet read from the input port\n"
186 #define FUNC_NAME s_scm_pipe
195 p_rd
= scm_fdes_to_port (fd
[0], "r", sym_read_pipe
);
196 p_wt
= scm_fdes_to_port (fd
[1], "w", sym_write_pipe
);
197 return scm_cons (p_rd
, p_wt
);
202 #ifdef HAVE_GETGROUPS
203 SCM_DEFINE (scm_getgroups
, "getgroups", 0, 0, 0,
205 "Returns a vector of integers representing the current supplimentary group IDs.")
206 #define FUNC_NAME s_scm_getgroups
209 int ngroups
= getgroups (0, NULL
);
218 groups
= SCM_MUST_MALLOC_TYPE_NUM(GETGROUPS_T
,ngroups
);
219 val
= getgroups(ngroups
, groups
);
223 scm_must_free((char *)groups
);
227 SCM_SETCHARS(grps
, groups
); /* set up grps as a GC protect */
228 SCM_SETLENGTH(grps
, 0L + ngroups
* sizeof(GETGROUPS_T
), scm_tc7_string
);
229 ans
= scm_make_vector (SCM_MAKINUM(ngroups
), SCM_UNDEFINED
);
230 while (--ngroups
>= 0) SCM_VELTS(ans
)[ngroups
] = SCM_MAKINUM(groups
[ngroups
]);
231 SCM_SETCHARS(grps
, groups
); /* to make sure grps stays around. */
240 SCM_DEFINE (scm_getpwuid
, "getpw", 0, 1, 0,
242 "Look up an entry in the user database. @var{obj} can be an integer,\n"
243 "a string, or omitted, giving the behaviour of getpwuid, getpwnam\n"
244 "or getpwent respectively.")
245 #define FUNC_NAME s_scm_getpwuid
248 struct passwd
*entry
;
251 result
= scm_make_vector (SCM_MAKINUM (7), SCM_UNSPECIFIED
);
252 ve
= SCM_VELTS (result
);
253 if (SCM_UNBNDP (user
) || SCM_FALSEP (user
))
255 SCM_SYSCALL (entry
= getpwent ());
261 else if (SCM_INUMP (user
))
263 entry
= getpwuid (SCM_INUM (user
));
267 SCM_VALIDATE_ROSTRING (1,user
);
268 if (SCM_SUBSTRP (user
))
269 user
= scm_makfromstr (SCM_ROCHARS (user
), SCM_ROLENGTH (user
), 0);
270 entry
= getpwnam (SCM_ROCHARS (user
));
273 SCM_MISC_ERROR ("entry not found", SCM_EOL
);
275 ve
[0] = scm_makfrom0str (entry
->pw_name
);
276 ve
[1] = scm_makfrom0str (entry
->pw_passwd
);
277 ve
[2] = scm_ulong2num ((unsigned long) entry
->pw_uid
);
278 ve
[3] = scm_ulong2num ((unsigned long) entry
->pw_gid
);
279 ve
[4] = scm_makfrom0str (entry
->pw_gecos
);
281 ve
[5] = scm_makfrom0str ("");
283 ve
[5] = scm_makfrom0str (entry
->pw_dir
);
284 if (!entry
->pw_shell
)
285 ve
[6] = scm_makfrom0str ("");
287 ve
[6] = scm_makfrom0str (entry
->pw_shell
);
294 SCM_DEFINE (scm_setpwent
, "setpw", 0, 1, 0,
296 "If called with a true argument, initialize or reset the password data\n"
297 "stream. Otherwise, close the stream. The @code{setpwent} and\n"
298 "@code{endpwent} procedures are implemented on top of this.")
299 #define FUNC_NAME s_scm_setpwent
301 if (SCM_UNBNDP (arg
) || SCM_FALSEP (arg
))
305 return SCM_UNSPECIFIED
;
312 /* Combines getgrgid and getgrnam. */
313 SCM_DEFINE (scm_getgrgid
, "getgr", 0, 1, 0,
315 "Look up an entry in the group database. @var{obj} can be an integer,\n"
316 "a string, or omitted, giving the behaviour of getgrgid, getgrnam\n"
317 "or getgrent respectively.")
318 #define FUNC_NAME s_scm_getgrgid
323 result
= scm_make_vector (SCM_MAKINUM (4), SCM_UNSPECIFIED
);
324 ve
= SCM_VELTS (result
);
325 if (SCM_UNBNDP (name
) || SCM_FALSEP (name
))
327 SCM_SYSCALL (entry
= getgrent ());
333 else if (SCM_INUMP (name
))
334 SCM_SYSCALL (entry
= getgrgid (SCM_INUM (name
)));
337 SCM_VALIDATE_ROSTRING (1,name
);
338 SCM_COERCE_SUBSTR (name
);
339 SCM_SYSCALL (entry
= getgrnam (SCM_ROCHARS (name
)));
344 ve
[0] = scm_makfrom0str (entry
->gr_name
);
345 ve
[1] = scm_makfrom0str (entry
->gr_passwd
);
346 ve
[2] = scm_ulong2num ((unsigned long) entry
->gr_gid
);
347 ve
[3] = scm_makfromstrs (-1, entry
->gr_mem
);
354 SCM_DEFINE (scm_setgrent
, "setgr", 0, 1, 0,
356 "If called with a true argument, initialize or reset the group data\n"
357 "stream. Otherwise, close the stream. The @code{setgrent} and\n"
358 "@code{endgrent} procedures are implemented on top of this.")
359 #define FUNC_NAME s_scm_setgrent
361 if (SCM_UNBNDP (arg
) || SCM_FALSEP (arg
))
365 return SCM_UNSPECIFIED
;
371 SCM_DEFINE (scm_kill
, "kill", 2, 0, 0,
373 "Sends a signal to the specified process or group of processes.\n\n"
374 "@var{pid} specifies the processes to which the signal is sent:\n\n"
376 "@item @var{pid} greater than 0\n"
377 "The process whose identifier is @var{pid}.\n"
378 "@item @var{pid} equal to 0\n"
379 "All processes in the current process group.\n"
380 "@item @var{pid} less than -1\n"
381 "The process group whose identifier is -@var{pid}\n"
382 "@item @var{pid} equal to -1\n"
383 "If the process is privileged, all processes except for some special\n"
384 "system processes. Otherwise, all processes with the current effective\n"
387 "@var{sig} should be specified using a variable corresponding to\n"
388 "the Unix symbolic name, e.g.,\n\n"
393 "Interrupt signal.\n"
395 #define FUNC_NAME s_scm_kill
397 SCM_VALIDATE_INUM (1,pid
);
398 SCM_VALIDATE_INUM (2,sig
);
399 /* Signal values are interned in scm_init_posix(). */
400 if (kill ((int) SCM_INUM (pid
), (int) SCM_INUM (sig
)) != 0)
402 return SCM_UNSPECIFIED
;
407 SCM_DEFINE (scm_waitpid
, "waitpid", 1, 1, 0,
408 (SCM pid
, SCM options
),
409 "This procedure collects status information from a child process which\n"
410 "has terminated or (optionally) stopped. Normally it will\n"
411 "suspend the calling process until this can be done. If more than one\n"
412 "child process is eligible then one will be chosen by the operating system.\n\n"
413 "The value of @var{pid} determines the behaviour:\n\n"
415 "@item @var{pid} greater than 0\n"
416 "Request status information from the specified child process.\n"
417 "@item @var{pid} equal to -1 or WAIT_ANY\n"
418 "Request status information for any child process.\n"
419 "@item @var{pid} equal to 0 or WAIT_MYPGRP\n"
420 "Request status information for any child process in the current process\n"
422 "@item @var{pid} less than -1\n"
423 "Request status information for any child process whose process group ID\n"
426 "The @var{options} argument, if supplied, should be the bitwise OR of the\n"
427 "values of zero or more of the following variables:\n\n"
429 "Return immediately even if there are no child processes to be collected.\n"
431 "@defvar WUNTRACED\n"
432 "Report status information for stopped processes as well as terminated\n"
435 "The return value is a pair containing:\n\n"
438 "The process ID of the child process, or 0 if @code{WNOHANG} was\n"
439 "specified and no process was collected.\n"
441 "The integer status value.\n"
443 #define FUNC_NAME s_scm_waitpid
448 SCM_VALIDATE_INUM (1,pid
);
449 if (SCM_UNBNDP (options
))
453 SCM_VALIDATE_INUM (2,options
);
454 /* Flags are interned in scm_init_posix. */
455 ioptions
= SCM_INUM (options
);
457 SCM_SYSCALL (i
= waitpid (SCM_INUM (pid
), &status
, ioptions
));
460 return scm_cons (SCM_MAKINUM (0L + i
), SCM_MAKINUM (0L + status
));
463 #endif /* HAVE_WAITPID */
465 SCM_DEFINE (scm_status_exit_val
, "status:exit-val", 1, 0, 0,
467 "Returns the exit status value, as would be\n"
468 "set if a process ended normally through a\n"
469 "call to @code{exit} or @code{_exit}, if any, otherwise @code{#f}.")
470 #define FUNC_NAME s_scm_status_exit_val
474 SCM_VALIDATE_INUM (1,status
);
476 /* On Ultrix, the WIF... macros assume their argument is an lvalue;
477 go figure. SCM_INUM does not yield an lvalue. */
478 lstatus
= SCM_INUM (status
);
479 if (WIFEXITED (lstatus
))
480 return (SCM_MAKINUM (WEXITSTATUS (lstatus
)));
486 SCM_DEFINE (scm_status_term_sig
, "status:term-sig", 1, 0, 0,
488 "Returns the signal number which terminated the\n"
489 "process, if any, otherwise @code{#f}.")
490 #define FUNC_NAME s_scm_status_term_sig
494 SCM_VALIDATE_INUM (1,status
);
496 lstatus
= SCM_INUM (status
);
497 if (WIFSIGNALED (lstatus
))
498 return SCM_MAKINUM (WTERMSIG (lstatus
));
504 SCM_DEFINE (scm_status_stop_sig
, "status:stop-sig", 1, 0, 0,
506 "Returns the signal number which stopped the\n"
507 "process, if any, otherwise @code{#f}.")
508 #define FUNC_NAME s_scm_status_stop_sig
512 SCM_VALIDATE_INUM (1,status
);
514 lstatus
= SCM_INUM (status
);
515 if (WIFSTOPPED (lstatus
))
516 return SCM_MAKINUM (WSTOPSIG (lstatus
));
522 SCM_DEFINE (scm_getppid
, "getppid", 0, 0, 0,
524 "Returns an integer representing the process ID of the parent process.")
525 #define FUNC_NAME s_scm_getppid
527 return SCM_MAKINUM (0L + getppid ());
533 SCM_DEFINE (scm_getuid
, "getuid", 0, 0, 0,
535 "Returns an integer representing the current real user ID.")
536 #define FUNC_NAME s_scm_getuid
538 return SCM_MAKINUM (0L + getuid ());
544 SCM_DEFINE (scm_getgid
, "getgid", 0, 0, 0,
546 "Returns an integer representing the current real group ID.")
547 #define FUNC_NAME s_scm_getgid
549 return SCM_MAKINUM (0L + getgid ());
555 SCM_DEFINE (scm_geteuid
, "geteuid", 0, 0, 0,
557 "Returns an integer representing the current effective user ID.\n"
558 "If the system does not support effective IDs, then the real ID\n"
559 "is returned. @code{(feature? 'EIDs)} reports whether the system\n"
560 "supports effective IDs.")
561 #define FUNC_NAME s_scm_geteuid
564 return SCM_MAKINUM (0L + geteuid ());
566 return SCM_MAKINUM (0L + getuid ());
573 SCM_DEFINE (scm_getegid
, "getegid", 0, 0, 0,
575 "Returns an integer representing the current effective group ID.\n"
576 "If the system does not support effective IDs, then the real ID\n"
577 "is returned. @code{(feature? 'EIDs)} reports whether the system\n"
578 "supports effective IDs.")
579 #define FUNC_NAME s_scm_getegid
582 return SCM_MAKINUM (0L + getegid ());
584 return SCM_MAKINUM (0L + getgid ());
590 SCM_DEFINE (scm_setuid
, "setuid", 1, 0, 0,
592 "Sets both the real and effective user IDs to the integer @var{id}, provided\n"
593 "the process has appropriate privileges.\n"
594 "The return value is unspecified.")
595 #define FUNC_NAME s_scm_setuid
597 SCM_VALIDATE_INUM (1,id
);
598 if (setuid (SCM_INUM (id
)) != 0)
600 return SCM_UNSPECIFIED
;
604 SCM_DEFINE (scm_setgid
, "setgid", 1, 0, 0,
606 "Sets both the real and effective group IDs to the integer @var{id}, provided\n"
607 "the process has appropriate privileges.\n"
608 "The return value is unspecified.")
609 #define FUNC_NAME s_scm_setgid
611 SCM_VALIDATE_INUM (1,id
);
612 if (setgid (SCM_INUM (id
)) != 0)
614 return SCM_UNSPECIFIED
;
618 SCM_DEFINE (scm_seteuid
, "seteuid", 1, 0, 0,
620 "Sets the effective user ID to the integer @var{id}, provided the process\n"
621 "has appropriate privileges. If effective IDs are not supported, the\n"
622 "real ID is set instead -- @code{(feature? 'EIDs)} reports whether the\n"
623 "system supports effective IDs.\n"
624 "The return value is unspecified.")
625 #define FUNC_NAME s_scm_seteuid
629 SCM_VALIDATE_INUM (1,id
);
631 rv
= seteuid (SCM_INUM (id
));
633 rv
= setuid (SCM_INUM (id
));
637 return SCM_UNSPECIFIED
;
642 SCM_DEFINE (scm_setegid
, "setegid", 1, 0, 0,
644 "Sets the effective group ID to the integer @var{id}, provided the process\n"
645 "has appropriate privileges. If effective IDs are not supported, the\n"
646 "real ID is set instead -- @code{(feature? 'EIDs)} reports whether the\n"
647 "system supports effective IDs.\n"
648 "The return value is unspecified.")
649 #define FUNC_NAME s_scm_setegid
653 SCM_VALIDATE_INUM (1,id
);
655 rv
= setegid (SCM_INUM (id
));
657 rv
= setgid (SCM_INUM (id
));
661 return SCM_UNSPECIFIED
;
667 SCM_DEFINE (scm_getpgrp
, "getpgrp", 0, 0, 0,
669 "Returns an integer representing the current process group ID.\n"
670 "This is the POSIX definition, not BSD.")
671 #define FUNC_NAME s_scm_getpgrp
674 fn
= (int (*) ()) getpgrp
;
675 return SCM_MAKINUM (fn (0));
680 SCM_DEFINE (scm_setpgid
, "setpgid", 2, 0, 0,
682 "Move the process @var{pid} into the process group @var{pgid}. @var{pid} or\n"
683 "@var{pgid} must be integers: they can be zero to indicate the ID of the\n"
685 "Fails on systems that do not support job control.\n"
686 "The return value is unspecified.")
687 #define FUNC_NAME s_scm_setpgid
689 SCM_VALIDATE_INUM (1,pid
);
690 SCM_VALIDATE_INUM (2,pgid
);
691 /* FIXME(?): may be known as setpgrp. */
692 if (setpgid (SCM_INUM (pid
), SCM_INUM (pgid
)) != 0)
694 return SCM_UNSPECIFIED
;
697 #endif /* HAVE_SETPGID */
700 SCM_DEFINE (scm_setsid
, "setsid", 0, 0, 0,
702 "Creates a new session. The current process becomes the session leader\n"
703 "and is put in a new process group. The process will be detached\n"
704 "from its controlling terminal if it has one.\n"
705 "The return value is an integer representing the new process group ID.")
706 #define FUNC_NAME s_scm_setsid
708 pid_t sid
= setsid ();
711 return SCM_UNSPECIFIED
;
714 #endif /* HAVE_SETSID */
716 SCM_DEFINE (scm_ttyname
, "ttyname", 1, 0, 0,
718 "Returns a string with the name of the serial terminal device underlying\n"
720 #define FUNC_NAME s_scm_ttyname
725 port
= SCM_COERCE_OUTPORT (port
);
726 SCM_VALIDATE_OPPORT (1,port
);
727 if (scm_tc16_fport
!= SCM_TYP16 (port
))
729 fd
= SCM_FPORT_FDES (port
);
730 SCM_SYSCALL (ans
= ttyname (fd
));
733 /* ans could be overwritten by another call to ttyname */
734 return (scm_makfrom0str (ans
));
739 SCM_DEFINE (scm_ctermid
, "ctermid", 0, 0, 0,
741 "Returns a string containing the file name of the controlling terminal\n"
742 "for the current process.")
743 #define FUNC_NAME s_scm_ctermid
745 char *result
= ctermid (NULL
);
748 return scm_makfrom0str (result
);
751 #endif /* HAVE_CTERMID */
753 #ifdef HAVE_TCGETPGRP
754 SCM_DEFINE (scm_tcgetpgrp
, "tcgetpgrp", 1, 0, 0,
756 "Returns the process group ID of the foreground\n"
757 "process group associated with the terminal open on the file descriptor\n"
758 "underlying @var{port}.\n\n"
759 "If there is no foreground process group, the return value is a\n"
760 "number greater than 1 that does not match the process group ID\n"
761 "of any existing process group. This can happen if all of the\n"
762 "processes in the job that was formerly the foreground job have\n"
763 "terminated, and no other job has yet been moved into the\n"
765 #define FUNC_NAME s_scm_tcgetpgrp
770 port
= SCM_COERCE_OUTPORT (port
);
772 SCM_VALIDATE_OPFPORT (1,port
);
773 fd
= SCM_FPORT_FDES (port
);
774 if ((pgid
= tcgetpgrp (fd
)) == -1)
776 return SCM_MAKINUM (pgid
);
779 #endif /* HAVE_TCGETPGRP */
781 #ifdef HAVE_TCSETPGRP
782 SCM_DEFINE (scm_tcsetpgrp
, "tcsetpgrp", 2, 0, 0,
783 (SCM port
, SCM pgid
),
784 "Set the foreground process group ID for the terminal used by the file\n"
785 "descriptor underlying @var{port} to the integer @var{pgid}.\n"
786 "The calling process\n"
787 "must be a member of the same session as @var{pgid} and must have the same\n"
788 "controlling terminal. The return value is unspecified.")
789 #define FUNC_NAME s_scm_tcsetpgrp
793 port
= SCM_COERCE_OUTPORT (port
);
795 SCM_VALIDATE_OPFPORT (1,port
);
796 SCM_VALIDATE_INUM (2,pgid
);
797 fd
= SCM_FPORT_FDES (port
);
798 if (tcsetpgrp (fd
, SCM_INUM (pgid
)) == -1)
800 return SCM_UNSPECIFIED
;
803 #endif /* HAVE_TCSETPGRP */
805 /* Copy exec args from an SCM vector into a new C array. */
808 scm_convert_exec_args (SCM args
, int pos
, const char *subr
)
814 SCM_ASSERT (SCM_NULLP (args
)
815 || (SCM_CONSP (args
)),
817 num_args
= scm_ilength (args
);
819 scm_must_malloc ((num_args
+ 1) * sizeof (char *), subr
);
820 for (i
= 0; SCM_NNULLP (args
); args
= SCM_CDR (args
), ++i
)
825 SCM_ASSERT (SCM_ROSTRINGP (SCM_CAR (args
)),
826 SCM_CAR (args
), SCM_ARGn
, subr
);
827 len
= 1 + SCM_ROLENGTH (SCM_CAR (args
));
828 dst
= (char *) scm_must_malloc ((long) len
, subr
);
829 src
= SCM_ROCHARS (SCM_CAR (args
));
838 SCM_DEFINE (scm_execl
, "execl", 1, 0, 1,
839 (SCM filename
, SCM args
),
840 "Executes the file named by @var{path} as a new process image.\n"
841 "The remaining arguments are supplied to the process; from a C program\n"
842 "they are accessable as the @code{argv} argument to @code{main}.\n"
843 "Conventionally the first @var{arg} is the same as @var{path}.\n"
844 "All arguments must be strings. \n\n"
845 "If @var{arg} is missing, @var{path} is executed with a null\n"
846 "argument list, which may have system-dependent side-effects.\n\n"
847 "This procedure is currently implemented using the @code{execv} system\n"
848 "call, but we call it @code{execl} because of its Scheme calling interface.")
849 #define FUNC_NAME s_scm_execl
852 SCM_VALIDATE_ROSTRING (1,filename
);
853 SCM_COERCE_SUBSTR (filename
);
854 execargv
= scm_convert_exec_args (args
, SCM_ARG2
, FUNC_NAME
);
855 execv (SCM_ROCHARS (filename
), execargv
);
862 SCM_DEFINE (scm_execlp
, "execlp", 1, 0, 1,
863 (SCM filename
, SCM args
),
864 "Similar to @code{execl}, however if\n"
865 "@var{filename} does not contain a slash\n"
866 "then the file to execute will be located by searching the\n"
867 "directories listed in the @code{PATH} environment variable.\n\n"
868 "This procedure is currently implemented using the @code{execvp} system\n"
869 "call, but we call it @code{execlp} because of its Scheme calling interface.")
870 #define FUNC_NAME s_scm_execlp
873 SCM_VALIDATE_ROSTRING (1,filename
);
874 SCM_COERCE_SUBSTR (filename
);
875 execargv
= scm_convert_exec_args (args
, SCM_ARG2
, FUNC_NAME
);
876 execvp (SCM_ROCHARS (filename
), execargv
);
884 environ_list_to_c (SCM envlist
, int arg
, const char *proc
)
890 SCM_ASSERT (SCM_NULLP (envlist
) || SCM_CONSP (envlist
),
892 num_strings
= scm_ilength (envlist
);
893 result
= (char **) malloc ((num_strings
+ 1) * sizeof (char *));
895 scm_memory_error (proc
);
896 while (SCM_NNULLP (envlist
))
901 SCM_ASSERT (SCM_ROSTRINGP (SCM_CAR (envlist
)),
903 len
= 1 + SCM_ROLENGTH (SCM_CAR (envlist
));
904 result
[i
] = malloc ((long) len
);
905 if (result
[i
] == NULL
)
906 scm_memory_error (proc
);
907 src
= SCM_ROCHARS (SCM_CAR (envlist
));
909 result
[i
][len
] = src
[len
];
910 envlist
= SCM_CDR (envlist
);
917 SCM_DEFINE (scm_execle
, "execle", 2, 0, 1,
918 (SCM filename
, SCM env
, SCM args
),
919 "Similar to @code{execl}, but the environment of the new process is\n"
920 "specified by @var{env}, which must be a list of strings as returned by the\n"
921 "@code{environ} procedure.\n\n"
922 "This procedure is currently implemented using the @code{execve} system\n"
923 "call, but we call it @code{execle} because of its Scheme calling interface.")
924 #define FUNC_NAME s_scm_execle
929 SCM_VALIDATE_ROSTRING (1,filename
);
930 SCM_COERCE_SUBSTR (filename
);
932 execargv
= scm_convert_exec_args (args
, SCM_ARG1
, FUNC_NAME
);
933 exec_env
= environ_list_to_c (env
, SCM_ARG2
, FUNC_NAME
);
934 execve (SCM_ROCHARS (filename
), execargv
, exec_env
);
941 SCM_DEFINE (scm_fork
, "primitive-fork", 0, 0, 0,
943 "Creates a new \"child\" process by duplicating the current \"parent\" process.\n"
944 "In the child the return value is 0. In the parent the return value is\n"
945 "the integer process ID of the child.\n\n"
946 "This procedure has been renamed from @code{fork} to avoid a naming conflict\n"
947 "with the scsh fork.")
948 #define FUNC_NAME s_scm_fork
954 return SCM_MAKINUM (0L+pid
);
959 SCM_DEFINE (scm_uname
, "uname", 0, 0, 0,
961 "Returns an object with some information about the computer system the\n"
962 "program is running on.")
963 #define FUNC_NAME s_scm_uname
966 SCM ans
= scm_make_vector (SCM_MAKINUM(5), SCM_UNSPECIFIED
);
967 SCM
*ve
= SCM_VELTS (ans
);
968 if (uname (&buf
) < 0)
970 ve
[0] = scm_makfrom0str (buf
.sysname
);
971 ve
[1] = scm_makfrom0str (buf
.nodename
);
972 ve
[2] = scm_makfrom0str (buf
.release
);
973 ve
[3] = scm_makfrom0str (buf
.version
);
974 ve
[4] = scm_makfrom0str (buf
.machine
);
977 ve[5] = scm_makfrom0str (buf.domainname);
982 #endif /* HAVE_UNAME */
984 SCM_DEFINE (scm_environ
, "environ", 0, 1, 0,
986 "If @var{env} is omitted, returns the current environment as a list of strings.\n"
987 "Otherwise it sets the current environment, which is also the\n"
988 "default environment for child processes, to the supplied list of strings.\n"
989 "Each member of @var{env} should be of the form\n"
990 "@code{NAME=VALUE} and values of @code{NAME} should not be duplicated.\n"
991 "If @var{env} is supplied then the return value is unspecified.")
992 #define FUNC_NAME s_scm_environ
994 if (SCM_UNBNDP (env
))
995 return scm_makfromstrs (-1, environ
);
1000 new_environ
= environ_list_to_c (env
, SCM_ARG1
, FUNC_NAME
);
1001 /* Free the old environment, except when called for the first
1006 static int first
= 1;
1009 for (ep
= environ
; *ep
!= NULL
; ep
++)
1011 free ((char *) environ
);
1015 environ
= new_environ
;
1016 return SCM_UNSPECIFIED
;
1023 SCM_DEFINE (scm_tmpnam
, "tmpnam", 0, 0, 0,
1025 "Create a new file in the file system with a unique name. The return\n"
1026 "value is the name of the new file. This function is implemented with\n"
1027 "the @code{tmpnam} function in the system libraries.")
1028 #define FUNC_NAME s_scm_tmpnam
1030 char name
[L_tmpnam
];
1031 SCM_SYSCALL (tmpnam (name
););
1032 return scm_makfrom0str (name
);
1038 SCM_DEFINE (scm_utime
, "utime", 1, 2, 0,
1039 (SCM pathname
, SCM actime
, SCM modtime
),
1040 "@code{utime} sets the access and modification times for\n"
1041 "the file named by @var{path}. If @var{actime} or @var{modtime}\n"
1042 "is not supplied, then the current time is used.\n"
1043 "@var{actime} and @var{modtime}\n"
1044 "must be integer time values as returned by the @code{current-time}\n"
1048 "(utime \"foo\" (- (current-time) 3600))\n"
1049 "@end smalllisp\n\n"
1050 "will set the access time to one hour in the past and the modification\n"
1051 "time to the current time.")
1052 #define FUNC_NAME s_scm_utime
1055 struct utimbuf utm_tmp
;
1057 SCM_VALIDATE_ROSTRING (1,pathname
);
1058 SCM_COERCE_SUBSTR (pathname
);
1059 if (SCM_UNBNDP (actime
))
1060 SCM_SYSCALL (time (&utm_tmp
.actime
));
1062 utm_tmp
.actime
= SCM_NUM2ULONG (2,actime
);
1064 if (SCM_UNBNDP (modtime
))
1065 SCM_SYSCALL (time (&utm_tmp
.modtime
));
1067 utm_tmp
.modtime
= SCM_NUM2ULONG (3,modtime
);
1069 SCM_SYSCALL (rv
= utime (SCM_ROCHARS (pathname
), &utm_tmp
));
1072 return SCM_UNSPECIFIED
;
1076 SCM_DEFINE (scm_access
, "access?", 2, 0, 0,
1077 (SCM path
, SCM how
),
1078 "Returns @code{#t} if @var{path} corresponds to an existing\n"
1079 "file and the current process\n"
1080 "has the type of access specified by @var{how}, otherwise \n"
1082 "@var{how} should be specified\n"
1083 "using the values of the variables listed below. Multiple values can\n"
1084 "be combined using a bitwise or, in which case @code{#t} will only\n"
1085 "be returned if all accesses are granted.\n\n"
1086 "Permissions are checked using the real id of the current process,\n"
1087 "not the effective id, although it's the effective id which determines\n"
1088 "whether the access would actually be granted.\n\n"
1090 "test for read permission.\n"
1093 "test for write permission.\n"
1096 "test for execute permission.\n"
1099 "test for existence of the file.\n"
1101 #define FUNC_NAME s_scm_access
1105 SCM_VALIDATE_ROSTRING (1,path
);
1106 if (SCM_SUBSTRP (path
))
1107 path
= scm_makfromstr (SCM_ROCHARS (path
), SCM_ROLENGTH (path
), 0);
1108 SCM_VALIDATE_INUM (2,how
);
1109 rv
= access (SCM_ROCHARS (path
), SCM_INUM (how
));
1110 return SCM_NEGATE_BOOL(rv
);
1114 SCM_DEFINE (scm_getpid
, "getpid", 0, 0, 0,
1116 "Returns an integer representing the current process ID.")
1117 #define FUNC_NAME s_scm_getpid
1119 return SCM_MAKINUM ((unsigned long) getpid ());
1123 SCM_DEFINE (scm_putenv
, "putenv", 1, 0, 0,
1125 "Modifies the environment of the current process, which is\n"
1126 "also the default environment inherited by child processes.\n\n"
1127 "If @var{string} is of the form @code{NAME=VALUE} then it will be written\n"
1128 "directly into the environment, replacing any existing environment string\n"
1130 "name matching @code{NAME}. If @var{string} does not contain an equal\n"
1131 "sign, then any existing string with name matching @var{string} will\n"
1133 "The return value is unspecified.")
1134 #define FUNC_NAME s_scm_putenv
1139 SCM_VALIDATE_ROSTRING (1,str
);
1140 /* must make a new copy to be left in the environment, safe from gc. */
1141 ptr
= malloc (SCM_LENGTH (str
) + 1);
1144 strncpy (ptr
, SCM_ROCHARS (str
), SCM_LENGTH (str
));
1145 ptr
[SCM_LENGTH(str
)] = 0;
1149 return SCM_UNSPECIFIED
;
1153 #ifdef HAVE_SETLOCALE
1154 SCM_DEFINE (scm_setlocale
, "setlocale", 1, 1, 0,
1155 (SCM category
, SCM locale
),
1156 "If @var{locale} is omitted, returns the current value of the specified\n"
1157 "locale category \n"
1158 "as a system-dependent string.\n"
1159 "@var{category} should be specified using the values @code{LC_COLLATE},\n"
1160 "@code{LC_ALL} etc.\n\n"
1161 "Otherwise the specified locale category is set to\n"
1162 "the string @var{locale}\n"
1163 "and the new value is returned as a system-dependent string. If @var{locale}\n"
1164 "is an empty string, the locale will be set using envirionment variables.")
1165 #define FUNC_NAME s_scm_setlocale
1170 SCM_VALIDATE_INUM (1,category
);
1171 if (SCM_UNBNDP (locale
))
1177 SCM_VALIDATE_ROSTRING (2,locale
);
1178 SCM_COERCE_SUBSTR (locale
);
1179 clocale
= SCM_ROCHARS (locale
);
1182 rv
= setlocale (SCM_INUM (category
), clocale
);
1185 return scm_makfrom0str (rv
);
1188 #endif /* HAVE_SETLOCALE */
1191 SCM_DEFINE (scm_mknod
, "mknod", 4, 0, 0,
1192 (SCM path
, SCM type
, SCM perms
, SCM dev
),
1193 "Creates a new special file, such as a file corresponding to a device.\n"
1194 "@var{path} specifies the name of the file. @var{type} should\n"
1195 "be one of the following symbols:\n"
1196 "regular, directory, symlink, block-special, char-special,\n"
1197 "fifo, or socket. @var{perms} (an integer) specifies the file permissions.\n"
1198 "@var{dev} (an integer) specifies which device the special file refers\n"
1199 "to. Its exact interpretation depends on the kind of special file\n"
1200 "being created.\n\n"
1203 "(mknod \"/dev/fd0\" 'block-special #o660 (+ (* 2 256) 2))"
1205 "The return value is unspecified.")
1206 #define FUNC_NAME s_scm_mknod
1212 SCM_VALIDATE_ROSTRING (1,path
);
1213 SCM_VALIDATE_SYMBOL (2,type
);
1214 SCM_VALIDATE_INUM (3,perms
);
1215 SCM_VALIDATE_INUM (4,dev
);
1216 SCM_COERCE_SUBSTR (path
);
1218 p
= SCM_CHARS (type
);
1219 if (strcmp (p
, "regular") == 0)
1221 else if (strcmp (p
, "directory") == 0)
1223 else if (strcmp (p
, "symlink") == 0)
1225 else if (strcmp (p
, "block-special") == 0)
1227 else if (strcmp (p
, "char-special") == 0)
1229 else if (strcmp (p
, "fifo") == 0)
1231 else if (strcmp (p
, "socket") == 0)
1234 SCM_OUT_OF_RANGE (2,type
);
1236 SCM_SYSCALL (val
= mknod(SCM_ROCHARS(path
), ctype
| SCM_INUM (perms
),
1240 return SCM_UNSPECIFIED
;
1243 #endif /* HAVE_MKNOD */
1246 SCM_DEFINE (scm_nice
, "nice", 1, 0, 0,
1248 "Increment the priority of the current process by @var{incr}. A higher\n"
1249 "priority value means that the process runs less often.\n"
1250 "The return value is unspecified.")
1251 #define FUNC_NAME s_scm_nice
1253 SCM_VALIDATE_INUM (1,incr
);
1254 if (nice(SCM_INUM(incr
)) != 0)
1256 return SCM_UNSPECIFIED
;
1259 #endif /* HAVE_NICE */
1262 SCM_DEFINE (scm_sync
, "sync", 0, 0, 0,
1264 "Flush the operating system disk buffers.\n"
1265 "The return value is unspecified.")
1266 #define FUNC_NAME s_scm_sync
1269 return SCM_UNSPECIFIED
;
1272 #endif /* HAVE_SYNC */
1277 scm_add_feature ("posix");
1279 scm_add_feature ("EIDs");
1282 scm_sysintern ("WAIT_ANY", SCM_MAKINUM (WAIT_ANY
));
1285 scm_sysintern ("WAIT_MYPGRP", SCM_MAKINUM (WAIT_MYPGRP
));
1288 scm_sysintern ("WNOHANG", SCM_MAKINUM (WNOHANG
));
1291 scm_sysintern ("WUNTRACED", SCM_MAKINUM (WUNTRACED
));
1294 /* access() symbols. */
1295 scm_sysintern ("R_OK", SCM_MAKINUM (R_OK
));
1296 scm_sysintern ("W_OK", SCM_MAKINUM (W_OK
));
1297 scm_sysintern ("X_OK", SCM_MAKINUM (X_OK
));
1298 scm_sysintern ("F_OK", SCM_MAKINUM (F_OK
));
1301 scm_sysintern ("LC_COLLATE", SCM_MAKINUM (LC_COLLATE
));
1304 scm_sysintern ("LC_CTYPE", SCM_MAKINUM (LC_CTYPE
));
1307 scm_sysintern ("LC_MONETARY", SCM_MAKINUM (LC_MONETARY
));
1310 scm_sysintern ("LC_NUMERIC", SCM_MAKINUM (LC_NUMERIC
));
1313 scm_sysintern ("LC_TIME", SCM_MAKINUM (LC_TIME
));
1316 scm_sysintern ("LC_MESSAGES", SCM_MAKINUM (LC_MESSAGES
));
1319 scm_sysintern ("LC_ALL", SCM_MAKINUM (LC_ALL
));
1322 scm_sysintern ("PIPE_BUF", scm_long2num (PIPE_BUF
));
1325 #include "libguile/cpp_sig_symbols.c"
1326 #include "libguile/posix.x"