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
)
404 SCM_ASSERT (SCM_INUMP (pid
), pid
, SCM_ARG1
, s_sys_waitpid
);
405 if (SCM_UNBNDP (options
))
409 SCM_ASSERT (SCM_INUMP (options
), options
, SCM_ARG2
, s_sys_waitpid
);
410 /* Flags are interned in scm_init_posix. */
411 ioptions
= SCM_INUM (options
);
413 SCM_SYSCALL (i
= waitpid (SCM_INUM (pid
), &status
, ioptions
));
415 SCM_SYSERROR (s_sys_waitpid
);
416 return scm_cons (SCM_MAKINUM (0L + i
), SCM_MAKINUM (0L + status
));
418 SCM_SYSMISSING (s_sys_waitpid
);
426 SCM_PROC (s_getppid
, "getppid", 0, 0, 0, scm_getppid
);
435 return SCM_MAKINUM (0L + getppid ());
440 SCM_PROC (s_getuid
, "getuid", 0, 0, 0, scm_getuid
);
449 return SCM_MAKINUM (0L + getuid ());
454 SCM_PROC (s_getgid
, "getgid", 0, 0, 0, scm_getgid
);
463 return SCM_MAKINUM (0L + getgid ());
468 SCM_PROC (s_geteuid
, "geteuid", 0, 0, 0, scm_geteuid
);
478 return SCM_MAKINUM (0L + geteuid ());
480 return SCM_MAKINUM (0L + getuid ());
486 SCM_PROC (s_getegid
, "getegid", 0, 0, 0, scm_getegid
);
496 return SCM_MAKINUM (0L + getegid ());
498 return SCM_MAKINUM (0L + getgid ());
503 SCM_PROC (s_sys_setuid
, "setuid", 1, 0, 0, scm_sys_setuid
);
506 scm_sys_setuid (SCM id
)
513 SCM_ASSERT (SCM_INUMP (id
), id
, SCM_ARG1
, s_sys_setuid
);
514 if (setuid (SCM_INUM (id
)) != 0)
515 SCM_SYSERROR (s_sys_setuid
);
516 return SCM_UNSPECIFIED
;
519 SCM_PROC (s_sys_setgid
, "setgid", 1, 0, 0, scm_sys_setgid
);
522 scm_sys_setgid (SCM id
)
529 SCM_ASSERT (SCM_INUMP (id
), id
, SCM_ARG1
, s_sys_setgid
);
530 if (setgid (SCM_INUM (id
)) != 0)
531 SCM_SYSERROR (s_sys_setgid
);
532 return SCM_UNSPECIFIED
;
535 SCM_PROC (s_sys_seteuid
, "seteuid", 1, 0, 0, scm_sys_seteuid
);
538 scm_sys_seteuid (SCM id
)
547 SCM_ASSERT (SCM_INUMP (id
), id
, SCM_ARG1
, s_sys_seteuid
);
549 rv
= seteuid (SCM_INUM (id
));
551 rv
= setuid (SCM_INUM (id
));
554 SCM_SYSERROR (s_sys_seteuid
);
555 return SCM_UNSPECIFIED
;
558 SCM_PROC (s_sys_setegid
, "setegid", 1, 0, 0, scm_sys_setegid
);
561 scm_sys_setegid (SCM id
)
570 SCM_ASSERT (SCM_INUMP (id
), id
, SCM_ARG1
, s_sys_setegid
);
572 rv
= setegid (SCM_INUM (id
));
574 rv
= setgid (SCM_INUM (id
));
577 SCM_SYSERROR (s_sys_setegid
);
578 return SCM_UNSPECIFIED
;
582 SCM_PROC (s_getpgrp
, "getpgrp", 0, 0, 0, scm_getpgrp
);
588 return SCM_MAKINUM (fn (0));
591 SCM_PROC (s_setpgid
, "setpgid", 2, 0, 0, scm_setpgid
);
593 scm_setpgid (pid
, pgid
)
597 SCM_ASSERT (SCM_INUMP (pid
), pid
, SCM_ARG1
, s_setpgid
);
598 SCM_ASSERT (SCM_INUMP (pgid
), pgid
, SCM_ARG2
, s_setpgid
);
599 /* FIXME(?): may be known as setpgrp. */
600 if (setpgid (SCM_INUM (pid
), SCM_INUM (pgid
)) != 0)
601 SCM_SYSERROR (s_setpgid
);
602 return SCM_UNSPECIFIED
;
604 SCM_SYSMISSING (s_sys_setpgid
);
610 SCM_PROC (s_setsid
, "setsid", 0, 0, 0, scm_setsid
);
615 pid_t sid
= setsid ();
617 SCM_SYSERROR (s_setsid
);
618 return SCM_UNSPECIFIED
;
620 SCM_SYSMISSING (s_sys_setsid
);
626 SCM_PROC (s_ttyname
, "ttyname", 1, 0, 0, scm_ttyname
);
629 scm_ttyname (SCM port
)
638 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPPORTP (port
), port
, SCM_ARG1
, s_ttyname
);
639 if (scm_tc16_fport
!= SCM_TYP16 (port
))
641 fd
= fileno ((FILE *)SCM_STREAM (port
));
643 SCM_SYSERROR (s_ttyname
);
644 SCM_SYSCALL (ans
= ttyname (fd
));
646 SCM_SYSERROR (s_ttyname
);
647 /* ans could be overwritten by another call to ttyname */
648 return (scm_makfrom0str (ans
));
652 SCM_PROC (s_ctermid
, "ctermid", 0, 0, 0, scm_ctermid
);
657 char *result
= ctermid (NULL
);
659 SCM_SYSERROR (s_ctermid
);
660 return scm_makfrom0str (result
);
662 SCM_SYSMISSING (s_sys_ctermid
);
668 SCM_PROC (s_tcgetpgrp
, "tcgetpgrp", 1, 0, 0, scm_tcgetpgrp
);
673 #ifdef HAVE_TCGETPGRP
676 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPFPORTP (port
), port
, SCM_ARG1
, s_tcgetpgrp
);
677 fd
= fileno ((FILE *)SCM_STREAM (port
));
678 if (fd
== -1 || (pgid
= tcgetpgrp (fd
)) == -1)
679 SCM_SYSERROR (s_tcgetpgrp
);
680 return SCM_MAKINUM (pgid
);
682 SCM_SYSMISSING (s_sys_tcgetpgrp
);
688 SCM_PROC (s_tcsetpgrp
, "tcsetpgrp", 2, 0, 0, scm_tcsetpgrp
);
690 scm_tcsetpgrp (port
, pgid
)
693 #ifdef HAVE_TCSETPGRP
695 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPFPORTP (port
), port
, SCM_ARG1
, s_tcsetpgrp
);
696 SCM_ASSERT (SCM_INUMP (pgid
), pgid
, SCM_ARG2
, s_tcsetpgrp
);
697 fd
= fileno ((FILE *)SCM_STREAM (port
));
698 if (fd
== -1 || tcsetpgrp (fd
, SCM_INUM (pgid
)) == -1)
699 SCM_SYSERROR (s_tcsetpgrp
);
700 return SCM_UNSPECIFIED
;
702 SCM_SYSMISSING (s_sys_tcsetpgrp
);
708 /* Copy exec args from an SCM vector into a new C array. */
711 scm_convert_exec_args (SCM args
)
714 scm_convert_exec_args (args
)
722 num_args
= scm_ilength (args
);
724 scm_must_malloc ((num_args
+ 1) * sizeof (char *), s_ttyname
);
725 for (i
= 0; SCM_NNULLP (args
); args
= SCM_CDR (args
), ++i
)
730 SCM_ASSERT (SCM_NIMP (SCM_CAR (args
)) && SCM_ROSTRINGP (SCM_CAR (args
)), SCM_CAR (args
),
731 "wrong type in SCM_ARG", "exec arg");
732 len
= 1 + SCM_ROLENGTH (SCM_CAR (args
));
733 dst
= (char *) scm_must_malloc ((long) len
, s_ttyname
);
734 src
= SCM_ROCHARS (SCM_CAR (args
));
744 SCM_PROC (s_sys_execl
, "execl", 0, 0, 1, scm_sys_execl
);
747 scm_sys_execl (SCM args
)
755 SCM filename
= SCM_CAR (args
);
756 SCM_ASSERT (SCM_NIMP (filename
) && SCM_ROSTRINGP (filename
), filename
, SCM_ARG1
, s_sys_execl
);
757 if (SCM_SUBSTRP (filename
))
758 filename
= scm_makfromstr (SCM_ROCHARS (filename
), SCM_ROLENGTH (filename
), 0);
759 args
= SCM_CDR (args
);
760 execargv
= scm_convert_exec_args (args
);
761 execv (SCM_ROCHARS (filename
), execargv
);
762 SCM_SYSERROR (s_sys_execl
);
767 SCM_PROC (s_sys_execlp
, "execlp", 0, 0, 1, scm_sys_execlp
);
770 scm_sys_execlp (SCM args
)
773 scm_sys_execlp (args
)
778 SCM filename
= SCM_CAR (args
);
779 SCM_ASSERT (SCM_NIMP (filename
) && SCM_ROSTRINGP (filename
), filename
, SCM_ARG1
, s_sys_execlp
);
780 if (SCM_SUBSTRP (filename
))
781 filename
= scm_makfromstr (SCM_ROCHARS (filename
), SCM_ROLENGTH (filename
), 0);
782 args
= SCM_CDR (args
);
783 execargv
= scm_convert_exec_args (args
);
784 execvp (SCM_ROCHARS (filename
), execargv
);
785 SCM_SYSERROR (s_sys_execlp
);
790 /* Flushing streams etc., is not done here. */
791 SCM_PROC (s_sys_fork
, "fork", 0, 0, 0, scm_sys_fork
);
803 SCM_SYSERROR (s_sys_fork
);
804 return SCM_MAKINUM (0L+pid
);
808 SCM_PROC (s_sys_uname
, "uname", 0, 0, 0, scm_sys_uname
);
819 SCM ans
= scm_make_vector(SCM_MAKINUM(5), SCM_UNSPECIFIED
, SCM_BOOL_F
);
820 SCM
*ve
= SCM_VELTS (ans
);
822 return SCM_MAKINUM (errno
);
823 ve
[0] = scm_makfrom0str (buf
.sysname
);
824 ve
[1] = scm_makfrom0str (buf
.nodename
);
825 ve
[2] = scm_makfrom0str (buf
.release
);
826 ve
[3] = scm_makfrom0str (buf
.version
);
827 ve
[4] = scm_makfrom0str (buf
.machine
);
830 ve[5] = scm_makfrom0str (buf.domainname);
834 SCM_SYSMISSING (s_sys_uname
);
840 SCM_PROC (s_environ
, "environ", 0, 1, 0, scm_environ
);
843 scm_environ (SCM env
)
850 if (SCM_UNBNDP (env
))
851 return scm_makfromstrs (-1, environ
);
857 SCM_ASSERT (SCM_NULLP (env
) || (SCM_NIMP (env
) && SCM_CONSP (env
)),
858 env
, SCM_ARG1
, s_environ
);
859 num_strings
= scm_ilength (env
);
860 new_environ
= (char **) scm_must_malloc ((num_strings
+ 1)
863 while (SCM_NNULLP (env
))
867 SCM_ASSERT (SCM_NIMP (SCM_CAR (env
)) && SCM_ROSTRINGP (SCM_CAR (env
)), env
, SCM_ARG1
,
869 len
= 1 + SCM_ROLENGTH (SCM_CAR (env
));
870 new_environ
[i
] = scm_must_malloc ((long) len
, s_environ
);
871 src
= SCM_ROCHARS (SCM_CAR (env
));
873 new_environ
[i
][len
] = src
[len
];
878 /* Free the old environment, except when called for the first
883 static int first
= 1;
886 for (ep
= environ
; *ep
!= NULL
; ep
++)
888 scm_must_free ((char *) environ
);
892 environ
= new_environ
;
893 return SCM_UNSPECIFIED
;
898 SCM_PROC (s_open_pipe
, "open-pipe", 2, 0, 0, scm_open_pipe
);
901 scm_open_pipe (SCM pipestr
, SCM modes
)
904 scm_open_pipe (pipestr
, modes
)
911 struct scm_port_table
* pt
;
913 SCM_ASSERT (SCM_NIMP (pipestr
) && SCM_ROSTRINGP (pipestr
), pipestr
, SCM_ARG1
, s_open_pipe
);
914 if (SCM_SUBSTRP (pipestr
))
915 pipestr
= scm_makfromstr (SCM_ROCHARS (pipestr
), SCM_ROLENGTH (pipestr
), 0);
916 SCM_ASSERT (SCM_NIMP (modes
) && SCM_ROSTRINGP (modes
), modes
, SCM_ARG2
, s_open_pipe
);
917 if (SCM_SUBSTRP (modes
))
918 modes
= scm_makfromstr (SCM_ROCHARS (modes
), SCM_ROLENGTH (modes
), 0);
921 scm_ignore_signals ();
922 SCM_SYSCALL (f
= popen (SCM_ROCHARS (pipestr
), SCM_ROCHARS (modes
)));
923 scm_unignore_signals ();
925 SCM_SYSERROR (s_open_pipe
);
926 pt
= scm_add_to_port_table (z
);
927 SCM_SETPTAB_ENTRY (z
, pt
);
928 SCM_CAR (z
) = scm_tc16_pipe
| SCM_OPN
929 | (strchr (SCM_ROCHARS (modes
), 'r') ? SCM_RDNG
: SCM_WRTNG
);
930 SCM_SETSTREAM (z
, (SCM
)f
);
936 SCM_PROC (s_open_input_pipe
, "open-input-pipe", 1, 0, 0, scm_open_input_pipe
);
939 scm_open_input_pipe(SCM pipestr
)
942 scm_open_input_pipe(pipestr
)
946 return scm_open_pipe(pipestr
, scm_makfromstr("r", (sizeof "r")-1, 0));
949 SCM_PROC (s_open_output_pipe
, "open-output-pipe", 1, 0, 0, scm_open_output_pipe
);
952 scm_open_output_pipe(SCM pipestr
)
955 scm_open_output_pipe(pipestr
)
959 return scm_open_pipe(pipestr
, scm_makfromstr("w", (sizeof "w")-1, 0));
964 #include <sys/utime.h>
969 SCM_PROC (s_sys_utime
, "utime", 1, 2, 0, scm_sys_utime
);
972 scm_sys_utime (SCM pathname
, SCM actime
, SCM modtime
)
975 scm_sys_utime (pathname
, actime
, modtime
)
982 struct utimbuf utm_tmp
;
984 SCM_ASSERT (SCM_NIMP (pathname
) && SCM_STRINGP (pathname
), pathname
, SCM_ARG1
, s_sys_utime
);
986 if (SCM_UNBNDP (actime
))
987 SCM_SYSCALL (time (&utm_tmp
.actime
));
989 utm_tmp
.actime
= scm_num2ulong (actime
, (char *) SCM_ARG2
, s_sys_utime
);
991 if (SCM_UNBNDP (modtime
))
992 SCM_SYSCALL (time (&utm_tmp
.modtime
));
994 utm_tmp
.modtime
= scm_num2ulong (modtime
, (char *) SCM_ARG3
, s_sys_utime
);
996 SCM_SYSCALL (rv
= utime (SCM_CHARS (pathname
), &utm_tmp
));
998 SCM_SYSERROR (s_sys_utime
);
999 return SCM_UNSPECIFIED
;
1002 SCM_PROC (s_sys_access
, "access?", 2, 0, 0, scm_sys_access
);
1005 scm_sys_access (SCM path
, SCM how
)
1008 scm_sys_access (path
, how
)
1015 SCM_ASSERT (SCM_NIMP (path
) && SCM_ROSTRINGP (path
), path
, SCM_ARG1
, s_sys_access
);
1016 if (SCM_SUBSTRP (path
))
1017 path
= scm_makfromstr (SCM_ROCHARS (path
), SCM_ROLENGTH (path
), 0);
1018 SCM_ASSERT (SCM_INUMP (how
), how
, SCM_ARG2
, s_sys_access
);
1019 rv
= access (SCM_ROCHARS (path
), SCM_INUM (how
));
1020 return rv
? SCM_BOOL_F
: SCM_BOOL_T
;
1023 SCM_PROC (s_getpid
, "getpid", 0, 0, 0, scm_getpid
);
1032 return SCM_MAKINUM ((unsigned long) getpid ());
1035 SCM_PROC (s_sys_putenv
, "putenv", 1, 0, 0, scm_sys_putenv
);
1038 scm_sys_putenv (SCM str
)
1041 scm_sys_putenv (str
)
1046 SCM_ASSERT (SCM_NIMP (str
) && SCM_STRINGP (str
), str
, SCM_ARG1
, s_sys_putenv
);
1047 return putenv (SCM_CHARS (str
)) ? SCM_MAKINUM (errno
) : SCM_BOOL_T
;
1049 SCM_SYSMISSING (s_sys_putenv
);
1055 SCM_PROC (s_read_line
, "read-line", 0, 2, 0, scm_read_line
);
1058 scm_read_line (SCM port
, SCM include_terminator
)
1061 scm_read_line (port
, include_terminator
)
1063 SCM include_terminator
;
1073 tok_buf
= scm_makstr ((long) len
, 0);
1074 p
= SCM_CHARS (tok_buf
);
1075 if (SCM_UNBNDP (port
))
1078 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPINPORTP (port
), port
, SCM_ARG1
, s_read_line
);
1080 if (SCM_UNBNDP (include_terminator
))
1083 include
= SCM_NFALSEP (include_terminator
);
1085 if (EOF
== (c
= scm_gen_getc (port
)))
1091 case SCM_LINE_INCREMENTORS
:
1094 p
= scm_grow_tok_buf (&tok_buf
);
1095 len
= SCM_LENGTH (tok_buf
);
1102 return scm_vector_set_length_x (tok_buf
, (SCM
) SCM_MAKINUM (j
));
1107 p
= scm_grow_tok_buf (&tok_buf
);
1108 len
= SCM_LENGTH (tok_buf
);
1111 c
= scm_gen_getc (port
);
1117 SCM_PROC (s_read_line_x
, "read-line!", 1, 1, 0, scm_read_line_x
);
1120 scm_read_line_x (SCM str
, SCM port
)
1123 scm_read_line_x (str
, port
)
1132 SCM_ASSERT (SCM_NIMP (str
) && SCM_STRINGP (str
), str
, SCM_ARG1
, s_read_line_x
);
1133 p
= SCM_CHARS (str
);
1134 len
= SCM_LENGTH (str
);
1136 (port
) port
= scm_cur_inp
;
1138 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPINPORTP (port
), port
, SCM_ARG2
, s_read_line_x
);
1139 c
= scm_gen_getc (port
);
1146 case SCM_LINE_INCREMENTORS
:
1148 return SCM_MAKINUM (j
);
1152 scm_gen_ungetc (c
, port
);
1156 c
= scm_gen_getc (port
);
1161 SCM_PROC (s_write_line
, "write-line", 1, 1, 0, scm_write_line
);
1164 scm_write_line (SCM obj
, SCM port
)
1167 scm_write_line (obj
, port
)
1172 scm_display (obj
, port
);
1173 return scm_newline (port
);
1176 SCM_PROC (s_setlocale
, "setlocale", 1, 1, 0, scm_setlocale
);
1179 scm_setlocale (SCM category
, SCM locale
)
1182 scm_setlocale (category
, locale
)
1187 #ifdef HAVE_SETLOCALE
1191 SCM_ASSERT (SCM_INUMP (category
), category
, SCM_ARG1
, s_setlocale
);
1192 if (SCM_UNBNDP (locale
))
1198 SCM_ASSERT (SCM_NIMP (locale
) && SCM_STRINGP (locale
), locale
, SCM_ARG2
, s_setlocale
);
1199 clocale
= SCM_CHARS (locale
);
1202 rv
= setlocale (SCM_INUM (category
), clocale
);
1204 SCM_SYSERROR (s_setlocale
);
1205 return scm_makfrom0str (rv
);
1207 SCM_SYSMISSING (s_setlocale
);
1213 SCM_PROC (s_strftime
, "strftime", 2, 0, 0, scm_strftime
);
1216 scm_strftime (SCM format
, SCM stime
)
1219 scm_strftime (format
, stime
)
1232 SCM_ASSERT (SCM_NIMP (format
) && SCM_STRINGP (format
), format
, SCM_ARG1
, s_strftime
);
1233 SCM_ASSERT (SCM_NIMP (stime
) && SCM_VECTORP (stime
) && scm_obj_length (stime
) == 9,
1234 stime
, SCM_ARG2
, s_strftime
);
1236 fmt
= SCM_ROCHARS (format
);
1237 len
= SCM_ROLENGTH (format
);
1239 #define tm_deref scm_num2long (SCM_VELTS (stime)[n++], (char *)SCM_ARG2, s_strftime)
1241 t
.tm_sec
= tm_deref
;
1242 t
.tm_min
= tm_deref
;
1243 t
.tm_hour
= tm_deref
;
1244 t
.tm_mday
= tm_deref
;
1245 t
.tm_mon
= tm_deref
;
1246 t
.tm_year
= tm_deref
;
1247 /* not used by mktime.
1248 t.tm_wday = tm_deref;
1249 t.tm_yday = tm_deref; */
1250 t
.tm_isdst
= tm_deref
;
1253 /* fill in missing fields and set the timezone. */
1256 tbuf
= scm_must_malloc (size
, s_strftime
);
1257 while ((len
= strftime (tbuf
, size
, fmt
, &t
)) == size
)
1259 scm_must_free (tbuf
);
1261 tbuf
= scm_must_malloc (size
, s_strftime
);
1263 return scm_makfromstr (tbuf
, len
, 0);
1266 SCM_PROC (s_sys_strptime
, "strptime", 2, 0, 0, scm_sys_strptime
);
1269 scm_sys_strptime (SCM format
, SCM string
)
1272 scm_sys_strptime (format
, string
)
1277 #ifdef HAVE_STRPTIME
1281 char *fmt
, *str
, *rest
;
1284 SCM_ASSERT (SCM_NIMP (format
) && SCM_ROSTRINGP (format
), format
, SCM_ARG1
, s_sys_strptime
);
1285 if (SCM_SUBSTRP (format
))
1286 format
= scm_makfromstr (SCM_ROCHARS (format
), SCM_ROLENGTH (format
), 0);
1287 SCM_ASSERT (SCM_NIMP (string
) && SCM_ROSTRINGP (string
), string
, SCM_ARG2
, s_sys_strptime
);
1288 if (SCM_SUBSTRP (string
))
1289 string
= scm_makfromstr (SCM_ROCHARS (string
), SCM_ROLENGTH (string
), 0);
1291 fmt
= SCM_CHARS (format
);
1292 str
= SCM_CHARS (string
);
1294 /* initialize the struct tm */
1295 #define tm_init(field) t.field = 0
1308 rest
= strptime (str
, fmt
, &t
);
1312 SCM_SYSERROR (s_sys_strptime
);
1314 stime
= scm_make_vector (SCM_MAKINUM (9), scm_long2num (0), SCM_UNDEFINED
);
1316 #define stime_set(val) scm_vector_set_x (stime, SCM_MAKINUM (n++), scm_long2num (t.val));
1320 stime_set (tm_hour
);
1321 stime_set (tm_mday
);
1323 stime_set (tm_year
);
1324 stime_set (tm_wday
);
1325 stime_set (tm_yday
);
1326 stime_set (tm_isdst
);
1329 return scm_cons (stime
, scm_makfrom0str (rest
));
1331 SCM_SYSMISSING (s_sys_strptime
);
1337 SCM_PROC (s_sys_mknod
, "mknod", 3, 0, 0, scm_sys_mknod
);
1340 scm_sys_mknod(SCM path
, SCM mode
, SCM dev
)
1343 scm_sys_mknod(path
, mode
, dev
)
1351 SCM_ASSERT(SCM_NIMP(path
) && SCM_STRINGP(path
), path
, SCM_ARG1
, s_sys_mknod
);
1352 SCM_ASSERT(SCM_INUMP(mode
), mode
, SCM_ARG2
, s_sys_mknod
);
1353 SCM_ASSERT(SCM_INUMP(dev
), dev
, SCM_ARG3
, s_sys_mknod
);
1354 SCM_SYSCALL(val
= mknod(SCM_CHARS(path
), SCM_INUM(mode
), SCM_INUM(dev
)));
1356 SCM_SYSERROR (s_sys_mknod
);
1357 return SCM_UNSPECIFIED
;
1359 SCM_SYSMISSING (s_sys_mknod
);
1366 SCM_PROC (s_sys_nice
, "nice", 1, 0, 0, scm_sys_nice
);
1369 scm_sys_nice(SCM incr
)
1377 SCM_ASSERT(SCM_INUMP(incr
), incr
, SCM_ARG1
, s_sys_nice
);
1378 if (nice(SCM_INUM(incr
)) != 0)
1379 SCM_SYSERROR (s_sys_nice
);
1380 return SCM_UNSPECIFIED
;
1382 SCM_SYSMISSING (s_sys_nice
);
1389 SCM_PROC (s_sync
, "sync", 0, 0, 0, scm_sync
);
1401 SCM_SYSMISSING (s_sync
);
1410 scm_init_posix (void)
1416 scm_add_feature ("posix");
1418 scm_add_feature ("EIDs");
1421 scm_sysintern ("WAIT_ANY", SCM_MAKINUM (WAIT_ANY
));
1424 scm_sysintern ("WAIT_MYPGRP", SCM_MAKINUM (WAIT_MYPGRP
));
1427 scm_sysintern ("WNOHANG", SCM_MAKINUM (WNOHANG
));
1430 scm_sysintern ("WUNTRACED", SCM_MAKINUM (WUNTRACED
));
1434 scm_sysintern ("EINTR", SCM_MAKINUM (EINTR
));
1438 scm_sysintern ("SIGHUP", SCM_MAKINUM (SIGHUP
));
1441 scm_sysintern ("SIGINT", SCM_MAKINUM (SIGINT
));
1444 scm_sysintern ("SIGQUIT", SCM_MAKINUM (SIGQUIT
));
1447 scm_sysintern ("SIGILL", SCM_MAKINUM (SIGILL
));
1450 scm_sysintern ("SIGTRAP", SCM_MAKINUM (SIGTRAP
));
1453 scm_sysintern ("SIGABRT", SCM_MAKINUM (SIGABRT
));
1456 scm_sysintern ("SIGIOT", SCM_MAKINUM (SIGIOT
));
1459 scm_sysintern ("SIGBUS", SCM_MAKINUM (SIGBUS
));
1462 scm_sysintern ("SIGFPE", SCM_MAKINUM (SIGFPE
));
1465 scm_sysintern ("SIGKILL", SCM_MAKINUM (SIGKILL
));
1468 scm_sysintern ("SIGUSR1", SCM_MAKINUM (SIGUSR1
));
1471 scm_sysintern ("SIGSEGV", SCM_MAKINUM (SIGSEGV
));
1474 scm_sysintern ("SIGUSR2", SCM_MAKINUM (SIGUSR2
));
1477 scm_sysintern ("SIGPIPE", SCM_MAKINUM (SIGPIPE
));
1480 scm_sysintern ("SIGALRM", SCM_MAKINUM (SIGALRM
));
1483 scm_sysintern ("SIGTERM", SCM_MAKINUM (SIGTERM
));
1486 scm_sysintern ("SIGSTKFLT", SCM_MAKINUM (SIGSTKFLT
));
1489 scm_sysintern ("SIGCHLD", SCM_MAKINUM (SIGCHLD
));
1492 scm_sysintern ("SIGCONT", SCM_MAKINUM (SIGCONT
));
1495 scm_sysintern ("SIGSTOP", SCM_MAKINUM (SIGSTOP
));
1498 scm_sysintern ("SIGTSTP", SCM_MAKINUM (SIGTSTP
));
1501 scm_sysintern ("SIGTTIN", SCM_MAKINUM (SIGTTIN
));
1504 scm_sysintern ("SIGTTOU", SCM_MAKINUM (SIGTTOU
));
1507 scm_sysintern ("SIGIO", SCM_MAKINUM (SIGIO
));
1510 scm_sysintern ("SIGPOLL", SCM_MAKINUM (SIGPOLL
));
1513 scm_sysintern ("SIGURG", SCM_MAKINUM (SIGURG
));
1516 scm_sysintern ("SIGXCPU", SCM_MAKINUM (SIGXCPU
));
1519 scm_sysintern ("SIGXFSZ", SCM_MAKINUM (SIGXFSZ
));
1522 scm_sysintern ("SIGVTALRM", SCM_MAKINUM (SIGVTALRM
));
1525 scm_sysintern ("SIGPROF", SCM_MAKINUM (SIGPROF
));
1528 scm_sysintern ("SIGWINCH", SCM_MAKINUM (SIGWINCH
));
1531 scm_sysintern ("SIGLOST", SCM_MAKINUM (SIGLOST
));
1534 scm_sysintern ("SIGPWR", SCM_MAKINUM (SIGPWR
));
1536 /* access() symbols. */
1537 scm_sysintern ("R_OK", SCM_MAKINUM (R_OK
));
1538 scm_sysintern ("W_OK", SCM_MAKINUM (W_OK
));
1539 scm_sysintern ("X_OK", SCM_MAKINUM (X_OK
));
1540 scm_sysintern ("F_OK", SCM_MAKINUM (F_OK
));
1543 scm_sysintern ("LC_COLLATE", SCM_MAKINUM (LC_COLLATE
));
1546 scm_sysintern ("LC_CTYPE", SCM_MAKINUM (LC_CTYPE
));
1549 scm_sysintern ("LC_MONETARY", SCM_MAKINUM (LC_MONETARY
));
1552 scm_sysintern ("LC_NUMERIC", SCM_MAKINUM (LC_NUMERIC
));
1555 scm_sysintern ("LC_TIME", SCM_MAKINUM (LC_TIME
));
1558 scm_sysintern ("LC_MESSAGES", SCM_MAKINUM (LC_MESSAGES
));
1561 scm_sysintern ("LC_ALL", SCM_MAKINUM (LC_ALL
));