1 /* Copyright (C) 1996, 1997 Free Software Foundation, Inc.
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
52 #ifdef TIME_WITH_SYS_TIME
53 # include <sys/time.h>
57 # include <sys/time.h>
67 #ifdef LIBC_H_WITH_UNISTD_H
71 #ifdef HAVE_SYS_SELECT_H
72 #include <sys/select.h>
79 #include <sys/types.h>
88 # define NAMLEN(dirent) strlen((dirent)->d_name)
90 # define dirent direct
91 # define NAMLEN(dirent) (dirent)->d_namlen
93 # include <sys/ndir.h>
103 /* Ultrix has S_IFSOCK, but no S_ISSOCK. Ipe! */
104 #if defined (S_IFSOCK) && ! defined (S_ISSOCK)
105 #define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
115 SCM_PROC (s_chown
, "chown", 3, 0, 0, scm_chown
);
118 scm_chown (object
, owner
, group
)
126 object
= SCM_COERCE_OUTPORT (object
);
128 SCM_ASSERT (SCM_INUMP (owner
), owner
, SCM_ARG2
, s_chown
);
129 SCM_ASSERT (SCM_INUMP (group
), group
, SCM_ARG3
, s_chown
);
131 if (SCM_INUMP (object
) || (SCM_NIMP (object
) && SCM_OPFPORTP (object
)))
133 if (SCM_INUMP (object
))
134 fdes
= SCM_INUM (object
);
137 fdes
= fileno ((FILE *) SCM_STREAM (object
));
139 scm_syserror (s_chown
);
141 SCM_SYSCALL (rv
= fchown (fdes
, SCM_INUM (owner
), SCM_INUM (group
)));
145 SCM_ASSERT (SCM_NIMP (object
) && SCM_ROSTRINGP (object
),
146 object
, SCM_ARG1
, s_chown
);
147 SCM_COERCE_SUBSTR (object
);
148 SCM_SYSCALL (rv
= chown (SCM_ROCHARS (object
),
149 SCM_INUM (owner
), SCM_INUM (group
)));
152 scm_syserror (s_chown
);
154 return SCM_UNSPECIFIED
;
158 SCM_PROC (s_chmod
, "chmod", 2, 0, 0, scm_chmod
);
161 scm_chmod (object
, mode
)
168 object
= SCM_COERCE_OUTPORT (object
);
170 SCM_ASSERT (SCM_INUMP (mode
), mode
, SCM_ARG2
, s_chmod
);
172 if (SCM_INUMP (object
) || (SCM_NIMP (object
) && SCM_OPFPORTP (object
)))
174 if (SCM_INUMP (object
))
175 fdes
= SCM_INUM (object
);
178 fdes
= fileno ((FILE *) SCM_STREAM (object
));
180 scm_syserror (s_chmod
);
182 SCM_SYSCALL (rv
= fchmod (fdes
, SCM_INUM (mode
)));
186 SCM_ASSERT (SCM_NIMP (object
) && SCM_ROSTRINGP (object
),
187 object
, SCM_ARG1
, s_chmod
);
188 SCM_COERCE_SUBSTR (object
);
189 SCM_SYSCALL (rv
= chmod (SCM_ROCHARS (object
), SCM_INUM (mode
)));
192 scm_syserror (s_chmod
);
194 return SCM_UNSPECIFIED
;
197 SCM_PROC (s_umask
, "umask", 0, 1, 0, scm_umask
);
204 if (SCM_UNBNDP (mode
))
211 SCM_ASSERT (SCM_INUMP (mode
), mode
, SCM_ARG1
, s_umask
);
212 mask
= umask (SCM_INUM (mode
));
214 return SCM_MAKINUM (mask
);
219 SCM_PROC (s_open_fdes
, "open-fdes", 2, 1, 0, scm_open_fdes
);
221 scm_open_fdes (SCM path
, SCM flags
, SCM mode
)
227 SCM_ASSERT (SCM_NIMP (path
) && SCM_ROSTRINGP (path
), path
, SCM_ARG1
,
229 SCM_COERCE_SUBSTR (path
);
230 iflags
= scm_num2long (flags
, (char *) SCM_ARG2
, s_open_fdes
);
233 if (SCM_UNBNDP (mode
))
237 SCM_ASSERT (SCM_INUMP (mode
), mode
, SCM_ARG3
, s_open_fdes
);
238 imode
= SCM_INUM (mode
);
240 SCM_SYSCALL (fd
= open (SCM_ROCHARS (path
), iflags
, imode
));
242 scm_syserror (s_open_fdes
);
244 return SCM_MAKINUM (fd
);
247 SCM_PROC (s_open
, "open", 2, 1, 0, scm_open
);
249 scm_open (SCM path
, SCM flags
, SCM mode
)
257 fd
= SCM_INUM (scm_open_fdes (path
, flags
, mode
));
258 iflags
= scm_num2long (flags
, (char *) SCM_ARG2
, s_open_fdes
);
263 if (iflags
& O_WRONLY
)
269 f
= fdopen (fd
, port_mode
);
272 SCM_SYSCALL (close (fd
));
273 scm_syserror (s_open
);
276 struct scm_port_table
* pt
;
278 pt
= scm_add_to_port_table (newpt
);
279 SCM_SETPTAB_ENTRY (newpt
, pt
);
280 SCM_SETCAR (newpt
, scm_tc16_fport
| scm_mode_bits (port_mode
));
281 /* if (SCM_BUF0 & SCM_CAR (newpt))
282 scm_setbuf0 (newpt); */
283 SCM_SETSTREAM (newpt
, (SCM
)f
);
284 SCM_PTAB_ENTRY (newpt
)->file_name
= path
;
291 SCM_PROC (s_close
, "close", 1, 0, 0, scm_close
);
293 scm_close (SCM fd_or_port
)
298 fd_or_port
= SCM_COERCE_OUTPORT (fd_or_port
);
300 if (SCM_NIMP (fd_or_port
) && SCM_PORTP (fd_or_port
))
301 return scm_close_port (fd_or_port
);
302 SCM_ASSERT (SCM_INUMP (fd_or_port
), fd_or_port
, SCM_ARG1
, s_close
);
303 fd
= SCM_INUM (fd_or_port
);
305 scm_evict_ports (fd
); /* see scsh manual. */
306 SCM_SYSCALL (rv
= close (fd
));
307 /* following scsh, closing an already closed file descriptor is
309 if (rv
< 0 && errno
!= EBADF
)
310 scm_syserror (s_close
);
312 return (rv
< 0) ? SCM_BOOL_F
: SCM_BOOL_T
;
319 SCM_SYMBOL (scm_sym_regular
, "regular");
320 SCM_SYMBOL (scm_sym_directory
, "directory");
321 SCM_SYMBOL (scm_sym_symlink
, "symlink");
322 SCM_SYMBOL (scm_sym_block_special
, "block-special");
323 SCM_SYMBOL (scm_sym_char_special
, "char-special");
324 SCM_SYMBOL (scm_sym_fifo
, "fifo");
325 SCM_SYMBOL (scm_sym_sock
, "socket");
326 SCM_SYMBOL (scm_sym_unknown
, "unknown");
328 static SCM scm_stat2scm
SCM_P ((struct stat
*stat_temp
));
331 scm_stat2scm (stat_temp
)
332 struct stat
*stat_temp
;
334 SCM ans
= scm_make_vector (SCM_MAKINUM (15), SCM_UNSPECIFIED
, SCM_BOOL_F
);
335 SCM
*ve
= SCM_VELTS (ans
);
337 ve
[0] = scm_ulong2num ((unsigned long) stat_temp
->st_dev
);
338 ve
[1] = scm_ulong2num ((unsigned long) stat_temp
->st_ino
);
339 ve
[2] = scm_ulong2num ((unsigned long) stat_temp
->st_mode
);
340 ve
[3] = scm_ulong2num ((unsigned long) stat_temp
->st_nlink
);
341 ve
[4] = scm_ulong2num ((unsigned long) stat_temp
->st_uid
);
342 ve
[5] = scm_ulong2num ((unsigned long) stat_temp
->st_gid
);
344 ve
[6] = scm_ulong2num ((unsigned long) stat_temp
->st_rdev
);
348 ve
[7] = scm_ulong2num ((unsigned long) stat_temp
->st_size
);
349 ve
[8] = scm_ulong2num ((unsigned long) stat_temp
->st_atime
);
350 ve
[9] = scm_ulong2num ((unsigned long) stat_temp
->st_mtime
);
351 ve
[10] = scm_ulong2num ((unsigned long) stat_temp
->st_ctime
);
352 #ifdef HAVE_ST_BLKSIZE
353 ve
[11] = scm_ulong2num ((unsigned long) stat_temp
->st_blksize
);
355 ve
[11] = scm_ulong2num (4096L);
357 #ifdef HAVE_ST_BLOCKS
358 ve
[12] = scm_ulong2num ((unsigned long) stat_temp
->st_blocks
);
363 int mode
= stat_temp
->st_mode
;
366 ve
[13] = scm_sym_regular
;
367 else if (S_ISDIR (mode
))
368 ve
[13] = scm_sym_directory
;
369 else if (S_ISLNK (mode
))
370 ve
[13] = scm_sym_symlink
;
371 else if (S_ISBLK (mode
))
372 ve
[13] = scm_sym_block_special
;
373 else if (S_ISCHR (mode
))
374 ve
[13] = scm_sym_char_special
;
375 else if (S_ISFIFO (mode
))
376 ve
[13] = scm_sym_fifo
;
377 else if (S_ISSOCK (mode
))
378 ve
[13] = scm_sym_sock
;
380 ve
[13] = scm_sym_unknown
;
382 ve
[14] = SCM_MAKINUM ((~S_IFMT
) & mode
);
384 /* the layout of the bits in ve[14] is intended to be portable.
385 If there are systems that don't follow the usual convention,
386 the following could be used:
389 if (S_ISUID & mode) tmp += 1;
391 if (S_IRGRP & mode) tmp += 1;
393 if (S_ISVTX & mode) tmp += 1;
395 if (S_IRUSR & mode) tmp += 1;
397 if (S_IWUSR & mode) tmp += 1;
399 if (S_IXUSR & mode) tmp += 1;
401 if (S_IWGRP & mode) tmp += 1;
403 if (S_IXGRP & mode) tmp += 1;
405 if (S_IROTH & mode) tmp += 1;
407 if (S_IWOTH & mode) tmp += 1;
409 if (S_IXOTH & mode) tmp += 1;
411 ve[14] = SCM_MAKINUM (tmp);
419 SCM_PROC (s_stat
, "stat", 1, 0, 0, scm_stat
);
427 struct stat stat_temp
;
430 if (SCM_INUMP (object
))
431 SCM_SYSCALL (rv
= fstat (SCM_INUM (object
), &stat_temp
));
434 SCM_ASSERT (SCM_NIMP (object
), object
, SCM_ARG1
, s_stat
);
435 if (SCM_ROSTRINGP (object
))
437 SCM_COERCE_SUBSTR (object
);
438 SCM_SYSCALL (rv
= stat (SCM_ROCHARS (object
), &stat_temp
));
442 object
= SCM_COERCE_OUTPORT (object
);
443 SCM_ASSERT (SCM_OPFPORTP (object
), object
, SCM_ARG1
, s_stat
);
444 fdes
= fileno ((FILE *) SCM_STREAM (object
));
446 scm_syserror (s_stat
);
447 SCM_SYSCALL (rv
= fstat (fdes
, &stat_temp
));
454 scm_syserror_msg (s_stat
, "%s: %S",
455 scm_listify (scm_makfrom0str (strerror (errno
)),
461 return scm_stat2scm (&stat_temp
);
466 /* {Modifying Directories}
469 SCM_PROC (s_link
, "link", 2, 0, 0, scm_link
);
472 scm_link (oldpath
, newpath
)
478 SCM_ASSERT (SCM_NIMP (oldpath
) && SCM_ROSTRINGP (oldpath
), oldpath
,
480 if (SCM_SUBSTRP (oldpath
))
481 oldpath
= scm_makfromstr (SCM_ROCHARS (oldpath
),
482 SCM_ROLENGTH (oldpath
), 0);
483 SCM_ASSERT (SCM_NIMP (newpath
) && SCM_ROSTRINGP (newpath
), newpath
,
485 if (SCM_SUBSTRP (newpath
))
486 newpath
= scm_makfromstr (SCM_ROCHARS (newpath
),
487 SCM_ROLENGTH (newpath
), 0);
489 SCM_SYSCALL (val
= link (SCM_ROCHARS (oldpath
), SCM_ROCHARS (newpath
)));
491 scm_syserror (s_link
);
493 return SCM_UNSPECIFIED
;
498 SCM_PROC (s_rename
, "rename-file", 2, 0, 0, scm_rename
);
501 scm_rename (oldname
, newname
)
506 SCM_ASSERT (SCM_NIMP (oldname
) && SCM_ROSTRINGP (oldname
), oldname
, SCM_ARG1
,
508 SCM_ASSERT (SCM_NIMP (newname
) && SCM_ROSTRINGP (newname
), newname
, SCM_ARG2
,
510 SCM_COERCE_SUBSTR (oldname
);
511 SCM_COERCE_SUBSTR (newname
);
514 SCM_SYSCALL (rv
= rename (SCM_ROCHARS (oldname
), SCM_ROCHARS (newname
)));
516 SCM_SYSCALL (rv
= link (SCM_ROCHARS (oldname
), SCM_ROCHARS (newname
)));
519 SCM_SYSCALL (rv
= unlink (SCM_ROCHARS (oldname
)));;
521 /* unlink failed. remove new name */
522 SCM_SYSCALL (unlink (SCM_ROCHARS (newname
)));
526 scm_syserror (s_rename
);
528 return SCM_UNSPECIFIED
;
532 SCM_PROC(s_delete_file
, "delete-file", 1, 0, 0, scm_delete_file
);
535 scm_delete_file (str
)
539 SCM_ASSERT (SCM_NIMP (str
) && SCM_ROSTRINGP (str
), str
, SCM_ARG1
,
541 SCM_COERCE_SUBSTR (str
);
543 SCM_SYSCALL (ans
= unlink (SCM_ROCHARS (str
)));
545 scm_syserror (s_delete_file
);
547 return SCM_UNSPECIFIED
;
550 SCM_PROC (s_truncate_file
, "truncate-file", 2, 0, 0, scm_truncate_file
);
552 scm_truncate_file (SCM object
, SCM size
)
558 object
= SCM_COERCE_OUTPORT (object
);
560 csize
= (scm_sizet
) scm_num2long (size
, (char *) SCM_ARG2
, s_truncate_file
);
562 if (SCM_INUMP (object
) || (SCM_NIMP (object
) && SCM_OPFPORTP (object
)))
564 if (SCM_INUMP (object
))
565 fdes
= SCM_INUM (object
);
568 fdes
= fileno ((FILE *) SCM_STREAM (object
));
570 scm_syserror (s_truncate_file
);
572 SCM_SYSCALL (rv
= ftruncate (fdes
, csize
));
576 SCM_ASSERT (SCM_NIMP (object
) && SCM_ROSTRINGP (object
),
577 object
, SCM_ARG1
, s_chown
);
578 SCM_COERCE_SUBSTR (object
);
579 SCM_SYSCALL (rv
= truncate (SCM_ROCHARS (object
), csize
));
582 scm_syserror (s_truncate_file
);
584 return SCM_UNSPECIFIED
;
587 SCM_PROC (s_mkdir
, "mkdir", 1, 1, 0, scm_mkdir
);
590 scm_mkdir (path
, mode
)
597 SCM_ASSERT (SCM_NIMP (path
) && SCM_ROSTRINGP (path
), path
, SCM_ARG1
,
599 SCM_COERCE_SUBSTR (path
);
601 if (SCM_UNBNDP (mode
))
605 SCM_SYSCALL (rv
= mkdir (SCM_ROCHARS (path
), 0777 ^ mask
));
609 SCM_ASSERT (SCM_INUMP (mode
), mode
, SCM_ARG2
, s_mkdir
);
610 SCM_SYSCALL (rv
= mkdir (SCM_ROCHARS (path
), SCM_INUM (mode
)));
613 scm_syserror (s_mkdir
);
615 return SCM_UNSPECIFIED
;
617 scm_sysmissing (s_mkdir
);
624 SCM_PROC (s_rmdir
, "rmdir", 1, 0, 0, scm_rmdir
);
633 SCM_ASSERT (SCM_NIMP (path
) && SCM_ROSTRINGP (path
), path
, SCM_ARG1
,
635 SCM_COERCE_SUBSTR (path
);
637 SCM_SYSCALL (val
= rmdir (SCM_ROCHARS (path
)));
639 scm_syserror (s_rmdir
);
641 return SCM_UNSPECIFIED
;
643 scm_sysmissing (s_rmdir
);
650 /* {Examining Directories}
655 SCM_PROC (s_opendir
, "opendir", 1, 0, 0, scm_opendir
);
658 scm_opendir (dirname
)
663 SCM_ASSERT (SCM_NIMP (dirname
) && SCM_ROSTRINGP (dirname
), dirname
, SCM_ARG1
,
665 SCM_COERCE_SUBSTR (dirname
);
668 SCM_SYSCALL (ds
= opendir (SCM_ROCHARS (dirname
)));
670 scm_syserror (s_opendir
);
671 SCM_SETCAR (dir
, scm_tc16_dir
| SCM_OPN
);
672 SCM_SETCDR (dir
, ds
);
678 SCM_PROC (s_readdir
, "readdir", 1, 0, 0, scm_readdir
);
684 struct dirent
*rdent
;
686 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPDIRP (port
), port
, SCM_ARG1
, s_readdir
);
688 SCM_SYSCALL (rdent
= readdir ((DIR *) SCM_CDR (port
)));
691 scm_syserror (s_readdir
);
692 return (rdent
? scm_makfromstr (rdent
->d_name
, NAMLEN (rdent
), 0)
698 SCM_PROC (s_rewinddir
, "rewinddir", 1, 0, 0, scm_rewinddir
);
704 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPDIRP (port
), port
, SCM_ARG1
, s_rewinddir
);
705 rewinddir ((DIR *) SCM_CDR (port
));
706 return SCM_UNSPECIFIED
;
711 SCM_PROC (s_closedir
, "closedir", 1, 0, 0, scm_closedir
);
719 SCM_ASSERT (SCM_NIMP (port
) && SCM_DIRP (port
), port
, SCM_ARG1
, s_closedir
);
721 if (SCM_CLOSEDP (port
))
724 return SCM_UNSPECIFIED
;
726 SCM_SYSCALL (sts
= closedir ((DIR *) SCM_CDR (port
)));
728 scm_syserror (s_closedir
);
729 SCM_SETCAR (port
, scm_tc16_dir
);
731 return SCM_UNSPECIFIED
;
737 static int scm_dir_print
SCM_P ((SCM sexp
, SCM port
, scm_print_state
*pstate
));
740 scm_dir_print (sexp
, port
, pstate
)
743 scm_print_state
*pstate
;
745 scm_prinport (sexp
, port
, "directory");
750 static scm_sizet scm_dir_free
SCM_P ((SCM p
));
757 closedir ((DIR *) SCM_CDR (p
));
761 static scm_smobfuns dir_smob
= {scm_mark0
, scm_dir_free
, scm_dir_print
, 0};
764 /* {Navigating Directories}
768 SCM_PROC (s_chdir
, "chdir", 1, 0, 0, scm_chdir
);
776 SCM_ASSERT (SCM_NIMP (str
) && SCM_ROSTRINGP (str
), str
, SCM_ARG1
, s_chdir
);
777 SCM_COERCE_SUBSTR (str
);
779 SCM_SYSCALL (ans
= chdir (SCM_ROCHARS (str
)));
781 scm_syserror (s_chdir
);
783 return SCM_UNSPECIFIED
;
788 SCM_PROC (s_getcwd
, "getcwd", 0, 0, 0, scm_getcwd
);
796 scm_sizet size
= 100;
801 wd
= scm_must_malloc (size
, s_getcwd
);
802 while ((rv
= getcwd (wd
, size
)) == 0 && errno
== ERANGE
)
806 wd
= scm_must_malloc (size
, s_getcwd
);
809 scm_syserror (s_getcwd
);
810 result
= scm_makfromstr (wd
, strlen (wd
), 0);
815 scm_sysmissing (s_getcwd
);
823 SCM_PROC (s_select
, "select", 3, 2, 0, scm_select
);
827 set_element (SELECT_TYPE
*set
, SCM element
, int arg
)
830 element
= SCM_COERCE_OUTPORT (element
);
831 if (SCM_NIMP (element
) && SCM_TYP16 (element
) == scm_tc16_fport
832 && SCM_OPPORTP (element
))
833 fd
= fileno ((FILE *) SCM_STREAM (element
));
835 SCM_ASSERT (SCM_INUMP (element
), element
, arg
, s_select
);
836 fd
= SCM_INUM (element
);
843 fill_select_type (SELECT_TYPE
*set
, SCM list
, int arg
)
846 if (SCM_NIMP (list
) && SCM_VECTORP (list
))
848 int len
= SCM_LENGTH (list
);
849 SCM
*ve
= SCM_VELTS (list
);
853 fd
= set_element (set
, ve
[len
- 1], arg
);
861 while (list
!= SCM_EOL
)
863 fd
= set_element (set
, SCM_CAR (list
), arg
);
866 list
= SCM_CDR (list
);
874 get_element (SELECT_TYPE
*set
, SCM element
, SCM list
)
876 element
= SCM_COERCE_OUTPORT (element
);
877 if (SCM_NIMP (element
)
878 && (scm_tc16_fport
== SCM_TYP16 (element
))
879 && SCM_OPPORTP (element
))
881 if (FD_ISSET (fileno ((FILE *)SCM_STREAM (element
)), set
))
882 list
= scm_cons (element
, list
);
884 else if (SCM_INUMP (element
))
886 if (FD_ISSET (SCM_INUM (element
), set
))
887 list
= scm_cons (element
, list
);
893 retrieve_select_type (SELECT_TYPE
*set
, SCM list
)
895 SCM answer_list
= SCM_EOL
;
897 if (SCM_NIMP (list
) && SCM_VECTORP (list
))
899 int len
= SCM_LENGTH (list
);
900 SCM
*ve
= SCM_VELTS (list
);
904 answer_list
= get_element (set
, ve
[len
- 1], answer_list
);
907 return scm_vector (answer_list
);
911 /* list is a list. */
912 while (list
!= SCM_EOL
)
914 answer_list
= get_element (set
, SCM_CAR (list
), answer_list
);
915 list
= SCM_CDR (list
);
923 scm_select (reads
, writes
, excepts
, secs
, usecs
)
931 struct timeval timeout
;
932 struct timeval
* time_p
;
933 SELECT_TYPE read_set
;
934 SELECT_TYPE write_set
;
935 SELECT_TYPE except_set
;
939 #define assert_set(x, arg) \
940 SCM_ASSERT (scm_ilength (x) > -1 || (SCM_NIMP (x) && SCM_VECTORP (x)), \
942 assert_set (reads
, SCM_ARG1
);
943 assert_set (writes
, SCM_ARG2
);
944 assert_set (excepts
, SCM_ARG3
);
948 FD_ZERO (&write_set
);
949 FD_ZERO (&except_set
);
951 max_fd
= fill_select_type (&read_set
, reads
, SCM_ARG1
);
952 fd
= fill_select_type (&write_set
, writes
, SCM_ARG2
);
955 fd
= fill_select_type (&except_set
, excepts
, SCM_ARG3
);
959 if (SCM_UNBNDP (secs
) || SCM_FALSEP (secs
))
963 if (SCM_INUMP (secs
))
965 timeout
.tv_sec
= SCM_INUM (secs
);
966 if (SCM_UNBNDP (usecs
))
970 SCM_ASSERT (SCM_INUMP (usecs
), usecs
, SCM_ARG5
, s_select
);
972 timeout
.tv_usec
= SCM_INUM (usecs
);
977 double fl
= scm_num2dbl (secs
, s_select
);
979 if (!SCM_UNBNDP (usecs
))
980 scm_wrong_type_arg (s_select
, 4, secs
);
982 scm_out_of_range (s_select
, secs
);
983 timeout
.tv_sec
= (long) fl
;
984 timeout
.tv_usec
= (long) ((fl
- timeout
.tv_sec
) * 1000000);
990 sreturn
= scm_internal_select (max_fd
+ 1,
991 &read_set
, &write_set
, &except_set
, time_p
);
994 sreturn
= select (max_fd
+ 1,
995 &read_set
, &write_set
, &except_set
, time_p
);
999 scm_syserror (s_select
);
1000 return scm_listify (retrieve_select_type (&read_set
, reads
),
1001 retrieve_select_type (&write_set
, writes
),
1002 retrieve_select_type (&except_set
, excepts
),
1005 scm_sysmissing (s_select
);
1011 /* Check if FILE has characters waiting to be read. */
1022 scm_input_waiting_p (f
, caller
)
1028 if (fileno (f
) == fileno (stdin
) && (isatty (fileno (stdin
))))
1041 # include <sys/io.h>
1045 # include <sys/ioctl.h>
1054 scm_input_waiting_p (f
, caller
)
1058 /* Can we return an end-of-file character? */
1062 /* Do we have characters in the stdio buffer? */
1063 # ifdef FILE_CNT_FIELD
1064 if (f
->FILE_CNT_FIELD
> 0)
1067 # ifdef FILE_CNT_GPTR
1068 if (f
->_gptr
!= f
->_egptr
)
1071 # ifdef FILE_CNT_READPTR
1072 if (f
->_IO_read_end
!= f
->_IO_read_ptr
)
1075 Configure
.in could
not guess the name of the correct field in a
FILE *.
1076 This function needs to be ported to your system
.
1077 It should
return zero iff no characters are waiting to be read
.;
1082 /* Is the file prepared to deliver input? */
1085 struct timeval timeout
;
1086 SELECT_TYPE read_set
;
1087 SELECT_TYPE write_set
;
1088 SELECT_TYPE except_set
;
1089 int fno
= fileno ((FILE *)f
);
1091 FD_ZERO (&read_set
);
1092 FD_ZERO (&write_set
);
1093 FD_ZERO (&except_set
);
1095 FD_SET (fno
, &read_set
);
1098 timeout
.tv_usec
= 0;
1101 if (select (SELECT_SET_SIZE
,
1102 &read_set
, &write_set
, &except_set
, &timeout
)
1104 scm_syserror (caller
);
1106 return FD_ISSET (fno
, &read_set
);
1112 ioctl(fileno(f
), FIONREAD
, &remir
);
1116 scm_misc_error ("char-ready?", "Not fully implemented on this platform",
1125 SCM_PROC (s_fcntl
, "fcntl", 2, 0, 1, scm_fcntl
);
1127 scm_fcntl (SCM object
, SCM cmd
, SCM value
)
1133 object
= SCM_COERCE_OUTPORT (object
);
1135 SCM_ASSERT (SCM_INUMP (cmd
), cmd
, SCM_ARG2
, s_fcntl
);
1136 if (SCM_NIMP (object
) && SCM_OPFPORTP (object
))
1137 fdes
= fileno ((FILE *) SCM_STREAM (object
));
1140 SCM_ASSERT (SCM_INUMP (object
), object
, SCM_ARG1
, s_fcntl
);
1141 fdes
= SCM_INUM (object
);
1143 if (SCM_NULLP (value
))
1147 SCM_ASSERT (SCM_INUMP (SCM_CAR (value
)), value
, SCM_ARG3
, s_fcntl
);
1148 ivalue
= SCM_INUM (SCM_CAR (value
));
1152 SCM_SYSCALL (rv
= fcntl (fdes
, SCM_INUM (cmd
), ivalue
));
1154 rv
= 0; /* avoid compiler warning. */
1155 if (rv
== -1 || fdes
== -1)
1156 scm_syserror (s_fcntl
);
1158 return SCM_MAKINUM (rv
);
1161 SCM_PROC (s_fsync
, "fsync", 1, 0, 0, scm_fsync
);
1163 scm_fsync (SCM object
)
1167 object
= SCM_COERCE_OUTPORT (object
);
1170 if (SCM_NIMP (object
) && SCM_OPFPORTP (object
))
1172 scm_force_output (object
);
1173 fdes
= fileno ((FILE *) SCM_STREAM (object
));
1175 scm_syserror (s_fsync
);
1179 SCM_ASSERT (SCM_INUMP (object
), object
, SCM_ARG1
, s_fsync
);
1180 fdes
= SCM_INUM (object
);
1182 if (fsync (fdes
) == -1)
1183 scm_syserror (s_fsync
);
1185 return SCM_UNSPECIFIED
;
1188 SCM_PROC (s_symlink
, "symlink", 2, 0, 0, scm_symlink
);
1191 scm_symlink(oldpath
, newpath
)
1198 SCM_ASSERT (SCM_NIMP (oldpath
) && SCM_ROSTRINGP (oldpath
), oldpath
, SCM_ARG1
,
1200 SCM_ASSERT (SCM_NIMP (newpath
) && SCM_ROSTRINGP (newpath
), newpath
, SCM_ARG2
,
1202 SCM_COERCE_SUBSTR (oldpath
);
1203 SCM_COERCE_SUBSTR (newpath
);
1205 SCM_SYSCALL (val
= symlink(SCM_ROCHARS(oldpath
), SCM_ROCHARS(newpath
)));
1207 scm_syserror (s_symlink
);
1209 return SCM_UNSPECIFIED
;
1211 scm_sysmissing (s_symlink
);
1218 SCM_PROC (s_readlink
, "readlink", 1, 0, 0, scm_readlink
);
1224 #ifdef HAVE_READLINK
1226 scm_sizet size
= 100;
1229 SCM_ASSERT (SCM_NIMP (path
) && SCM_ROSTRINGP (path
), path
, (char *) SCM_ARG1
,
1231 SCM_COERCE_SUBSTR (path
);
1233 buf
= scm_must_malloc (size
, s_readlink
);
1234 while ((rv
= readlink (SCM_ROCHARS (path
), buf
, (scm_sizet
) size
)) == size
)
1236 scm_must_free (buf
);
1238 buf
= scm_must_malloc (size
, s_readlink
);
1241 scm_syserror (s_readlink
);
1242 result
= scm_makfromstr (buf
, rv
, 0);
1243 scm_must_free (buf
);
1247 scm_sysmissing (s_readlink
);
1254 SCM_PROC (s_lstat
, "lstat", 1, 0, 0, scm_lstat
);
1262 struct stat stat_temp
;
1264 SCM_ASSERT (SCM_NIMP (str
) && SCM_ROSTRINGP (str
), str
, (char *) SCM_ARG1
,
1266 SCM_COERCE_SUBSTR (str
);
1268 SCM_SYSCALL(rv
= lstat(SCM_ROCHARS(str
), &stat_temp
));
1273 scm_syserror_msg (s_lstat
, "%s: %S",
1274 scm_listify (scm_makfrom0str (strerror (errno
)),
1280 return scm_stat2scm(&stat_temp
);
1282 scm_sysmissing (s_lstat
);
1289 SCM_PROC (s_copy_file
, "copy-file", 2, 0, 0, scm_copy_file
);
1292 scm_copy_file (oldfile
, newfile
)
1298 char buf
[BUFSIZ
]; /* this space could be shared. */
1299 struct stat oldstat
;
1301 SCM_ASSERT (SCM_NIMP (oldfile
) && SCM_ROSTRINGP (oldfile
), oldfile
, SCM_ARG1
, s_copy_file
);
1302 if (SCM_SUBSTRP (oldfile
))
1303 oldfile
= scm_makfromstr (SCM_ROCHARS (oldfile
), SCM_ROLENGTH (oldfile
), 0);
1304 SCM_ASSERT (SCM_NIMP (newfile
) && SCM_ROSTRINGP (newfile
), newfile
, SCM_ARG2
, s_copy_file
);
1305 if (SCM_SUBSTRP (newfile
))
1306 newfile
= scm_makfromstr (SCM_ROCHARS (newfile
), SCM_ROLENGTH (newfile
), 0);
1307 if (stat (SCM_ROCHARS (oldfile
), &oldstat
) == -1)
1308 scm_syserror (s_copy_file
);
1310 oldfd
= open (SCM_ROCHARS (oldfile
), O_RDONLY
);
1312 scm_syserror (s_copy_file
);
1314 /* use POSIX flags instead of 07777?. */
1315 newfd
= open (SCM_ROCHARS (newfile
), O_WRONLY
| O_CREAT
| O_TRUNC
,
1316 oldstat
.st_mode
& 07777);
1318 scm_syserror (s_copy_file
);
1320 while ((n
= read (oldfd
, buf
, sizeof buf
)) > 0)
1321 if (write (newfd
, buf
, n
) != n
)
1325 scm_syserror (s_copy_file
);
1328 if (close (newfd
) == -1)
1329 scm_syserror (s_copy_file
);
1331 return SCM_UNSPECIFIED
;
1339 scm_add_feature ("i/o-extensions");
1341 scm_tc16_dir
= scm_newsmob (&dir_smob
);
1344 scm_sysintern ("O_RDONLY", scm_long2num (O_RDONLY
));
1347 scm_sysintern ("O_WRONLY", scm_long2num (O_WRONLY
));
1350 scm_sysintern ("O_RDWR", scm_long2num (O_RDWR
));
1353 scm_sysintern ("O_CREAT", scm_long2num (O_CREAT
));
1356 scm_sysintern ("O_EXCL", scm_long2num (O_EXCL
));
1359 scm_sysintern ("O_NOCTTY", scm_long2num (O_NOCTTY
));
1362 scm_sysintern ("O_TRUNC", scm_long2num (O_TRUNC
));
1365 scm_sysintern ("O_APPEND", scm_long2num (O_APPEND
));
1368 scm_sysintern ("O_NONBLOCK", scm_long2num (O_NONBLOCK
));
1371 scm_sysintern ("O_NDELAY", scm_long2num (O_NDELAY
));
1374 scm_sysintern ("O_SYNC", scm_long2num (O_SYNC
));
1378 scm_sysintern ("F_DUPFD", scm_long2num (F_DUPFD
));
1381 scm_sysintern ("F_GETFD", scm_long2num (F_GETFD
));
1384 scm_sysintern ("F_SETFD", scm_long2num (F_SETFD
));
1387 scm_sysintern ("F_GETFL", scm_long2num (F_GETFL
));
1390 scm_sysintern ("F_SETFL", scm_long2num (F_SETFL
));
1393 scm_sysintern ("F_GETOWN", scm_long2num (F_GETOWN
));
1396 scm_sysintern ("F_SETOWN", scm_long2num (F_SETOWN
));
1399 scm_sysintern ("FD_CLOEXEC", scm_long2num (FD_CLOEXEC
));
1402 #include "filesys.x"