1 /* Copyright (C) 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.
45 #ifdef TIME_WITH_SYS_TIME
46 # include <sys/time.h>
50 # include <sys/time.h>
64 #ifdef HAVE_SYS_SELECT_H
65 #include <sys/select.h>
72 #include <sys/types.h>
81 #define SELECT_TYPE fd_set
82 #define SELECT_SET_SIZE FD_SETSIZE
86 /* Define the macros to access a single-int bitmap of descriptors. */
87 #define SELECT_SET_SIZE 32
88 #define SELECT_TYPE int
89 #define FD_SET(n, p) (*(p) |= (1 << (n)))
90 #define FD_CLR(n, p) (*(p) &= ~(1 << (n)))
91 #define FD_ISSET(n, p) (*(p) & (1 << (n)))
92 #define FD_ZERO(p) (*(p) = 0)
94 #endif /* no FD_SET */
98 # define NAMLEN(dirent) strlen((dirent)->d_name)
100 # define dirent direct
101 # define NAMLEN(dirent) (dirent)->d_namlen
103 # include <sys/ndir.h>
106 # include <sys/dir.h>
116 SCM_CONST_LONG (scm_O_CREAT
, "O_CREAT", O_CREAT
);
120 SCM_CONST_LONG (scm_O_EXCL
, "O_EXCL", O_EXCL
);
124 SCM_CONST_LONG (scm_O_NOCTTY
, "O_NOCTTY", O_NOCTTY
);
128 SCM_CONST_LONG (scm_O_TRUNC
, "O_TRUNC", O_TRUNC
);
132 SCM_CONST_LONG (scm_O_APPEND
, "O_APPEND", O_APPEND
);
136 SCM_CONST_LONG (scm_O_NONBLOCK
, "O_NONBLOCK", O_NONBLOCK
);
140 SCM_CONST_LONG (scm_O_NDELAY
, "O_NDELAY", O_NDELAY
);
144 SCM_CONST_LONG (scm_O_SYNC
, "O_SYNC", O_SYNC
);
154 SCM_PROC (s_sys_chown
, "chown", 3, 0, 0, scm_sys_chown
);
157 scm_sys_chown (SCM path
, SCM owner
, SCM group
)
160 scm_sys_chown (path
, owner
, group
)
168 SCM_ASSERT (SCM_NIMP (path
) && SCM_ROSTRINGP (path
), path
, SCM_ARG1
, s_sys_chown
);
169 if (SCM_SUBSTRP (path
))
170 path
= scm_makfromstr (SCM_ROCHARS (path
), SCM_ROLENGTH (path
), 0);
171 SCM_ASSERT (SCM_INUMP (owner
), owner
, SCM_ARG2
, s_sys_chown
);
172 SCM_ASSERT (SCM_INUMP (group
), group
, SCM_ARG3
, s_sys_chown
);
173 SCM_SYSCALL (val
= chown (SCM_ROCHARS (path
),
174 SCM_INUM (owner
), SCM_INUM (group
)));
176 SCM_SYSERROR (s_sys_chown
);
177 return SCM_UNSPECIFIED
;
181 SCM_PROC (s_sys_chmod
, "chmod", 2, 0, 0, scm_sys_chmod
);
184 scm_sys_chmod (SCM port_or_path
, SCM mode
)
187 scm_sys_chmod (port_or_path
, mode
)
193 SCM_ASSERT (SCM_INUMP (mode
), mode
, SCM_ARG2
, s_sys_chmod
);
194 SCM_ASSERT (SCM_NIMP (port_or_path
), port_or_path
, SCM_ARG1
, s_sys_chmod
);
195 if (SCM_STRINGP (port_or_path
))
196 SCM_SYSCALL (rv
= chmod (SCM_CHARS (port_or_path
), SCM_INUM (mode
)));
199 SCM_ASSERT (SCM_OPFPORTP (port_or_path
), port_or_path
, SCM_ARG1
, s_sys_chmod
);
200 rv
= fileno ((FILE *)SCM_STREAM (port_or_path
));
202 SCM_SYSCALL (rv
= fchmod (rv
, SCM_INUM (mode
)));
205 SCM_SYSERROR (s_sys_chmod
);
206 return SCM_UNSPECIFIED
;
209 SCM_PROC (s_umask
, "umask", 0, 1, 0, scm_umask
);
220 if (SCM_UNBNDP (mode
))
227 SCM_ASSERT (SCM_INUMP (mode
), mode
, SCM_ARG1
, s_umask
);
228 mask
= umask (SCM_INUM (mode
));
230 return SCM_MAKINUM (mask
);
234 /* {File Descriptors}
240 scm_fd_print (SCM sexp
, SCM port
, int writing
)
243 scm_fd_print (sexp
, port
, writing
)
249 scm_gen_puts (scm_regular_string
, "#<fd ", port
);
250 scm_intprint (SCM_CDR (sexp
), 10, port
);
251 scm_gen_puts (scm_regular_string
, ">", port
);
266 flags
= SCM_FD_FLAGS (p
);
267 if ((scm_close_fd_on_gc
& flags
) && (scm_fd_is_open
& flags
))
269 SCM_SYSCALL( close (SCM_FD (p
)) );
274 static scm_smobfuns fd_smob
= {scm_mark0
, scm_fd_free
, scm_fd_print
, 0};
278 scm_intern_fd (int fd
, int flags
)
281 scm_intern_fd (fd
, flags
)
289 SCM_SETCAR (it
, (scm_tc16_fd
| (flags
<< 16)));
290 SCM_SETCDR (it
, (SCM
)fd
);
297 SCM_PROC (s_sys_open
, "open", 3, 0, 0, scm_sys_open
);
300 scm_sys_open (SCM path
, SCM flags
, SCM mode
)
303 scm_sys_open (path
, flags
, mode
)
312 SCM_ASSERT (SCM_NIMP (path
) && SCM_ROSTRINGP (path
), path
, SCM_ARG1
, s_sys_open
);
313 SCM_ASSERT (SCM_INUMP (flags
), flags
, SCM_ARG2
, s_sys_open
);
314 SCM_ASSERT (SCM_INUMP (mode
), mode
, SCM_ARG3
, s_sys_open
);
316 if (SCM_SUBSTRP (path
))
317 path
= scm_makfromstr (SCM_ROCHARS (path
), SCM_ROLENGTH (path
), 0);
320 SCM_SYSCALL ( fd
= open (SCM_ROCHARS (path
), SCM_INUM (flags
), SCM_INUM (mode
)) );
322 SCM_SYSERROR (s_sys_open
);
323 sfd
= scm_intern_fd (fd
, scm_fd_is_open
| scm_close_fd_on_gc
);
326 return scm_return_first (sfd
, path
);
330 SCM_PROC (s_sys_create
, "create", 2, 0, 0, scm_sys_create
);
333 scm_sys_create (SCM path
, SCM mode
)
336 scm_sys_create (path
, mode
)
344 SCM_ASSERT (SCM_NIMP (path
) && SCM_ROSTRINGP (path
), path
, SCM_ARG1
, s_sys_create
);
345 SCM_ASSERT (SCM_INUMP (mode
), mode
, SCM_ARG2
, s_sys_create
);
347 if (SCM_SUBSTRP (path
))
348 path
= scm_makfromstr (SCM_ROCHARS (path
), SCM_ROLENGTH (path
), 0);
351 SCM_SYSCALL ( fd
= creat (SCM_ROCHARS (path
), SCM_INUM (mode
)) );
353 SCM_SYSERROR (s_sys_create
);
354 sfd
= scm_intern_fd (fd
, scm_fd_is_open
| scm_close_fd_on_gc
);
357 return scm_return_first (sfd
, path
);
361 SCM_PROC (s_sys_close
, "close", 1, 0, 0, scm_sys_close
);
364 scm_sys_close (SCM sfd
)
373 SCM_ASSERT (SCM_NIMP (sfd
) && SCM_FD_P (sfd
), sfd
, SCM_ARG1
, s_sys_close
);
378 SCM_SETCAR (sfd
, scm_tc16_fd
);
381 SCM_SYSERROR (s_sys_close
);
382 return SCM_UNSPECIFIED
;
386 SCM_PROC (s_sys_write_fd
, "write-fd", 2, 0, 0, scm_sys_write_fd
);
389 scm_sys_write_fd (SCM sfd
, SCM buf
)
392 scm_sys_write_fd (sfd
, buf
)
400 SCM_ASSERT (SCM_NIMP (sfd
) && SCM_FD_P (sfd
), sfd
, SCM_ARG1
, s_sys_write_fd
);
401 SCM_ASSERT (SCM_NIMP (buf
) && SCM_ROSTRINGP (buf
), buf
, SCM_ARG2
, s_sys_write_fd
);
404 written
= write (fd
, SCM_ROCHARS (buf
), SCM_ROLENGTH (buf
));
406 SCM_SYSERROR (s_sys_write_fd
);
407 answer
= scm_long2num (written
);
409 return scm_return_first (answer
, buf
);
413 SCM_PROC (s_sys_read_fd
, "read-fd", 2, 2, 0, scm_sys_read_fd
);
416 scm_sys_read_fd (SCM sfd
, SCM buf
, SCM offset
, SCM length
)
419 scm_sys_read_fd (sfd
, buf
, offset
, length
)
433 SCM_ASSERT (SCM_NIMP (sfd
) && SCM_FD_P (sfd
), sfd
, SCM_ARG1
, s_sys_read_fd
);
436 SCM_ASSERT (SCM_NIMP (buf
) && SCM_STRINGP (buf
), buf
, SCM_ARG2
, s_sys_read_fd
);
437 bytes
= SCM_CHARS (buf
);
439 if (SCM_UNBNDP (offset
))
443 SCM_ASSERT (SCM_INUMP (offset
), offset
, SCM_ARG3
, s_sys_read_fd
);
444 off
= SCM_INUM (offset
);
447 if (SCM_UNBNDP (length
))
448 len
= SCM_LENGTH (buf
);
451 SCM_ASSERT (SCM_INUMP (length
), length
, SCM_ARG3
, s_sys_read_fd
);
452 len
= SCM_INUM (length
);
456 got
= read (fd
, bytes
+ off
, len
);
458 SCM_SYSERROR (s_sys_read_fd
);
459 answer
= scm_long2num (got
);
461 return scm_return_first (answer
, buf
);
464 SCM_PROC (s_sys_lseek
, "lseek", 2, 1, 0, scm_sys_lseek
);
467 scm_sys_lseek (SCM sfd
, SCM offset
, SCM whence
)
470 scm_sys_lseek (sfd
, offset
, whence
)
482 SCM_ASSERT (SCM_NIMP (sfd
) && SCM_FD_P (sfd
), sfd
, SCM_ARG1
, s_sys_lseek
);
485 off
= scm_num2long (offset
, (char *)SCM_ARG2
, s_sys_lseek
);
486 if (SCM_UNBNDP (whence
))
490 SCM_ASSERT (SCM_INUMP (whence
), whence
, SCM_ARG3
, s_sys_lseek
);
491 wh
= SCM_INUM (whence
);
495 SCM_SYSCALL (got
= lseek (fd
, off
, wh
));
497 SCM_SYSERROR (s_sys_lseek
);
498 answer
= scm_long2num (got
);
504 SCM_PROC (s_sys_dup
, "dup", 1, 1, 0, scm_sys_dup
);
507 scm_sys_dup (SCM oldfd
, SCM newfd
)
510 scm_sys_dup (oldfd
, newfd
)
520 SCM_ASSERT (SCM_NIMP (oldfd
) && SCM_FD_P (oldfd
), oldfd
, SCM_ARG1
, s_sys_dup
);
521 SCM_ASSERT (SCM_UNBNDP (newfd
) || SCM_INUMP (newfd
), newfd
, SCM_ARG2
, s_sys_dup
);
523 nfd
= (SCM_INUMP (newfd
) ? SCM_INUM (newfd
) : -1);
526 fn
= ((nfd
== -1) ? (int (*)())dup
: (int (*)())dup2
);
529 SCM_SYSERROR (s_sys_dup
);
530 answer
= SCM_MAKINUM (nfd
);
541 scm_stat2scm (struct stat
*stat_temp
)
544 scm_stat2scm (stat_temp
)
545 struct stat
*stat_temp
;
548 SCM ans
= scm_make_vector (SCM_MAKINUM (13), SCM_UNSPECIFIED
, SCM_BOOL_F
);
549 SCM
*ve
= SCM_VELTS (ans
);
550 ve
[0] = scm_ulong2num ((unsigned long) stat_temp
->st_dev
);
551 ve
[1] = scm_ulong2num ((unsigned long) stat_temp
->st_ino
);
552 ve
[2] = scm_ulong2num ((unsigned long) stat_temp
->st_mode
);
553 ve
[3] = scm_ulong2num ((unsigned long) stat_temp
->st_nlink
);
554 ve
[4] = scm_ulong2num ((unsigned long) stat_temp
->st_uid
);
555 ve
[5] = scm_ulong2num ((unsigned long) stat_temp
->st_gid
);
557 ve
[6] = scm_ulong2num ((unsigned long) stat_temp
->st_rdev
);
561 ve
[7] = scm_ulong2num ((unsigned long) stat_temp
->st_size
);
562 ve
[8] = scm_ulong2num ((unsigned long) stat_temp
->st_atime
);
563 ve
[9] = scm_ulong2num ((unsigned long) stat_temp
->st_mtime
);
564 ve
[10] = scm_ulong2num ((unsigned long) stat_temp
->st_ctime
);
565 #ifdef HAVE_ST_BLKSIZE
566 ve
[11] = scm_ulong2num ((unsigned long) stat_temp
->st_blksize
);
568 ve
[11] = scm_ulong2num (4096L);
570 #ifdef HAVE_ST_BLOCKS
571 ve
[12] = scm_ulong2num ((unsigned long) stat_temp
->st_blocks
);
579 SCM_PROC (s_sys_stat
, "stat", 1, 0, 0, scm_sys_stat
);
582 scm_sys_stat (SCM fd_or_path
)
585 scm_sys_stat (fd_or_path
)
590 struct stat stat_temp
;
592 if (SCM_INUMP (fd_or_path
))
594 SCM_ASSERT (SCM_OPFPORTP (fd_or_path
), fd_or_path
, SCM_ARG1
, s_sys_stat
);
595 rv
= SCM_INUM (fd_or_path
);
596 SCM_SYSCALL (rv
= fstat (rv
, &stat_temp
));
598 else if (SCM_NIMP (fd_or_path
) && SCM_FD_P (fd_or_path
))
600 rv
= SCM_FD (fd_or_path
);
601 SCM_SYSCALL (rv
= fstat (rv
, &stat_temp
));
605 SCM_ASSERT (SCM_NIMP (fd_or_path
), fd_or_path
, SCM_ARG1
, s_sys_stat
);
606 SCM_ASSERT (SCM_ROSTRINGP (fd_or_path
), fd_or_path
, SCM_ARG1
, s_sys_stat
);
607 if (SCM_ROSTRINGP (fd_or_path
))
609 if (SCM_SUBSTRP (fd_or_path
))
610 fd_or_path
= scm_makfromstr (SCM_ROCHARS (fd_or_path
), SCM_ROLENGTH (fd_or_path
), 0);
611 SCM_SYSCALL (rv
= stat (SCM_CHARS (fd_or_path
), &stat_temp
));
616 SCM_SYSERROR (s_sys_stat
);
617 return scm_stat2scm (&stat_temp
);
622 /* {Modifying Directories}
625 SCM_PROC (s_sys_link
, "link", 2, 0, 0, scm_sys_link
);
628 scm_sys_link (SCM oldpath
, SCM newpath
)
631 scm_sys_link (oldpath
, newpath
)
638 SCM_ASSERT (SCM_NIMP (oldpath
) && SCM_ROSTRINGP (oldpath
), oldpath
, SCM_ARG1
, s_sys_link
);
639 if (SCM_SUBSTRP (oldpath
))
640 oldpath
= scm_makfromstr (SCM_ROCHARS (oldpath
), SCM_ROLENGTH (oldpath
), 0);
641 SCM_ASSERT (SCM_NIMP (newpath
) && SCM_ROSTRINGP (newpath
), newpath
, SCM_ARG2
, s_sys_link
);
642 if (SCM_SUBSTRP (newpath
))
643 newpath
= scm_makfromstr (SCM_ROCHARS (newpath
), SCM_ROLENGTH (newpath
), 0);
644 SCM_SYSCALL (val
= link (SCM_ROCHARS (oldpath
), SCM_ROCHARS (newpath
)));
646 SCM_SYSERROR (s_sys_link
);
647 return SCM_UNSPECIFIED
;
652 SCM_PROC (s_sys_rename
, "rename-file", 2, 0, 0, scm_sys_rename
);
655 scm_sys_rename (SCM oldname
, SCM newname
)
658 scm_sys_rename (oldname
, newname
)
664 SCM_ASSERT (SCM_NIMP (oldname
) && SCM_STRINGP (oldname
), oldname
, SCM_ARG1
, s_sys_rename
);
665 SCM_ASSERT (SCM_NIMP (newname
) && SCM_STRINGP (newname
), newname
, SCM_ARG2
, s_sys_rename
);
667 SCM_SYSCALL (rv
= rename (SCM_CHARS (oldname
), SCM_CHARS (newname
)));
669 SCM_SYSERROR (s_sys_rename
);
670 return SCM_UNSPECIFIED
;
673 SCM_SYSCALL (rv
= link (SCM_CHARS (oldname
), SCM_CHARS (newname
)));
676 SCM_SYSCALL (rv
= unlink (SCM_CHARS (oldname
)));;
678 /* unlink failed. remove new name */
679 SCM_SYSCALL (unlink (SCM_CHARS (newname
)));
683 SCM_SYSERROR (s_sys_rename
);
684 return SCM_UNSPECIFIED
;
690 SCM_PROC (s_sys_mkdir
, "mkdir", 1, 1, 0, scm_sys_mkdir
);
693 scm_sys_mkdir (SCM path
, SCM mode
)
696 scm_sys_mkdir (path
, mode
)
704 SCM_ASSERT (SCM_NIMP (path
) && SCM_STRINGP (path
), path
, SCM_ARG1
, s_sys_mkdir
);
705 if (SCM_UNBNDP (mode
))
709 SCM_SYSCALL (rv
= mkdir (SCM_CHARS (path
), 0777 ^ mask
));
713 SCM_ASSERT (SCM_INUMP (mode
), mode
, SCM_ARG2
, s_sys_mkdir
);
714 SCM_SYSCALL (rv
= mkdir (SCM_CHARS (path
), SCM_INUM (mode
)));
717 SCM_SYSERROR (s_sys_mkdir
);
718 return SCM_UNSPECIFIED
;
720 SCM_SYSMISSING (s_sys_mkdir
);
727 SCM_PROC (s_sys_rmdir
, "rmdir", 1, 0, 0, scm_sys_rmdir
);
730 scm_sys_rmdir (SCM path
)
740 SCM_ASSERT (SCM_NIMP (path
) && SCM_STRINGP (path
), path
, SCM_ARG1
, s_sys_rmdir
);
741 SCM_SYSCALL (val
= rmdir (SCM_CHARS (path
)));
743 SCM_SYSERROR (s_sys_rmdir
);
744 return SCM_UNSPECIFIED
;
746 SCM_SYSMISSING (s_sys_rmdir
);
753 /* {Examining Directories}
758 SCM_PROC (s_sys_opendir
, "opendir", 1, 0, 0, scm_sys_opendir
);
761 scm_sys_opendir (SCM dirname
)
764 scm_sys_opendir (dirname
)
770 SCM_ASSERT (SCM_NIMP (dirname
) && SCM_STRINGP (dirname
), dirname
, SCM_ARG1
, s_sys_opendir
);
773 SCM_SYSCALL (ds
= opendir (SCM_CHARS (dirname
)));
775 SCM_SYSERROR (s_sys_opendir
);
776 SCM_CAR (dir
) = scm_tc16_dir
| SCM_OPN
;
777 SCM_SETCDR (dir
, ds
);
783 SCM_PROC (s_sys_readdir
, "readdir", 1, 0, 0, scm_sys_readdir
);
786 scm_sys_readdir (SCM port
)
789 scm_sys_readdir (port
)
793 struct dirent
*rdent
;
795 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPDIRP (port
), port
, SCM_ARG1
, s_sys_readdir
);
797 SCM_SYSCALL (rdent
= readdir ((DIR *) SCM_CDR (port
)));
800 SCM_SYSERROR (s_sys_readdir
);
801 return (rdent
? scm_makfromstr (rdent
->d_name
, NAMLEN (rdent
), 0)
807 SCM_PROC (s_rewinddir
, "rewinddir", 1, 0, 0, scm_rewinddir
);
810 scm_rewinddir (SCM port
)
817 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPDIRP (port
), port
, SCM_ARG1
, s_rewinddir
);
818 rewinddir ((DIR *) SCM_CDR (port
));
819 return SCM_UNSPECIFIED
;
824 SCM_PROC (s_sys_closedir
, "closedir", 1, 0, 0, scm_sys_closedir
);
827 scm_sys_closedir (SCM port
)
830 scm_sys_closedir (port
)
836 SCM_ASSERT (SCM_NIMP (port
) && SCM_DIRP (port
), port
, SCM_ARG1
, s_sys_closedir
);
838 if (SCM_CLOSEDP (port
))
841 return SCM_UNSPECIFIED
;
843 SCM_SYSCALL (sts
= closedir ((DIR *) SCM_CDR (port
)));
845 SCM_SYSERROR (s_sys_closedir
);
846 SCM_CAR (port
) = scm_tc16_dir
;
848 return SCM_UNSPECIFIED
;
855 scm_dir_print (SCM sexp
, SCM port
, int writing
)
858 scm_dir_print (sexp
, port
, writing
)
864 scm_prinport (sexp
, port
, "directory");
878 closedir ((DIR *) SCM_CDR (p
));
882 static scm_smobfuns dir_smob
= {scm_mark0
, scm_dir_free
, scm_dir_print
, 0};
885 /* {Navigating Directories}
889 SCM_PROC (s_sys_chdir
, "chdir", 1, 0, 0, scm_sys_chdir
);
892 scm_sys_chdir (SCM str
)
901 SCM_ASSERT (SCM_NIMP (str
) && SCM_STRINGP (str
), str
, SCM_ARG1
, s_sys_chdir
);
902 SCM_SYSCALL (ans
= chdir (SCM_CHARS (str
)));
904 SCM_SYSERROR (s_sys_chdir
);
905 return SCM_UNSPECIFIED
;
910 SCM_PROC (s_sys_getcwd
, "getcwd", 0, 0, 0, scm_sys_getcwd
);
913 scm_sys_getcwd (void)
922 scm_sizet size
= 100;
927 wd
= scm_must_malloc (size
, s_sys_getcwd
);
928 while ((rv
= getcwd (wd
, size
)) == 0 && errno
== ERANGE
)
932 wd
= scm_must_malloc (size
, s_sys_getcwd
);
935 SCM_SYSERROR (s_sys_getcwd
);
936 result
= scm_makfromstr (wd
, strlen (wd
), 0);
941 SCM_SYSMISSING (s_sys_getcwd
);
951 fill_select_type (SELECT_TYPE
* set
, SCM list
)
954 fill_select_type (set
, list
)
959 while (list
!= SCM_EOL
)
961 if ( SCM_NIMP (SCM_CAR (list
))
962 && (scm_tc16_fport
== SCM_TYP16 (SCM_CAR (list
)))
963 && SCM_OPPORTP (SCM_CAR (list
)))
964 FD_SET (fileno ((FILE *)SCM_STREAM (SCM_CAR (list
))), set
);
965 else if (SCM_INUMP (SCM_CAR (list
)))
966 FD_SET (SCM_INUM (SCM_CAR (list
)), set
);
967 else if (SCM_NIMP (SCM_CAR (list
)) && SCM_FD_P (SCM_CAR (list
)))
968 FD_SET (SCM_FD (SCM_CAR (list
)), set
);
969 list
= SCM_CDR (list
);
975 retrieve_select_type (SELECT_TYPE
* set
, SCM list
)
978 retrieve_select_type (set
, list
)
985 while (list
!= SCM_EOL
)
987 if ( SCM_NIMP (SCM_CAR (list
))
988 && (scm_tc16_fport
== SCM_TYP16 (SCM_CAR (list
)))
989 && SCM_OPPORTP (SCM_CAR (list
)))
991 if (FD_ISSET (fileno ((FILE *)SCM_STREAM (SCM_CAR (list
))), set
))
992 answer
= scm_cons (SCM_CAR (list
), answer
);
994 else if (SCM_INUMP (SCM_CAR (list
)))
996 if (FD_ISSET (SCM_INUM (SCM_CAR (list
)), set
))
997 answer
= scm_cons (SCM_CAR (list
), answer
);
999 else if (SCM_NIMP (SCM_CAR (list
)) && SCM_FD_P (SCM_CAR (list
)))
1001 if (FD_ISSET (SCM_FD (SCM_CAR (list
)), set
))
1002 answer
= scm_cons (SCM_CAR (list
), answer
);
1004 list
= SCM_CDR (list
);
1010 SCM_PROC (s_sys_select
, "select", 3, 2, 0, scm_sys_select
);
1013 scm_sys_select (SCM reads
, SCM writes
, SCM excepts
, SCM secs
, SCM msecs
)
1016 scm_sys_select (reads
, writes
, excepts
, secs
, msecs
)
1025 struct timeval timeout
;
1026 struct timeval
* time_p
;
1027 SELECT_TYPE read_set
;
1028 SELECT_TYPE write_set
;
1029 SELECT_TYPE except_set
;
1032 SCM_ASSERT (-1 < scm_ilength (reads
), reads
, SCM_ARG1
, s_sys_select
);
1033 SCM_ASSERT (-1 < scm_ilength (writes
), reads
, SCM_ARG1
, s_sys_select
);
1034 SCM_ASSERT (-1 < scm_ilength (excepts
), reads
, SCM_ARG1
, s_sys_select
);
1036 FD_ZERO (&read_set
);
1037 FD_ZERO (&write_set
);
1038 FD_ZERO (&except_set
);
1040 fill_select_type (&read_set
, reads
);
1041 fill_select_type (&write_set
, writes
);
1042 fill_select_type (&except_set
, excepts
);
1044 if (SCM_UNBNDP (secs
))
1048 SCM_ASSERT (SCM_INUMP (secs
), secs
, SCM_ARG4
, s_sys_select
);
1049 if (SCM_UNBNDP (msecs
))
1052 SCM_ASSERT (SCM_INUMP (msecs
), msecs
, SCM_ARG5
, s_sys_select
);
1054 timeout
.tv_sec
= SCM_INUM (secs
);
1055 timeout
.tv_usec
= 1000 * SCM_INUM (msecs
);
1060 sreturn
= select (SELECT_SET_SIZE
,
1061 &read_set
, &write_set
, &except_set
, time_p
);
1064 SCM_SYSERROR (s_sys_select
);
1065 return scm_listify (retrieve_select_type (&read_set
, reads
),
1066 retrieve_select_type (&write_set
, writes
),
1067 retrieve_select_type (&except_set
, excepts
),
1070 SCM_SYSMISSING (s_sys_select
);
1080 SCM_PROC (s_sys_symlink
, "symlink", 2, 0, 0, scm_sys_symlink
);
1083 scm_sys_symlink(SCM oldpath
, SCM newpath
)
1086 scm_sys_symlink(oldpath
, newpath
)
1094 SCM_ASSERT(SCM_NIMP(oldpath
) && SCM_STRINGP(oldpath
), oldpath
, SCM_ARG1
, s_sys_symlink
);
1095 SCM_ASSERT(SCM_NIMP(newpath
) && SCM_STRINGP(newpath
), newpath
, SCM_ARG2
, s_sys_symlink
);
1096 SCM_SYSCALL (val
= symlink(SCM_CHARS(oldpath
), SCM_CHARS(newpath
)));
1098 SCM_SYSERROR (s_sys_symlink
);
1099 return SCM_UNSPECIFIED
;
1101 SCM_SYSMISSING (s_sys_symlink
);
1108 SCM_PROC (s_sys_readlink
, "readlink", 1, 0, 0, scm_sys_readlink
);
1111 scm_sys_readlink(SCM path
)
1114 scm_sys_readlink(path
)
1118 #ifdef HAVE_READLINK
1120 scm_sizet size
= 100;
1123 SCM_ASSERT (SCM_NIMP (path
) && SCM_STRINGP (path
), path
, (char *) SCM_ARG1
, s_sys_readlink
);
1125 buf
= scm_must_malloc (size
, s_sys_readlink
);
1126 while ((rv
= readlink (SCM_CHARS (path
), buf
, (scm_sizet
) size
)) == size
)
1128 scm_must_free (buf
);
1130 buf
= scm_must_malloc (size
, s_sys_readlink
);
1133 SCM_SYSERROR (s_sys_readlink
);
1134 result
= scm_makfromstr (buf
, rv
, 0);
1135 scm_must_free (buf
);
1139 SCM_SYSMISSING (s_sys_readlink
);
1146 SCM_PROC (s_sys_lstat
, "lstat", 1, 0, 0, scm_sys_lstat
);
1149 scm_sys_lstat(SCM str
)
1158 struct stat stat_temp
;
1160 SCM_ASSERT(SCM_NIMP(str
) && SCM_STRINGP(str
), str
, (char *)SCM_ARG1
, s_sys_lstat
);
1161 SCM_SYSCALL(rv
= lstat(SCM_CHARS(str
), &stat_temp
));
1163 SCM_SYSERROR (s_sys_lstat
);
1164 return scm_stat2scm(&stat_temp
);
1166 SCM_SYSMISSING (s_sys_lstat
);
1173 SCM_PROC (s_sys_copy_file
, "copy-file", 2, 0, 0, scm_sys_copy_file
);
1176 scm_sys_copy_file (SCM oldfile
, SCM newfile
)
1179 scm_sys_copy_file (oldfile
, newfile
)
1186 char buf
[BUFSIZ
]; /* this space could be shared. */
1187 struct stat oldstat
;
1189 SCM_ASSERT (SCM_NIMP (oldfile
) && SCM_ROSTRINGP (oldfile
), oldfile
, SCM_ARG1
, s_sys_copy_file
);
1190 if (SCM_SUBSTRP (oldfile
))
1191 oldfile
= scm_makfromstr (SCM_ROCHARS (oldfile
), SCM_ROLENGTH (oldfile
), 0);
1192 SCM_ASSERT (SCM_NIMP (newfile
) && SCM_ROSTRINGP (newfile
), newfile
, SCM_ARG2
, s_sys_copy_file
);
1193 if (SCM_SUBSTRP (newfile
))
1194 newfile
= scm_makfromstr (SCM_ROCHARS (newfile
), SCM_ROLENGTH (newfile
), 0);
1195 if (stat (SCM_ROCHARS (oldfile
), &oldstat
) == -1)
1196 SCM_SYSERROR (s_sys_copy_file
);
1198 oldfd
= open (SCM_ROCHARS (oldfile
), O_RDONLY
);
1200 SCM_SYSERROR (s_sys_copy_file
);
1202 /* use POSIX flags instead of 07777?. */
1203 newfd
= open (SCM_ROCHARS (newfile
), O_WRONLY
| O_CREAT
| O_TRUNC
,
1204 oldstat
.st_mode
& 07777);
1206 SCM_SYSERROR (s_sys_copy_file
);
1208 while ((n
= read (oldfd
, buf
, sizeof buf
)) > 0)
1209 if (write (newfd
, buf
, n
) != n
)
1213 SCM_SYSERROR (s_sys_copy_file
);
1216 if (close (newfd
) == -1)
1217 SCM_SYSERROR (s_sys_copy_file
);
1219 return SCM_UNSPECIFIED
;
1225 scm_init_filesys (void)
1231 /* File type/permission bits. */
1233 scm_sysintern ("S_IRUSR", SCM_MAKINUM (S_IRUSR
));
1236 scm_sysintern ("S_IWUSR", SCM_MAKINUM (S_IWUSR
));
1239 scm_sysintern ("S_IXUSR", SCM_MAKINUM (S_IXUSR
));
1242 scm_sysintern ("S_IRWXU", SCM_MAKINUM (S_IRWXU
));
1246 scm_sysintern ("S_IRGRP", SCM_MAKINUM (S_IRGRP
));
1249 scm_sysintern ("S_IWGRP", SCM_MAKINUM (S_IWGRP
));
1252 scm_sysintern ("S_IXGRP", SCM_MAKINUM (S_IXGRP
));
1255 scm_sysintern ("S_IRWXG", SCM_MAKINUM (S_IRWXG
));
1259 scm_sysintern ("S_IROTH", SCM_MAKINUM (S_IROTH
));
1262 scm_sysintern ("S_IWOTH", SCM_MAKINUM (S_IWOTH
));
1265 scm_sysintern ("S_IXOTH", SCM_MAKINUM (S_IXOTH
));
1268 scm_sysintern ("S_IRWXO", SCM_MAKINUM (S_IRWXO
));
1272 scm_sysintern ("S_ISUID", SCM_MAKINUM (S_ISUID
));
1275 scm_sysintern ("S_ISGID", SCM_MAKINUM (S_ISGID
));
1278 scm_sysintern ("S_ISVTX", SCM_MAKINUM (S_ISVTX
));
1282 scm_sysintern ("S_IFMT", SCM_MAKINUM (S_IFMT
));
1285 scm_sysintern ("S_IFDIR", SCM_MAKINUM (S_IFDIR
));
1288 scm_sysintern ("S_IFCHR", SCM_MAKINUM (S_IFCHR
));
1291 scm_sysintern ("S_IFBLK", SCM_MAKINUM (S_IFBLK
));
1294 scm_sysintern ("S_IFREG", SCM_MAKINUM (S_IFREG
));
1297 scm_sysintern ("S_IFLNK", SCM_MAKINUM (S_IFLNK
));
1300 scm_sysintern ("S_IFSOCK", SCM_MAKINUM (S_IFSOCK
));
1303 scm_sysintern ("S_IFIFO", SCM_MAKINUM (S_IFIFO
));
1307 scm_tc16_fd
= scm_newsmob (&fd_smob
);
1308 scm_tc16_dir
= scm_newsmob (&dir_smob
);
1310 #include "filesys.x"