1 /* Copyright (C) 1995, 1996 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, 675 Mass Ave, Cambridge, MA 02139, USA.
17 * As a special exception, the Free Software Foundation gives permission
18 * for additional uses of the text contained in its release of GUILE.
20 * The exception is that, if you link the GUILE library with other files
21 * to produce an executable, this does not by itself cause the
22 * resulting executable to be covered by the GNU General Public License.
23 * Your use of that executable is in no way restricted on account of
24 * linking the GUILE library code into it.
26 * This exception does not however invalidate any other reasons why
27 * the executable file might be covered by the GNU General Public License.
29 * This exception applies only to the code released by the
30 * Free Software Foundation under the name GUILE. If you copy
31 * code from other Free Software Foundation releases into a copy of
32 * GUILE, as the General Public License permits, the exception does
33 * not apply to the code that you add in this way. To avoid misleading
34 * anyone as to the status of such modified files, you must delete
35 * this exception notice from them.
37 * If you write modifications of your own for GUILE, it is your choice
38 * whether to permit this exception to apply to your modifications.
39 * If you do not wish that, delete this exception notice.
51 #include "sequences.h"
59 #ifdef TIME_WITH_SYS_TIME
60 # include <sys/time.h>
64 # include <sys/time.h>
74 extern char *ttyname();
82 #ifdef HAVE_SYS_SELECT_H
83 #include <sys/select.h>
86 #include <sys/types.h>
93 # include <sys/wait.h>
96 # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
99 # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
106 #define SELECT_TYPE fd_set
107 #define SELECT_SET_SIZE FD_SETSIZE
109 #else /* no FD_SET */
111 /* Define the macros to access a single-int bitmap of descriptors. */
112 #define SELECT_SET_SIZE 32
113 #define SELECT_TYPE int
114 #define FD_SET(n, p) (*(p) |= (1 << (n)))
115 #define FD_CLR(n, p) (*(p) &= ~(1 << (n)))
116 #define FD_ISSET(n, p) (*(p) & (1 << (n)))
117 #define FD_ZERO(p) (*(p) = 0)
119 #endif /* no FD_SET */
121 extern FILE *popen ();
122 extern char ** environ
;
125 #include <sys/utsname.h>
129 # define NAMLEN(dirent) strlen((dirent)->d_name)
131 # define dirent direct
132 # define NAMLEN(dirent) (dirent)->d_namlen
134 # include <sys/ndir.h>
137 # include <sys/dir.h>
146 #ifdef HAVE_SETLOCALE
150 /* Some Unix systems don't define these. CPP hair is dangerous, but
151 this seems safe enough... */
168 /* On NextStep, <utime.h> doesn't define struct utime, unless we
169 #define _POSIX_SOURCE before #including it. I think this is less
170 of a kludge than defining struct utimbuf ourselves. */
171 #ifdef UTIMBUF_NEEDS_POSIX
172 #define _POSIX_SOURCE
175 #ifdef HAVE_SYS_UTIME_H
176 #include <sys/utime.h>
183 /* Please don't add any more #includes or #defines here. The hack
184 above means that _POSIX_SOURCE may be #defined, which will
185 encourage header files to do strange things. */
190 SCM_PROC (s_sys_pipe
, "pipe", 0, 0, 0, scm_sys_pipe
);
202 struct scm_port_table
* ptr
;
203 struct scm_port_table
* ptw
;
209 SCM_SYSERROR (s_sys_pipe
);
210 f_rd
= fdopen (fd
[0], "r");
213 SCM_SYSCALL (close (fd
[0]));
214 SCM_SYSCALL (close (fd
[1]));
215 SCM_SYSERROR (s_sys_pipe
);
217 f_wt
= fdopen (fd
[1], "w");
223 SCM_SYSCALL (close (fd
[1]));
225 SCM_SYSERROR (s_sys_pipe
);
227 ptr
= scm_add_to_port_table (p_rd
);
228 ptw
= scm_add_to_port_table (p_wt
);
229 SCM_SETPTAB_ENTRY (p_rd
, ptr
);
230 SCM_SETPTAB_ENTRY (p_wt
, ptw
);
231 SCM_CAR (p_rd
) = scm_tc16_fport
| scm_mode_bits ("r");
232 SCM_CAR (p_wt
) = scm_tc16_fport
| scm_mode_bits ("w");
233 SCM_SETSTREAM (p_rd
, (SCM
)f_rd
);
234 SCM_SETSTREAM (p_wt
, (SCM
)f_wt
);
237 return scm_cons (p_rd
, p_wt
);
242 SCM_PROC (s_sys_getgroups
, "getgroups", 0, 0, 0, scm_sys_getgroups
);
245 scm_sys_getgroups(void)
252 int ngroups
= getgroups (0, NULL
);
254 SCM_SYSERROR (s_sys_getgroups
);
261 groups
= (GETGROUPS_T
*) scm_must_malloc(ngroups
* sizeof(GETGROUPS_T
),
263 val
= getgroups(ngroups
, groups
);
266 scm_must_free((char *)groups
);
267 SCM_SYSERROR (s_sys_getgroups
);
269 SCM_SETCHARS(grps
, groups
); /* set up grps as a GC protect */
270 SCM_SETLENGTH(grps
, 0L + ngroups
* sizeof(GETGROUPS_T
), scm_tc7_string
);
272 ans
= scm_make_vector(SCM_MAKINUM(ngroups
), SCM_UNDEFINED
, SCM_BOOL_F
);
273 while (--ngroups
>= 0) SCM_VELTS(ans
)[ngroups
] = SCM_MAKINUM(groups
[ngroups
]);
274 SCM_SETCHARS(grps
, groups
); /* to make sure grps stays around. */
281 SCM_PROC (s_sys_getpwuid
, "getpw", 0, 1, 0, scm_sys_getpwuid
);
284 scm_sys_getpwuid (SCM user
)
287 scm_sys_getpwuid (user
)
292 struct passwd
*entry
;
295 result
= scm_make_vector (SCM_MAKINUM (7), SCM_UNSPECIFIED
, SCM_BOOL_F
);
296 ve
= SCM_VELTS (result
);
297 if (SCM_UNBNDP (user
) || SCM_FALSEP (user
))
300 SCM_SYSCALL (entry
= getpwent ());
302 else if (SCM_INUMP (user
))
305 entry
= getpwuid (SCM_INUM (user
));
309 SCM_ASSERT (SCM_NIMP (user
) && SCM_ROSTRINGP (user
), user
, SCM_ARG1
, s_sys_getpwuid
);
310 if (SCM_SUBSTRP (user
))
311 user
= scm_makfromstr (SCM_ROCHARS (user
), SCM_ROLENGTH (user
), 0);
313 entry
= getpwnam (SCM_ROCHARS (user
));
316 SCM_SYSERROR (s_sys_getpwuid
);
318 ve
[0] = scm_makfrom0str (entry
->pw_name
);
319 ve
[1] = scm_makfrom0str (entry
->pw_passwd
);
320 ve
[2] = scm_ulong2num ((unsigned long) entry
->pw_uid
);
321 ve
[3] = scm_ulong2num ((unsigned long) entry
->pw_gid
);
322 ve
[4] = scm_makfrom0str (entry
->pw_gecos
);
324 ve
[5] = scm_makfrom0str ("");
326 ve
[5] = scm_makfrom0str (entry
->pw_dir
);
327 if (!entry
->pw_shell
)
328 ve
[6] = scm_makfrom0str ("");
330 ve
[6] = scm_makfrom0str (entry
->pw_shell
);
337 SCM_PROC (s_setpwent
, "setpw", 0, 1, 0, scm_setpwent
);
340 scm_setpwent (SCM arg
)
347 if (SCM_UNBNDP (arg
) || SCM_FALSEP (arg
))
351 return SCM_UNSPECIFIED
;
356 /* Combines getgrgid and getgrnam. */
357 SCM_PROC (s_sys_getgrgid
, "getgr", 0, 1, 0, scm_sys_getgrgid
);
360 scm_sys_getgrgid (SCM name
)
363 scm_sys_getgrgid (name
)
370 result
= scm_make_vector (SCM_MAKINUM (4), SCM_UNSPECIFIED
, SCM_BOOL_F
);
371 ve
= SCM_VELTS (result
);
373 if (SCM_UNBNDP (name
) || (name
== SCM_BOOL_F
))
374 SCM_SYSCALL (entry
= getgrent ());
375 else if (SCM_INUMP (name
))
376 SCM_SYSCALL (entry
= getgrgid (SCM_INUM (name
)));
379 SCM_ASSERT (SCM_NIMP (name
) && SCM_STRINGP (name
), name
, SCM_ARG1
, s_sys_getgrgid
);
380 if (SCM_SUBSTRP (name
))
381 name
= scm_makfromstr (SCM_ROCHARS (name
), SCM_ROLENGTH (name
), 0);
382 SCM_SYSCALL (entry
= getgrnam (SCM_CHARS (name
)));
385 SCM_SYSERROR (s_sys_getgrgid
);
387 ve
[0] = scm_makfrom0str (entry
->gr_name
);
388 ve
[1] = scm_makfrom0str (entry
->gr_passwd
);
389 ve
[2] = scm_ulong2num ((unsigned long) entry
->gr_gid
);
390 ve
[3] = scm_makfromstrs (-1, entry
->gr_mem
);
397 SCM_PROC (s_setgrent
, "setgr", 0, 1, 0, scm_setgrent
);
400 scm_setgrent (SCM arg
)
407 if (SCM_UNBNDP (arg
) || SCM_FALSEP (arg
))
411 return SCM_UNSPECIFIED
;
416 SCM_PROC (s_sys_kill
, "kill", 2, 0, 0, scm_sys_kill
);
419 scm_sys_kill (SCM pid
, SCM sig
)
422 scm_sys_kill (pid
, sig
)
427 SCM_ASSERT (SCM_INUMP (pid
), pid
, SCM_ARG1
, s_sys_kill
);
428 SCM_ASSERT (SCM_INUMP (sig
), sig
, SCM_ARG2
, s_sys_kill
);
429 /* Signal values are interned in scm_init_posix(). */
430 if (kill ((int) SCM_INUM (pid
), (int) SCM_INUM (sig
)) != 0)
431 SCM_SYSERROR (s_sys_kill
);
432 return SCM_UNSPECIFIED
;
437 SCM_PROC (s_sys_waitpid
, "waitpid", 1, 1, 0, scm_sys_waitpid
);
440 scm_sys_waitpid (SCM pid
, SCM options
)
443 scm_sys_waitpid (pid
, options
)
452 SCM_ASSERT (SCM_INUMP (pid
), pid
, SCM_ARG1
, s_sys_waitpid
);
453 if (SCM_UNBNDP (options
))
457 SCM_ASSERT (SCM_INUMP (options
), options
, SCM_ARG2
, s_sys_waitpid
);
458 /* Flags are interned in scm_init_posix. */
459 ioptions
= SCM_INUM (options
);
461 SCM_SYSCALL (i
= waitpid (SCM_INUM (pid
), &status
, ioptions
));
463 SCM_SYSERROR (s_sys_waitpid
);
464 return scm_cons (SCM_MAKINUM (0L + i
), SCM_MAKINUM (0L + status
));
466 SCM_SYSMISSING (s_sys_waitpid
);
474 SCM_PROC (s_getppid
, "getppid", 0, 0, 0, scm_getppid
);
483 return SCM_MAKINUM (0L + getppid ());
488 SCM_PROC (s_getuid
, "getuid", 0, 0, 0, scm_getuid
);
497 return SCM_MAKINUM (0L + getuid ());
502 SCM_PROC (s_getgid
, "getgid", 0, 0, 0, scm_getgid
);
511 return SCM_MAKINUM (0L + getgid ());
516 SCM_PROC (s_geteuid
, "geteuid", 0, 0, 0, scm_geteuid
);
526 return SCM_MAKINUM (0L + geteuid ());
528 return SCM_MAKINUM (0L + getuid ());
534 SCM_PROC (s_getegid
, "getegid", 0, 0, 0, scm_getegid
);
544 return SCM_MAKINUM (0L + getegid ());
546 return SCM_MAKINUM (0L + getgid ());
551 SCM_PROC (s_sys_setuid
, "setuid", 1, 0, 0, scm_sys_setuid
);
554 scm_sys_setuid (SCM id
)
561 SCM_ASSERT (SCM_INUMP (id
), id
, SCM_ARG1
, s_sys_setuid
);
562 if (setuid (SCM_INUM (id
)) != 0)
563 SCM_SYSERROR (s_sys_setuid
);
564 return SCM_UNSPECIFIED
;
567 SCM_PROC (s_sys_setgid
, "setgid", 1, 0, 0, scm_sys_setgid
);
570 scm_sys_setgid (SCM id
)
577 SCM_ASSERT (SCM_INUMP (id
), id
, SCM_ARG1
, s_sys_setgid
);
578 if (setgid (SCM_INUM (id
)) != 0)
579 SCM_SYSERROR (s_sys_setgid
);
580 return SCM_UNSPECIFIED
;
583 SCM_PROC (s_sys_seteuid
, "seteuid", 1, 0, 0, scm_sys_seteuid
);
586 scm_sys_seteuid (SCM id
)
595 SCM_ASSERT (SCM_INUMP (id
), id
, SCM_ARG1
, s_sys_seteuid
);
597 rv
= seteuid (SCM_INUM (id
));
599 rv
= setuid (SCM_INUM (id
));
602 SCM_SYSERROR (s_sys_seteuid
);
603 return SCM_UNSPECIFIED
;
606 SCM_PROC (s_sys_setegid
, "setegid", 1, 0, 0, scm_sys_setegid
);
609 scm_sys_setegid (SCM id
)
618 SCM_ASSERT (SCM_INUMP (id
), id
, SCM_ARG1
, s_sys_setegid
);
620 rv
= setegid (SCM_INUM (id
));
622 rv
= setgid (SCM_INUM (id
));
625 SCM_SYSERROR (s_sys_setegid
);
626 return SCM_UNSPECIFIED
;
630 SCM_PROC (s_getpgrp
, "getpgrp", 0, 0, 0, scm_getpgrp
);
635 fn
= (int (*) ()) getpgrp
;
636 return SCM_MAKINUM (fn (0));
639 SCM_PROC (s_sys_setpgid
, "setpgid", 2, 0, 0, scm_setpgid
);
641 scm_setpgid (pid
, pgid
)
645 SCM_ASSERT (SCM_INUMP (pid
), pid
, SCM_ARG1
, s_sys_setpgid
);
646 SCM_ASSERT (SCM_INUMP (pgid
), pgid
, SCM_ARG2
, s_sys_setpgid
);
647 /* FIXME(?): may be known as setpgrp. */
648 if (setpgid (SCM_INUM (pid
), SCM_INUM (pgid
)) != 0)
649 SCM_SYSERROR (s_sys_setpgid
);
650 return SCM_UNSPECIFIED
;
652 SCM_SYSMISSING (s_sys_setpgid
);
658 SCM_PROC (s_sys_setsid
, "setsid", 0, 0, 0, scm_setsid
);
663 pid_t sid
= setsid ();
665 SCM_SYSERROR (s_sys_setsid
);
666 return SCM_UNSPECIFIED
;
668 SCM_SYSMISSING (s_sys_setsid
);
674 SCM_PROC (s_ttyname
, "ttyname", 1, 0, 0, scm_ttyname
);
677 scm_ttyname (SCM port
)
686 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPPORTP (port
), port
, SCM_ARG1
, s_ttyname
);
687 if (scm_tc16_fport
!= SCM_TYP16 (port
))
689 fd
= fileno ((FILE *)SCM_STREAM (port
));
691 SCM_SYSERROR (s_ttyname
);
692 SCM_SYSCALL (ans
= ttyname (fd
));
694 SCM_SYSERROR (s_ttyname
);
695 /* ans could be overwritten by another call to ttyname */
696 return (scm_makfrom0str (ans
));
700 SCM_PROC (s_sys_ctermid
, "ctermid", 0, 0, 0, scm_ctermid
);
705 char *result
= ctermid (NULL
);
707 SCM_SYSERROR (s_sys_ctermid
);
708 return scm_makfrom0str (result
);
710 SCM_SYSMISSING (s_sys_ctermid
);
716 SCM_PROC (s_sys_tcgetpgrp
, "tcgetpgrp", 1, 0, 0, scm_tcgetpgrp
);
721 #ifdef HAVE_TCGETPGRP
724 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPFPORTP (port
), port
, SCM_ARG1
, s_sys_tcgetpgrp
);
725 fd
= fileno ((FILE *)SCM_STREAM (port
));
726 if (fd
== -1 || (pgid
= tcgetpgrp (fd
)) == -1)
727 SCM_SYSERROR (s_sys_tcgetpgrp
);
728 return SCM_MAKINUM (pgid
);
730 SCM_SYSMISSING (s_sys_tcgetpgrp
);
736 SCM_PROC (s_sys_tcsetpgrp
, "tcsetpgrp", 2, 0, 0, scm_tcsetpgrp
);
738 scm_tcsetpgrp (port
, pgid
)
741 #ifdef HAVE_TCSETPGRP
743 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPFPORTP (port
), port
, SCM_ARG1
, s_sys_tcsetpgrp
);
744 SCM_ASSERT (SCM_INUMP (pgid
), pgid
, SCM_ARG2
, s_sys_tcsetpgrp
);
745 fd
= fileno ((FILE *)SCM_STREAM (port
));
746 if (fd
== -1 || tcsetpgrp (fd
, SCM_INUM (pgid
)) == -1)
747 SCM_SYSERROR (s_sys_tcsetpgrp
);
748 return SCM_UNSPECIFIED
;
750 SCM_SYSMISSING (s_sys_tcsetpgrp
);
756 /* Copy exec args from an SCM vector into a new C array. */
759 scm_convert_exec_args (SCM args
)
762 scm_convert_exec_args (args
)
770 num_args
= scm_ilength (args
);
772 scm_must_malloc ((num_args
+ 1) * sizeof (char *), s_ttyname
);
773 for (i
= 0; SCM_NNULLP (args
); args
= SCM_CDR (args
), ++i
)
778 SCM_ASSERT (SCM_NIMP (SCM_CAR (args
)) && SCM_ROSTRINGP (SCM_CAR (args
)), SCM_CAR (args
),
779 "wrong type in SCM_ARG", "exec arg");
780 len
= 1 + SCM_ROLENGTH (SCM_CAR (args
));
781 dst
= (char *) scm_must_malloc ((long) len
, s_ttyname
);
782 src
= SCM_ROCHARS (SCM_CAR (args
));
792 SCM_PROC (s_sys_execl
, "execl", 0, 0, 1, scm_sys_execl
);
795 scm_sys_execl (SCM args
)
803 SCM filename
= SCM_CAR (args
);
804 SCM_ASSERT (SCM_NIMP (filename
) && SCM_ROSTRINGP (filename
), filename
, SCM_ARG1
, s_sys_execl
);
805 if (SCM_SUBSTRP (filename
))
806 filename
= scm_makfromstr (SCM_ROCHARS (filename
), SCM_ROLENGTH (filename
), 0);
807 args
= SCM_CDR (args
);
808 execargv
= scm_convert_exec_args (args
);
809 execv (SCM_ROCHARS (filename
), execargv
);
810 SCM_SYSERROR (s_sys_execl
);
815 SCM_PROC (s_sys_execlp
, "execlp", 0, 0, 1, scm_sys_execlp
);
818 scm_sys_execlp (SCM args
)
821 scm_sys_execlp (args
)
826 SCM filename
= SCM_CAR (args
);
827 SCM_ASSERT (SCM_NIMP (filename
) && SCM_ROSTRINGP (filename
), filename
, SCM_ARG1
, s_sys_execlp
);
828 if (SCM_SUBSTRP (filename
))
829 filename
= scm_makfromstr (SCM_ROCHARS (filename
), SCM_ROLENGTH (filename
), 0);
830 args
= SCM_CDR (args
);
831 execargv
= scm_convert_exec_args (args
);
832 execvp (SCM_ROCHARS (filename
), execargv
);
833 SCM_SYSERROR (s_sys_execlp
);
838 /* Flushing streams etc., is not done here. */
839 SCM_PROC (s_sys_fork
, "fork", 0, 0, 0, scm_sys_fork
);
851 SCM_SYSERROR (s_sys_fork
);
852 return SCM_MAKINUM (0L+pid
);
856 SCM_PROC (s_sys_uname
, "uname", 0, 0, 0, scm_sys_uname
);
867 SCM ans
= scm_make_vector(SCM_MAKINUM(5), SCM_UNSPECIFIED
, SCM_BOOL_F
);
868 SCM
*ve
= SCM_VELTS (ans
);
870 return SCM_MAKINUM (errno
);
871 ve
[0] = scm_makfrom0str (buf
.sysname
);
872 ve
[1] = scm_makfrom0str (buf
.nodename
);
873 ve
[2] = scm_makfrom0str (buf
.release
);
874 ve
[3] = scm_makfrom0str (buf
.version
);
875 ve
[4] = scm_makfrom0str (buf
.machine
);
878 ve[5] = scm_makfrom0str (buf.domainname);
882 SCM_SYSMISSING (s_sys_uname
);
888 SCM_PROC (s_environ
, "environ", 0, 1, 0, scm_environ
);
891 scm_environ (SCM env
)
898 if (SCM_UNBNDP (env
))
899 return scm_makfromstrs (-1, environ
);
905 SCM_ASSERT (SCM_NULLP (env
) || (SCM_NIMP (env
) && SCM_CONSP (env
)),
906 env
, SCM_ARG1
, s_environ
);
907 num_strings
= scm_ilength (env
);
908 new_environ
= (char **) scm_must_malloc ((num_strings
+ 1)
911 while (SCM_NNULLP (env
))
915 SCM_ASSERT (SCM_NIMP (SCM_CAR (env
)) && SCM_ROSTRINGP (SCM_CAR (env
)), env
, SCM_ARG1
,
917 len
= 1 + SCM_ROLENGTH (SCM_CAR (env
));
918 new_environ
[i
] = scm_must_malloc ((long) len
, s_environ
);
919 src
= SCM_ROCHARS (SCM_CAR (env
));
921 new_environ
[i
][len
] = src
[len
];
926 /* Free the old environment, except when called for the first
931 static int first
= 1;
934 for (ep
= environ
; *ep
!= NULL
; ep
++)
936 scm_must_free ((char *) environ
);
940 environ
= new_environ
;
941 return SCM_UNSPECIFIED
;
946 SCM_PROC (s_open_pipe
, "open-pipe", 2, 0, 0, scm_open_pipe
);
949 scm_open_pipe (SCM pipestr
, SCM modes
)
952 scm_open_pipe (pipestr
, modes
)
959 struct scm_port_table
* pt
;
961 SCM_ASSERT (SCM_NIMP (pipestr
) && SCM_ROSTRINGP (pipestr
), pipestr
, SCM_ARG1
, s_open_pipe
);
962 if (SCM_SUBSTRP (pipestr
))
963 pipestr
= scm_makfromstr (SCM_ROCHARS (pipestr
), SCM_ROLENGTH (pipestr
), 0);
964 SCM_ASSERT (SCM_NIMP (modes
) && SCM_ROSTRINGP (modes
), modes
, SCM_ARG2
, s_open_pipe
);
965 if (SCM_SUBSTRP (modes
))
966 modes
= scm_makfromstr (SCM_ROCHARS (modes
), SCM_ROLENGTH (modes
), 0);
969 scm_ignore_signals ();
970 SCM_SYSCALL (f
= popen (SCM_ROCHARS (pipestr
), SCM_ROCHARS (modes
)));
971 scm_unignore_signals ();
973 SCM_SYSERROR (s_open_pipe
);
974 pt
= scm_add_to_port_table (z
);
975 SCM_SETPTAB_ENTRY (z
, pt
);
976 SCM_CAR (z
) = scm_tc16_pipe
| SCM_OPN
977 | (strchr (SCM_ROCHARS (modes
), 'r') ? SCM_RDNG
: SCM_WRTNG
);
978 SCM_SETSTREAM (z
, (SCM
)f
);
984 SCM_PROC (s_open_input_pipe
, "open-input-pipe", 1, 0, 0, scm_open_input_pipe
);
987 scm_open_input_pipe(SCM pipestr
)
990 scm_open_input_pipe(pipestr
)
994 return scm_open_pipe(pipestr
, scm_makfromstr("r", (sizeof "r")-1, 0));
997 SCM_PROC (s_open_output_pipe
, "open-output-pipe", 1, 0, 0, scm_open_output_pipe
);
1000 scm_open_output_pipe(SCM pipestr
)
1003 scm_open_output_pipe(pipestr
)
1007 return scm_open_pipe(pipestr
, scm_makfromstr("w", (sizeof "w")-1, 0));
1011 SCM_PROC (s_sys_utime
, "utime", 1, 2, 0, scm_sys_utime
);
1014 scm_sys_utime (SCM pathname
, SCM actime
, SCM modtime
)
1017 scm_sys_utime (pathname
, actime
, modtime
)
1024 struct utimbuf utm_tmp
;
1026 SCM_ASSERT (SCM_NIMP (pathname
) && SCM_STRINGP (pathname
), pathname
, SCM_ARG1
, s_sys_utime
);
1028 if (SCM_UNBNDP (actime
))
1029 SCM_SYSCALL (time (&utm_tmp
.actime
));
1031 utm_tmp
.actime
= scm_num2ulong (actime
, (char *) SCM_ARG2
, s_sys_utime
);
1033 if (SCM_UNBNDP (modtime
))
1034 SCM_SYSCALL (time (&utm_tmp
.modtime
));
1036 utm_tmp
.modtime
= scm_num2ulong (modtime
, (char *) SCM_ARG3
, s_sys_utime
);
1038 SCM_SYSCALL (rv
= utime (SCM_CHARS (pathname
), &utm_tmp
));
1040 SCM_SYSERROR (s_sys_utime
);
1041 return SCM_UNSPECIFIED
;
1044 SCM_PROC (s_sys_access
, "access?", 2, 0, 0, scm_sys_access
);
1047 scm_sys_access (SCM path
, SCM how
)
1050 scm_sys_access (path
, how
)
1057 SCM_ASSERT (SCM_NIMP (path
) && SCM_ROSTRINGP (path
), path
, SCM_ARG1
, s_sys_access
);
1058 if (SCM_SUBSTRP (path
))
1059 path
= scm_makfromstr (SCM_ROCHARS (path
), SCM_ROLENGTH (path
), 0);
1060 SCM_ASSERT (SCM_INUMP (how
), how
, SCM_ARG2
, s_sys_access
);
1061 rv
= access (SCM_ROCHARS (path
), SCM_INUM (how
));
1062 return rv
? SCM_BOOL_F
: SCM_BOOL_T
;
1065 SCM_PROC (s_getpid
, "getpid", 0, 0, 0, scm_getpid
);
1074 return SCM_MAKINUM ((unsigned long) getpid ());
1077 SCM_PROC (s_sys_putenv
, "putenv", 1, 0, 0, scm_sys_putenv
);
1080 scm_sys_putenv (SCM str
)
1083 scm_sys_putenv (str
)
1088 SCM_ASSERT (SCM_NIMP (str
) && SCM_STRINGP (str
), str
, SCM_ARG1
, s_sys_putenv
);
1089 return putenv (SCM_CHARS (str
)) ? SCM_MAKINUM (errno
) : SCM_BOOL_T
;
1091 SCM_SYSMISSING (s_sys_putenv
);
1097 SCM_PROC (s_read_line
, "read-line", 0, 2, 0, scm_read_line
);
1100 scm_read_line (SCM port
, SCM include_terminator
)
1103 scm_read_line (port
, include_terminator
)
1105 SCM include_terminator
;
1115 tok_buf
= scm_makstr ((long) len
, 0);
1116 p
= SCM_CHARS (tok_buf
);
1117 if (SCM_UNBNDP (port
))
1120 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPINPORTP (port
), port
, SCM_ARG1
, s_read_line
);
1122 if (SCM_UNBNDP (include_terminator
))
1125 include
= SCM_NFALSEP (include_terminator
);
1127 if (EOF
== (c
= scm_gen_getc (port
)))
1133 case SCM_LINE_INCREMENTORS
:
1136 p
= scm_grow_tok_buf (&tok_buf
);
1137 len
= SCM_LENGTH (tok_buf
);
1144 return scm_vector_set_length_x (tok_buf
, (SCM
) SCM_MAKINUM (j
));
1149 p
= scm_grow_tok_buf (&tok_buf
);
1150 len
= SCM_LENGTH (tok_buf
);
1153 c
= scm_gen_getc (port
);
1159 SCM_PROC (s_read_line_x
, "read-line!", 1, 1, 0, scm_read_line_x
);
1162 scm_read_line_x (SCM str
, SCM port
)
1165 scm_read_line_x (str
, port
)
1174 SCM_ASSERT (SCM_NIMP (str
) && SCM_STRINGP (str
), str
, SCM_ARG1
, s_read_line_x
);
1175 p
= SCM_CHARS (str
);
1176 len
= SCM_LENGTH (str
);
1178 (port
) port
= scm_cur_inp
;
1180 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPINPORTP (port
), port
, SCM_ARG2
, s_read_line_x
);
1181 c
= scm_gen_getc (port
);
1188 case SCM_LINE_INCREMENTORS
:
1190 return SCM_MAKINUM (j
);
1194 scm_gen_ungetc (c
, port
);
1198 c
= scm_gen_getc (port
);
1203 SCM_PROC (s_write_line
, "write-line", 1, 1, 0, scm_write_line
);
1206 scm_write_line (SCM obj
, SCM port
)
1209 scm_write_line (obj
, port
)
1214 scm_display (obj
, port
);
1215 return scm_newline (port
);
1218 SCM_PROC (s_setlocale
, "setlocale", 1, 1, 0, scm_setlocale
);
1221 scm_setlocale (SCM category
, SCM locale
)
1224 scm_setlocale (category
, locale
)
1229 #ifdef HAVE_SETLOCALE
1233 SCM_ASSERT (SCM_INUMP (category
), category
, SCM_ARG1
, s_setlocale
);
1234 if (SCM_UNBNDP (locale
))
1240 SCM_ASSERT (SCM_NIMP (locale
) && SCM_STRINGP (locale
), locale
, SCM_ARG2
, s_setlocale
);
1241 clocale
= SCM_CHARS (locale
);
1244 rv
= setlocale (SCM_INUM (category
), clocale
);
1246 SCM_SYSERROR (s_setlocale
);
1247 return scm_makfrom0str (rv
);
1249 SCM_SYSMISSING (s_setlocale
);
1255 SCM_PROC (s_strftime
, "strftime", 2, 0, 0, scm_strftime
);
1258 scm_strftime (SCM format
, SCM stime
)
1261 scm_strftime (format
, stime
)
1274 SCM_ASSERT (SCM_NIMP (format
) && SCM_STRINGP (format
), format
, SCM_ARG1
, s_strftime
);
1275 SCM_ASSERT (SCM_NIMP (stime
) && SCM_VECTORP (stime
) && scm_obj_length (stime
) == 9,
1276 stime
, SCM_ARG2
, s_strftime
);
1278 fmt
= SCM_ROCHARS (format
);
1279 len
= SCM_ROLENGTH (format
);
1281 #define tm_deref scm_num2long (SCM_VELTS (stime)[n++], (char *)SCM_ARG2, s_strftime)
1283 t
.tm_sec
= tm_deref
;
1284 t
.tm_min
= tm_deref
;
1285 t
.tm_hour
= tm_deref
;
1286 t
.tm_mday
= tm_deref
;
1287 t
.tm_mon
= tm_deref
;
1288 t
.tm_year
= tm_deref
;
1289 /* not used by mktime.
1290 t.tm_wday = tm_deref;
1291 t.tm_yday = tm_deref; */
1292 t
.tm_isdst
= tm_deref
;
1295 /* fill in missing fields and set the timezone. */
1298 tbuf
= scm_must_malloc (size
, s_strftime
);
1299 while ((len
= strftime (tbuf
, size
, fmt
, &t
)) == size
)
1301 scm_must_free (tbuf
);
1303 tbuf
= scm_must_malloc (size
, s_strftime
);
1305 return scm_makfromstr (tbuf
, len
, 0);
1308 SCM_PROC (s_sys_strptime
, "strptime", 2, 0, 0, scm_sys_strptime
);
1311 scm_sys_strptime (SCM format
, SCM string
)
1314 scm_sys_strptime (format
, string
)
1319 #ifdef HAVE_STRPTIME
1323 char *fmt
, *str
, *rest
;
1326 SCM_ASSERT (SCM_NIMP (format
) && SCM_ROSTRINGP (format
), format
, SCM_ARG1
, s_sys_strptime
);
1327 if (SCM_SUBSTRP (format
))
1328 format
= scm_makfromstr (SCM_ROCHARS (format
), SCM_ROLENGTH (format
), 0);
1329 SCM_ASSERT (SCM_NIMP (string
) && SCM_ROSTRINGP (string
), string
, SCM_ARG2
, s_sys_strptime
);
1330 if (SCM_SUBSTRP (string
))
1331 string
= scm_makfromstr (SCM_ROCHARS (string
), SCM_ROLENGTH (string
), 0);
1333 fmt
= SCM_CHARS (format
);
1334 str
= SCM_CHARS (string
);
1336 /* initialize the struct tm */
1337 #define tm_init(field) t.field = 0
1350 rest
= strptime (str
, fmt
, &t
);
1354 SCM_SYSERROR (s_sys_strptime
);
1356 stime
= scm_make_vector (SCM_MAKINUM (9), scm_long2num (0), SCM_UNDEFINED
);
1358 #define stime_set(val) scm_vector_set_x (stime, SCM_MAKINUM (n++), scm_long2num (t.val));
1362 stime_set (tm_hour
);
1363 stime_set (tm_mday
);
1365 stime_set (tm_year
);
1366 stime_set (tm_wday
);
1367 stime_set (tm_yday
);
1368 stime_set (tm_isdst
);
1371 return scm_cons (stime
, scm_makfrom0str (rest
));
1373 SCM_SYSMISSING (s_sys_strptime
);
1379 SCM_PROC (s_sys_mknod
, "mknod", 3, 0, 0, scm_sys_mknod
);
1382 scm_sys_mknod(SCM path
, SCM mode
, SCM dev
)
1385 scm_sys_mknod(path
, mode
, dev
)
1393 SCM_ASSERT(SCM_NIMP(path
) && SCM_STRINGP(path
), path
, SCM_ARG1
, s_sys_mknod
);
1394 SCM_ASSERT(SCM_INUMP(mode
), mode
, SCM_ARG2
, s_sys_mknod
);
1395 SCM_ASSERT(SCM_INUMP(dev
), dev
, SCM_ARG3
, s_sys_mknod
);
1396 SCM_SYSCALL(val
= mknod(SCM_CHARS(path
), SCM_INUM(mode
), SCM_INUM(dev
)));
1398 SCM_SYSERROR (s_sys_mknod
);
1399 return SCM_UNSPECIFIED
;
1401 SCM_SYSMISSING (s_sys_mknod
);
1408 SCM_PROC (s_sys_nice
, "nice", 1, 0, 0, scm_sys_nice
);
1411 scm_sys_nice(SCM incr
)
1419 SCM_ASSERT(SCM_INUMP(incr
), incr
, SCM_ARG1
, s_sys_nice
);
1420 if (nice(SCM_INUM(incr
)) != 0)
1421 SCM_SYSERROR (s_sys_nice
);
1422 return SCM_UNSPECIFIED
;
1424 SCM_SYSMISSING (s_sys_nice
);
1431 SCM_PROC (s_sync
, "sync", 0, 0, 0, scm_sync
);
1443 SCM_SYSMISSING (s_sync
);
1452 scm_init_posix (void)
1458 scm_add_feature ("posix");
1460 scm_add_feature ("EIDs");
1463 scm_sysintern ("WAIT_ANY", SCM_MAKINUM (WAIT_ANY
));
1466 scm_sysintern ("WAIT_MYPGRP", SCM_MAKINUM (WAIT_MYPGRP
));
1469 scm_sysintern ("WNOHANG", SCM_MAKINUM (WNOHANG
));
1472 scm_sysintern ("WUNTRACED", SCM_MAKINUM (WUNTRACED
));
1476 scm_sysintern ("EINTR", SCM_MAKINUM (EINTR
));
1480 scm_sysintern ("SIGHUP", SCM_MAKINUM (SIGHUP
));
1483 scm_sysintern ("SIGINT", SCM_MAKINUM (SIGINT
));
1486 scm_sysintern ("SIGQUIT", SCM_MAKINUM (SIGQUIT
));
1489 scm_sysintern ("SIGILL", SCM_MAKINUM (SIGILL
));
1492 scm_sysintern ("SIGTRAP", SCM_MAKINUM (SIGTRAP
));
1495 scm_sysintern ("SIGABRT", SCM_MAKINUM (SIGABRT
));
1498 scm_sysintern ("SIGIOT", SCM_MAKINUM (SIGIOT
));
1501 scm_sysintern ("SIGBUS", SCM_MAKINUM (SIGBUS
));
1504 scm_sysintern ("SIGFPE", SCM_MAKINUM (SIGFPE
));
1507 scm_sysintern ("SIGKILL", SCM_MAKINUM (SIGKILL
));
1510 scm_sysintern ("SIGUSR1", SCM_MAKINUM (SIGUSR1
));
1513 scm_sysintern ("SIGSEGV", SCM_MAKINUM (SIGSEGV
));
1516 scm_sysintern ("SIGUSR2", SCM_MAKINUM (SIGUSR2
));
1519 scm_sysintern ("SIGPIPE", SCM_MAKINUM (SIGPIPE
));
1522 scm_sysintern ("SIGALRM", SCM_MAKINUM (SIGALRM
));
1525 scm_sysintern ("SIGTERM", SCM_MAKINUM (SIGTERM
));
1528 scm_sysintern ("SIGSTKFLT", SCM_MAKINUM (SIGSTKFLT
));
1531 scm_sysintern ("SIGCHLD", SCM_MAKINUM (SIGCHLD
));
1534 scm_sysintern ("SIGCONT", SCM_MAKINUM (SIGCONT
));
1537 scm_sysintern ("SIGSTOP", SCM_MAKINUM (SIGSTOP
));
1540 scm_sysintern ("SIGTSTP", SCM_MAKINUM (SIGTSTP
));
1543 scm_sysintern ("SIGTTIN", SCM_MAKINUM (SIGTTIN
));
1546 scm_sysintern ("SIGTTOU", SCM_MAKINUM (SIGTTOU
));
1549 scm_sysintern ("SIGIO", SCM_MAKINUM (SIGIO
));
1552 scm_sysintern ("SIGPOLL", SCM_MAKINUM (SIGPOLL
));
1555 scm_sysintern ("SIGURG", SCM_MAKINUM (SIGURG
));
1558 scm_sysintern ("SIGXCPU", SCM_MAKINUM (SIGXCPU
));
1561 scm_sysintern ("SIGXFSZ", SCM_MAKINUM (SIGXFSZ
));
1564 scm_sysintern ("SIGVTALRM", SCM_MAKINUM (SIGVTALRM
));
1567 scm_sysintern ("SIGPROF", SCM_MAKINUM (SIGPROF
));
1570 scm_sysintern ("SIGWINCH", SCM_MAKINUM (SIGWINCH
));
1573 scm_sysintern ("SIGLOST", SCM_MAKINUM (SIGLOST
));
1576 scm_sysintern ("SIGPWR", SCM_MAKINUM (SIGPWR
));
1578 /* access() symbols. */
1579 scm_sysintern ("R_OK", SCM_MAKINUM (R_OK
));
1580 scm_sysintern ("W_OK", SCM_MAKINUM (W_OK
));
1581 scm_sysintern ("X_OK", SCM_MAKINUM (X_OK
));
1582 scm_sysintern ("F_OK", SCM_MAKINUM (F_OK
));
1585 scm_sysintern ("LC_COLLATE", SCM_MAKINUM (LC_COLLATE
));
1588 scm_sysintern ("LC_CTYPE", SCM_MAKINUM (LC_CTYPE
));
1591 scm_sysintern ("LC_MONETARY", SCM_MAKINUM (LC_MONETARY
));
1594 scm_sysintern ("LC_NUMERIC", SCM_MAKINUM (LC_NUMERIC
));
1597 scm_sysintern ("LC_TIME", SCM_MAKINUM (LC_TIME
));
1600 scm_sysintern ("LC_MESSAGES", SCM_MAKINUM (LC_MESSAGES
));
1603 scm_sysintern ("LC_ALL", SCM_MAKINUM (LC_ALL
));