1 /* Copyright (C) 1996, 1997, 1998 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
);
130 if (SCM_INUMP (object
) || (SCM_NIMP (object
) && SCM_OPFPORTP (object
)))
132 if (SCM_INUMP (object
))
133 fdes
= SCM_INUM (object
);
135 fdes
= SCM_FPORT_FDES (object
);
136 SCM_SYSCALL (rv
= fchown (fdes
, SCM_INUM (owner
), SCM_INUM (group
)));
140 SCM_ASSERT (SCM_NIMP (object
) && SCM_ROSTRINGP (object
),
141 object
, SCM_ARG1
, s_chown
);
142 SCM_COERCE_SUBSTR (object
);
143 SCM_SYSCALL (rv
= chown (SCM_ROCHARS (object
),
144 SCM_INUM (owner
), SCM_INUM (group
)));
147 scm_syserror (s_chown
);
148 return SCM_UNSPECIFIED
;
152 SCM_PROC (s_chmod
, "chmod", 2, 0, 0, scm_chmod
);
155 scm_chmod (object
, mode
)
162 object
= SCM_COERCE_OUTPORT (object
);
164 SCM_ASSERT (SCM_INUMP (mode
), mode
, SCM_ARG2
, s_chmod
);
165 if (SCM_INUMP (object
) || (SCM_NIMP (object
) && SCM_OPFPORTP (object
)))
167 if (SCM_INUMP (object
))
168 fdes
= SCM_INUM (object
);
170 fdes
= SCM_FPORT_FDES (object
);
171 SCM_SYSCALL (rv
= fchmod (fdes
, SCM_INUM (mode
)));
175 SCM_ASSERT (SCM_NIMP (object
) && SCM_ROSTRINGP (object
),
176 object
, SCM_ARG1
, s_chmod
);
177 SCM_COERCE_SUBSTR (object
);
178 SCM_SYSCALL (rv
= chmod (SCM_ROCHARS (object
), SCM_INUM (mode
)));
181 scm_syserror (s_chmod
);
182 return SCM_UNSPECIFIED
;
185 SCM_PROC (s_umask
, "umask", 0, 1, 0, scm_umask
);
192 if (SCM_UNBNDP (mode
))
199 SCM_ASSERT (SCM_INUMP (mode
), mode
, SCM_ARG1
, s_umask
);
200 mask
= umask (SCM_INUM (mode
));
202 return SCM_MAKINUM (mask
);
207 SCM_PROC (s_open_fdes
, "open-fdes", 2, 1, 0, scm_open_fdes
);
209 scm_open_fdes (SCM path
, SCM flags
, SCM mode
)
215 SCM_ASSERT (SCM_NIMP (path
) && SCM_ROSTRINGP (path
), path
, SCM_ARG1
,
217 SCM_COERCE_SUBSTR (path
);
218 iflags
= scm_num2long (flags
, (char *) SCM_ARG2
, s_open_fdes
);
220 if (SCM_UNBNDP (mode
))
224 SCM_ASSERT (SCM_INUMP (mode
), mode
, SCM_ARG3
, s_open_fdes
);
225 imode
= SCM_INUM (mode
);
227 SCM_SYSCALL (fd
= open (SCM_ROCHARS (path
), iflags
, imode
));
229 scm_syserror (s_open_fdes
);
230 return SCM_MAKINUM (fd
);
233 SCM_PROC (s_open
, "open", 2, 1, 0, scm_open
);
235 scm_open (SCM path
, SCM flags
, SCM mode
)
242 fd
= SCM_INUM (scm_open_fdes (path
, flags
, mode
));
243 iflags
= scm_num2long (flags
, (char *) SCM_ARG2
, s_open_fdes
);
246 if (iflags
& O_APPEND
)
248 else if (iflags
& O_CREAT
)
254 if (iflags
& O_APPEND
)
256 else if (iflags
& O_WRONLY
)
261 newpt
= scm_fdes_to_port (fd
, port_mode
, path
);
265 SCM_PROC (s_close
, "close", 1, 0, 0, scm_close
);
267 scm_close (SCM fd_or_port
)
272 fd_or_port
= SCM_COERCE_OUTPORT (fd_or_port
);
274 if (SCM_NIMP (fd_or_port
) && SCM_PORTP (fd_or_port
))
275 return scm_close_port (fd_or_port
);
276 SCM_ASSERT (SCM_INUMP (fd_or_port
), fd_or_port
, SCM_ARG1
, s_close
);
277 fd
= SCM_INUM (fd_or_port
);
278 scm_evict_ports (fd
); /* see scsh manual. */
279 SCM_SYSCALL (rv
= close (fd
));
280 /* following scsh, closing an already closed file descriptor is
282 if (rv
< 0 && errno
!= EBADF
)
283 scm_syserror (s_close
);
284 return (rv
< 0) ? SCM_BOOL_F
: SCM_BOOL_T
;
291 SCM_SYMBOL (scm_sym_regular
, "regular");
292 SCM_SYMBOL (scm_sym_directory
, "directory");
293 SCM_SYMBOL (scm_sym_symlink
, "symlink");
294 SCM_SYMBOL (scm_sym_block_special
, "block-special");
295 SCM_SYMBOL (scm_sym_char_special
, "char-special");
296 SCM_SYMBOL (scm_sym_fifo
, "fifo");
297 SCM_SYMBOL (scm_sym_sock
, "socket");
298 SCM_SYMBOL (scm_sym_unknown
, "unknown");
300 static SCM scm_stat2scm
SCM_P ((struct stat
*stat_temp
));
303 scm_stat2scm (stat_temp
)
304 struct stat
*stat_temp
;
306 SCM ans
= scm_make_vector (SCM_MAKINUM (15), SCM_UNSPECIFIED
);
307 SCM
*ve
= SCM_VELTS (ans
);
309 ve
[0] = scm_ulong2num ((unsigned long) stat_temp
->st_dev
);
310 ve
[1] = scm_ulong2num ((unsigned long) stat_temp
->st_ino
);
311 ve
[2] = scm_ulong2num ((unsigned long) stat_temp
->st_mode
);
312 ve
[3] = scm_ulong2num ((unsigned long) stat_temp
->st_nlink
);
313 ve
[4] = scm_ulong2num ((unsigned long) stat_temp
->st_uid
);
314 ve
[5] = scm_ulong2num ((unsigned long) stat_temp
->st_gid
);
316 ve
[6] = scm_ulong2num ((unsigned long) stat_temp
->st_rdev
);
320 ve
[7] = scm_ulong2num ((unsigned long) stat_temp
->st_size
);
321 ve
[8] = scm_ulong2num ((unsigned long) stat_temp
->st_atime
);
322 ve
[9] = scm_ulong2num ((unsigned long) stat_temp
->st_mtime
);
323 ve
[10] = scm_ulong2num ((unsigned long) stat_temp
->st_ctime
);
324 #ifdef HAVE_ST_BLKSIZE
325 ve
[11] = scm_ulong2num ((unsigned long) stat_temp
->st_blksize
);
327 ve
[11] = scm_ulong2num (4096L);
329 #ifdef HAVE_ST_BLOCKS
330 ve
[12] = scm_ulong2num ((unsigned long) stat_temp
->st_blocks
);
335 int mode
= stat_temp
->st_mode
;
338 ve
[13] = scm_sym_regular
;
339 else if (S_ISDIR (mode
))
340 ve
[13] = scm_sym_directory
;
341 else if (S_ISLNK (mode
))
342 ve
[13] = scm_sym_symlink
;
343 else if (S_ISBLK (mode
))
344 ve
[13] = scm_sym_block_special
;
345 else if (S_ISCHR (mode
))
346 ve
[13] = scm_sym_char_special
;
347 else if (S_ISFIFO (mode
))
348 ve
[13] = scm_sym_fifo
;
349 else if (S_ISSOCK (mode
))
350 ve
[13] = scm_sym_sock
;
352 ve
[13] = scm_sym_unknown
;
354 ve
[14] = SCM_MAKINUM ((~S_IFMT
) & mode
);
356 /* the layout of the bits in ve[14] is intended to be portable.
357 If there are systems that don't follow the usual convention,
358 the following could be used:
361 if (S_ISUID & mode) tmp += 1;
363 if (S_IRGRP & mode) tmp += 1;
365 if (S_ISVTX & mode) tmp += 1;
367 if (S_IRUSR & mode) tmp += 1;
369 if (S_IWUSR & mode) tmp += 1;
371 if (S_IXUSR & mode) tmp += 1;
373 if (S_IWGRP & mode) tmp += 1;
375 if (S_IXGRP & mode) tmp += 1;
377 if (S_IROTH & mode) tmp += 1;
379 if (S_IWOTH & mode) tmp += 1;
381 if (S_IXOTH & mode) tmp += 1;
383 ve[14] = SCM_MAKINUM (tmp);
391 SCM_PROC (s_stat
, "stat", 1, 0, 0, scm_stat
);
399 struct stat stat_temp
;
401 if (SCM_INUMP (object
))
402 SCM_SYSCALL (rv
= fstat (SCM_INUM (object
), &stat_temp
));
405 SCM_ASSERT (SCM_NIMP (object
), object
, SCM_ARG1
, s_stat
);
406 if (SCM_ROSTRINGP (object
))
408 SCM_COERCE_SUBSTR (object
);
409 SCM_SYSCALL (rv
= stat (SCM_ROCHARS (object
), &stat_temp
));
413 object
= SCM_COERCE_OUTPORT (object
);
414 SCM_ASSERT (SCM_OPFPORTP (object
), object
, SCM_ARG1
, s_stat
);
415 fdes
= SCM_FPORT_FDES (object
);
416 SCM_SYSCALL (rv
= fstat (fdes
, &stat_temp
));
423 scm_syserror_msg (s_stat
, "%s: %S",
424 scm_listify (scm_makfrom0str (strerror (errno
)),
429 return scm_stat2scm (&stat_temp
);
433 /* {Modifying Directories}
436 SCM_PROC (s_link
, "link", 2, 0, 0, scm_link
);
439 scm_link (oldpath
, newpath
)
445 SCM_ASSERT (SCM_NIMP (oldpath
) && SCM_ROSTRINGP (oldpath
), oldpath
,
447 if (SCM_SUBSTRP (oldpath
))
448 oldpath
= scm_makfromstr (SCM_ROCHARS (oldpath
),
449 SCM_ROLENGTH (oldpath
), 0);
450 SCM_ASSERT (SCM_NIMP (newpath
) && SCM_ROSTRINGP (newpath
), newpath
,
452 if (SCM_SUBSTRP (newpath
))
453 newpath
= scm_makfromstr (SCM_ROCHARS (newpath
),
454 SCM_ROLENGTH (newpath
), 0);
455 SCM_SYSCALL (val
= link (SCM_ROCHARS (oldpath
), SCM_ROCHARS (newpath
)));
457 scm_syserror (s_link
);
458 return SCM_UNSPECIFIED
;
463 SCM_PROC (s_rename
, "rename-file", 2, 0, 0, scm_rename
);
466 scm_rename (oldname
, newname
)
471 SCM_ASSERT (SCM_NIMP (oldname
) && SCM_ROSTRINGP (oldname
), oldname
, SCM_ARG1
,
473 SCM_ASSERT (SCM_NIMP (newname
) && SCM_ROSTRINGP (newname
), newname
, SCM_ARG2
,
475 SCM_COERCE_SUBSTR (oldname
);
476 SCM_COERCE_SUBSTR (newname
);
478 SCM_SYSCALL (rv
= rename (SCM_ROCHARS (oldname
), SCM_ROCHARS (newname
)));
480 SCM_SYSCALL (rv
= link (SCM_ROCHARS (oldname
), SCM_ROCHARS (newname
)));
483 SCM_SYSCALL (rv
= unlink (SCM_ROCHARS (oldname
)));;
485 /* unlink failed. remove new name */
486 SCM_SYSCALL (unlink (SCM_ROCHARS (newname
)));
490 scm_syserror (s_rename
);
491 return SCM_UNSPECIFIED
;
495 SCM_PROC(s_delete_file
, "delete-file", 1, 0, 0, scm_delete_file
);
498 scm_delete_file (str
)
502 SCM_ASSERT (SCM_NIMP (str
) && SCM_ROSTRINGP (str
), str
, SCM_ARG1
,
504 SCM_COERCE_SUBSTR (str
);
505 SCM_SYSCALL (ans
= unlink (SCM_ROCHARS (str
)));
507 scm_syserror (s_delete_file
);
508 return SCM_UNSPECIFIED
;
511 SCM_PROC (s_truncate_file
, "truncate-file", 2, 0, 0, scm_truncate_file
);
513 scm_truncate_file (SCM object
, SCM size
)
519 object
= SCM_COERCE_OUTPORT (object
);
521 csize
= (scm_sizet
) scm_num2long (size
, (char *) SCM_ARG2
, s_truncate_file
);
522 if (SCM_INUMP (object
) || (SCM_NIMP (object
) && SCM_OPFPORTP (object
)))
524 if (SCM_INUMP (object
))
525 fdes
= SCM_INUM (object
);
527 fdes
= SCM_FPORT_FDES (object
);
528 SCM_SYSCALL (rv
= ftruncate (fdes
, csize
));
532 SCM_ASSERT (SCM_NIMP (object
) && SCM_ROSTRINGP (object
),
533 object
, SCM_ARG1
, s_chown
);
534 SCM_COERCE_SUBSTR (object
);
535 SCM_SYSCALL (rv
= truncate (SCM_ROCHARS (object
), csize
));
538 scm_syserror (s_truncate_file
);
539 return SCM_UNSPECIFIED
;
542 SCM_PROC (s_mkdir
, "mkdir", 1, 1, 0, scm_mkdir
);
545 scm_mkdir (path
, mode
)
552 SCM_ASSERT (SCM_NIMP (path
) && SCM_ROSTRINGP (path
), path
, SCM_ARG1
,
554 SCM_COERCE_SUBSTR (path
);
555 if (SCM_UNBNDP (mode
))
559 SCM_SYSCALL (rv
= mkdir (SCM_ROCHARS (path
), 0777 ^ mask
));
563 SCM_ASSERT (SCM_INUMP (mode
), mode
, SCM_ARG2
, s_mkdir
);
564 SCM_SYSCALL (rv
= mkdir (SCM_ROCHARS (path
), SCM_INUM (mode
)));
567 scm_syserror (s_mkdir
);
568 return SCM_UNSPECIFIED
;
570 scm_sysmissing (s_mkdir
);
577 SCM_PROC (s_rmdir
, "rmdir", 1, 0, 0, scm_rmdir
);
586 SCM_ASSERT (SCM_NIMP (path
) && SCM_ROSTRINGP (path
), path
, SCM_ARG1
,
588 SCM_COERCE_SUBSTR (path
);
589 SCM_SYSCALL (val
= rmdir (SCM_ROCHARS (path
)));
591 scm_syserror (s_rmdir
);
592 return SCM_UNSPECIFIED
;
594 scm_sysmissing (s_rmdir
);
601 /* {Examining Directories}
606 SCM_PROC (s_opendir
, "opendir", 1, 0, 0, scm_opendir
);
609 scm_opendir (dirname
)
613 SCM_ASSERT (SCM_NIMP (dirname
) && SCM_ROSTRINGP (dirname
), dirname
, SCM_ARG1
,
615 SCM_COERCE_SUBSTR (dirname
);
616 SCM_SYSCALL (ds
= opendir (SCM_ROCHARS (dirname
)));
618 scm_syserror (s_opendir
);
619 SCM_RETURN_NEWSMOB (scm_tc16_dir
| SCM_OPN
, ds
);
623 SCM_PROC (s_readdir
, "readdir", 1, 0, 0, scm_readdir
);
629 struct dirent
*rdent
;
630 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPDIRP (port
), port
, SCM_ARG1
, s_readdir
);
632 SCM_SYSCALL (rdent
= readdir ((DIR *) SCM_CDR (port
)));
634 scm_syserror (s_readdir
);
635 return (rdent
? scm_makfromstr (rdent
->d_name
, NAMLEN (rdent
), 0)
641 SCM_PROC (s_rewinddir
, "rewinddir", 1, 0, 0, scm_rewinddir
);
647 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPDIRP (port
), port
, SCM_ARG1
, s_rewinddir
);
648 rewinddir ((DIR *) SCM_CDR (port
));
649 return SCM_UNSPECIFIED
;
654 SCM_PROC (s_closedir
, "closedir", 1, 0, 0, scm_closedir
);
662 SCM_ASSERT (SCM_NIMP (port
) && SCM_DIRP (port
), port
, SCM_ARG1
, s_closedir
);
663 if (SCM_CLOSEDP (port
))
665 return SCM_UNSPECIFIED
;
667 SCM_SYSCALL (sts
= closedir ((DIR *) SCM_CDR (port
)));
669 scm_syserror (s_closedir
);
670 SCM_SETCAR (port
, scm_tc16_dir
);
671 return SCM_UNSPECIFIED
;
677 static int scm_dir_print
SCM_P ((SCM sexp
, SCM port
, scm_print_state
*pstate
));
680 scm_dir_print (sexp
, port
, pstate
)
683 scm_print_state
*pstate
;
685 scm_prinport (sexp
, port
, "directory");
690 static scm_sizet scm_dir_free
SCM_P ((SCM p
));
697 closedir ((DIR *) SCM_CDR (p
));
702 /* {Navigating Directories}
706 SCM_PROC (s_chdir
, "chdir", 1, 0, 0, scm_chdir
);
714 SCM_ASSERT (SCM_NIMP (str
) && SCM_ROSTRINGP (str
), str
, SCM_ARG1
, s_chdir
);
715 SCM_COERCE_SUBSTR (str
);
716 SCM_SYSCALL (ans
= chdir (SCM_ROCHARS (str
)));
718 scm_syserror (s_chdir
);
719 return SCM_UNSPECIFIED
;
724 SCM_PROC (s_getcwd
, "getcwd", 0, 0, 0, scm_getcwd
);
732 scm_sizet size
= 100;
736 wd
= scm_must_malloc (size
, s_getcwd
);
737 while ((rv
= getcwd (wd
, size
)) == 0 && errno
== ERANGE
)
741 wd
= scm_must_malloc (size
, s_getcwd
);
744 scm_syserror (s_getcwd
);
745 result
= scm_makfromstr (wd
, strlen (wd
), 0);
749 scm_sysmissing (s_getcwd
);
757 SCM_PROC (s_select
, "select", 3, 2, 0, scm_select
);
761 set_element (SELECT_TYPE
*set
, SCM element
, int arg
)
764 element
= SCM_COERCE_OUTPORT (element
);
765 if (SCM_NIMP (element
) && SCM_OPFPORTP (element
))
766 fd
= SCM_FPORT_FDES (element
);
768 SCM_ASSERT (SCM_INUMP (element
), element
, arg
, s_select
);
769 fd
= SCM_INUM (element
);
776 fill_select_type (SELECT_TYPE
*set
, SCM list
, int arg
)
779 if (SCM_NIMP (list
) && SCM_VECTORP (list
))
781 int len
= SCM_LENGTH (list
);
782 SCM
*ve
= SCM_VELTS (list
);
786 fd
= set_element (set
, ve
[len
- 1], arg
);
794 while (list
!= SCM_EOL
)
796 fd
= set_element (set
, SCM_CAR (list
), arg
);
799 list
= SCM_CDR (list
);
807 get_element (SELECT_TYPE
*set
, SCM element
, SCM list
)
809 element
= SCM_COERCE_OUTPORT (element
);
810 if (SCM_NIMP (element
) && SCM_OPFPORTP (element
))
812 if (FD_ISSET (SCM_FPORT_FDES (element
), set
))
813 list
= scm_cons (element
, list
);
815 else if (SCM_INUMP (element
))
817 if (FD_ISSET (SCM_INUM (element
), set
))
818 list
= scm_cons (element
, list
);
824 retrieve_select_type (SELECT_TYPE
*set
, SCM list
)
826 SCM answer_list
= SCM_EOL
;
828 if (SCM_NIMP (list
) && SCM_VECTORP (list
))
830 int len
= SCM_LENGTH (list
);
831 SCM
*ve
= SCM_VELTS (list
);
835 answer_list
= get_element (set
, ve
[len
- 1], answer_list
);
838 return scm_vector (answer_list
);
842 /* list is a list. */
843 while (list
!= SCM_EOL
)
845 answer_list
= get_element (set
, SCM_CAR (list
), answer_list
);
846 list
= SCM_CDR (list
);
854 scm_select (reads
, writes
, excepts
, secs
, usecs
)
862 struct timeval timeout
;
863 struct timeval
* time_p
;
864 SELECT_TYPE read_set
;
865 SELECT_TYPE write_set
;
866 SELECT_TYPE except_set
;
870 #define assert_set(x, arg) \
871 SCM_ASSERT (scm_ilength (x) > -1 || (SCM_NIMP (x) && SCM_VECTORP (x)), \
873 assert_set (reads
, SCM_ARG1
);
874 assert_set (writes
, SCM_ARG2
);
875 assert_set (excepts
, SCM_ARG3
);
879 FD_ZERO (&write_set
);
880 FD_ZERO (&except_set
);
882 max_fd
= fill_select_type (&read_set
, reads
, SCM_ARG1
);
883 fd
= fill_select_type (&write_set
, writes
, SCM_ARG2
);
886 fd
= fill_select_type (&except_set
, excepts
, SCM_ARG3
);
890 if (SCM_UNBNDP (secs
) || SCM_FALSEP (secs
))
894 if (SCM_INUMP (secs
))
896 timeout
.tv_sec
= SCM_INUM (secs
);
897 if (SCM_UNBNDP (usecs
))
901 SCM_ASSERT (SCM_INUMP (usecs
), usecs
, SCM_ARG5
, s_select
);
903 timeout
.tv_usec
= SCM_INUM (usecs
);
908 double fl
= scm_num2dbl (secs
, s_select
);
910 if (!SCM_UNBNDP (usecs
))
911 scm_wrong_type_arg (s_select
, 4, secs
);
913 scm_out_of_range (s_select
, secs
);
914 timeout
.tv_sec
= (long) fl
;
915 timeout
.tv_usec
= (long) ((fl
- timeout
.tv_sec
) * 1000000);
921 sreturn
= scm_internal_select (max_fd
+ 1,
922 &read_set
, &write_set
, &except_set
, time_p
);
924 sreturn
= select (max_fd
+ 1,
925 &read_set
, &write_set
, &except_set
, time_p
);
928 scm_syserror (s_select
);
929 return scm_listify (retrieve_select_type (&read_set
, reads
),
930 retrieve_select_type (&write_set
, writes
),
931 retrieve_select_type (&except_set
, excepts
),
934 scm_sysmissing (s_select
);
942 SCM_PROC (s_fcntl
, "fcntl", 2, 0, 1, scm_fcntl
);
944 scm_fcntl (SCM object
, SCM cmd
, SCM value
)
950 object
= SCM_COERCE_OUTPORT (object
);
952 SCM_ASSERT (SCM_INUMP (cmd
), cmd
, SCM_ARG2
, s_fcntl
);
953 if (SCM_NIMP (object
) && SCM_OPFPORTP (object
))
954 fdes
= SCM_FPORT_FDES (object
);
957 SCM_ASSERT (SCM_INUMP (object
), object
, SCM_ARG1
, s_fcntl
);
958 fdes
= SCM_INUM (object
);
960 if (SCM_NULLP (value
))
964 SCM_ASSERT (SCM_INUMP (SCM_CAR (value
)), value
, SCM_ARG3
, s_fcntl
);
965 ivalue
= SCM_INUM (SCM_CAR (value
));
967 SCM_SYSCALL (rv
= fcntl (fdes
, SCM_INUM (cmd
), ivalue
));
969 scm_syserror (s_fcntl
);
970 return SCM_MAKINUM (rv
);
973 SCM_PROC (s_fsync
, "fsync", 1, 0, 0, scm_fsync
);
975 scm_fsync (SCM object
)
979 object
= SCM_COERCE_OUTPORT (object
);
981 if (SCM_NIMP (object
) && SCM_OPFPORTP (object
))
984 fdes
= SCM_FPORT_FDES (object
);
988 SCM_ASSERT (SCM_INUMP (object
), object
, SCM_ARG1
, s_fsync
);
989 fdes
= SCM_INUM (object
);
991 if (fsync (fdes
) == -1)
992 scm_syserror (s_fsync
);
993 return SCM_UNSPECIFIED
;
996 SCM_PROC (s_symlink
, "symlink", 2, 0, 0, scm_symlink
);
999 scm_symlink(oldpath
, newpath
)
1006 SCM_ASSERT (SCM_NIMP (oldpath
) && SCM_ROSTRINGP (oldpath
), oldpath
, SCM_ARG1
,
1008 SCM_ASSERT (SCM_NIMP (newpath
) && SCM_ROSTRINGP (newpath
), newpath
, SCM_ARG2
,
1010 SCM_COERCE_SUBSTR (oldpath
);
1011 SCM_COERCE_SUBSTR (newpath
);
1012 SCM_SYSCALL (val
= symlink(SCM_ROCHARS(oldpath
), SCM_ROCHARS(newpath
)));
1014 scm_syserror (s_symlink
);
1015 return SCM_UNSPECIFIED
;
1017 scm_sysmissing (s_symlink
);
1024 SCM_PROC (s_readlink
, "readlink", 1, 0, 0, scm_readlink
);
1030 #ifdef HAVE_READLINK
1035 SCM_ASSERT (SCM_NIMP (path
) && SCM_ROSTRINGP (path
), path
, (char *) SCM_ARG1
,
1037 SCM_COERCE_SUBSTR (path
);
1038 buf
= scm_must_malloc (size
, s_readlink
);
1039 while ((rv
= readlink (SCM_ROCHARS (path
), buf
, size
)) == size
)
1041 scm_must_free (buf
);
1043 buf
= scm_must_malloc (size
, s_readlink
);
1046 scm_syserror (s_readlink
);
1047 result
= scm_makfromstr (buf
, rv
, 0);
1048 scm_must_free (buf
);
1051 scm_sysmissing (s_readlink
);
1058 SCM_PROC (s_lstat
, "lstat", 1, 0, 0, scm_lstat
);
1066 struct stat stat_temp
;
1068 SCM_ASSERT (SCM_NIMP (str
) && SCM_ROSTRINGP (str
), str
, (char *) SCM_ARG1
,
1070 SCM_COERCE_SUBSTR (str
);
1071 SCM_SYSCALL(rv
= lstat(SCM_ROCHARS(str
), &stat_temp
));
1076 scm_syserror_msg (s_lstat
, "%s: %S",
1077 scm_listify (scm_makfrom0str (strerror (errno
)),
1082 return scm_stat2scm(&stat_temp
);
1084 scm_sysmissing (s_lstat
);
1091 SCM_PROC (s_copy_file
, "copy-file", 2, 0, 0, scm_copy_file
);
1094 scm_copy_file (oldfile
, newfile
)
1101 struct stat oldstat
;
1103 SCM_ASSERT (SCM_NIMP (oldfile
) && SCM_ROSTRINGP (oldfile
), oldfile
, SCM_ARG1
, s_copy_file
);
1104 if (SCM_SUBSTRP (oldfile
))
1105 oldfile
= scm_makfromstr (SCM_ROCHARS (oldfile
), SCM_ROLENGTH (oldfile
), 0);
1106 SCM_ASSERT (SCM_NIMP (newfile
) && SCM_ROSTRINGP (newfile
), newfile
, SCM_ARG2
, s_copy_file
);
1107 if (SCM_SUBSTRP (newfile
))
1108 newfile
= scm_makfromstr (SCM_ROCHARS (newfile
), SCM_ROLENGTH (newfile
), 0);
1109 if (stat (SCM_ROCHARS (oldfile
), &oldstat
) == -1)
1110 scm_syserror (s_copy_file
);
1111 oldfd
= open (SCM_ROCHARS (oldfile
), O_RDONLY
);
1113 scm_syserror (s_copy_file
);
1115 /* use POSIX flags instead of 07777?. */
1116 newfd
= open (SCM_ROCHARS (newfile
), O_WRONLY
| O_CREAT
| O_TRUNC
,
1117 oldstat
.st_mode
& 07777);
1119 scm_syserror (s_copy_file
);
1121 while ((n
= read (oldfd
, buf
, sizeof buf
)) > 0)
1122 if (write (newfd
, buf
, n
) != n
)
1126 scm_syserror (s_copy_file
);
1129 if (close (newfd
) == -1)
1130 scm_syserror (s_copy_file
);
1131 return SCM_UNSPECIFIED
;
1135 /* Filename manipulation */
1139 SCM_PROC (s_dirname
, "dirname", 1, 0, 0, scm_dirname
);
1142 scm_dirname (SCM filename
)
1146 SCM_ASSERT (SCM_NIMP (filename
) && SCM_ROSTRINGP (filename
),
1150 s
= SCM_ROCHARS (filename
);
1151 len
= SCM_LENGTH (filename
);
1153 while (i
>= 0 && s
[i
] == '/') --i
;
1154 while (i
>= 0 && s
[i
] != '/') --i
;
1155 while (i
>= 0 && s
[i
] == '/') --i
;
1158 if (len
> 0 && s
[0] == '/')
1159 return scm_make_shared_substring (filename
, SCM_INUM0
, SCM_MAKINUM (1));
1161 return scm_dot_string
;
1164 return scm_make_shared_substring (filename
, SCM_INUM0
, SCM_MAKINUM (i
+ 1));
1167 SCM_PROC (s_basename
, "basename", 1, 1, 0, scm_basename
);
1170 scm_basename (SCM filename
, SCM suffix
)
1174 SCM_ASSERT (SCM_NIMP (filename
) && SCM_ROSTRINGP (filename
),
1178 SCM_ASSERT (SCM_UNBNDP (suffix
)
1179 || (SCM_NIMP (suffix
) && SCM_ROSTRINGP (suffix
)),
1183 f
= SCM_ROCHARS (filename
);
1184 if (SCM_UNBNDP (suffix
))
1188 s
= SCM_ROCHARS (suffix
);
1189 j
= SCM_LENGTH (suffix
) - 1;
1191 len
= SCM_LENGTH (filename
);
1193 while (i
>= 0 && f
[i
] == '/') --i
;
1195 while (i
>= 0 && j
>= 0 && f
[i
] == s
[j
]) --i
, --j
;
1198 while (i
>= 0 && f
[i
] != '/') --i
;
1201 if (len
> 0 && f
[0] == '/')
1202 return scm_make_shared_substring (filename
, SCM_INUM0
, SCM_MAKINUM (1));
1204 return scm_dot_string
;
1207 return scm_make_shared_substring (filename
,
1208 SCM_MAKINUM (i
+ 1),
1209 SCM_MAKINUM (end
+ 1));
1219 scm_add_feature ("i/o-extensions");
1221 scm_tc16_dir
= scm_make_smob_type_mfpe ("directory", 0,
1222 NULL
, scm_dir_free
,scm_dir_print
, NULL
);
1224 scm_dot_string
= scm_permanent_object (scm_makfrom0str ("."));
1227 scm_sysintern ("O_RDONLY", scm_long2num (O_RDONLY
));
1230 scm_sysintern ("O_WRONLY", scm_long2num (O_WRONLY
));
1233 scm_sysintern ("O_RDWR", scm_long2num (O_RDWR
));
1236 scm_sysintern ("O_CREAT", scm_long2num (O_CREAT
));
1239 scm_sysintern ("O_EXCL", scm_long2num (O_EXCL
));
1242 scm_sysintern ("O_NOCTTY", scm_long2num (O_NOCTTY
));
1245 scm_sysintern ("O_TRUNC", scm_long2num (O_TRUNC
));
1248 scm_sysintern ("O_APPEND", scm_long2num (O_APPEND
));
1251 scm_sysintern ("O_NONBLOCK", scm_long2num (O_NONBLOCK
));
1254 scm_sysintern ("O_NDELAY", scm_long2num (O_NDELAY
));
1257 scm_sysintern ("O_SYNC", scm_long2num (O_SYNC
));
1261 scm_sysintern ("F_DUPFD", scm_long2num (F_DUPFD
));
1264 scm_sysintern ("F_GETFD", scm_long2num (F_GETFD
));
1267 scm_sysintern ("F_SETFD", scm_long2num (F_SETFD
));
1270 scm_sysintern ("F_GETFL", scm_long2num (F_GETFL
));
1273 scm_sysintern ("F_SETFL", scm_long2num (F_SETFL
));
1276 scm_sysintern ("F_GETOWN", scm_long2num (F_GETOWN
));
1279 scm_sysintern ("F_SETOWN", scm_long2num (F_SETOWN
));
1282 scm_sysintern ("FD_CLOEXEC", scm_long2num (FD_CLOEXEC
));
1285 #include "filesys.x"