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.
48 #ifdef TIME_WITH_SYS_TIME
49 # include <sys/time.h>
53 # include <sys/time.h>
63 #ifdef HAVE_SYS_SELECT_H
64 #include <sys/select.h>
67 #include <sys/types.h>
74 # include <sys/wait.h>
77 # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
80 # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
87 #define SELECT_TYPE fd_set
88 #define SELECT_SET_SIZE FD_SETSIZE
92 /* Define the macros to access a single-int bitmap of descriptors. */
93 #define SELECT_SET_SIZE 32
94 #define SELECT_TYPE int
95 #define FD_SET(n, p) (*(p) |= (1 << (n)))
96 #define FD_CLR(n, p) (*(p) &= ~(1 << (n)))
97 #define FD_ISSET(n, p) (*(p) & (1 << (n)))
98 #define FD_ZERO(p) (*(p) = 0)
100 #endif /* no FD_SET */
102 extern char *ttyname ();
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>
128 #ifdef HAVE_SETLOCALE
136 SCM_PROC (s_sys_pipe
, "%pipe", 0, 0, 0, scm_sys_pipe
);
156 f_rd
= fdopen (fd
[0], "r");
159 SCM_SYSCALL (close (fd
[0]));
160 SCM_SYSCALL (close (fd
[1]));
164 f_wt
= fdopen (fd
[1], "w");
170 SCM_SYSCALL (close (fd
[1]));
172 return SCM_MAKINUM (en
);
175 struct scm_port_table
* ptr
;
176 struct scm_port_table
* ptw
;
178 ptr
= scm_add_to_port_table (p_rd
);
179 ptw
= scm_add_to_port_table (p_wt
);
180 SCM_SETPTAB_ENTRY (p_rd
, ptr
);
181 SCM_SETPTAB_ENTRY (p_wt
, ptw
);
182 SCM_CAR (p_rd
) = scm_tc16_fport
| scm_mode_bits ("r");
183 SCM_CAR (p_wt
) = scm_tc16_fport
| scm_mode_bits ("w");
184 SCM_SETSTREAM (p_rd
, (SCM
)f_rd
);
185 SCM_SETSTREAM (p_wt
, (SCM
)f_wt
);
188 return scm_cons (p_rd
, p_wt
);
193 SCM_PROC (s_sys_getgroups
, "%getgroups", 0, 0, 0, scm_sys_getgroups
);
196 scm_sys_getgroups(void)
203 int ngroups
= getgroups (0, NULL
);
204 if (!ngroups
) return SCM_BOOL_F
;
211 groups
= (gid_t
*)scm_must_malloc(ngroups
* sizeof(GETGROUPS_T
),
213 val
= getgroups(ngroups
, groups
);
216 scm_must_free((char *)groups
);
218 return SCM_MAKINUM (errno
);
220 SCM_SETCHARS(grps
, groups
); /* set up grps as a GC protect */
221 SCM_SETLENGTH(grps
, 0L + ngroups
* sizeof(GETGROUPS_T
), scm_tc7_string
);
223 ans
= scm_make_vector(SCM_MAKINUM(ngroups
), SCM_UNDEFINED
, SCM_BOOL_F
);
224 while (--ngroups
>= 0) SCM_VELTS(ans
)[ngroups
] = SCM_MAKINUM(groups
[ngroups
]);
225 SCM_SETCHARS(grps
, groups
); /* to make sure grps stays around. */
232 SCM_PROC (s_sys_getpwuid
, "%getpw", 0, 1, 0, scm_sys_getpwuid
);
235 scm_sys_getpwuid (SCM user
)
238 scm_sys_getpwuid (user
)
243 struct passwd
*entry
;
246 result
= scm_make_vector (SCM_MAKINUM (7), SCM_UNSPECIFIED
, SCM_BOOL_F
);
247 ve
= SCM_VELTS (result
);
248 if (SCM_UNBNDP (user
) || SCM_FALSEP (user
))
251 SCM_SYSCALL (entry
= getpwent ());
253 else if (SCM_INUMP (user
))
256 entry
= getpwuid (SCM_INUM (user
));
260 SCM_ASSERT (SCM_NIMP (user
) && SCM_ROSTRINGP (user
), user
, SCM_ARG1
, s_sys_getpwuid
);
261 if (SCM_SUBSTRP (user
))
262 user
= scm_makfromstr (SCM_ROCHARS (user
), SCM_ROLENGTH (user
), 0);
264 entry
= getpwnam (SCM_ROCHARS (user
));
271 ve
[0] = scm_makfrom0str (entry
->pw_name
);
272 ve
[1] = scm_makfrom0str (entry
->pw_passwd
);
273 ve
[2] = scm_ulong2num ((unsigned long) entry
->pw_uid
);
274 ve
[3] = scm_ulong2num ((unsigned long) entry
->pw_gid
);
275 ve
[4] = scm_makfrom0str (entry
->pw_gecos
);
277 ve
[5] = scm_makfrom0str ("");
279 ve
[5] = scm_makfrom0str (entry
->pw_dir
);
280 if (!entry
->pw_shell
)
281 ve
[6] = scm_makfrom0str ("");
283 ve
[6] = scm_makfrom0str (entry
->pw_shell
);
290 SCM_PROC (s_setpwent
, "setpw", 0, 1, 0, scm_setpwent
);
293 scm_setpwent (SCM arg
)
300 if (SCM_UNBNDP (arg
) || SCM_FALSEP (arg
))
304 return SCM_UNSPECIFIED
;
309 /* Combines getgrgid and getgrnam. */
310 SCM_PROC (s_sys_getgrgid
, "%getgr", 0, 1, 0, scm_sys_getgrgid
);
313 scm_sys_getgrgid (SCM name
)
316 scm_sys_getgrgid (name
)
323 result
= scm_make_vector (SCM_MAKINUM (4), SCM_UNSPECIFIED
, SCM_BOOL_F
);
324 ve
= SCM_VELTS (result
);
326 if (SCM_UNBNDP (name
) || (name
== SCM_BOOL_F
))
327 SCM_SYSCALL (entry
= getgrent ());
328 else if (SCM_INUMP (name
))
329 SCM_SYSCALL (entry
= getgrgid (SCM_INUM (name
)));
332 SCM_ASSERT (SCM_NIMP (name
) && SCM_STRINGP (name
), name
, SCM_ARG1
, s_sys_getgrgid
);
333 if (SCM_SUBSTRP (name
))
334 name
= scm_makfromstr (SCM_ROCHARS (name
), SCM_ROLENGTH (name
), 0);
335 SCM_SYSCALL (entry
= getgrnam (SCM_CHARS (name
)));
340 return SCM_MAKINUM (errno
);
342 ve
[0] = scm_makfrom0str (entry
->gr_name
);
343 ve
[1] = scm_makfrom0str (entry
->gr_passwd
);
344 ve
[2] = scm_ulong2num ((unsigned long) entry
->gr_gid
);
345 ve
[3] = scm_makfromstrs (-1, entry
->gr_mem
);
352 SCM_PROC (s_setgrent
, "setgr", 0, 1, 0, scm_setgrent
);
355 scm_setgrent (SCM arg
)
362 if (SCM_UNBNDP (arg
) || SCM_FALSEP (arg
))
366 return SCM_UNSPECIFIED
;
371 SCM_PROC (s_sys_kill
, "%kill", 2, 0, 0, scm_sys_kill
);
374 scm_sys_kill (SCM pid
, SCM sig
)
377 scm_sys_kill (pid
, sig
)
383 SCM_ASSERT (SCM_INUMP (pid
), pid
, SCM_ARG1
, s_sys_kill
);
384 SCM_ASSERT (SCM_INUMP (sig
), sig
, SCM_ARG2
, s_sys_kill
);
385 /* Signal values are interned in scm_init_posix(). */
386 SCM_SYSCALL (i
= kill ((int) SCM_INUM (pid
), (int) SCM_INUM (sig
)));
387 return i
? SCM_MAKINUM (errno
) : SCM_BOOL_T
;
392 SCM_PROC (s_sys_waitpid
, "%waitpid", 1, 1, 0, scm_sys_waitpid
);
395 scm_sys_waitpid (SCM pid
, SCM options
)
398 scm_sys_waitpid (pid
, options
)
406 SCM_ASSERT (SCM_INUMP (pid
), pid
, SCM_ARG1
, s_sys_waitpid
);
407 if (SCM_UNBNDP (options
))
411 SCM_ASSERT (SCM_INUMP (options
), options
, SCM_ARG2
, s_sys_waitpid
);
412 /* Flags are interned in scm_init_posix. */
413 ioptions
= SCM_INUM (options
);
415 SCM_SYSCALL (i
= waitpid (SCM_INUM (pid
), &status
, ioptions
));
417 ? SCM_MAKINUM (errno
)
418 : scm_cons (SCM_MAKINUM (0L + i
), SCM_MAKINUM (0L + status
)));
423 SCM_PROC (s_getppid
, "getppid", 0, 0, 0, scm_getppid
);
432 return SCM_MAKINUM (0L + getppid ());
437 SCM_PROC (s_getuid
, "getuid", 0, 0, 0, scm_getuid
);
446 return SCM_MAKINUM (0L + getuid ());
451 SCM_PROC (s_getgid
, "getgid", 0, 0, 0, scm_getgid
);
460 return SCM_MAKINUM (0L + getgid ());
465 SCM_PROC (s_geteuid
, "geteuid", 0, 0, 0, scm_geteuid
);
475 return SCM_MAKINUM (0L + geteuid ());
477 return SCM_MAKINUM (0L + getuid ());
483 SCM_PROC (s_getegid
, "getegid", 0, 0, 0, scm_getegid
);
493 return SCM_MAKINUM (0L + getegid ());
495 return SCM_MAKINUM (0L + getgid ());
500 SCM_PROC (s_sys_setuid
, "%setuid", 1, 0, 0, scm_sys_setuid
);
503 scm_sys_setuid (SCM id
)
510 SCM_ASSERT (SCM_INUMP (id
), id
, SCM_ARG1
, s_sys_setuid
);
511 return setuid (SCM_INUM (id
)) ? SCM_MAKINUM (errno
) : SCM_BOOL_T
;
514 SCM_PROC (s_sys_setgid
, "%setgid", 1, 0, 0, scm_sys_setgid
);
517 scm_sys_setgid (SCM id
)
524 SCM_ASSERT (SCM_INUMP (id
), id
, SCM_ARG1
, s_sys_setgid
);
525 return setgid (SCM_INUM (id
)) ? SCM_MAKINUM (errno
) : SCM_BOOL_T
;
528 SCM_PROC (s_sys_seteuid
, "%seteuid", 1, 0, 0, scm_sys_seteuid
);
531 scm_sys_seteuid (SCM id
)
538 SCM_ASSERT (SCM_INUMP (id
), id
, SCM_ARG1
, s_sys_seteuid
);
540 return seteuid (SCM_INUM (id
)) ? SCM_MAKINUM (errno
) : SCM_BOOL_T
;
542 return setuid (SCM_INUM (id
)) ? SCM_MAKINUM (errno
) : SCM_BOOL_T
;
546 SCM_PROC (s_sys_setegid
, "%setegid", 1, 0, 0, scm_sys_setegid
);
549 scm_sys_setegid (SCM id
)
556 SCM_ASSERT (SCM_INUMP (id
), id
, SCM_ARG1
, s_sys_setegid
);
558 return setegid (SCM_INUM (id
)) ? SCM_MAKINUM (errno
) : SCM_BOOL_T
;
560 return setgid (SCM_INUM (id
)) ? SCM_MAKINUM (errno
) : SCM_BOOL_T
;
564 SCM_PROC (s_getpgrp
, "getpgrp", 0, 0, 0, scm_getpgrp
);
570 return SCM_MAKINUM (fn (0));
573 SCM_PROC (s_setpgid
, "%setpgid", 2, 0, 0, scm_setpgid
);
575 scm_setpgid (pid
, pgid
)
578 SCM_ASSERT (SCM_INUMP (pid
), pid
, SCM_ARG1
, s_setpgid
);
579 SCM_ASSERT (SCM_INUMP (pgid
), pgid
, SCM_ARG2
, s_setpgid
);
580 /* This may be known as setpgrp, from BSD. */
581 return setpgid (SCM_INUM (pid
), SCM_INUM (pgid
)) ? SCM_MAKINUM (errno
) : SCM_BOOL_T
;
584 SCM_PROC (s_setsid
, "%setsid", 0, 0, 0, scm_setsid
);
588 pid_t sid
= setsid ();
589 return (sid
== -1) ? SCM_BOOL_F
: SCM_MAKINUM (sid
);
593 extern char * ttyname();
596 SCM_PROC (s_ttyname
, "%ttyname", 1, 0, 0, scm_ttyname
);
599 scm_ttyname (SCM port
)
608 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPPORTP (port
), port
, SCM_ARG1
, s_ttyname
);
609 if (scm_tc16_fport
!= SCM_TYP16 (port
))
611 fd
= fileno ((FILE *)SCM_STREAM (port
));
613 SCM_SYSCALL (ans
= ttyname (fd
));
614 /* ans could be overwritten by another call to ttyname */
615 return (((fd
!= -1) && ans
)
616 ? scm_makfrom0str (ans
)
617 : SCM_MAKINUM (errno
));
621 SCM_PROC (s_ctermid
, "%ctermid", 0, 0, 0, scm_ctermid
);
625 char *result
= ctermid (NULL
);
626 return *result
== '\0' ? SCM_BOOL_F
: scm_makfrom0str (result
);
629 SCM_PROC (s_tcgetpgrp
, "%tcgetpgrp", 1, 0, 0, scm_tcgetpgrp
);
636 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPFPORTP (port
), port
, SCM_ARG1
, s_tcgetpgrp
);
637 fd
= fileno ((FILE *)SCM_STREAM (port
));
638 if (fd
== -1 || (pgid
= tcgetpgrp (fd
)) == -1)
641 return SCM_MAKINUM (pgid
);
644 SCM_PROC (s_tcsetpgrp
, "%tcsetpgrp", 2, 0, 0, scm_tcsetpgrp
);
646 scm_tcsetpgrp (port
, pgid
)
650 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPFPORTP (port
), port
, SCM_ARG1
, s_tcsetpgrp
);
651 SCM_ASSERT (SCM_INUMP (pgid
), pgid
, SCM_ARG2
, s_tcsetpgrp
);
652 fd
= fileno ((FILE *)SCM_STREAM (port
));
653 if (fd
== -1 || tcsetpgrp (fd
, SCM_INUM (pgid
)) == -1)
659 /* Copy exec args from an SCM vector into a new C array. */
662 scm_convert_exec_args (SCM args
)
665 scm_convert_exec_args (args
)
673 num_args
= scm_ilength (args
);
675 scm_must_malloc ((num_args
+ 1) * sizeof (char *), s_ttyname
);
676 for (i
= 0; SCM_NNULLP (args
); args
= SCM_CDR (args
), ++i
)
681 SCM_ASSERT (SCM_NIMP (SCM_CAR (args
)) && SCM_ROSTRINGP (SCM_CAR (args
)), SCM_CAR (args
),
682 "wrong type in SCM_ARG", "exec arg");
683 len
= 1 + SCM_ROLENGTH (SCM_CAR (args
));
684 dst
= (char *) scm_must_malloc ((long) len
, s_ttyname
);
685 src
= SCM_ROCHARS (SCM_CAR (args
));
695 SCM_PROC (s_sys_execl
, "%execl", 0, 0, 1, scm_sys_execl
);
698 scm_sys_execl (SCM args
)
706 SCM filename
= SCM_CAR (args
);
707 SCM_ASSERT (SCM_NIMP (filename
) && SCM_ROSTRINGP (filename
), filename
, SCM_ARG1
, s_sys_execl
);
708 if (SCM_SUBSTRP (filename
))
709 filename
= scm_makfromstr (SCM_ROCHARS (filename
), SCM_ROLENGTH (filename
), 0);
710 args
= SCM_CDR (args
);
711 execargv
= scm_convert_exec_args (args
);
712 execv (SCM_ROCHARS (filename
), execargv
);
713 return SCM_MAKINUM (errno
);
716 SCM_PROC (s_sys_execlp
, "%execlp", 0, 0, 1, scm_sys_execlp
);
719 scm_sys_execlp (SCM args
)
722 scm_sys_execlp (args
)
727 SCM filename
= SCM_CAR (args
);
728 SCM_ASSERT (SCM_NIMP (filename
) && SCM_ROSTRINGP (filename
), filename
, SCM_ARG1
, s_sys_execlp
);
729 if (SCM_SUBSTRP (filename
))
730 filename
= scm_makfromstr (SCM_ROCHARS (filename
), SCM_ROLENGTH (filename
), 0);
731 args
= SCM_CDR (args
);
732 execargv
= scm_convert_exec_args (args
);
733 execvp (SCM_ROCHARS (filename
), execargv
);
734 return SCM_MAKINUM (errno
);
737 /* Flushing streams etc., is not done here. */
738 SCM_PROC (s_sys_fork
, "%fork", 0, 0, 0, scm_sys_fork
);
752 return SCM_MAKINUM (0L+pid
);
756 SCM_PROC (s_sys_uname
, "%uname", 0, 0, 0, scm_sys_uname
);
767 SCM ans
= scm_make_vector(SCM_MAKINUM(5), SCM_UNSPECIFIED
, SCM_BOOL_F
);
768 SCM
*ve
= SCM_VELTS (ans
);
770 return SCM_MAKINUM (errno
);
771 ve
[0] = scm_makfrom0str (buf
.sysname
);
772 ve
[1] = scm_makfrom0str (buf
.nodename
);
773 ve
[2] = scm_makfrom0str (buf
.release
);
774 ve
[3] = scm_makfrom0str (buf
.version
);
775 ve
[4] = scm_makfrom0str (buf
.machine
);
778 ve[5] = scm_makfrom0str (buf.domainname);
782 return SCM_MAKINUM (ENOSYS
);
786 SCM_PROC (s_environ
, "environ", 0, 1, 0, scm_environ
);
789 scm_environ (SCM env
)
796 if (SCM_UNBNDP (env
))
797 return scm_makfromstrs (-1, environ
);
803 SCM_ASSERT (SCM_NULLP (env
) || (SCM_NIMP (env
) && SCM_CONSP (env
)),
804 env
, SCM_ARG1
, s_environ
);
805 num_strings
= scm_ilength (env
);
806 new_environ
= (char **) scm_must_malloc ((num_strings
+ 1)
809 while (SCM_NNULLP (env
))
813 SCM_ASSERT (SCM_NIMP (SCM_CAR (env
)) && SCM_ROSTRINGP (SCM_CAR (env
)), env
, SCM_ARG1
,
815 len
= 1 + SCM_ROLENGTH (SCM_CAR (env
));
816 new_environ
[i
] = scm_must_malloc ((long) len
, s_environ
);
817 src
= SCM_ROCHARS (SCM_CAR (env
));
819 new_environ
[i
][len
] = src
[len
];
824 /* Free the old environment, except when called for the first
829 static int first
= 1;
832 for (ep
= environ
; *ep
!= NULL
; ep
++)
834 scm_must_free ((char *) environ
);
838 environ
= new_environ
;
839 return SCM_UNSPECIFIED
;
844 SCM_PROC (s_open_pipe
, "open-pipe", 2, 0, 0, scm_open_pipe
);
847 scm_open_pipe (SCM pipestr
, SCM modes
)
850 scm_open_pipe (pipestr
, modes
)
857 SCM_ASSERT (SCM_NIMP (pipestr
) && SCM_ROSTRINGP (pipestr
), pipestr
, SCM_ARG1
, s_open_pipe
);
858 if (SCM_SUBSTRP (pipestr
))
859 pipestr
= scm_makfromstr (SCM_ROCHARS (pipestr
), SCM_ROLENGTH (pipestr
), 0);
860 SCM_ASSERT (SCM_NIMP (modes
) && SCM_ROSTRINGP (modes
), modes
, SCM_ARG2
, s_open_pipe
);
861 if (SCM_SUBSTRP (modes
))
862 modes
= scm_makfromstr (SCM_ROCHARS (modes
), SCM_ROLENGTH (modes
), 0);
865 scm_ignore_signals ();
866 SCM_SYSCALL (f
= popen (SCM_ROCHARS (pipestr
), SCM_ROCHARS (modes
)));
867 scm_unignore_signals ();
872 struct scm_port_table
* pt
;
873 pt
= scm_add_to_port_table (z
);
874 SCM_SETPTAB_ENTRY (z
, pt
);
875 SCM_CAR (z
) = scm_tc16_pipe
| SCM_OPN
| (strchr (SCM_ROCHARS (modes
), 'r') ? SCM_RDNG
: SCM_WRTNG
);
876 SCM_SETSTREAM (z
, (SCM
)f
);
883 SCM_PROC (s_open_input_pipe
, "open-input-pipe", 1, 0, 0, scm_open_input_pipe
);
886 scm_open_input_pipe(SCM pipestr
)
889 scm_open_input_pipe(pipestr
)
893 return scm_open_pipe(pipestr
, scm_makfromstr("r", (sizeof "r")-1, 0));
896 SCM_PROC (s_open_output_pipe
, "open-output-pipe", 1, 0, 0, scm_open_output_pipe
);
899 scm_open_output_pipe(SCM pipestr
)
902 scm_open_output_pipe(pipestr
)
906 return scm_open_pipe(pipestr
, scm_makfromstr("w", (sizeof "w")-1, 0));
911 #include <sys/utime.h>
916 SCM_PROC (s_sys_utime
, "%utime", 1, 2, 0, scm_sys_utime
);
919 scm_sys_utime (SCM pathname
, SCM actime
, SCM modtime
)
922 scm_sys_utime (pathname
, actime
, modtime
)
929 struct utimbuf utm_tmp
;
931 SCM_ASSERT (SCM_NIMP (pathname
) && SCM_STRINGP (pathname
), pathname
, SCM_ARG1
, s_sys_utime
);
933 if (SCM_UNBNDP (actime
))
934 SCM_SYSCALL (time (&utm_tmp
.actime
));
936 utm_tmp
.actime
= scm_num2ulong (actime
, (char *) SCM_ARG2
, s_sys_utime
);
938 if (SCM_UNBNDP (modtime
))
939 SCM_SYSCALL (time (&utm_tmp
.modtime
));
941 utm_tmp
.modtime
= scm_num2ulong (modtime
, (char *) SCM_ARG3
, s_sys_utime
);
943 SCM_SYSCALL (rv
= utime (SCM_CHARS (pathname
), &utm_tmp
));
944 return rv
? SCM_MAKINUM (errno
) : SCM_BOOL_T
;
951 SCM_PROC (s_sys_access
, "access?", 2, 0, 0, scm_sys_access
);
954 scm_sys_access (SCM path
, SCM how
)
957 scm_sys_access (path
, how
)
964 SCM_ASSERT (SCM_NIMP (path
) && SCM_ROSTRINGP (path
), path
, SCM_ARG1
, s_sys_access
);
965 if (SCM_SUBSTRP (path
))
966 path
= scm_makfromstr (SCM_ROCHARS (path
), SCM_ROLENGTH (path
), 0);
967 SCM_ASSERT (SCM_INUMP (how
), how
, SCM_ARG2
, s_sys_access
);
968 rv
= access (SCM_ROCHARS (path
), SCM_INUM (how
));
969 return rv
? SCM_BOOL_F
: SCM_BOOL_T
;
974 SCM_PROC (s_getpid
, "getpid", 0, 0, 0, scm_getpid
);
983 return SCM_MAKINUM ((unsigned long) getpid ());
987 SCM_PROC (s_sys_putenv
, "%putenv", 1, 0, 0, scm_sys_putenv
);
990 scm_sys_putenv (SCM str
)
998 SCM_ASSERT (SCM_NIMP (str
) && SCM_STRINGP (str
), str
, SCM_ARG1
, s_sys_putenv
);
999 return putenv (SCM_CHARS (str
)) ? SCM_MAKINUM (errno
) : SCM_BOOL_T
;
1001 return SCM_MAKINUM (ENOSYS
);
1006 SCM_PROC (s_read_line
, "read-line", 0, 2, 0, scm_read_line
);
1009 scm_read_line (SCM port
, SCM include_terminator
)
1012 scm_read_line (port
, include_terminator
)
1014 SCM include_terminator
;
1024 tok_buf
= scm_makstr ((long) len
, 0);
1025 p
= SCM_CHARS (tok_buf
);
1026 if (SCM_UNBNDP (port
))
1029 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPINPORTP (port
), port
, SCM_ARG1
, s_read_line
);
1031 if (SCM_UNBNDP (include_terminator
))
1034 include
= SCM_NFALSEP (include_terminator
);
1036 if (EOF
== (c
= scm_gen_getc (port
)))
1042 case SCM_LINE_INCREMENTORS
:
1045 p
= scm_grow_tok_buf (&tok_buf
);
1046 len
= SCM_LENGTH (tok_buf
);
1053 return scm_vector_set_length_x (tok_buf
, (SCM
) SCM_MAKINUM (j
));
1058 p
= scm_grow_tok_buf (&tok_buf
);
1059 len
= SCM_LENGTH (tok_buf
);
1062 c
= scm_gen_getc (port
);
1070 SCM_PROC (s_read_line_x
, "read-line!", 1, 1, 0, scm_read_line_x
);
1073 scm_read_line_x (SCM str
, SCM port
)
1076 scm_read_line_x (str
, port
)
1085 SCM_ASSERT (SCM_NIMP (str
) && SCM_STRINGP (str
), str
, SCM_ARG1
, s_read_line_x
);
1086 p
= SCM_CHARS (str
);
1087 len
= SCM_LENGTH (str
);
1089 (port
) port
= scm_cur_inp
;
1091 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPINPORTP (port
), port
, SCM_ARG2
, s_read_line_x
);
1092 c
= scm_gen_getc (port
);
1099 case SCM_LINE_INCREMENTORS
:
1101 return SCM_MAKINUM (j
);
1105 scm_gen_ungetc (c
, port
);
1109 c
= scm_gen_getc (port
);
1116 SCM_PROC (s_write_line
, "write-line", 1, 1, 0, scm_write_line
);
1119 scm_write_line (SCM obj
, SCM port
)
1122 scm_write_line (obj
, port
)
1127 scm_display (obj
, port
);
1128 return scm_newline (port
);
1133 SCM_PROC (s_setlocale
, "%setlocale", 1, 1, 0, scm_setlocale
);
1136 scm_setlocale (SCM category
, SCM locale
)
1139 scm_setlocale (category
, locale
)
1144 #ifdef HAVE_SETLOCALE
1148 SCM_ASSERT (SCM_INUMP (category
), category
, SCM_ARG1
, s_setlocale
);
1149 if (SCM_UNBNDP (locale
))
1155 SCM_ASSERT (SCM_NIMP (locale
) && SCM_STRINGP (locale
), locale
, SCM_ARG2
, s_setlocale
);
1156 clocale
= SCM_CHARS (locale
);
1159 rv
= setlocale (SCM_INUM (category
), clocale
);
1160 return rv
? scm_makfrom0str (rv
) : SCM_MAKINUM (errno
);
1162 /* setlocale not available. */
1163 return SCM_MAKINUM (errno
);
1167 SCM_PROC (s_strftime
, "strftime", 2, 0, 0, scm_strftime
);
1170 scm_strftime (SCM format
, SCM stime
)
1173 scm_strftime (format
, stime
)
1186 SCM_ASSERT (SCM_NIMP (format
) && SCM_STRINGP (format
), format
, SCM_ARG1
, s_strftime
);
1187 SCM_ASSERT (SCM_NIMP (stime
) && SCM_VECTORP (stime
) && scm_obj_length (stime
) == 9,
1188 stime
, SCM_ARG2
, s_strftime
);
1190 fmt
= SCM_ROCHARS (format
);
1191 len
= SCM_ROLENGTH (format
);
1193 #define tm_deref scm_num2long (SCM_VELTS (stime)[n++], (char *)SCM_ARG2, s_strftime)
1195 t
.tm_sec
= tm_deref
;
1196 t
.tm_min
= tm_deref
;
1197 t
.tm_hour
= tm_deref
;
1198 t
.tm_mday
= tm_deref
;
1199 t
.tm_mon
= tm_deref
;
1200 t
.tm_year
= tm_deref
;
1201 /* not used by mktime.
1202 t.tm_wday = tm_deref;
1203 t.tm_yday = tm_deref; */
1204 t
.tm_isdst
= tm_deref
;
1207 /* fill in missing fields and set the timezone. */
1210 tbuf
= scm_must_malloc (size
, s_strftime
);
1211 while ((len
= strftime (tbuf
, size
, fmt
, &t
)) == size
)
1213 scm_must_free (tbuf
);
1215 tbuf
= scm_must_malloc (size
, s_strftime
);
1217 return scm_makfromstr (tbuf
, len
, 0);
1222 SCM_PROC (s_sys_strptime
, "%strptime", 2, 0, 0, scm_sys_strptime
);
1225 scm_sys_strptime (SCM format
, SCM string
)
1228 scm_sys_strptime (format
, string
)
1233 #ifdef HAVE_STRPTIME
1237 char *fmt
, *str
, *rest
;
1241 SCM_ASSERT (SCM_NIMP (format
) && SCM_ROSTRINGP (format
), format
, SCM_ARG1
, s_sys_strptime
);
1242 if (SCM_SUBSTRP (format
))
1243 format
= scm_makfromstr (SCM_ROCHARS (format
), SCM_ROLENGTH (format
), 0);
1244 SCM_ASSERT (SCM_NIMP (string
) && SCM_ROSTRINGP (string
), string
, SCM_ARG2
, s_sys_strptime
);
1245 if (SCM_SUBSTRP (string
))
1246 string
= scm_makfromstr (SCM_ROCHARS (string
), SCM_ROLENGTH (string
), 0);
1248 fmt
= SCM_CHARS (format
);
1249 str
= SCM_CHARS (string
);
1251 /* initialize the struct tm */
1252 #define tm_init(field) t.field = 0
1265 rest
= strptime (str
, fmt
, &t
);
1272 stime
= scm_make_vector (SCM_MAKINUM (9), scm_long2num (0), SCM_UNDEFINED
);
1274 #define stime_set(val) scm_vector_set_x (stime, SCM_MAKINUM (n++), scm_long2num (t.val));
1278 stime_set (tm_hour
);
1279 stime_set (tm_mday
);
1281 stime_set (tm_year
);
1282 stime_set (tm_wday
);
1283 stime_set (tm_yday
);
1284 stime_set (tm_isdst
);
1287 return scm_cons (stime
, scm_makfrom0str (rest
));
1289 scm_wta (SCM_UNSPECIFIED
, "strptime is not available and no replacement has (yet) been supplied", "strptime");
1294 SCM_PROC (s_sys_mknod
, "%mknod", 3, 0, 0, scm_sys_mknod
);
1297 scm_sys_mknod(SCM path
, SCM mode
, SCM dev
)
1300 scm_sys_mknod(path
, mode
, dev
)
1308 SCM_ASSERT(SCM_NIMP(path
) && SCM_STRINGP(path
), path
, SCM_ARG1
, s_sys_mknod
);
1309 SCM_ASSERT(SCM_INUMP(mode
), mode
, SCM_ARG2
, s_sys_mknod
);
1310 SCM_ASSERT(SCM_INUMP(dev
), dev
, SCM_ARG3
, s_sys_mknod
);
1311 SCM_SYSCALL(val
= mknod(SCM_CHARS(path
), SCM_INUM(mode
), SCM_INUM(dev
)));
1312 return val
? SCM_BOOL_F
: SCM_BOOL_T
;
1319 SCM_PROC (s_sys_nice
, "%nice", 1, 0, 0, scm_sys_nice
);
1322 scm_sys_nice(SCM incr
)
1330 SCM_ASSERT(SCM_INUMP(incr
), incr
, SCM_ARG1
, s_sys_nice
);
1331 return nice(SCM_INUM(incr
)) ? SCM_MAKINUM (errno
) : SCM_BOOL_T
;
1333 return SCM_MAKINUM (ENOSYS
);
1338 SCM_PROC (s_sync
, "sync", 0, 0, 0, scm_sync
);
1350 return SCM_UNSPECIFIED
;
1357 scm_init_posix (void)
1363 scm_add_feature ("posix");
1365 scm_add_feature ("EIDs");
1368 scm_sysintern ("WAIT_ANY", SCM_MAKINUM (WAIT_ANY
));
1371 scm_sysintern ("WAIT_MYPGRP", SCM_MAKINUM (WAIT_MYPGRP
));
1374 scm_sysintern ("WNOHANG", SCM_MAKINUM (WNOHANG
));
1377 scm_sysintern ("WUNTRACED", SCM_MAKINUM (WUNTRACED
));
1381 scm_sysintern ("EINTR", SCM_MAKINUM (EINTR
));
1385 scm_sysintern ("SIGHUP", SCM_MAKINUM (SIGHUP
));
1388 scm_sysintern ("SIGINT", SCM_MAKINUM (SIGINT
));
1391 scm_sysintern ("SIGQUIT", SCM_MAKINUM (SIGQUIT
));
1394 scm_sysintern ("SIGILL", SCM_MAKINUM (SIGILL
));
1397 scm_sysintern ("SIGTRAP", SCM_MAKINUM (SIGTRAP
));
1400 scm_sysintern ("SIGABRT", SCM_MAKINUM (SIGABRT
));
1403 scm_sysintern ("SIGIOT", SCM_MAKINUM (SIGIOT
));
1406 scm_sysintern ("SIGBUS", SCM_MAKINUM (SIGBUS
));
1409 scm_sysintern ("SIGFPE", SCM_MAKINUM (SIGFPE
));
1412 scm_sysintern ("SIGKILL", SCM_MAKINUM (SIGKILL
));
1415 scm_sysintern ("SIGUSR1", SCM_MAKINUM (SIGUSR1
));
1418 scm_sysintern ("SIGSEGV", SCM_MAKINUM (SIGSEGV
));
1421 scm_sysintern ("SIGUSR2", SCM_MAKINUM (SIGUSR2
));
1424 scm_sysintern ("SIGPIPE", SCM_MAKINUM (SIGPIPE
));
1427 scm_sysintern ("SIGALRM", SCM_MAKINUM (SIGALRM
));
1430 scm_sysintern ("SIGTERM", SCM_MAKINUM (SIGTERM
));
1433 scm_sysintern ("SIGSTKFLT", SCM_MAKINUM (SIGSTKFLT
));
1436 scm_sysintern ("SIGCHLD", SCM_MAKINUM (SIGCHLD
));
1439 scm_sysintern ("SIGCONT", SCM_MAKINUM (SIGCONT
));
1442 scm_sysintern ("SIGSTOP", SCM_MAKINUM (SIGSTOP
));
1445 scm_sysintern ("SIGTSTP", SCM_MAKINUM (SIGTSTP
));
1448 scm_sysintern ("SIGTTIN", SCM_MAKINUM (SIGTTIN
));
1451 scm_sysintern ("SIGTTOU", SCM_MAKINUM (SIGTTOU
));
1454 scm_sysintern ("SIGIO", SCM_MAKINUM (SIGIO
));
1457 scm_sysintern ("SIGPOLL", SCM_MAKINUM (SIGPOLL
));
1460 scm_sysintern ("SIGURG", SCM_MAKINUM (SIGURG
));
1463 scm_sysintern ("SIGXCPU", SCM_MAKINUM (SIGXCPU
));
1466 scm_sysintern ("SIGXFSZ", SCM_MAKINUM (SIGXFSZ
));
1469 scm_sysintern ("SIGVTALRM", SCM_MAKINUM (SIGVTALRM
));
1472 scm_sysintern ("SIGPROF", SCM_MAKINUM (SIGPROF
));
1475 scm_sysintern ("SIGWINCH", SCM_MAKINUM (SIGWINCH
));
1478 scm_sysintern ("SIGLOST", SCM_MAKINUM (SIGLOST
));
1481 scm_sysintern ("SIGPWR", SCM_MAKINUM (SIGPWR
));
1483 /* access() symbols. */
1484 scm_sysintern ("R_OK", SCM_MAKINUM (R_OK
));
1485 scm_sysintern ("W_OK", SCM_MAKINUM (W_OK
));
1486 scm_sysintern ("X_OK", SCM_MAKINUM (X_OK
));
1487 scm_sysintern ("F_OK", SCM_MAKINUM (F_OK
));
1490 scm_sysintern ("LC_COLLATE", SCM_MAKINUM (LC_COLLATE
));
1493 scm_sysintern ("LC_CTYPE", SCM_MAKINUM (LC_CTYPE
));
1496 scm_sysintern ("LC_MONETARY", SCM_MAKINUM (LC_MONETARY
));
1499 scm_sysintern ("LC_NUMERIC", SCM_MAKINUM (LC_NUMERIC
));
1502 scm_sysintern ("LC_TIME", SCM_MAKINUM (LC_TIME
));
1505 scm_sysintern ("LC_MESSAGES", SCM_MAKINUM (LC_MESSAGES
));
1508 scm_sysintern ("LC_ALL", SCM_MAKINUM (LC_ALL
));