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 #ifdef TIME_WITH_SYS_TIME
52 # include <sys/time.h>
56 # include <sys/time.h>
66 extern char *ttyname();
70 #ifdef HAVE_SYS_SELECT_H
71 #include <sys/select.h>
74 #include <sys/types.h>
81 # include <sys/wait.h>
84 # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
87 # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
94 #define SELECT_TYPE fd_set
95 #define SELECT_SET_SIZE FD_SETSIZE
99 /* Define the macros to access a single-int bitmap of descriptors. */
100 #define SELECT_SET_SIZE 32
101 #define SELECT_TYPE int
102 #define FD_SET(n, p) (*(p) |= (1 << (n)))
103 #define FD_CLR(n, p) (*(p) &= ~(1 << (n)))
104 #define FD_ISSET(n, p) (*(p) & (1 << (n)))
105 #define FD_ZERO(p) (*(p) = 0)
107 #endif /* no FD_SET */
109 extern FILE *popen ();
110 extern char ** environ
;
113 #include <sys/utsname.h>
117 # define NAMLEN(dirent) strlen((dirent)->d_name)
119 # define dirent direct
120 # define NAMLEN(dirent) (dirent)->d_namlen
122 # include <sys/ndir.h>
125 # include <sys/dir.h>
134 #ifdef HAVE_SETLOCALE
142 SCM_PROC (s_sys_pipe
, "pipe", 0, 0, 0, scm_sys_pipe
);
154 struct scm_port_table
* ptr
;
155 struct scm_port_table
* ptw
;
161 SCM_SYSERROR (s_sys_pipe
);
162 f_rd
= fdopen (fd
[0], "r");
165 SCM_SYSCALL (close (fd
[0]));
166 SCM_SYSCALL (close (fd
[1]));
167 SCM_SYSERROR (s_sys_pipe
);
169 f_wt
= fdopen (fd
[1], "w");
175 SCM_SYSCALL (close (fd
[1]));
177 SCM_SYSERROR (s_sys_pipe
);
179 ptr
= scm_add_to_port_table (p_rd
);
180 ptw
= scm_add_to_port_table (p_wt
);
181 SCM_SETPTAB_ENTRY (p_rd
, ptr
);
182 SCM_SETPTAB_ENTRY (p_wt
, ptw
);
183 SCM_CAR (p_rd
) = scm_tc16_fport
| scm_mode_bits ("r");
184 SCM_CAR (p_wt
) = scm_tc16_fport
| scm_mode_bits ("w");
185 SCM_SETSTREAM (p_rd
, (SCM
)f_rd
);
186 SCM_SETSTREAM (p_wt
, (SCM
)f_wt
);
189 return scm_cons (p_rd
, p_wt
);
194 SCM_PROC (s_sys_getgroups
, "getgroups", 0, 0, 0, scm_sys_getgroups
);
197 scm_sys_getgroups(void)
204 int ngroups
= getgroups (0, NULL
);
206 SCM_SYSERROR (s_sys_getgroups
);
213 groups
= (gid_t
*)scm_must_malloc(ngroups
* sizeof(GETGROUPS_T
),
215 val
= getgroups(ngroups
, groups
);
218 scm_must_free((char *)groups
);
219 SCM_SYSERROR (s_sys_getgroups
);
221 SCM_SETCHARS(grps
, groups
); /* set up grps as a GC protect */
222 SCM_SETLENGTH(grps
, 0L + ngroups
* sizeof(GETGROUPS_T
), scm_tc7_string
);
224 ans
= scm_make_vector(SCM_MAKINUM(ngroups
), SCM_UNDEFINED
, SCM_BOOL_F
);
225 while (--ngroups
>= 0) SCM_VELTS(ans
)[ngroups
] = SCM_MAKINUM(groups
[ngroups
]);
226 SCM_SETCHARS(grps
, groups
); /* to make sure grps stays around. */
233 SCM_PROC (s_sys_getpwuid
, "getpw", 0, 1, 0, scm_sys_getpwuid
);
236 scm_sys_getpwuid (SCM user
)
239 scm_sys_getpwuid (user
)
244 struct passwd
*entry
;
247 result
= scm_make_vector (SCM_MAKINUM (7), SCM_UNSPECIFIED
, SCM_BOOL_F
);
248 ve
= SCM_VELTS (result
);
249 if (SCM_UNBNDP (user
) || SCM_FALSEP (user
))
252 SCM_SYSCALL (entry
= getpwent ());
254 else if (SCM_INUMP (user
))
257 entry
= getpwuid (SCM_INUM (user
));
261 SCM_ASSERT (SCM_NIMP (user
) && SCM_ROSTRINGP (user
), user
, SCM_ARG1
, s_sys_getpwuid
);
262 if (SCM_SUBSTRP (user
))
263 user
= scm_makfromstr (SCM_ROCHARS (user
), SCM_ROLENGTH (user
), 0);
265 entry
= getpwnam (SCM_ROCHARS (user
));
268 SCM_SYSERROR (s_sys_getpwuid
);
270 ve
[0] = scm_makfrom0str (entry
->pw_name
);
271 ve
[1] = scm_makfrom0str (entry
->pw_passwd
);
272 ve
[2] = scm_ulong2num ((unsigned long) entry
->pw_uid
);
273 ve
[3] = scm_ulong2num ((unsigned long) entry
->pw_gid
);
274 ve
[4] = scm_makfrom0str (entry
->pw_gecos
);
276 ve
[5] = scm_makfrom0str ("");
278 ve
[5] = scm_makfrom0str (entry
->pw_dir
);
279 if (!entry
->pw_shell
)
280 ve
[6] = scm_makfrom0str ("");
282 ve
[6] = scm_makfrom0str (entry
->pw_shell
);
289 SCM_PROC (s_setpwent
, "setpw", 0, 1, 0, scm_setpwent
);
292 scm_setpwent (SCM arg
)
299 if (SCM_UNBNDP (arg
) || SCM_FALSEP (arg
))
303 return SCM_UNSPECIFIED
;
308 /* Combines getgrgid and getgrnam. */
309 SCM_PROC (s_sys_getgrgid
, "getgr", 0, 1, 0, scm_sys_getgrgid
);
312 scm_sys_getgrgid (SCM name
)
315 scm_sys_getgrgid (name
)
322 result
= scm_make_vector (SCM_MAKINUM (4), SCM_UNSPECIFIED
, SCM_BOOL_F
);
323 ve
= SCM_VELTS (result
);
325 if (SCM_UNBNDP (name
) || (name
== SCM_BOOL_F
))
326 SCM_SYSCALL (entry
= getgrent ());
327 else if (SCM_INUMP (name
))
328 SCM_SYSCALL (entry
= getgrgid (SCM_INUM (name
)));
331 SCM_ASSERT (SCM_NIMP (name
) && SCM_STRINGP (name
), name
, SCM_ARG1
, s_sys_getgrgid
);
332 if (SCM_SUBSTRP (name
))
333 name
= scm_makfromstr (SCM_ROCHARS (name
), SCM_ROLENGTH (name
), 0);
334 SCM_SYSCALL (entry
= getgrnam (SCM_CHARS (name
)));
337 SCM_SYSERROR (s_sys_getgrgid
);
339 ve
[0] = scm_makfrom0str (entry
->gr_name
);
340 ve
[1] = scm_makfrom0str (entry
->gr_passwd
);
341 ve
[2] = scm_ulong2num ((unsigned long) entry
->gr_gid
);
342 ve
[3] = scm_makfromstrs (-1, entry
->gr_mem
);
349 SCM_PROC (s_setgrent
, "setgr", 0, 1, 0, scm_setgrent
);
352 scm_setgrent (SCM arg
)
359 if (SCM_UNBNDP (arg
) || SCM_FALSEP (arg
))
363 return SCM_UNSPECIFIED
;
368 SCM_PROC (s_sys_kill
, "kill", 2, 0, 0, scm_sys_kill
);
371 scm_sys_kill (SCM pid
, SCM sig
)
374 scm_sys_kill (pid
, sig
)
379 SCM_ASSERT (SCM_INUMP (pid
), pid
, SCM_ARG1
, s_sys_kill
);
380 SCM_ASSERT (SCM_INUMP (sig
), sig
, SCM_ARG2
, s_sys_kill
);
381 /* Signal values are interned in scm_init_posix(). */
382 if (kill ((int) SCM_INUM (pid
), (int) SCM_INUM (sig
)) != 0)
383 SCM_SYSERROR (s_sys_kill
);
384 return SCM_UNSPECIFIED
;
389 SCM_PROC (s_sys_waitpid
, "waitpid", 1, 1, 0, scm_sys_waitpid
);
392 scm_sys_waitpid (SCM pid
, SCM options
)
395 scm_sys_waitpid (pid
, options
)
403 SCM_ASSERT (SCM_INUMP (pid
), pid
, SCM_ARG1
, s_sys_waitpid
);
404 if (SCM_UNBNDP (options
))
408 SCM_ASSERT (SCM_INUMP (options
), options
, SCM_ARG2
, s_sys_waitpid
);
409 /* Flags are interned in scm_init_posix. */
410 ioptions
= SCM_INUM (options
);
412 SCM_SYSCALL (i
= waitpid (SCM_INUM (pid
), &status
, ioptions
));
414 SCM_SYSERROR (s_sys_waitpid
);
415 return scm_cons (SCM_MAKINUM (0L + i
), SCM_MAKINUM (0L + status
));
420 SCM_PROC (s_getppid
, "getppid", 0, 0, 0, scm_getppid
);
429 return SCM_MAKINUM (0L + getppid ());
434 SCM_PROC (s_getuid
, "getuid", 0, 0, 0, scm_getuid
);
443 return SCM_MAKINUM (0L + getuid ());
448 SCM_PROC (s_getgid
, "getgid", 0, 0, 0, scm_getgid
);
457 return SCM_MAKINUM (0L + getgid ());
462 SCM_PROC (s_geteuid
, "geteuid", 0, 0, 0, scm_geteuid
);
472 return SCM_MAKINUM (0L + geteuid ());
474 return SCM_MAKINUM (0L + getuid ());
480 SCM_PROC (s_getegid
, "getegid", 0, 0, 0, scm_getegid
);
490 return SCM_MAKINUM (0L + getegid ());
492 return SCM_MAKINUM (0L + getgid ());
497 SCM_PROC (s_sys_setuid
, "setuid", 1, 0, 0, scm_sys_setuid
);
500 scm_sys_setuid (SCM id
)
507 SCM_ASSERT (SCM_INUMP (id
), id
, SCM_ARG1
, s_sys_setuid
);
508 if (setuid (SCM_INUM (id
)) != 0)
509 SCM_SYSERROR (s_sys_setuid
);
510 return SCM_UNSPECIFIED
;
513 SCM_PROC (s_sys_setgid
, "setgid", 1, 0, 0, scm_sys_setgid
);
516 scm_sys_setgid (SCM id
)
523 SCM_ASSERT (SCM_INUMP (id
), id
, SCM_ARG1
, s_sys_setgid
);
524 if (setgid (SCM_INUM (id
)) != 0)
525 SCM_SYSERROR (s_sys_setgid
);
526 return SCM_UNSPECIFIED
;
529 SCM_PROC (s_sys_seteuid
, "seteuid", 1, 0, 0, scm_sys_seteuid
);
532 scm_sys_seteuid (SCM id
)
541 SCM_ASSERT (SCM_INUMP (id
), id
, SCM_ARG1
, s_sys_seteuid
);
543 rv
= seteuid (SCM_INUM (id
));
545 rv
= setuid (SCM_INUM (id
));
548 SCM_SYSERROR (s_sys_seteuid
);
549 return SCM_UNSPECIFIED
;
552 SCM_PROC (s_sys_setegid
, "setegid", 1, 0, 0, scm_sys_setegid
);
555 scm_sys_setegid (SCM id
)
564 SCM_ASSERT (SCM_INUMP (id
), id
, SCM_ARG1
, s_sys_setegid
);
566 rv
= setegid (SCM_INUM (id
));
568 rv
= setgid (SCM_INUM (id
));
571 SCM_SYSERROR (s_sys_setegid
);
572 return SCM_UNSPECIFIED
;
576 SCM_PROC (s_getpgrp
, "getpgrp", 0, 0, 0, scm_getpgrp
);
582 return SCM_MAKINUM (fn (0));
585 SCM_PROC (s_setpgid
, "setpgid", 2, 0, 0, scm_setpgid
);
587 scm_setpgid (pid
, pgid
)
590 SCM_ASSERT (SCM_INUMP (pid
), pid
, SCM_ARG1
, s_setpgid
);
591 SCM_ASSERT (SCM_INUMP (pgid
), pgid
, SCM_ARG2
, s_setpgid
);
592 /* FIXME(?): may be known as setpgrp. */
593 if (setpgid (SCM_INUM (pid
), SCM_INUM (pgid
)) != 0)
594 SCM_SYSERROR (s_setpgid
);
595 return SCM_UNSPECIFIED
;
598 SCM_PROC (s_setsid
, "setsid", 0, 0, 0, scm_setsid
);
602 pid_t sid
= setsid ();
604 SCM_SYSERROR (s_setsid
);
605 return SCM_UNSPECIFIED
;
608 SCM_PROC (s_ttyname
, "ttyname", 1, 0, 0, scm_ttyname
);
611 scm_ttyname (SCM port
)
620 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPPORTP (port
), port
, SCM_ARG1
, s_ttyname
);
621 if (scm_tc16_fport
!= SCM_TYP16 (port
))
623 fd
= fileno ((FILE *)SCM_STREAM (port
));
625 SCM_SYSERROR (s_ttyname
);
626 SCM_SYSCALL (ans
= ttyname (fd
));
628 SCM_SYSERROR (s_ttyname
);
629 /* ans could be overwritten by another call to ttyname */
630 return (scm_makfrom0str (ans
));
634 SCM_PROC (s_ctermid
, "ctermid", 0, 0, 0, scm_ctermid
);
638 char *result
= ctermid (NULL
);
640 SCM_SYSERROR (s_ctermid
);
641 return scm_makfrom0str (result
);
644 SCM_PROC (s_tcgetpgrp
, "tcgetpgrp", 1, 0, 0, scm_tcgetpgrp
);
651 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPFPORTP (port
), port
, SCM_ARG1
, s_tcgetpgrp
);
652 fd
= fileno ((FILE *)SCM_STREAM (port
));
653 if (fd
== -1 || (pgid
= tcgetpgrp (fd
)) == -1)
654 SCM_SYSERROR (s_tcgetpgrp
);
655 return SCM_MAKINUM (pgid
);
658 SCM_PROC (s_tcsetpgrp
, "tcsetpgrp", 2, 0, 0, scm_tcsetpgrp
);
660 scm_tcsetpgrp (port
, pgid
)
664 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPFPORTP (port
), port
, SCM_ARG1
, s_tcsetpgrp
);
665 SCM_ASSERT (SCM_INUMP (pgid
), pgid
, SCM_ARG2
, s_tcsetpgrp
);
666 fd
= fileno ((FILE *)SCM_STREAM (port
));
667 if (fd
== -1 || tcsetpgrp (fd
, SCM_INUM (pgid
)) == -1)
668 SCM_SYSERROR (s_tcsetpgrp
);
669 return SCM_UNSPECIFIED
;
672 /* Copy exec args from an SCM vector into a new C array. */
675 scm_convert_exec_args (SCM args
)
678 scm_convert_exec_args (args
)
686 num_args
= scm_ilength (args
);
688 scm_must_malloc ((num_args
+ 1) * sizeof (char *), s_ttyname
);
689 for (i
= 0; SCM_NNULLP (args
); args
= SCM_CDR (args
), ++i
)
694 SCM_ASSERT (SCM_NIMP (SCM_CAR (args
)) && SCM_ROSTRINGP (SCM_CAR (args
)), SCM_CAR (args
),
695 "wrong type in SCM_ARG", "exec arg");
696 len
= 1 + SCM_ROLENGTH (SCM_CAR (args
));
697 dst
= (char *) scm_must_malloc ((long) len
, s_ttyname
);
698 src
= SCM_ROCHARS (SCM_CAR (args
));
708 SCM_PROC (s_sys_execl
, "execl", 0, 0, 1, scm_sys_execl
);
711 scm_sys_execl (SCM args
)
719 SCM filename
= SCM_CAR (args
);
720 SCM_ASSERT (SCM_NIMP (filename
) && SCM_ROSTRINGP (filename
), filename
, SCM_ARG1
, s_sys_execl
);
721 if (SCM_SUBSTRP (filename
))
722 filename
= scm_makfromstr (SCM_ROCHARS (filename
), SCM_ROLENGTH (filename
), 0);
723 args
= SCM_CDR (args
);
724 execargv
= scm_convert_exec_args (args
);
725 execv (SCM_ROCHARS (filename
), execargv
);
726 SCM_SYSERROR (s_sys_execl
);
731 SCM_PROC (s_sys_execlp
, "execlp", 0, 0, 1, scm_sys_execlp
);
734 scm_sys_execlp (SCM args
)
737 scm_sys_execlp (args
)
742 SCM filename
= SCM_CAR (args
);
743 SCM_ASSERT (SCM_NIMP (filename
) && SCM_ROSTRINGP (filename
), filename
, SCM_ARG1
, s_sys_execlp
);
744 if (SCM_SUBSTRP (filename
))
745 filename
= scm_makfromstr (SCM_ROCHARS (filename
), SCM_ROLENGTH (filename
), 0);
746 args
= SCM_CDR (args
);
747 execargv
= scm_convert_exec_args (args
);
748 execvp (SCM_ROCHARS (filename
), execargv
);
749 SCM_SYSERROR (s_sys_execlp
);
754 /* Flushing streams etc., is not done here. */
755 SCM_PROC (s_sys_fork
, "fork", 0, 0, 0, scm_sys_fork
);
767 SCM_SYSERROR (s_sys_fork
);
768 return SCM_MAKINUM (0L+pid
);
772 SCM_PROC (s_sys_uname
, "uname", 0, 0, 0, scm_sys_uname
);
783 SCM ans
= scm_make_vector(SCM_MAKINUM(5), SCM_UNSPECIFIED
, SCM_BOOL_F
);
784 SCM
*ve
= SCM_VELTS (ans
);
786 return SCM_MAKINUM (errno
);
787 ve
[0] = scm_makfrom0str (buf
.sysname
);
788 ve
[1] = scm_makfrom0str (buf
.nodename
);
789 ve
[2] = scm_makfrom0str (buf
.release
);
790 ve
[3] = scm_makfrom0str (buf
.version
);
791 ve
[4] = scm_makfrom0str (buf
.machine
);
794 ve[5] = scm_makfrom0str (buf.domainname);
798 SCM_SYSMISSING (s_sys_uname
);
804 SCM_PROC (s_environ
, "environ", 0, 1, 0, scm_environ
);
807 scm_environ (SCM env
)
814 if (SCM_UNBNDP (env
))
815 return scm_makfromstrs (-1, environ
);
821 SCM_ASSERT (SCM_NULLP (env
) || (SCM_NIMP (env
) && SCM_CONSP (env
)),
822 env
, SCM_ARG1
, s_environ
);
823 num_strings
= scm_ilength (env
);
824 new_environ
= (char **) scm_must_malloc ((num_strings
+ 1)
827 while (SCM_NNULLP (env
))
831 SCM_ASSERT (SCM_NIMP (SCM_CAR (env
)) && SCM_ROSTRINGP (SCM_CAR (env
)), env
, SCM_ARG1
,
833 len
= 1 + SCM_ROLENGTH (SCM_CAR (env
));
834 new_environ
[i
] = scm_must_malloc ((long) len
, s_environ
);
835 src
= SCM_ROCHARS (SCM_CAR (env
));
837 new_environ
[i
][len
] = src
[len
];
842 /* Free the old environment, except when called for the first
847 static int first
= 1;
850 for (ep
= environ
; *ep
!= NULL
; ep
++)
852 scm_must_free ((char *) environ
);
856 environ
= new_environ
;
857 return SCM_UNSPECIFIED
;
862 SCM_PROC (s_open_pipe
, "open-pipe", 2, 0, 0, scm_open_pipe
);
865 scm_open_pipe (SCM pipestr
, SCM modes
)
868 scm_open_pipe (pipestr
, modes
)
875 struct scm_port_table
* pt
;
877 SCM_ASSERT (SCM_NIMP (pipestr
) && SCM_ROSTRINGP (pipestr
), pipestr
, SCM_ARG1
, s_open_pipe
);
878 if (SCM_SUBSTRP (pipestr
))
879 pipestr
= scm_makfromstr (SCM_ROCHARS (pipestr
), SCM_ROLENGTH (pipestr
), 0);
880 SCM_ASSERT (SCM_NIMP (modes
) && SCM_ROSTRINGP (modes
), modes
, SCM_ARG2
, s_open_pipe
);
881 if (SCM_SUBSTRP (modes
))
882 modes
= scm_makfromstr (SCM_ROCHARS (modes
), SCM_ROLENGTH (modes
), 0);
885 scm_ignore_signals ();
886 SCM_SYSCALL (f
= popen (SCM_ROCHARS (pipestr
), SCM_ROCHARS (modes
)));
887 scm_unignore_signals ();
889 SCM_SYSERROR (s_open_pipe
);
890 pt
= scm_add_to_port_table (z
);
891 SCM_SETPTAB_ENTRY (z
, pt
);
892 SCM_CAR (z
) = scm_tc16_pipe
| SCM_OPN
893 | (strchr (SCM_ROCHARS (modes
), 'r') ? SCM_RDNG
: SCM_WRTNG
);
894 SCM_SETSTREAM (z
, (SCM
)f
);
900 SCM_PROC (s_open_input_pipe
, "open-input-pipe", 1, 0, 0, scm_open_input_pipe
);
903 scm_open_input_pipe(SCM pipestr
)
906 scm_open_input_pipe(pipestr
)
910 return scm_open_pipe(pipestr
, scm_makfromstr("r", (sizeof "r")-1, 0));
913 SCM_PROC (s_open_output_pipe
, "open-output-pipe", 1, 0, 0, scm_open_output_pipe
);
916 scm_open_output_pipe(SCM pipestr
)
919 scm_open_output_pipe(pipestr
)
923 return scm_open_pipe(pipestr
, scm_makfromstr("w", (sizeof "w")-1, 0));
928 #include <sys/utime.h>
933 SCM_PROC (s_sys_utime
, "utime", 1, 2, 0, scm_sys_utime
);
936 scm_sys_utime (SCM pathname
, SCM actime
, SCM modtime
)
939 scm_sys_utime (pathname
, actime
, modtime
)
946 struct utimbuf utm_tmp
;
948 SCM_ASSERT (SCM_NIMP (pathname
) && SCM_STRINGP (pathname
), pathname
, SCM_ARG1
, s_sys_utime
);
950 if (SCM_UNBNDP (actime
))
951 SCM_SYSCALL (time (&utm_tmp
.actime
));
953 utm_tmp
.actime
= scm_num2ulong (actime
, (char *) SCM_ARG2
, s_sys_utime
);
955 if (SCM_UNBNDP (modtime
))
956 SCM_SYSCALL (time (&utm_tmp
.modtime
));
958 utm_tmp
.modtime
= scm_num2ulong (modtime
, (char *) SCM_ARG3
, s_sys_utime
);
960 SCM_SYSCALL (rv
= utime (SCM_CHARS (pathname
), &utm_tmp
));
962 SCM_SYSERROR (s_sys_utime
);
963 return SCM_UNSPECIFIED
;
966 SCM_PROC (s_sys_access
, "access?", 2, 0, 0, scm_sys_access
);
969 scm_sys_access (SCM path
, SCM how
)
972 scm_sys_access (path
, how
)
979 SCM_ASSERT (SCM_NIMP (path
) && SCM_ROSTRINGP (path
), path
, SCM_ARG1
, s_sys_access
);
980 if (SCM_SUBSTRP (path
))
981 path
= scm_makfromstr (SCM_ROCHARS (path
), SCM_ROLENGTH (path
), 0);
982 SCM_ASSERT (SCM_INUMP (how
), how
, SCM_ARG2
, s_sys_access
);
983 rv
= access (SCM_ROCHARS (path
), SCM_INUM (how
));
984 return rv
? SCM_BOOL_F
: SCM_BOOL_T
;
987 SCM_PROC (s_getpid
, "getpid", 0, 0, 0, scm_getpid
);
996 return SCM_MAKINUM ((unsigned long) getpid ());
999 SCM_PROC (s_sys_putenv
, "putenv", 1, 0, 0, scm_sys_putenv
);
1002 scm_sys_putenv (SCM str
)
1005 scm_sys_putenv (str
)
1010 SCM_ASSERT (SCM_NIMP (str
) && SCM_STRINGP (str
), str
, SCM_ARG1
, s_sys_putenv
);
1011 return putenv (SCM_CHARS (str
)) ? SCM_MAKINUM (errno
) : SCM_BOOL_T
;
1013 SCM_SYSMISSING (s_sys_putenv
);
1019 SCM_PROC (s_read_line
, "read-line", 0, 2, 0, scm_read_line
);
1022 scm_read_line (SCM port
, SCM include_terminator
)
1025 scm_read_line (port
, include_terminator
)
1027 SCM include_terminator
;
1037 tok_buf
= scm_makstr ((long) len
, 0);
1038 p
= SCM_CHARS (tok_buf
);
1039 if (SCM_UNBNDP (port
))
1042 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPINPORTP (port
), port
, SCM_ARG1
, s_read_line
);
1044 if (SCM_UNBNDP (include_terminator
))
1047 include
= SCM_NFALSEP (include_terminator
);
1049 if (EOF
== (c
= scm_gen_getc (port
)))
1055 case SCM_LINE_INCREMENTORS
:
1058 p
= scm_grow_tok_buf (&tok_buf
);
1059 len
= SCM_LENGTH (tok_buf
);
1066 return scm_vector_set_length_x (tok_buf
, (SCM
) SCM_MAKINUM (j
));
1071 p
= scm_grow_tok_buf (&tok_buf
);
1072 len
= SCM_LENGTH (tok_buf
);
1075 c
= scm_gen_getc (port
);
1081 SCM_PROC (s_read_line_x
, "read-line!", 1, 1, 0, scm_read_line_x
);
1084 scm_read_line_x (SCM str
, SCM port
)
1087 scm_read_line_x (str
, port
)
1096 SCM_ASSERT (SCM_NIMP (str
) && SCM_STRINGP (str
), str
, SCM_ARG1
, s_read_line_x
);
1097 p
= SCM_CHARS (str
);
1098 len
= SCM_LENGTH (str
);
1100 (port
) port
= scm_cur_inp
;
1102 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPINPORTP (port
), port
, SCM_ARG2
, s_read_line_x
);
1103 c
= scm_gen_getc (port
);
1110 case SCM_LINE_INCREMENTORS
:
1112 return SCM_MAKINUM (j
);
1116 scm_gen_ungetc (c
, port
);
1120 c
= scm_gen_getc (port
);
1125 SCM_PROC (s_write_line
, "write-line", 1, 1, 0, scm_write_line
);
1128 scm_write_line (SCM obj
, SCM port
)
1131 scm_write_line (obj
, port
)
1136 scm_display (obj
, port
);
1137 return scm_newline (port
);
1140 SCM_PROC (s_setlocale
, "setlocale", 1, 1, 0, scm_setlocale
);
1143 scm_setlocale (SCM category
, SCM locale
)
1146 scm_setlocale (category
, locale
)
1151 #ifdef HAVE_SETLOCALE
1155 SCM_ASSERT (SCM_INUMP (category
), category
, SCM_ARG1
, s_setlocale
);
1156 if (SCM_UNBNDP (locale
))
1162 SCM_ASSERT (SCM_NIMP (locale
) && SCM_STRINGP (locale
), locale
, SCM_ARG2
, s_setlocale
);
1163 clocale
= SCM_CHARS (locale
);
1166 rv
= setlocale (SCM_INUM (category
), clocale
);
1168 SCM_SYSERROR (s_setlocale
);
1169 return scm_makfrom0str (rv
);
1171 SCM_SYSMISSING (s_setlocale
);
1177 SCM_PROC (s_strftime
, "strftime", 2, 0, 0, scm_strftime
);
1180 scm_strftime (SCM format
, SCM stime
)
1183 scm_strftime (format
, stime
)
1196 SCM_ASSERT (SCM_NIMP (format
) && SCM_STRINGP (format
), format
, SCM_ARG1
, s_strftime
);
1197 SCM_ASSERT (SCM_NIMP (stime
) && SCM_VECTORP (stime
) && scm_obj_length (stime
) == 9,
1198 stime
, SCM_ARG2
, s_strftime
);
1200 fmt
= SCM_ROCHARS (format
);
1201 len
= SCM_ROLENGTH (format
);
1203 #define tm_deref scm_num2long (SCM_VELTS (stime)[n++], (char *)SCM_ARG2, s_strftime)
1205 t
.tm_sec
= tm_deref
;
1206 t
.tm_min
= tm_deref
;
1207 t
.tm_hour
= tm_deref
;
1208 t
.tm_mday
= tm_deref
;
1209 t
.tm_mon
= tm_deref
;
1210 t
.tm_year
= tm_deref
;
1211 /* not used by mktime.
1212 t.tm_wday = tm_deref;
1213 t.tm_yday = tm_deref; */
1214 t
.tm_isdst
= tm_deref
;
1217 /* fill in missing fields and set the timezone. */
1220 tbuf
= scm_must_malloc (size
, s_strftime
);
1221 while ((len
= strftime (tbuf
, size
, fmt
, &t
)) == size
)
1223 scm_must_free (tbuf
);
1225 tbuf
= scm_must_malloc (size
, s_strftime
);
1227 return scm_makfromstr (tbuf
, len
, 0);
1230 SCM_PROC (s_sys_strptime
, "strptime", 2, 0, 0, scm_sys_strptime
);
1233 scm_sys_strptime (SCM format
, SCM string
)
1236 scm_sys_strptime (format
, string
)
1241 #ifdef HAVE_STRPTIME
1245 char *fmt
, *str
, *rest
;
1248 SCM_ASSERT (SCM_NIMP (format
) && SCM_ROSTRINGP (format
), format
, SCM_ARG1
, s_sys_strptime
);
1249 if (SCM_SUBSTRP (format
))
1250 format
= scm_makfromstr (SCM_ROCHARS (format
), SCM_ROLENGTH (format
), 0);
1251 SCM_ASSERT (SCM_NIMP (string
) && SCM_ROSTRINGP (string
), string
, SCM_ARG2
, s_sys_strptime
);
1252 if (SCM_SUBSTRP (string
))
1253 string
= scm_makfromstr (SCM_ROCHARS (string
), SCM_ROLENGTH (string
), 0);
1255 fmt
= SCM_CHARS (format
);
1256 str
= SCM_CHARS (string
);
1258 /* initialize the struct tm */
1259 #define tm_init(field) t.field = 0
1272 rest
= strptime (str
, fmt
, &t
);
1276 SCM_SYSERROR (s_sys_strptime
);
1278 stime
= scm_make_vector (SCM_MAKINUM (9), scm_long2num (0), SCM_UNDEFINED
);
1280 #define stime_set(val) scm_vector_set_x (stime, SCM_MAKINUM (n++), scm_long2num (t.val));
1284 stime_set (tm_hour
);
1285 stime_set (tm_mday
);
1287 stime_set (tm_year
);
1288 stime_set (tm_wday
);
1289 stime_set (tm_yday
);
1290 stime_set (tm_isdst
);
1293 return scm_cons (stime
, scm_makfrom0str (rest
));
1295 SCM_SYSMISSING (s_sys_strptime
);
1301 SCM_PROC (s_sys_mknod
, "mknod", 3, 0, 0, scm_sys_mknod
);
1304 scm_sys_mknod(SCM path
, SCM mode
, SCM dev
)
1307 scm_sys_mknod(path
, mode
, dev
)
1315 SCM_ASSERT(SCM_NIMP(path
) && SCM_STRINGP(path
), path
, SCM_ARG1
, s_sys_mknod
);
1316 SCM_ASSERT(SCM_INUMP(mode
), mode
, SCM_ARG2
, s_sys_mknod
);
1317 SCM_ASSERT(SCM_INUMP(dev
), dev
, SCM_ARG3
, s_sys_mknod
);
1318 SCM_SYSCALL(val
= mknod(SCM_CHARS(path
), SCM_INUM(mode
), SCM_INUM(dev
)));
1320 SCM_SYSERROR (s_sys_mknod
);
1321 return SCM_UNSPECIFIED
;
1323 SCM_SYSMISSING (s_sys_mknod
);
1330 SCM_PROC (s_sys_nice
, "nice", 1, 0, 0, scm_sys_nice
);
1333 scm_sys_nice(SCM incr
)
1341 SCM_ASSERT(SCM_INUMP(incr
), incr
, SCM_ARG1
, s_sys_nice
);
1342 if (nice(SCM_INUM(incr
)) != 0)
1343 SCM_SYSERROR (s_sys_nice
);
1344 return SCM_UNSPECIFIED
;
1346 SCM_SYSMISSING (s_sys_nice
);
1353 SCM_PROC (s_sync
, "sync", 0, 0, 0, scm_sync
);
1365 SCM_SYSMISSING (s_sync
);
1374 scm_init_posix (void)
1380 scm_add_feature ("posix");
1382 scm_add_feature ("EIDs");
1385 scm_sysintern ("WAIT_ANY", SCM_MAKINUM (WAIT_ANY
));
1388 scm_sysintern ("WAIT_MYPGRP", SCM_MAKINUM (WAIT_MYPGRP
));
1391 scm_sysintern ("WNOHANG", SCM_MAKINUM (WNOHANG
));
1394 scm_sysintern ("WUNTRACED", SCM_MAKINUM (WUNTRACED
));
1398 scm_sysintern ("EINTR", SCM_MAKINUM (EINTR
));
1402 scm_sysintern ("SIGHUP", SCM_MAKINUM (SIGHUP
));
1405 scm_sysintern ("SIGINT", SCM_MAKINUM (SIGINT
));
1408 scm_sysintern ("SIGQUIT", SCM_MAKINUM (SIGQUIT
));
1411 scm_sysintern ("SIGILL", SCM_MAKINUM (SIGILL
));
1414 scm_sysintern ("SIGTRAP", SCM_MAKINUM (SIGTRAP
));
1417 scm_sysintern ("SIGABRT", SCM_MAKINUM (SIGABRT
));
1420 scm_sysintern ("SIGIOT", SCM_MAKINUM (SIGIOT
));
1423 scm_sysintern ("SIGBUS", SCM_MAKINUM (SIGBUS
));
1426 scm_sysintern ("SIGFPE", SCM_MAKINUM (SIGFPE
));
1429 scm_sysintern ("SIGKILL", SCM_MAKINUM (SIGKILL
));
1432 scm_sysintern ("SIGUSR1", SCM_MAKINUM (SIGUSR1
));
1435 scm_sysintern ("SIGSEGV", SCM_MAKINUM (SIGSEGV
));
1438 scm_sysintern ("SIGUSR2", SCM_MAKINUM (SIGUSR2
));
1441 scm_sysintern ("SIGPIPE", SCM_MAKINUM (SIGPIPE
));
1444 scm_sysintern ("SIGALRM", SCM_MAKINUM (SIGALRM
));
1447 scm_sysintern ("SIGTERM", SCM_MAKINUM (SIGTERM
));
1450 scm_sysintern ("SIGSTKFLT", SCM_MAKINUM (SIGSTKFLT
));
1453 scm_sysintern ("SIGCHLD", SCM_MAKINUM (SIGCHLD
));
1456 scm_sysintern ("SIGCONT", SCM_MAKINUM (SIGCONT
));
1459 scm_sysintern ("SIGSTOP", SCM_MAKINUM (SIGSTOP
));
1462 scm_sysintern ("SIGTSTP", SCM_MAKINUM (SIGTSTP
));
1465 scm_sysintern ("SIGTTIN", SCM_MAKINUM (SIGTTIN
));
1468 scm_sysintern ("SIGTTOU", SCM_MAKINUM (SIGTTOU
));
1471 scm_sysintern ("SIGIO", SCM_MAKINUM (SIGIO
));
1474 scm_sysintern ("SIGPOLL", SCM_MAKINUM (SIGPOLL
));
1477 scm_sysintern ("SIGURG", SCM_MAKINUM (SIGURG
));
1480 scm_sysintern ("SIGXCPU", SCM_MAKINUM (SIGXCPU
));
1483 scm_sysintern ("SIGXFSZ", SCM_MAKINUM (SIGXFSZ
));
1486 scm_sysintern ("SIGVTALRM", SCM_MAKINUM (SIGVTALRM
));
1489 scm_sysintern ("SIGPROF", SCM_MAKINUM (SIGPROF
));
1492 scm_sysintern ("SIGWINCH", SCM_MAKINUM (SIGWINCH
));
1495 scm_sysintern ("SIGLOST", SCM_MAKINUM (SIGLOST
));
1498 scm_sysintern ("SIGPWR", SCM_MAKINUM (SIGPWR
));
1500 /* access() symbols. */
1501 scm_sysintern ("R_OK", SCM_MAKINUM (R_OK
));
1502 scm_sysintern ("W_OK", SCM_MAKINUM (W_OK
));
1503 scm_sysintern ("X_OK", SCM_MAKINUM (X_OK
));
1504 scm_sysintern ("F_OK", SCM_MAKINUM (F_OK
));
1507 scm_sysintern ("LC_COLLATE", SCM_MAKINUM (LC_COLLATE
));
1510 scm_sysintern ("LC_CTYPE", SCM_MAKINUM (LC_CTYPE
));
1513 scm_sysintern ("LC_MONETARY", SCM_MAKINUM (LC_MONETARY
));
1516 scm_sysintern ("LC_NUMERIC", SCM_MAKINUM (LC_NUMERIC
));
1519 scm_sysintern ("LC_TIME", SCM_MAKINUM (LC_TIME
));
1522 scm_sysintern ("LC_MESSAGES", SCM_MAKINUM (LC_MESSAGES
));
1525 scm_sysintern ("LC_ALL", SCM_MAKINUM (LC_ALL
));