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, 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 #ifdef LIBC_H_WITH_UNISTD_H
70 #ifdef HAVE_SYS_SELECT_H
71 #include <sys/select.h>
78 #include <sys/types.h>
87 #define SELECT_TYPE fd_set
88 #define SELECT_SET_SIZE FD_SETSIZE
92 /* Define the macros to access a single-int bitmap of descriptors. */
93 #define SELECT_SET_SIZE 32
94 #define SELECT_TYPE int
95 #define FD_SET(n, p) (*(p) |= (1 << (n)))
96 #define FD_CLR(n, p) (*(p) &= ~(1 << (n)))
97 #define FD_ISSET(n, p) (*(p) & (1 << (n)))
98 #define FD_ZERO(p) (*(p) = 0)
100 #endif /* no FD_SET */
104 # define NAMLEN(dirent) strlen((dirent)->d_name)
106 # define dirent direct
107 # define NAMLEN(dirent) (dirent)->d_namlen
109 # include <sys/ndir.h>
112 # include <sys/dir.h>
119 /* Ultrix has S_IFSOCK, but no S_ISSOCK. Ipe! */
120 #if defined (S_IFSOCK) && ! defined (S_ISSOCK)
121 #define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
131 SCM_PROC (s_chown
, "chown", 3, 0, 0, scm_chown
);
134 scm_chown (path
, owner
, group
)
141 SCM_ASSERT (SCM_NIMP (path
) && SCM_ROSTRINGP (path
), path
, SCM_ARG1
, s_chown
);
142 if (SCM_SUBSTRP (path
))
143 path
= scm_makfromstr (SCM_ROCHARS (path
), SCM_ROLENGTH (path
), 0);
144 SCM_ASSERT (SCM_INUMP (owner
), owner
, SCM_ARG2
, s_chown
);
145 SCM_ASSERT (SCM_INUMP (group
), group
, SCM_ARG3
, s_chown
);
146 SCM_SYSCALL (val
= chown (SCM_ROCHARS (path
),
147 SCM_INUM (owner
), SCM_INUM (group
)));
149 scm_syserror (s_chown
);
150 return SCM_UNSPECIFIED
;
154 SCM_PROC (s_chmod
, "chmod", 2, 0, 0, scm_chmod
);
157 scm_chmod (port_or_path
, mode
)
162 SCM_ASSERT (SCM_INUMP (mode
), mode
, SCM_ARG2
, s_chmod
);
163 SCM_ASSERT (SCM_NIMP (port_or_path
), port_or_path
, SCM_ARG1
, s_chmod
);
164 if (SCM_STRINGP (port_or_path
))
165 SCM_SYSCALL (rv
= chmod (SCM_CHARS (port_or_path
), SCM_INUM (mode
)));
168 SCM_ASSERT (SCM_OPFPORTP (port_or_path
), port_or_path
, SCM_ARG1
, s_chmod
);
169 rv
= fileno ((FILE *)SCM_STREAM (port_or_path
));
171 SCM_SYSCALL (rv
= fchmod (rv
, SCM_INUM (mode
)));
174 scm_syserror (s_chmod
);
175 return SCM_UNSPECIFIED
;
178 SCM_PROC (s_umask
, "umask", 0, 1, 0, scm_umask
);
185 if (SCM_UNBNDP (mode
))
192 SCM_ASSERT (SCM_INUMP (mode
), mode
, SCM_ARG1
, s_umask
);
193 mask
= umask (SCM_INUM (mode
));
195 return SCM_MAKINUM (mask
);
200 SCM_PROC (s_open
, "open", 2, 1, 0, scm_open
);
203 scm_open (path
, flags
, mode
)
214 SCM_ASSERT (SCM_NIMP (path
) && SCM_ROSTRINGP (path
), path
, SCM_ARG1
, s_open
);
215 iflags
= scm_num2long (flags
, (char *) SCM_ARG2
, s_open
);
217 if (SCM_SUBSTRP (path
))
218 path
= scm_makfromstr (SCM_ROCHARS (path
), SCM_ROLENGTH (path
), 0);
221 if (SCM_UNBNDP (mode
))
222 SCM_SYSCALL (fd
= open (SCM_ROCHARS (path
), iflags
));
225 SCM_ASSERT (SCM_INUMP (mode
), mode
, SCM_ARG3
, s_open
);
226 SCM_SYSCALL (fd
= open (SCM_ROCHARS (path
), iflags
, SCM_INUM (mode
)));
229 scm_syserror (s_open
);
234 if (iflags
& O_WRONLY
)
239 f
= fdopen (fd
, port_mode
);
242 SCM_SYSCALL (close (fd
));
243 scm_syserror (s_open
);
246 struct scm_port_table
* pt
;
248 pt
= scm_add_to_port_table (newpt
);
249 SCM_SETPTAB_ENTRY (newpt
, pt
);
250 SCM_SETCAR (newpt
, scm_tc16_fport
| scm_mode_bits (port_mode
));
251 /* if (SCM_BUF0 & SCM_CAR (newpt))
252 scm_setbuf0 (newpt); */
253 SCM_SETSTREAM (newpt
, (SCM
)f
);
254 SCM_PTAB_ENTRY (newpt
)->file_name
= path
;
265 SCM_SYMBOL (scm_sym_regular
, "regular");
266 SCM_SYMBOL (scm_sym_directory
, "directory");
267 SCM_SYMBOL (scm_sym_symlink
, "symlink");
268 SCM_SYMBOL (scm_sym_block_special
, "block-special");
269 SCM_SYMBOL (scm_sym_char_special
, "char-special");
270 SCM_SYMBOL (scm_sym_fifo
, "fifo");
271 SCM_SYMBOL (scm_sym_sock
, "socket");
272 SCM_SYMBOL (scm_sym_unknown
, "unknown");
274 static SCM scm_stat2scm
SCM_P ((struct stat
*stat_temp
));
277 scm_stat2scm (stat_temp
)
278 struct stat
*stat_temp
;
280 SCM ans
= scm_make_vector (SCM_MAKINUM (15), SCM_UNSPECIFIED
, SCM_BOOL_F
);
281 SCM
*ve
= SCM_VELTS (ans
);
283 ve
[0] = scm_ulong2num ((unsigned long) stat_temp
->st_dev
);
284 ve
[1] = scm_ulong2num ((unsigned long) stat_temp
->st_ino
);
285 ve
[2] = scm_ulong2num ((unsigned long) stat_temp
->st_mode
);
286 ve
[3] = scm_ulong2num ((unsigned long) stat_temp
->st_nlink
);
287 ve
[4] = scm_ulong2num ((unsigned long) stat_temp
->st_uid
);
288 ve
[5] = scm_ulong2num ((unsigned long) stat_temp
->st_gid
);
290 ve
[6] = scm_ulong2num ((unsigned long) stat_temp
->st_rdev
);
294 ve
[7] = scm_ulong2num ((unsigned long) stat_temp
->st_size
);
295 ve
[8] = scm_ulong2num ((unsigned long) stat_temp
->st_atime
);
296 ve
[9] = scm_ulong2num ((unsigned long) stat_temp
->st_mtime
);
297 ve
[10] = scm_ulong2num ((unsigned long) stat_temp
->st_ctime
);
298 #ifdef HAVE_ST_BLKSIZE
299 ve
[11] = scm_ulong2num ((unsigned long) stat_temp
->st_blksize
);
301 ve
[11] = scm_ulong2num (4096L);
303 #ifdef HAVE_ST_BLOCKS
304 ve
[12] = scm_ulong2num ((unsigned long) stat_temp
->st_blocks
);
309 int mode
= stat_temp
->st_mode
;
312 ve
[13] = scm_sym_regular
;
313 else if (S_ISDIR (mode
))
314 ve
[13] = scm_sym_directory
;
315 else if (S_ISLNK (mode
))
316 ve
[13] = scm_sym_symlink
;
317 else if (S_ISBLK (mode
))
318 ve
[13] = scm_sym_block_special
;
319 else if (S_ISCHR (mode
))
320 ve
[13] = scm_sym_char_special
;
321 else if (S_ISFIFO (mode
))
322 ve
[13] = scm_sym_fifo
;
323 else if (S_ISSOCK (mode
))
324 ve
[13] = scm_sym_sock
;
326 ve
[13] = scm_sym_unknown
;
328 ve
[14] = SCM_MAKINUM ((~S_IFMT
) & mode
);
330 /* the layout of the bits in ve[14] is intended to be portable.
331 If there are systems that don't follow the usual convention,
332 the following could be used:
335 if (S_ISUID & mode) tmp += 1;
337 if (S_IRGRP & mode) tmp += 1;
339 if (S_ISVTX & mode) tmp += 1;
341 if (S_IRUSR & mode) tmp += 1;
343 if (S_IWUSR & mode) tmp += 1;
345 if (S_IXUSR & mode) tmp += 1;
347 if (S_IWGRP & mode) tmp += 1;
349 if (S_IXGRP & mode) tmp += 1;
351 if (S_IROTH & mode) tmp += 1;
353 if (S_IWOTH & mode) tmp += 1;
355 if (S_IXOTH & mode) tmp += 1;
357 ve[14] = SCM_MAKINUM (tmp);
365 SCM_PROC (s_stat
, "stat", 1, 0, 0, scm_stat
);
372 struct stat stat_temp
;
374 if (SCM_INUMP (file
))
375 SCM_SYSCALL (rv
= fstat (SCM_INUM (file
), &stat_temp
));
378 SCM_ASSERT (SCM_NIMP (file
), file
, SCM_ARG1
, s_stat
);
379 if (SCM_FPORTP (file
))
380 SCM_SYSCALL (rv
= fstat (fileno ((FILE *) SCM_STREAM (file
)),
384 SCM_ASSERT (SCM_ROSTRINGP (file
), file
, SCM_ARG1
, s_stat
);
385 if (SCM_SUBSTRP (file
))
386 file
= scm_makfromstr (SCM_ROCHARS (file
),
389 SCM_SYSCALL (rv
= stat (SCM_CHARS (file
), &stat_temp
));
396 scm_syserror_msg (s_stat
, "%s: %S",
397 scm_listify (scm_makfrom0str (strerror (errno
)),
402 return scm_stat2scm (&stat_temp
);
407 /* {Modifying Directories}
410 SCM_PROC (s_link
, "link", 2, 0, 0, scm_link
);
413 scm_link (oldpath
, newpath
)
419 SCM_ASSERT (SCM_NIMP (oldpath
) && SCM_ROSTRINGP (oldpath
), oldpath
, SCM_ARG1
, s_link
);
420 if (SCM_SUBSTRP (oldpath
))
421 oldpath
= scm_makfromstr (SCM_ROCHARS (oldpath
), SCM_ROLENGTH (oldpath
), 0);
422 SCM_ASSERT (SCM_NIMP (newpath
) && SCM_ROSTRINGP (newpath
), newpath
, SCM_ARG2
, s_link
);
423 if (SCM_SUBSTRP (newpath
))
424 newpath
= scm_makfromstr (SCM_ROCHARS (newpath
), SCM_ROLENGTH (newpath
), 0);
425 SCM_SYSCALL (val
= link (SCM_ROCHARS (oldpath
), SCM_ROCHARS (newpath
)));
427 scm_syserror (s_link
);
428 return SCM_UNSPECIFIED
;
433 SCM_PROC (s_rename
, "rename-file", 2, 0, 0, scm_rename
);
436 scm_rename (oldname
, newname
)
441 SCM_ASSERT (SCM_NIMP (oldname
) && SCM_STRINGP (oldname
), oldname
, SCM_ARG1
, s_rename
);
442 SCM_ASSERT (SCM_NIMP (newname
) && SCM_STRINGP (newname
), newname
, SCM_ARG2
, s_rename
);
444 SCM_SYSCALL (rv
= rename (SCM_CHARS (oldname
), SCM_CHARS (newname
)));
446 scm_syserror (s_rename
);
447 return SCM_UNSPECIFIED
;
450 SCM_SYSCALL (rv
= link (SCM_CHARS (oldname
), SCM_CHARS (newname
)));
453 SCM_SYSCALL (rv
= unlink (SCM_CHARS (oldname
)));;
455 /* unlink failed. remove new name */
456 SCM_SYSCALL (unlink (SCM_CHARS (newname
)));
460 scm_syserror (s_rename
);
461 return SCM_UNSPECIFIED
;
466 SCM_PROC(s_delete_file
, "delete-file", 1, 0, 0, scm_delete_file
);
469 scm_delete_file (str
)
473 SCM_ASSERT (SCM_NIMP (str
) && SCM_STRINGP (str
), str
, SCM_ARG1
, s_delete_file
);
474 SCM_SYSCALL (ans
= unlink (SCM_CHARS (str
)));
476 scm_syserror (s_delete_file
);
477 return SCM_UNSPECIFIED
;
481 SCM_PROC (s_mkdir
, "mkdir", 1, 1, 0, scm_mkdir
);
484 scm_mkdir (path
, mode
)
491 SCM_ASSERT (SCM_NIMP (path
) && SCM_STRINGP (path
), path
, SCM_ARG1
, s_mkdir
);
492 if (SCM_UNBNDP (mode
))
496 SCM_SYSCALL (rv
= mkdir (SCM_CHARS (path
), 0777 ^ mask
));
500 SCM_ASSERT (SCM_INUMP (mode
), mode
, SCM_ARG2
, s_mkdir
);
501 SCM_SYSCALL (rv
= mkdir (SCM_CHARS (path
), SCM_INUM (mode
)));
504 scm_syserror (s_mkdir
);
505 return SCM_UNSPECIFIED
;
507 scm_sysmissing (s_mkdir
);
514 SCM_PROC (s_rmdir
, "rmdir", 1, 0, 0, scm_rmdir
);
523 SCM_ASSERT (SCM_NIMP (path
) && SCM_STRINGP (path
), path
, SCM_ARG1
, s_rmdir
);
524 SCM_SYSCALL (val
= rmdir (SCM_CHARS (path
)));
526 scm_syserror (s_rmdir
);
527 return SCM_UNSPECIFIED
;
529 scm_sysmissing (s_rmdir
);
536 /* {Examining Directories}
541 SCM_PROC (s_opendir
, "opendir", 1, 0, 0, scm_opendir
);
544 scm_opendir (dirname
)
549 SCM_ASSERT (SCM_NIMP (dirname
) && SCM_STRINGP (dirname
), dirname
, SCM_ARG1
, s_opendir
);
552 SCM_SYSCALL (ds
= opendir (SCM_CHARS (dirname
)));
554 scm_syserror (s_opendir
);
555 SCM_SETCAR (dir
, scm_tc16_dir
| SCM_OPN
);
556 SCM_SETCDR (dir
, ds
);
562 SCM_PROC (s_readdir
, "readdir", 1, 0, 0, scm_readdir
);
568 struct dirent
*rdent
;
570 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPDIRP (port
), port
, SCM_ARG1
, s_readdir
);
572 SCM_SYSCALL (rdent
= readdir ((DIR *) SCM_CDR (port
)));
575 scm_syserror (s_readdir
);
576 return (rdent
? scm_makfromstr (rdent
->d_name
, NAMLEN (rdent
), 0)
582 SCM_PROC (s_rewinddir
, "rewinddir", 1, 0, 0, scm_rewinddir
);
588 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPDIRP (port
), port
, SCM_ARG1
, s_rewinddir
);
589 rewinddir ((DIR *) SCM_CDR (port
));
590 return SCM_UNSPECIFIED
;
595 SCM_PROC (s_closedir
, "closedir", 1, 0, 0, scm_closedir
);
603 SCM_ASSERT (SCM_NIMP (port
) && SCM_DIRP (port
), port
, SCM_ARG1
, s_closedir
);
605 if (SCM_CLOSEDP (port
))
608 return SCM_UNSPECIFIED
;
610 SCM_SYSCALL (sts
= closedir ((DIR *) SCM_CDR (port
)));
612 scm_syserror (s_closedir
);
613 SCM_SETCAR (port
, scm_tc16_dir
);
615 return SCM_UNSPECIFIED
;
621 static int scm_dir_print
SCM_P ((SCM sexp
, SCM port
, scm_print_state
*pstate
));
624 scm_dir_print (sexp
, port
, pstate
)
627 scm_print_state
*pstate
;
629 scm_prinport (sexp
, port
, "directory");
634 static scm_sizet scm_dir_free
SCM_P ((SCM p
));
641 closedir ((DIR *) SCM_CDR (p
));
645 static scm_smobfuns dir_smob
= {scm_mark0
, scm_dir_free
, scm_dir_print
, 0};
648 /* {Navigating Directories}
652 SCM_PROC (s_chdir
, "chdir", 1, 0, 0, scm_chdir
);
660 SCM_ASSERT (SCM_NIMP (str
) && SCM_STRINGP (str
), str
, SCM_ARG1
, s_chdir
);
661 SCM_SYSCALL (ans
= chdir (SCM_CHARS (str
)));
663 scm_syserror (s_chdir
);
664 return SCM_UNSPECIFIED
;
669 SCM_PROC (s_getcwd
, "getcwd", 0, 0, 0, scm_getcwd
);
677 scm_sizet size
= 100;
682 wd
= scm_must_malloc (size
, s_getcwd
);
683 while ((rv
= getcwd (wd
, size
)) == 0 && errno
== ERANGE
)
687 wd
= scm_must_malloc (size
, s_getcwd
);
690 scm_syserror (s_getcwd
);
691 result
= scm_makfromstr (wd
, strlen (wd
), 0);
696 scm_sysmissing (s_getcwd
);
705 static void fill_select_type
SCM_P ((SELECT_TYPE
* set
, SCM list
));
708 fill_select_type (set
, list
)
712 while (list
!= SCM_EOL
)
714 if ( SCM_NIMP (SCM_CAR (list
))
715 && (scm_tc16_fport
== SCM_TYP16 (SCM_CAR (list
)))
716 && SCM_OPPORTP (SCM_CAR (list
)))
717 FD_SET (fileno ((FILE *)SCM_STREAM (SCM_CAR (list
))), set
);
718 else if (SCM_INUMP (SCM_CAR (list
)))
719 FD_SET (SCM_INUM (SCM_CAR (list
)), set
);
720 list
= SCM_CDR (list
);
725 static SCM retrieve_select_type
SCM_P ((SELECT_TYPE
* set
, SCM list
));
728 retrieve_select_type (set
, list
)
734 while (list
!= SCM_EOL
)
736 if ( SCM_NIMP (SCM_CAR (list
))
737 && (scm_tc16_fport
== SCM_TYP16 (SCM_CAR (list
)))
738 && SCM_OPPORTP (SCM_CAR (list
)))
740 if (FD_ISSET (fileno ((FILE *)SCM_STREAM (SCM_CAR (list
))), set
))
741 answer
= scm_cons (SCM_CAR (list
), answer
);
743 else if (SCM_INUMP (SCM_CAR (list
)))
745 if (FD_ISSET (SCM_INUM (SCM_CAR (list
)), set
))
746 answer
= scm_cons (SCM_CAR (list
), answer
);
748 list
= SCM_CDR (list
);
754 /* {Checking for events}
757 SCM_PROC (s_select
, "select", 3, 2, 0, scm_select
);
760 scm_select (reads
, writes
, excepts
, secs
, msecs
)
768 struct timeval timeout
;
769 struct timeval
* time_p
;
770 SELECT_TYPE read_set
;
771 SELECT_TYPE write_set
;
772 SELECT_TYPE except_set
;
775 SCM_ASSERT (-1 < scm_ilength (reads
), reads
, SCM_ARG1
, s_select
);
776 SCM_ASSERT (-1 < scm_ilength (writes
), reads
, SCM_ARG1
, s_select
);
777 SCM_ASSERT (-1 < scm_ilength (excepts
), reads
, SCM_ARG1
, s_select
);
780 FD_ZERO (&write_set
);
781 FD_ZERO (&except_set
);
783 fill_select_type (&read_set
, reads
);
784 fill_select_type (&write_set
, writes
);
785 fill_select_type (&except_set
, excepts
);
787 if (SCM_UNBNDP (secs
))
791 SCM_ASSERT (SCM_INUMP (secs
), secs
, SCM_ARG4
, s_select
);
792 if (SCM_UNBNDP (msecs
))
795 SCM_ASSERT (SCM_INUMP (msecs
), msecs
, SCM_ARG5
, s_select
);
797 timeout
.tv_sec
= SCM_INUM (secs
);
798 timeout
.tv_usec
= 1000 * SCM_INUM (msecs
);
803 sreturn
= select (SELECT_SET_SIZE
,
804 &read_set
, &write_set
, &except_set
, time_p
);
806 scm_syserror (s_select
);
808 return scm_listify (retrieve_select_type (&read_set
, reads
),
809 retrieve_select_type (&write_set
, writes
),
810 retrieve_select_type (&except_set
, excepts
),
813 scm_sysmissing (s_select
);
819 /* Check if FILE has characters waiting to be read. */
830 scm_input_waiting_p (f
, caller
)
836 if (fileno (f
) == fileno (stdin
) && (isatty (fileno (stdin
))))
853 # include <sys/ioctl.h>
862 scm_input_waiting_p (f
, caller
)
866 /* Can we return an end-of-file character? */
870 /* Do we have characters in the stdio buffer? */
871 # ifdef FILE_CNT_FIELD
872 if (f
->FILE_CNT_FIELD
> 0)
875 # ifdef FILE_CNT_GPTR
876 if (f
->_gptr
!= f
->_egptr
)
879 # ifdef FILE_CNT_READPTR
880 if (f
->_IO_read_end
!= f
->_IO_read_ptr
)
883 Configure
.in could
not guess the name of the correct field in a
FILE *.
884 This function needs to be ported to your system
.
885 It should
return zero iff no characters are waiting to be read
.;
890 /* Is the file prepared to deliver input? */
893 struct timeval timeout
;
894 SELECT_TYPE read_set
;
895 SELECT_TYPE write_set
;
896 SELECT_TYPE except_set
;
897 int fno
= fileno ((FILE *)f
);
900 FD_ZERO (&write_set
);
901 FD_ZERO (&except_set
);
903 FD_SET (fno
, &read_set
);
909 if (select (SELECT_SET_SIZE
,
910 &read_set
, &write_set
, &except_set
, &timeout
)
912 scm_syserror (caller
);
914 return FD_ISSET (fno
, &read_set
);
920 ioctl(fileno(f
), FIONREAD
, &remir
);
924 scm_misc_error ("char-ready?", "Not fully implemented");
932 SCM_PROC (s_fcntl
, "fcntl", 3, 0, 0, scm_fcntl
);
935 scm_fcntl (port
, cmd
, value
)
942 SCM_ASSERT (SCM_OPFPORTP (port
), port
, SCM_ARG1
, s_fcntl
);
943 SCM_ASSERT (SCM_INUMP (cmd
), cmd
, SCM_ARG2
, s_fcntl
);
944 SCM_ASSERT (SCM_INUMP (value
), value
, SCM_ARG3
, s_fcntl
);
946 rv
= fileno ((FILE *)SCM_STREAM (port
));
948 SCM_SYSCALL (rv
= fcntl (rv
, SCM_INUM (cmd
), SCM_INUM (value
)));
950 scm_syserror (s_fcntl
);
951 return SCM_MAKINUM (rv
);
957 SCM_PROC (s_symlink
, "symlink", 2, 0, 0, scm_symlink
);
960 scm_symlink(oldpath
, newpath
)
967 SCM_ASSERT(SCM_NIMP(oldpath
) && SCM_STRINGP(oldpath
), oldpath
, SCM_ARG1
, s_symlink
);
968 SCM_ASSERT(SCM_NIMP(newpath
) && SCM_STRINGP(newpath
), newpath
, SCM_ARG2
, s_symlink
);
969 SCM_SYSCALL (val
= symlink(SCM_CHARS(oldpath
), SCM_CHARS(newpath
)));
971 scm_syserror (s_symlink
);
972 return SCM_UNSPECIFIED
;
974 scm_sysmissing (s_symlink
);
981 SCM_PROC (s_readlink
, "readlink", 1, 0, 0, scm_readlink
);
989 scm_sizet size
= 100;
992 SCM_ASSERT (SCM_NIMP (path
) && SCM_STRINGP (path
), path
, (char *) SCM_ARG1
, s_readlink
);
994 buf
= scm_must_malloc (size
, s_readlink
);
995 while ((rv
= readlink (SCM_CHARS (path
), buf
, (scm_sizet
) size
)) == size
)
999 buf
= scm_must_malloc (size
, s_readlink
);
1002 scm_syserror (s_readlink
);
1003 result
= scm_makfromstr (buf
, rv
, 0);
1004 scm_must_free (buf
);
1008 scm_sysmissing (s_readlink
);
1015 SCM_PROC (s_lstat
, "lstat", 1, 0, 0, scm_lstat
);
1023 struct stat stat_temp
;
1025 SCM_ASSERT(SCM_NIMP(str
) && SCM_STRINGP(str
), str
, (char *)SCM_ARG1
, s_lstat
);
1026 SCM_SYSCALL(rv
= lstat(SCM_CHARS(str
), &stat_temp
));
1031 scm_syserror_msg (s_lstat
, "%s: %S",
1032 scm_listify (scm_makfrom0str (strerror (errno
)),
1037 return scm_stat2scm(&stat_temp
);
1039 scm_sysmissing (s_lstat
);
1046 SCM_PROC (s_copy_file
, "copy-file", 2, 0, 0, scm_copy_file
);
1049 scm_copy_file (oldfile
, newfile
)
1055 char buf
[BUFSIZ
]; /* this space could be shared. */
1056 struct stat oldstat
;
1058 SCM_ASSERT (SCM_NIMP (oldfile
) && SCM_ROSTRINGP (oldfile
), oldfile
, SCM_ARG1
, s_copy_file
);
1059 if (SCM_SUBSTRP (oldfile
))
1060 oldfile
= scm_makfromstr (SCM_ROCHARS (oldfile
), SCM_ROLENGTH (oldfile
), 0);
1061 SCM_ASSERT (SCM_NIMP (newfile
) && SCM_ROSTRINGP (newfile
), newfile
, SCM_ARG2
, s_copy_file
);
1062 if (SCM_SUBSTRP (newfile
))
1063 newfile
= scm_makfromstr (SCM_ROCHARS (newfile
), SCM_ROLENGTH (newfile
), 0);
1064 if (stat (SCM_ROCHARS (oldfile
), &oldstat
) == -1)
1065 scm_syserror (s_copy_file
);
1067 oldfd
= open (SCM_ROCHARS (oldfile
), O_RDONLY
);
1069 scm_syserror (s_copy_file
);
1071 /* use POSIX flags instead of 07777?. */
1072 newfd
= open (SCM_ROCHARS (newfile
), O_WRONLY
| O_CREAT
| O_TRUNC
,
1073 oldstat
.st_mode
& 07777);
1075 scm_syserror (s_copy_file
);
1077 while ((n
= read (oldfd
, buf
, sizeof buf
)) > 0)
1078 if (write (newfd
, buf
, n
) != n
)
1082 scm_syserror (s_copy_file
);
1085 if (close (newfd
) == -1)
1086 scm_syserror (s_copy_file
);
1088 return SCM_UNSPECIFIED
;
1096 scm_add_feature ("i/o-extensions");
1098 scm_tc16_dir
= scm_newsmob (&dir_smob
);
1101 scm_sysintern ("O_RDONLY", scm_long2num (O_RDONLY
));
1104 scm_sysintern ("O_WRONLY", scm_long2num (O_WRONLY
));
1107 scm_sysintern ("O_RDWR", scm_long2num (O_RDWR
));
1110 scm_sysintern ("O_CREAT", scm_long2num (O_CREAT
));
1113 scm_sysintern ("O_EXCL", scm_long2num (O_EXCL
));
1116 scm_sysintern ("O_NOCTTY", scm_long2num (O_NOCTTY
));
1119 scm_sysintern ("O_TRUNC", scm_long2num (O_TRUNC
));
1122 scm_sysintern ("O_APPEND", scm_long2num (O_APPEND
));
1125 scm_sysintern ("O_NONBLOCK", scm_long2num (O_NONBLOCK
));
1128 scm_sysintern ("O_NDELAY", scm_long2num (O_NDELAY
));
1131 scm_sysintern ("O_SYNC", scm_long2num (O_SYNC
));
1135 scm_sysintern ("F_DUPFD", scm_long2num (F_DUPFD
));
1138 scm_sysintern ("F_GETFD", scm_long2num (F_GETFD
));
1141 scm_sysintern ("F_SETFD", scm_long2num (F_SETFD
));
1144 scm_sysintern ("F_GETFL", scm_long2num (F_GETFL
));
1147 scm_sysintern ("F_SETFL", scm_long2num (F_SETFL
));
1150 scm_sysintern ("F_GETOWN", scm_long2num (F_GETOWN
));
1153 scm_sysintern ("F_SETOWN", scm_long2num (F_SETOWN
));
1156 scm_sysintern ("FD_CLOEXEC", scm_long2num (FD_CLOEXEC
));
1159 #include "filesys.x"