1 /* Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2004, 2006,
2 * 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; either version 3 of
7 * the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 /* This file contains POSIX file system access procedures. Procedures
23 essential to the compiler and run-time (`stat', `canonicalize-path',
24 etc.) are compiled even with `--disable-posix'. */
27 /* See stime.c for comments on why _POSIX_C_SOURCE is not always defined. */
28 #define _LARGEFILE64_SOURCE /* ask for stat64 etc */
30 #define _POSIX_C_SOURCE 199506L /* for readdir_r */
43 #include "libguile/_scm.h"
44 #include "libguile/smob.h"
45 #include "libguile/feature.h"
46 #include "libguile/fports.h"
47 #include "libguile/private-gc.h" /* for SCM_MAX */
48 #include "libguile/strings.h"
49 #include "libguile/vectors.h"
50 #include "libguile/dynwind.h"
52 #include "libguile/validate.h"
53 #include "libguile/filesys.h"
64 #ifdef TIME_WITH_SYS_TIME
65 # include <sys/time.h>
69 # include <sys/time.h>
79 #ifdef LIBC_H_WITH_UNISTD_H
83 #include <sys/select.h>
89 #include <sys/types.h>
99 #define NAMLEN(dirent) strlen ((dirent)->d_name)
101 #ifdef HAVE_SYS_SENDFILE_H
102 # include <sys/sendfile.h>
105 /* Glibc's `sendfile' function. */
106 #define sendfile_or_sendfile64 \
107 CHOOSE_LARGEFILE (sendfile, sendfile64)
109 #include <full-read.h>
110 #include <full-write.h>
113 /* Some more definitions for the native Windows port. */
115 # define fsync(fd) _commit (fd)
116 #endif /* __MINGW32__ */
121 /* Two helper macros for an often used pattern */
123 #define STRING_SYSCALL(str,cstr,code) \
126 char *cstr = scm_to_locale_string (str); \
127 SCM_SYSCALL (code); \
128 eno = errno; free (cstr); errno = eno; \
131 #define STRING2_SYSCALL(str1,cstr1,str2,cstr2,code) \
134 char *cstr1, *cstr2; \
135 scm_dynwind_begin (0); \
136 cstr1 = scm_to_locale_string (str1); \
137 scm_dynwind_free (cstr1); \
138 cstr2 = scm_to_locale_string (str2); \
139 scm_dynwind_free (cstr2); \
140 SCM_SYSCALL (code); \
141 eno = errno; scm_dynwind_end (); errno = eno; \
152 SCM_DEFINE (scm_chown
, "chown", 3, 0, 0,
153 (SCM object
, SCM owner
, SCM group
),
154 "Change the ownership and group of the file referred to by @var{object} to\n"
155 "the integer values @var{owner} and @var{group}. @var{object} can be\n"
156 "a string containing a file name or, if the platform\n"
157 "supports fchown, a port or integer file descriptor\n"
158 "which is open on the file. The return value\n"
159 "is unspecified.\n\n"
160 "If @var{object} is a symbolic link, either the\n"
161 "ownership of the link or the ownership of the referenced file will be\n"
162 "changed depending on the operating system (lchown is\n"
163 "unsupported at present). If @var{owner} or @var{group} is specified\n"
164 "as @code{-1}, then that ID is not changed.")
165 #define FUNC_NAME s_scm_chown
169 object
= SCM_COERCE_OUTPORT (object
);
172 if (scm_is_integer (object
) || (SCM_OPFPORTP (object
)))
174 int fdes
= (SCM_OPFPORTP (object
)?
175 SCM_FPORT_FDES (object
) : scm_to_int (object
));
177 SCM_SYSCALL (rv
= fchown (fdes
, scm_to_int (owner
), scm_to_int (group
)));
182 STRING_SYSCALL (object
, c_object
,
183 rv
= chown (c_object
,
184 scm_to_int (owner
), scm_to_int (group
)));
188 return SCM_UNSPECIFIED
;
191 #endif /* HAVE_CHOWN */
195 SCM_DEFINE (scm_open_fdes
, "open-fdes", 2, 1, 0,
196 (SCM path
, SCM flags
, SCM mode
),
197 "Similar to @code{open} but return a file descriptor instead of\n"
199 #define FUNC_NAME s_scm_open_fdes
205 iflags
= SCM_NUM2INT (2, flags
);
206 imode
= SCM_NUM2INT_DEF (3, mode
, 0666);
207 STRING_SYSCALL (path
, c_path
, fd
= open_or_open64 (c_path
, iflags
, imode
));
210 return scm_from_int (fd
);
214 SCM_DEFINE (scm_open
, "open", 2, 1, 0,
215 (SCM path
, SCM flags
, SCM mode
),
216 "Open the file named by @var{path} for reading and/or writing.\n"
217 "@var{flags} is an integer specifying how the file should be opened.\n"
218 "@var{mode} is an integer specifying the permission bits of the file, if\n"
219 "it needs to be created, before the umask is applied. The default is 666\n"
220 "(Unix itself has no default).\n\n"
221 "@var{flags} can be constructed by combining variables using @code{logior}.\n"
222 "Basic flags are:\n\n"
224 "Open the file read-only.\n"
227 "Open the file write-only.\n"
230 "Open the file read/write.\n"
233 "Append to the file instead of truncating.\n"
236 "Create the file if it does not already exist.\n"
238 "See the Unix documentation of the @code{open} system call\n"
239 "for additional flags.")
240 #define FUNC_NAME s_scm_open
247 fd
= scm_to_int (scm_open_fdes (path
, flags
, mode
));
248 iflags
= SCM_NUM2INT (2, flags
);
250 if ((iflags
& O_RDWR
) == O_RDWR
)
252 /* Opened read-write. */
253 if (iflags
& O_APPEND
)
255 else if (iflags
& O_CREAT
)
262 /* Opened read-only or write-only. */
263 if (iflags
& O_APPEND
)
265 else if (iflags
& O_WRONLY
)
271 newpt
= scm_fdes_to_port (fd
, port_mode
, path
);
276 SCM_DEFINE (scm_close
, "close", 1, 0, 0,
278 "Similar to close-port (@pxref{Closing, close-port}),\n"
279 "but also works on file descriptors. A side\n"
280 "effect of closing a file descriptor is that any ports using that file\n"
281 "descriptor are moved to a different file descriptor and have\n"
282 "their revealed counts set to zero.")
283 #define FUNC_NAME s_scm_close
288 fd_or_port
= SCM_COERCE_OUTPORT (fd_or_port
);
290 if (SCM_PORTP (fd_or_port
))
291 return scm_close_port (fd_or_port
);
292 fd
= scm_to_int (fd_or_port
);
293 scm_evict_ports (fd
); /* see scsh manual. */
294 SCM_SYSCALL (rv
= close (fd
));
295 /* following scsh, closing an already closed file descriptor is
297 if (rv
< 0 && errno
!= EBADF
)
299 return scm_from_bool (rv
>= 0);
303 SCM_DEFINE (scm_close_fdes
, "close-fdes", 1, 0, 0,
305 "A simple wrapper for the @code{close} system call.\n"
306 "Close file descriptor @var{fd}, which must be an integer.\n"
307 "Unlike close (@pxref{Ports and File Descriptors, close}),\n"
308 "the file descriptor will be closed even if a port is using it.\n"
309 "The return value is unspecified.")
310 #define FUNC_NAME s_scm_close_fdes
315 c_fd
= scm_to_int (fd
);
316 SCM_SYSCALL (rv
= close (c_fd
));
319 return SCM_UNSPECIFIED
;
323 #endif /* HAVE_POSIX */
329 SCM_SYMBOL (scm_sym_regular
, "regular");
330 SCM_SYMBOL (scm_sym_directory
, "directory");
332 SCM_SYMBOL (scm_sym_symlink
, "symlink");
334 SCM_SYMBOL (scm_sym_block_special
, "block-special");
335 SCM_SYMBOL (scm_sym_char_special
, "char-special");
336 SCM_SYMBOL (scm_sym_fifo
, "fifo");
337 SCM_SYMBOL (scm_sym_sock
, "socket");
338 SCM_SYMBOL (scm_sym_unknown
, "unknown");
341 scm_stat2scm (struct stat_or_stat64
*stat_temp
)
343 SCM ans
= scm_c_make_vector (18, SCM_UNSPECIFIED
);
345 SCM_SIMPLE_VECTOR_SET(ans
, 0, scm_from_ulong (stat_temp
->st_dev
));
346 SCM_SIMPLE_VECTOR_SET(ans
, 1, scm_from_ino_t_or_ino64_t (stat_temp
->st_ino
));
347 SCM_SIMPLE_VECTOR_SET(ans
, 2, scm_from_ulong (stat_temp
->st_mode
));
348 SCM_SIMPLE_VECTOR_SET(ans
, 3, scm_from_ulong (stat_temp
->st_nlink
));
349 SCM_SIMPLE_VECTOR_SET(ans
, 4, scm_from_ulong (stat_temp
->st_uid
));
350 SCM_SIMPLE_VECTOR_SET(ans
, 5, scm_from_ulong (stat_temp
->st_gid
));
351 #ifdef HAVE_STRUCT_STAT_ST_RDEV
352 SCM_SIMPLE_VECTOR_SET(ans
, 6, scm_from_ulong (stat_temp
->st_rdev
));
354 SCM_SIMPLE_VECTOR_SET(ans
, 6, SCM_BOOL_F
);
356 SCM_SIMPLE_VECTOR_SET(ans
, 7, scm_from_off_t_or_off64_t (stat_temp
->st_size
));
357 SCM_SIMPLE_VECTOR_SET(ans
, 8, scm_from_ulong (stat_temp
->st_atime
));
358 SCM_SIMPLE_VECTOR_SET(ans
, 9, scm_from_ulong (stat_temp
->st_mtime
));
359 SCM_SIMPLE_VECTOR_SET(ans
, 10, scm_from_ulong (stat_temp
->st_ctime
));
360 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
361 SCM_SIMPLE_VECTOR_SET(ans
, 11, scm_from_ulong (stat_temp
->st_blksize
));
363 SCM_SIMPLE_VECTOR_SET(ans
, 11, scm_from_ulong (4096L));
365 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
366 SCM_SIMPLE_VECTOR_SET(ans
, 12, scm_from_blkcnt_t_or_blkcnt64_t (stat_temp
->st_blocks
));
368 SCM_SIMPLE_VECTOR_SET(ans
, 12, SCM_BOOL_F
);
371 int mode
= stat_temp
->st_mode
;
374 SCM_SIMPLE_VECTOR_SET(ans
, 13, scm_sym_regular
);
375 else if (S_ISDIR (mode
))
376 SCM_SIMPLE_VECTOR_SET(ans
, 13, scm_sym_directory
);
378 /* systems without symlinks probably don't have S_ISLNK */
379 else if (S_ISLNK (mode
))
380 SCM_SIMPLE_VECTOR_SET(ans
, 13, scm_sym_symlink
);
382 else if (S_ISBLK (mode
))
383 SCM_SIMPLE_VECTOR_SET(ans
, 13, scm_sym_block_special
);
384 else if (S_ISCHR (mode
))
385 SCM_SIMPLE_VECTOR_SET(ans
, 13, scm_sym_char_special
);
386 else if (S_ISFIFO (mode
))
387 SCM_SIMPLE_VECTOR_SET(ans
, 13, scm_sym_fifo
);
389 else if (S_ISSOCK (mode
))
390 SCM_SIMPLE_VECTOR_SET(ans
, 13, scm_sym_sock
);
393 SCM_SIMPLE_VECTOR_SET(ans
, 13, scm_sym_unknown
);
395 SCM_SIMPLE_VECTOR_SET(ans
, 14, scm_from_int ((~S_IFMT
) & mode
));
397 /* the layout of the bits in ve[14] is intended to be portable.
398 If there are systems that don't follow the usual convention,
399 the following could be used:
402 if (S_ISUID & mode) tmp += 1;
404 if (S_IRGRP & mode) tmp += 1;
406 if (S_ISVTX & mode) tmp += 1;
408 if (S_IRUSR & mode) tmp += 1;
410 if (S_IWUSR & mode) tmp += 1;
412 if (S_IXUSR & mode) tmp += 1;
414 if (S_IWGRP & mode) tmp += 1;
416 if (S_IXGRP & mode) tmp += 1;
418 if (S_IROTH & mode) tmp += 1;
420 if (S_IWOTH & mode) tmp += 1;
422 if (S_IXOTH & mode) tmp += 1;
424 SCM_SIMPLE_VECTOR_SET(ans, 14, scm_from_int (tmp));
428 #ifdef HAVE_STRUCT_STAT_ST_ATIM
429 SCM_SIMPLE_VECTOR_SET(ans
, 15, scm_from_long (stat_temp
->st_atim
.tv_nsec
));
431 SCM_SIMPLE_VECTOR_SET(ans
, 15, SCM_I_MAKINUM (0));
433 #ifdef HAVE_STRUCT_STAT_ST_MTIM
434 SCM_SIMPLE_VECTOR_SET(ans
, 16, scm_from_long (stat_temp
->st_mtim
.tv_nsec
));
436 SCM_SIMPLE_VECTOR_SET(ans
, 16, SCM_I_MAKINUM (0));
438 #ifdef HAVE_STRUCT_STAT_ST_CTIM
439 SCM_SIMPLE_VECTOR_SET(ans
, 17, scm_from_ulong (stat_temp
->st_ctim
.tv_sec
));
441 SCM_SIMPLE_VECTOR_SET(ans
, 17, SCM_I_MAKINUM (0));
448 is_file_name_separator (SCM c
)
450 if (scm_is_eq (c
, SCM_MAKE_CHAR ('/')))
453 if (scm_is_eq (c
, SCM_MAKE_CHAR ('\\')))
459 SCM_DEFINE (scm_stat
, "stat", 1, 1, 0,
460 (SCM object
, SCM exception_on_error
),
461 "Return an object containing various information about the file\n"
462 "determined by @var{object}. @var{object} can be a string containing\n"
463 "a file name or a port or integer file descriptor which is open\n"
464 "on a file (in which case @code{fstat} is used as the underlying\n"
467 "If the optional @var{exception_on_error} argument is true, which\n"
468 "is the default, an exception will be raised if the underlying\n"
469 "system call returns an error, for example if the file is not\n"
470 "found or is not readable. Otherwise, an error will cause\n"
471 "@code{stat} to return @code{#f}."
473 "The object returned by a successful call to @code{stat} can be\n"
474 "passed as a single parameter to the following procedures, all of\n"
475 "which return integers:\n"
479 "The device containing the file.\n"
481 "The file serial number, which distinguishes this file from all\n"
482 "other files on the same device.\n"
484 "The mode of the file. This includes file type information and\n"
485 "the file permission bits. See @code{stat:type} and\n"
486 "@code{stat:perms} below.\n"
488 "The number of hard links to the file.\n"
490 "The user ID of the file's owner.\n"
492 "The group ID of the file.\n"
494 "Device ID; this entry is defined only for character or block\n"
497 "The size of a regular file in bytes.\n"
499 "The last access time for the file.\n"
501 "The last modification time for the file.\n"
503 "The last modification time for the attributes of the file.\n"
504 "@item stat:blksize\n"
505 "The optimal block size for reading or writing the file, in\n"
507 "@item stat:blocks\n"
508 "The amount of disk space that the file occupies measured in\n"
509 "units of 512 byte blocks.\n"
512 "In addition, the following procedures return the information\n"
513 "from stat:mode in a more convenient form:\n"
517 "A symbol representing the type of file. Possible values are\n"
518 "regular, directory, symlink, block-special, char-special, fifo,\n"
519 "socket and unknown\n"
521 "An integer representing the access permission bits.\n"
523 #define FUNC_NAME s_scm_stat
527 struct stat_or_stat64 stat_temp
;
529 if (scm_is_integer (object
))
531 SCM_SYSCALL (rv
= fstat_or_fstat64 (scm_to_int (object
), &stat_temp
));
533 else if (scm_is_string (object
))
535 char *file
= scm_to_locale_string (object
);
536 SCM_SYSCALL (rv
= stat_or_stat64 (file
, &stat_temp
));
541 object
= SCM_COERCE_OUTPORT (object
);
542 SCM_VALIDATE_OPFPORT (1, object
);
543 fdes
= SCM_FPORT_FDES (object
);
544 SCM_SYSCALL (rv
= fstat_or_fstat64 (fdes
, &stat_temp
));
549 if (SCM_UNBNDP (exception_on_error
) || scm_is_true (exception_on_error
))
552 SCM_SYSERROR_MSG ("~A: ~S",
553 scm_list_2 (scm_strerror (scm_from_int (en
)),
560 return scm_stat2scm (&stat_temp
);
565 SCM_DEFINE (scm_lstat
, "lstat", 1, 0, 0,
567 "Similar to @code{stat}, but does not follow symbolic links, i.e.,\n"
568 "it will return information about a symbolic link itself, not the\n"
569 "file it points to. @var{str} must be a string.")
570 #define FUNC_NAME s_scm_lstat
573 struct stat_or_stat64 stat_temp
;
575 STRING_SYSCALL (str
, c_str
, rv
= lstat_or_lstat64 (c_str
, &stat_temp
));
580 SCM_SYSERROR_MSG ("~A: ~S",
581 scm_list_2 (scm_strerror (scm_from_int (en
)), str
),
584 return scm_stat2scm (&stat_temp
);
587 #endif /* HAVE_LSTAT */
592 /* {Modifying Directories}
596 SCM_DEFINE (scm_link
, "link", 2, 0, 0,
597 (SCM oldpath
, SCM newpath
),
598 "Creates a new name @var{newpath} in the file system for the\n"
599 "file named by @var{oldpath}. If @var{oldpath} is a symbolic\n"
600 "link, the link may or may not be followed depending on the\n"
602 #define FUNC_NAME s_scm_link
606 STRING2_SYSCALL (oldpath
, c_oldpath
,
608 val
= link (c_oldpath
, c_newpath
));
611 return SCM_UNSPECIFIED
;
614 #endif /* HAVE_LINK */
617 /* {Navigating Directories}
621 SCM_DEFINE (scm_chdir
, "chdir", 1, 0, 0,
623 "Change the current working directory to @var{str}.\n"
624 "The return value is unspecified.")
625 #define FUNC_NAME s_scm_chdir
629 STRING_SYSCALL (str
, c_str
, ans
= chdir (c_str
));
632 return SCM_UNSPECIFIED
;
638 /* check that element is a port or file descriptor. if it's a port
639 and its buffer is ready for use, add it to the ports_ready list.
640 otherwise add its file descriptor to *set. the type of list can be
641 determined from pos: SCM_ARG1 for reads, SCM_ARG2 for writes,
642 SCM_ARG3 for excepts. */
644 set_element (fd_set
*set
, SCM
*ports_ready
, SCM element
, int pos
)
648 if (scm_is_integer (element
))
650 fd
= scm_to_int (element
);
656 element
= SCM_COERCE_OUTPORT (element
);
657 SCM_ASSERT (SCM_OPFPORTP (element
), element
, pos
, "select");
660 /* check whether port has buffered input. */
661 scm_t_port
*pt
= SCM_PTAB_ENTRY (element
);
663 if (pt
->read_pos
< pt
->read_end
)
666 else if (pos
== SCM_ARG2
)
668 /* check whether port's output buffer has room. */
669 scm_t_port
*pt
= SCM_PTAB_ENTRY (element
);
671 /* > 1 since writing the last byte in the buffer causes flush. */
672 if (pt
->write_end
- pt
->write_pos
> 1)
675 fd
= use_buf
? -1 : SCM_FPORT_FDES (element
);
678 *ports_ready
= scm_cons (element
, *ports_ready
);
684 /* check list_or_vec, a list or vector of ports or file descriptors,
685 adding each member to either the ports_ready list (if it's a port
686 with a usable buffer) or to *set. the kind of list_or_vec can be
687 determined from pos: SCM_ARG1 for reads, SCM_ARG2 for writes,
688 SCM_ARG3 for excepts. */
690 fill_select_type (fd_set
*set
, SCM
*ports_ready
, SCM list_or_vec
, int pos
)
694 if (scm_is_simple_vector (list_or_vec
))
696 int i
= SCM_SIMPLE_VECTOR_LENGTH (list_or_vec
);
700 int fd
= set_element (set
, ports_ready
,
701 SCM_SIMPLE_VECTOR_REF (list_or_vec
, i
), pos
);
709 while (!SCM_NULL_OR_NIL_P (list_or_vec
))
711 int fd
= set_element (set
, ports_ready
, SCM_CAR (list_or_vec
), pos
);
715 list_or_vec
= SCM_CDR (list_or_vec
);
722 /* if element (a file descriptor or port) appears in *set, cons it to
723 list. return list. */
725 get_element (fd_set
*set
, SCM element
, SCM list
)
729 if (scm_is_integer (element
))
731 fd
= scm_to_int (element
);
735 fd
= SCM_FPORT_FDES (SCM_COERCE_OUTPORT (element
));
737 if (FD_ISSET (fd
, set
))
738 list
= scm_cons (element
, list
);
742 /* construct component of scm_select return value.
743 set: pointer to set of file descriptors found by select to be ready
744 ports_ready: ports ready due to buffering
745 list_or_vec: original list/vector handed to scm_select.
746 the return value is a list/vector of ready ports/file descriptors.
747 works by finding the objects in list which correspond to members of
748 *set and appending them to ports_ready. result is converted to a
749 vector if list_or_vec is a vector. */
751 retrieve_select_type (fd_set
*set
, SCM ports_ready
, SCM list_or_vec
)
753 SCM answer_list
= ports_ready
;
755 if (scm_is_simple_vector (list_or_vec
))
757 int i
= SCM_SIMPLE_VECTOR_LENGTH (list_or_vec
);
761 answer_list
= get_element (set
,
762 SCM_SIMPLE_VECTOR_REF (list_or_vec
, i
),
765 return scm_vector (answer_list
);
769 /* list_or_vec must be a list. */
770 while (!SCM_NULL_OR_NIL_P (list_or_vec
))
772 answer_list
= get_element (set
, SCM_CAR (list_or_vec
), answer_list
);
773 list_or_vec
= SCM_CDR (list_or_vec
);
779 /* Static helper functions above refer to s_scm_select directly as s_select */
780 SCM_DEFINE (scm_select
, "select", 3, 2, 0,
781 (SCM reads
, SCM writes
, SCM excepts
, SCM secs
, SCM usecs
),
782 "This procedure has a variety of uses: waiting for the ability\n"
783 "to provide input, accept output, or the existence of\n"
784 "exceptional conditions on a collection of ports or file\n"
785 "descriptors, or waiting for a timeout to occur.\n"
786 "It also returns if interrupted by a signal.\n\n"
787 "@var{reads}, @var{writes} and @var{excepts} can be lists or\n"
788 "vectors, with each member a port or a file descriptor.\n"
789 "The value returned is a list of three corresponding\n"
790 "lists or vectors containing only the members which meet the\n"
791 "specified requirement. The ability of port buffers to\n"
792 "provide input or accept output is taken into account.\n"
793 "Ordering of the input lists or vectors is not preserved.\n\n"
794 "The optional arguments @var{secs} and @var{usecs} specify the\n"
795 "timeout. Either @var{secs} can be specified alone, as\n"
796 "either an integer or a real number, or both @var{secs} and\n"
797 "@var{usecs} can be specified as integers, in which case\n"
798 "@var{usecs} is an additional timeout expressed in\n"
799 "microseconds. If @var{secs} is omitted or is @code{#f} then\n"
800 "select will wait for as long as it takes for one of the other\n"
801 "conditions to be satisfied.\n\n"
802 "The scsh version of @code{select} differs as follows:\n"
803 "Only vectors are accepted for the first three arguments.\n"
804 "The @var{usecs} argument is not supported.\n"
805 "Multiple values are returned instead of a list.\n"
806 "Duplicates in the input vectors appear only once in output.\n"
807 "An additional @code{select!} interface is provided.")
808 #define FUNC_NAME s_scm_select
810 struct timeval timeout
;
811 struct timeval
* time_ptr
;
818 /* these lists accumulate ports which are ready due to buffering.
819 their file descriptors don't need to be added to the select sets. */
820 SCM read_ports_ready
= SCM_EOL
;
821 SCM write_ports_ready
= SCM_EOL
;
824 if (scm_is_simple_vector (reads
))
826 read_count
= SCM_SIMPLE_VECTOR_LENGTH (reads
);
830 read_count
= scm_ilength (reads
);
831 SCM_ASSERT (read_count
>= 0, reads
, SCM_ARG1
, FUNC_NAME
);
833 if (scm_is_simple_vector (writes
))
835 write_count
= SCM_SIMPLE_VECTOR_LENGTH (writes
);
839 write_count
= scm_ilength (writes
);
840 SCM_ASSERT (write_count
>= 0, writes
, SCM_ARG2
, FUNC_NAME
);
842 if (scm_is_simple_vector (excepts
))
844 except_count
= SCM_SIMPLE_VECTOR_LENGTH (excepts
);
848 except_count
= scm_ilength (excepts
);
849 SCM_ASSERT (except_count
>= 0, excepts
, SCM_ARG3
, FUNC_NAME
);
853 FD_ZERO (&write_set
);
854 FD_ZERO (&except_set
);
856 max_fd
= fill_select_type (&read_set
, &read_ports_ready
, reads
, SCM_ARG1
);
859 int write_max
= fill_select_type (&write_set
, &write_ports_ready
,
861 int except_max
= fill_select_type (&except_set
, NULL
,
864 if (write_max
> max_fd
)
866 if (except_max
> max_fd
)
870 /* if there's a port with a ready buffer, don't block, just
871 check for ready file descriptors. */
872 if (!scm_is_null (read_ports_ready
) || !scm_is_null (write_ports_ready
))
878 else if (SCM_UNBNDP (secs
) || scm_is_false (secs
))
882 if (scm_is_unsigned_integer (secs
, 0, ULONG_MAX
))
884 timeout
.tv_sec
= scm_to_ulong (secs
);
885 if (SCM_UNBNDP (usecs
))
888 timeout
.tv_usec
= scm_to_long (usecs
);
892 double fl
= scm_to_double (secs
);
894 if (!SCM_UNBNDP (usecs
))
895 SCM_WRONG_TYPE_ARG (4, secs
);
897 SCM_OUT_OF_RANGE (4, secs
);
898 timeout
.tv_sec
= (long) fl
;
899 timeout
.tv_usec
= (long) ((fl
- timeout
.tv_sec
) * 1000000);
905 int rv
= select (max_fd
+ 1,
906 &read_set
, &write_set
, &except_set
,
911 return scm_list_3 (retrieve_select_type (&read_set
, read_ports_ready
, reads
),
912 retrieve_select_type (&write_set
, write_ports_ready
, writes
),
913 retrieve_select_type (&except_set
, SCM_EOL
, excepts
));
920 SCM_DEFINE (scm_fcntl
, "fcntl", 2, 1, 0,
921 (SCM object
, SCM cmd
, SCM value
),
922 "Apply @var{cmd} to the specified file descriptor or the underlying\n"
923 "file descriptor of the specified port. @var{value} is an optional\n"
924 "integer argument.\n\n"
925 "Values for @var{cmd} are:\n\n"
928 "Duplicate a file descriptor\n"
930 "Get flags associated with the file descriptor.\n"
932 "Set flags associated with the file descriptor to @var{value}.\n"
934 "Get flags associated with the open file.\n"
936 "Set flags associated with the open file to @var{value}\n"
938 "Get the process ID of a socket's owner, for @code{SIGIO} signals.\n"
940 "Set the process that owns a socket to @var{value}, for @code{SIGIO} signals.\n"
942 "The value used to indicate the \"close on exec\" flag with @code{F_GETFL} or\n"
945 #define FUNC_NAME s_scm_fcntl
951 object
= SCM_COERCE_OUTPORT (object
);
953 if (SCM_OPFPORTP (object
))
954 fdes
= SCM_FPORT_FDES (object
);
956 fdes
= scm_to_int (object
);
958 if (SCM_UNBNDP (value
))
961 ivalue
= scm_to_int (value
);
963 SCM_SYSCALL (rv
= fcntl (fdes
, scm_to_int (cmd
), ivalue
));
966 return scm_from_int (rv
);
969 #endif /* HAVE_FCNTL */
971 SCM_DEFINE (scm_fsync
, "fsync", 1, 0, 0,
973 "Copies any unwritten data for the specified output file\n"
974 "descriptor to disk. If @var{object} is a port, its buffer is\n"
975 "flushed before the underlying file descriptor is fsync'd.\n"
976 "The return value is unspecified.")
977 #define FUNC_NAME s_scm_fsync
981 object
= SCM_COERCE_OUTPORT (object
);
983 if (SCM_OPFPORTP (object
))
985 scm_flush_unlocked (object
);
986 fdes
= SCM_FPORT_FDES (object
);
989 fdes
= scm_to_int (object
);
991 if (fsync (fdes
) == -1)
993 return SCM_UNSPECIFIED
;
998 SCM_DEFINE (scm_symlink
, "symlink", 2, 0, 0,
999 (SCM oldpath
, SCM newpath
),
1000 "Create a symbolic link named @var{oldpath} with the value\n"
1001 "(i.e., pointing to) @var{newpath}. The return value is\n"
1003 #define FUNC_NAME s_scm_symlink
1007 STRING2_SYSCALL (oldpath
, c_oldpath
,
1009 val
= symlink (c_oldpath
, c_newpath
));
1012 return SCM_UNSPECIFIED
;
1015 #endif /* HAVE_SYMLINK */
1017 #ifdef HAVE_READLINK
1018 SCM_DEFINE (scm_readlink
, "readlink", 1, 0, 0,
1020 "Return the value of the symbolic link named by @var{path} (a\n"
1021 "string), i.e., the file that the link points to.")
1022 #define FUNC_NAME s_scm_readlink
1030 scm_dynwind_begin (0);
1032 c_path
= scm_to_locale_string (path
);
1033 scm_dynwind_free (c_path
);
1035 buf
= scm_malloc (size
);
1037 while ((rv
= readlink (c_path
, buf
, size
)) == size
)
1041 buf
= scm_malloc (size
);
1045 int save_errno
= errno
;
1050 result
= scm_take_locale_stringn (buf
, rv
);
1056 #endif /* HAVE_READLINK */
1058 SCM_DEFINE (scm_copy_file
, "copy-file", 2, 0, 0,
1059 (SCM oldfile
, SCM newfile
),
1060 "Copy the file specified by @var{oldfile} to @var{newfile}.\n"
1061 "The return value is unspecified.")
1062 #define FUNC_NAME s_scm_copy_file
1064 char *c_oldfile
, *c_newfile
;
1068 struct stat_or_stat64 oldstat
;
1070 scm_dynwind_begin (0);
1072 c_oldfile
= scm_to_locale_string (oldfile
);
1073 scm_dynwind_free (c_oldfile
);
1074 c_newfile
= scm_to_locale_string (newfile
);
1075 scm_dynwind_free (c_newfile
);
1077 oldfd
= open_or_open64 (c_oldfile
, O_RDONLY
| O_BINARY
);
1081 SCM_SYSCALL (rv
= fstat_or_fstat64 (oldfd
, &oldstat
));
1083 goto err_close_oldfd
;
1085 /* use POSIX flags instead of 07777?. */
1086 newfd
= open_or_open64 (c_newfile
, O_WRONLY
| O_CREAT
| O_TRUNC
,
1087 oldstat
.st_mode
& 07777);
1095 while ((n
= read (oldfd
, buf
, sizeof buf
)) > 0)
1096 if (write (newfd
, buf
, n
) != n
)
1103 if (close (newfd
) == -1)
1107 return SCM_UNSPECIFIED
;
1111 SCM_DEFINE (scm_sendfile
, "sendfile", 3, 1, 0,
1112 (SCM out
, SCM in
, SCM count
, SCM offset
),
1113 "Send @var{count} bytes from @var{in} to @var{out}, both of which "
1114 "are either open file ports or file descriptors. When "
1115 "@var{offset} is omitted, start reading from @var{in}'s current "
1116 "position; otherwise, start reading at @var{offset}.")
1117 #define FUNC_NAME s_scm_sendfile
1119 #define VALIDATE_FD_OR_PORT(cvar, svar, pos) \
1120 if (scm_is_integer (svar)) \
1121 cvar = scm_to_int (svar); \
1124 SCM_VALIDATE_OPFPORT (pos, svar); \
1126 cvar = SCM_FPORT_FDES (svar); \
1134 VALIDATE_FD_OR_PORT (out_fd
, out
, 1);
1135 VALIDATE_FD_OR_PORT (in_fd
, in
, 2);
1136 c_count
= scm_to_size_t (count
);
1137 c_offset
= SCM_UNBNDP (offset
) ? 0 : scm_to_off_t (offset
);
1139 #if defined HAVE_SYS_SENDFILE_H && defined HAVE_SENDFILE
1140 /* The Linux-style sendfile(2), which is different from the BSD-style. */
1142 result
= sendfile_or_sendfile64 (out_fd
, in_fd
,
1143 SCM_UNBNDP (offset
) ? NULL
: &c_offset
,
1146 /* Quoting the Linux man page: "In Linux kernels before 2.6.33, out_fd
1147 must refer to a socket. Since Linux 2.6.33 it can be any file."
1148 Fall back to read(2) and write(2) when such an error occurs. */
1149 if (result
< 0 && errno
!= EINVAL
&& errno
!= ENOSYS
)
1151 else if (result
< 0)
1155 size_t result
, left
;
1157 if (!SCM_UNBNDP (offset
))
1160 scm_seek (in
, offset
, scm_from_int (SEEK_SET
));
1163 if (lseek_or_lseek64 (in_fd
, c_offset
, SEEK_SET
) < 0)
1168 for (result
= 0, left
= c_count
; result
< c_count
; )
1170 size_t asked
, obtained
;
1172 asked
= SCM_MIN (sizeof buf
, left
);
1173 obtained
= full_read (in_fd
, buf
, asked
);
1174 if (obtained
< asked
)
1179 obtained
= full_write (out_fd
, buf
, asked
);
1180 if (obtained
< asked
)
1186 return scm_from_size_t (result
);
1189 return scm_from_ssize_t (result
);
1191 #undef VALIDATE_FD_OR_PORT
1195 #endif /* HAVE_POSIX */
1198 /* Essential procedures used in (system base compile). */
1201 SCM_DEFINE (scm_getcwd
, "getcwd", 0, 0, 0,
1203 "Return the name of the current working directory.")
1204 #define FUNC_NAME s_scm_getcwd
1211 wd
= scm_malloc (size
);
1212 while ((rv
= getcwd (wd
, size
)) == 0 && errno
== ERANGE
)
1216 wd
= scm_malloc (size
);
1220 int save_errno
= errno
;
1225 result
= scm_from_locale_stringn (wd
, strlen (wd
));
1230 #endif /* HAVE_GETCWD */
1233 SCM_DEFINE (scm_mkdir
, "mkdir", 1, 1, 0,
1234 (SCM path
, SCM mode
),
1235 "Create a new directory named by @var{path}. If @var{mode} is omitted\n"
1236 "then the permissions of the directory file are set using the current\n"
1237 "umask. Otherwise they are set to the decimal value specified with\n"
1238 "@var{mode}. The return value is unspecified.")
1239 #define FUNC_NAME s_scm_mkdir
1244 if (SCM_UNBNDP (mode
))
1248 STRING_SYSCALL (path
, c_path
, rv
= mkdir (c_path
, 0777 ^ mask
));
1252 STRING_SYSCALL (path
, c_path
, rv
= mkdir (c_path
, scm_to_uint (mode
)));
1256 return SCM_UNSPECIFIED
;
1259 #endif /* HAVE_MKDIR */
1262 SCM_DEFINE (scm_rmdir
, "rmdir", 1, 0, 0,
1264 "Remove the existing directory named by @var{path}. The directory must\n"
1265 "be empty for this to succeed. The return value is unspecified.")
1266 #define FUNC_NAME s_scm_rmdir
1270 STRING_SYSCALL (path
, c_path
, val
= rmdir (c_path
));
1273 return SCM_UNSPECIFIED
;
1279 #define my_rename rename
1282 my_rename (const char *oldname
, const char *newname
)
1286 SCM_SYSCALL (rv
= link (oldname
, newname
));
1289 SCM_SYSCALL (rv
= unlink (oldname
));
1291 /* unlink failed. remove new name */
1292 SCM_SYSCALL (unlink (newname
));
1298 SCM_DEFINE (scm_rename
, "rename-file", 2, 0, 0,
1299 (SCM oldname
, SCM newname
),
1300 "Renames the file specified by @var{oldname} to @var{newname}.\n"
1301 "The return value is unspecified.")
1302 #define FUNC_NAME s_scm_rename
1306 STRING2_SYSCALL (oldname
, c_oldname
,
1308 rv
= my_rename (c_oldname
, c_newname
));
1311 return SCM_UNSPECIFIED
;
1316 SCM_DEFINE (scm_delete_file
, "delete-file", 1, 0, 0,
1318 "Deletes (or \"unlinks\") the file specified by @var{str}.")
1319 #define FUNC_NAME s_scm_delete_file
1322 STRING_SYSCALL (str
, c_str
, ans
= unlink (c_str
));
1325 return SCM_UNSPECIFIED
;
1329 SCM_DEFINE (scm_access
, "access?", 2, 0, 0,
1330 (SCM path
, SCM how
),
1331 "Test accessibility of a file under the real UID and GID of the\n"
1332 "calling process. The return is @code{#t} if @var{path} exists\n"
1333 "and the permissions requested by @var{how} are all allowed, or\n"
1334 "@code{#f} if not.\n"
1336 "@var{how} is an integer which is one of the following values,\n"
1337 "or a bitwise-OR (@code{logior}) of multiple values.\n"
1340 "Test for read permission.\n"
1343 "Test for write permission.\n"
1346 "Test for execute permission.\n"
1349 "Test for existence of the file. This is implied by each of the\n"
1350 "other tests, so there's no need to combine it with them.\n"
1353 "It's important to note that @code{access?} does not simply\n"
1354 "indicate what will happen on attempting to read or write a\n"
1355 "file. In normal circumstances it does, but in a set-UID or\n"
1356 "set-GID program it doesn't because @code{access?} tests the\n"
1357 "real ID, whereas an open or execute attempt uses the effective\n"
1360 "A program which will never run set-UID/GID can ignore the\n"
1361 "difference between real and effective IDs, but for maximum\n"
1362 "generality, especially in library functions, it's best not to\n"
1363 "use @code{access?} to predict the result of an open or execute,\n"
1364 "instead simply attempt that and catch any exception.\n"
1366 "The main use for @code{access?} is to let a set-UID/GID program\n"
1367 "determine what the invoking user would have been allowed to do,\n"
1368 "without the greater (or perhaps lesser) privileges afforded by\n"
1369 "the effective ID. For more on this, see ``Testing File\n"
1370 "Access'' in The GNU C Library Reference Manual.")
1371 #define FUNC_NAME s_scm_access
1376 c_path
= scm_to_locale_string (path
);
1377 rv
= access (c_path
, scm_to_int (how
));
1380 return scm_from_bool (!rv
);
1384 SCM_DEFINE (scm_chmod
, "chmod", 2, 0, 0,
1385 (SCM object
, SCM mode
),
1386 "Changes the permissions of the file referred to by\n"
1387 "@var{object}. @var{object} can be a string containing a file\n"
1388 "name or a port or integer file descriptor which is open on a\n"
1389 "file (in which case @code{fchmod} is used as the underlying\n"
1390 "system call). @var{mode} specifies the new permissions as a\n"
1391 "decimal number, e.g., @code{(chmod \"foo\" #o755)}.\n"
1392 "The return value is unspecified.")
1393 #define FUNC_NAME s_scm_chmod
1397 object
= SCM_COERCE_OUTPORT (object
);
1400 if (scm_is_integer (object
) || SCM_OPFPORTP (object
))
1403 if (scm_is_integer (object
))
1404 fdes
= scm_to_int (object
);
1406 fdes
= SCM_FPORT_FDES (object
);
1407 SCM_SYSCALL (rv
= fchmod (fdes
, scm_to_int (mode
)));
1412 STRING_SYSCALL (object
, c_object
,
1413 rv
= chmod (c_object
, scm_to_int (mode
)));
1417 return SCM_UNSPECIFIED
;
1421 SCM_DEFINE (scm_umask
, "umask", 0, 1, 0,
1423 "If @var{mode} is omitted, returns a decimal number representing the current\n"
1424 "file creation mask. Otherwise the file creation mask is set to\n"
1425 "@var{mode} and the previous value is returned.\n\n"
1426 "E.g., @code{(umask #o022)} sets the mask to octal 22, decimal 18.")
1427 #define FUNC_NAME s_scm_umask
1430 if (SCM_UNBNDP (mode
))
1437 mask
= umask (scm_to_uint (mode
));
1439 return scm_from_uint (mask
);
1443 #ifndef HAVE_MKSTEMP
1444 extern int mkstemp (char *);
1447 SCM_DEFINE (scm_mkstemp
, "mkstemp!", 1, 0, 0,
1449 "Create a new unique file in the file system and return a new\n"
1450 "buffered port open for reading and writing to the file.\n"
1452 "@var{tmpl} is a string specifying where the file should be\n"
1453 "created: it must end with @samp{XXXXXX} and those @samp{X}s\n"
1454 "will be changed in the string to return the name of the file.\n"
1455 "(@code{port-filename} on the port also gives the name.)\n"
1457 "POSIX doesn't specify the permissions mode of the file, on GNU\n"
1458 "and most systems it's @code{#o600}. An application can use\n"
1459 "@code{chmod} to relax that if desired. For example\n"
1460 "@code{#o666} less @code{umask}, which is usual for ordinary\n"
1464 "(let ((port (mkstemp! (string-copy \"/tmp/myfile-XXXXXX\"))))\n"
1465 " (chmod port (logand #o666 (lognot (umask))))\n"
1468 #define FUNC_NAME s_scm_mkstemp
1473 scm_dynwind_begin (0);
1475 c_tmpl
= scm_to_locale_string (tmpl
);
1476 scm_dynwind_free (c_tmpl
);
1478 SCM_SYSCALL (rv
= mkstemp (c_tmpl
));
1482 scm_substring_move_x (scm_from_locale_string (c_tmpl
),
1483 SCM_INUM0
, scm_string_length (tmpl
),
1487 return scm_fdes_to_port (rv
, "w+", tmpl
);
1492 /* Filename manipulation */
1497 SCM_SYMBOL (sym_file_name_convention
, "windows");
1499 SCM_SYMBOL (sym_file_name_convention
, "posix");
1502 SCM_INTERNAL SCM
scm_system_file_name_convention (void);
1504 SCM_DEFINE (scm_system_file_name_convention
,
1505 "system-file-name-convention", 0, 0, 0, (void),
1506 "Return either @code{posix} or @code{windows}, depending on\n"
1507 "what kind of system this Guile is running on.")
1508 #define FUNC_NAME s_scm_system_file_name_convention
1510 return sym_file_name_convention
;
1514 SCM_DEFINE (scm_dirname
, "dirname", 1, 0, 0,
1516 "Return the directory name component of the file name\n"
1517 "@var{filename}. If @var{filename} does not contain a directory\n"
1518 "component, @code{.} is returned.")
1519 #define FUNC_NAME s_scm_dirname
1522 unsigned long int len
;
1524 SCM_VALIDATE_STRING (1, filename
);
1526 len
= scm_i_string_length (filename
);
1530 while (i
>= 0 && is_file_name_separator (scm_c_string_ref (filename
, i
)))
1532 while (i
>= 0 && !is_file_name_separator (scm_c_string_ref (filename
, i
)))
1534 while (i
>= 0 && is_file_name_separator (scm_c_string_ref (filename
, i
)))
1539 if (len
> 0 && is_file_name_separator (scm_c_string_ref (filename
, 0)))
1540 return scm_c_substring (filename
, 0, 1);
1542 return scm_dot_string
;
1545 return scm_c_substring (filename
, 0, i
+ 1);
1549 SCM_DEFINE (scm_basename
, "basename", 1, 1, 0,
1550 (SCM filename
, SCM suffix
),
1551 "Return the base name of the file name @var{filename}. The\n"
1552 "base name is the file name without any directory components.\n"
1553 "If @var{suffix} is provided, and is equal to the end of\n"
1554 "@var{filename}, it is removed also.")
1555 #define FUNC_NAME s_scm_basename
1559 SCM_VALIDATE_STRING (1, filename
);
1560 len
= scm_i_string_length (filename
);
1562 if (SCM_UNBNDP (suffix
))
1566 SCM_VALIDATE_STRING (2, suffix
);
1567 j
= scm_i_string_length (suffix
) - 1;
1570 while (i
>= 0 && is_file_name_separator (scm_c_string_ref (filename
, i
)))
1573 while (i
>= 0 && j
>= 0
1574 && (scm_i_string_ref (filename
, i
)
1575 == scm_i_string_ref (suffix
, j
)))
1582 while (i
>= 0 && !is_file_name_separator (scm_c_string_ref (filename
, i
)))
1586 if (len
> 0 && is_file_name_separator (scm_c_string_ref (filename
, 0)))
1587 return scm_c_substring (filename
, 0, 1);
1589 return scm_dot_string
;
1592 return scm_c_substring (filename
, i
+1, end
+1);
1596 SCM_DEFINE (scm_canonicalize_path
, "canonicalize-path", 1, 0, 0,
1598 "Return the canonical path of @var{path}. A canonical path has\n"
1599 "no @code{.} or @code{..} components, nor any repeated path\n"
1600 "separators (@code{/}) nor symlinks.\n\n"
1601 "Raises an error if any component of @var{path} does not exist.")
1602 #define FUNC_NAME s_scm_canonicalize_path
1606 SCM_VALIDATE_STRING (1, path
);
1608 str
= scm_to_locale_string (path
);
1609 canon
= canonicalize_file_name (str
);
1613 return scm_take_locale_string (canon
);
1620 scm_i_relativize_path (SCM path
, SCM in_path
)
1625 str
= scm_to_locale_string (path
);
1626 canon
= canonicalize_file_name (str
);
1632 scanon
= scm_take_locale_string (canon
);
1634 for (; scm_is_pair (in_path
); in_path
= scm_cdr (in_path
))
1636 SCM dir
= scm_car (in_path
);
1637 size_t len
= scm_c_string_length (dir
);
1639 /* When DIR is empty, it means "current working directory". We
1640 could set DIR to (getcwd) in that case, but then the
1641 canonicalization would depend on the current directory, which
1642 is not what we want in the context of `compile-file', for
1645 && scm_is_true (scm_string_prefix_p (dir
, scanon
,
1646 SCM_UNDEFINED
, SCM_UNDEFINED
,
1647 SCM_UNDEFINED
, SCM_UNDEFINED
)))
1649 /* DIR either has a trailing delimiter or doesn't. SCANON
1650 will be delimited by single delimiters. When DIR does not
1651 have a trailing delimiter, add one to the length to strip
1652 off the delimiter within SCANON. */
1653 if (!is_file_name_separator (scm_c_string_ref (dir
, len
- 1)))
1656 if (scm_c_string_length (scanon
) > len
)
1657 return scm_substring (scanon
, scm_from_size_t (len
), SCM_UNDEFINED
);
1667 /* Examining directories. These procedures are used by `check-guile'
1668 and thus compiled unconditionally. */
1670 scm_t_bits scm_tc16_dir
;
1673 SCM_DEFINE (scm_directory_stream_p
, "directory-stream?", 1, 0, 0,
1675 "Return a boolean indicating whether @var{obj} is a directory\n"
1676 "stream as returned by @code{opendir}.")
1677 #define FUNC_NAME s_scm_directory_stream_p
1679 return scm_from_bool (SCM_DIRP (obj
));
1684 SCM_DEFINE (scm_opendir
, "opendir", 1, 0, 0,
1686 "Open the directory specified by @var{dirname} and return a directory\n"
1688 #define FUNC_NAME s_scm_opendir
1691 STRING_SYSCALL (dirname
, c_dirname
, ds
= opendir (c_dirname
));
1694 SCM_RETURN_NEWSMOB (scm_tc16_dir
| (SCM_DIR_FLAG_OPEN
<<16), ds
);
1699 /* FIXME: The glibc manual has a portability note that readdir_r may not
1700 null-terminate its return string. The circumstances outlined for this
1701 are not clear, nor is it clear what should be done about it. Lets use
1702 NAMLEN and worry about what else should be done if/when someone can
1705 SCM_DEFINE (scm_readdir
, "readdir", 1, 0, 0,
1707 "Return (as a string) the next directory entry from the directory stream\n"
1708 "@var{port}. If there is no remaining entry to be read then the\n"
1709 "end of file object is returned.")
1710 #define FUNC_NAME s_scm_readdir
1712 struct dirent_or_dirent64
*rdent
;
1714 SCM_VALIDATE_DIR (1, port
);
1715 if (!SCM_DIR_OPEN_P (port
))
1716 SCM_MISC_ERROR ("Directory ~S is not open.", scm_list_1 (port
));
1719 /* As noted in the glibc manual, on various systems (such as Solaris) the
1720 d_name[] field is only 1 char and you're expected to size the dirent
1721 buffer for readdir_r based on NAME_MAX. The SCM_MAX expressions below
1722 effectively give either sizeof(d_name) or NAME_MAX+1, whichever is
1725 On solaris 10 there's no NAME_MAX constant, it's necessary to use
1726 pathconf(). We prefer NAME_MAX though, since it should be a constant
1727 and will therefore save a system call. We also prefer it since dirfd()
1728 is not available everywhere.
1730 An alternative to dirfd() would be to open() the directory and then use
1731 fdopendir(), if the latter is available. That'd let us hold the fd
1732 somewhere in the smob, or just the dirent size calculated once. */
1734 struct dirent_or_dirent64 de
; /* just for sizeof */
1735 DIR *ds
= (DIR *) SCM_SMOB_DATA_1 (port
);
1737 char buf
[SCM_MAX (sizeof (de
),
1738 sizeof (de
) - sizeof (de
.d_name
) + NAME_MAX
+ 1)];
1741 long name_max
= fpathconf (dirfd (ds
), _PC_NAME_MAX
);
1744 buf
= alloca (SCM_MAX (sizeof (de
),
1745 sizeof (de
) - sizeof (de
.d_name
) + name_max
+ 1));
1749 SCM_SYSCALL (readdir_r_or_readdir64_r (ds
, (struct dirent_or_dirent64
*) buf
, &rdent
));
1755 return (rdent
? scm_from_locale_stringn (rdent
->d_name
, NAMLEN (rdent
))
1761 scm_dynwind_begin (0);
1762 scm_i_dynwind_pthread_mutex_lock (&scm_i_misc_mutex
);
1765 SCM_SYSCALL (rdent
= readdir_or_readdir64 ((DIR *) SCM_SMOB_DATA_1 (port
)));
1769 ret
= (rdent
? scm_from_locale_stringn (rdent
->d_name
, NAMLEN (rdent
))
1780 SCM_DEFINE (scm_rewinddir
, "rewinddir", 1, 0, 0,
1782 "Reset the directory port @var{port} so that the next call to\n"
1783 "@code{readdir} will return the first directory entry.")
1784 #define FUNC_NAME s_scm_rewinddir
1786 SCM_VALIDATE_DIR (1, port
);
1787 if (!SCM_DIR_OPEN_P (port
))
1788 SCM_MISC_ERROR ("Directory ~S is not open.", scm_list_1 (port
));
1790 rewinddir ((DIR *) SCM_SMOB_DATA_1 (port
));
1792 return SCM_UNSPECIFIED
;
1797 SCM_DEFINE (scm_closedir
, "closedir", 1, 0, 0,
1799 "Close the directory stream @var{port}.\n"
1800 "The return value is unspecified.")
1801 #define FUNC_NAME s_scm_closedir
1803 SCM_VALIDATE_DIR (1, port
);
1805 if (SCM_DIR_OPEN_P (port
))
1809 SCM_SYSCALL (sts
= closedir ((DIR *) SCM_SMOB_DATA_1 (port
)));
1813 SCM_SET_SMOB_DATA_0 (port
, scm_tc16_dir
);
1816 return SCM_UNSPECIFIED
;
1823 scm_dir_print (SCM exp
, SCM port
, scm_print_state
*pstate SCM_UNUSED
)
1825 scm_puts_unlocked ("#<", port
);
1826 if (!SCM_DIR_OPEN_P (exp
))
1827 scm_puts_unlocked ("closed: ", port
);
1828 scm_puts_unlocked ("directory stream ", port
);
1829 scm_uintprint (SCM_SMOB_DATA_1 (exp
), 16, port
);
1830 scm_putc_unlocked ('>', port
);
1836 scm_dir_free (SCM p
)
1838 if (SCM_DIR_OPEN_P (p
))
1839 closedir ((DIR *) SCM_SMOB_DATA_1 (p
));
1850 scm_tc16_dir
= scm_make_smob_type ("directory", 0);
1851 scm_set_smob_free (scm_tc16_dir
, scm_dir_free
);
1852 scm_set_smob_print (scm_tc16_dir
, scm_dir_print
);
1855 scm_c_define ("O_RDONLY", scm_from_int (O_RDONLY
));
1858 scm_c_define ("O_WRONLY", scm_from_int (O_WRONLY
));
1861 scm_c_define ("O_RDWR", scm_from_int (O_RDWR
));
1864 scm_c_define ("O_CREAT", scm_from_int (O_CREAT
));
1867 scm_c_define ("O_EXCL", scm_from_int (O_EXCL
));
1870 scm_c_define ("O_NOCTTY", scm_from_int (O_NOCTTY
));
1873 scm_c_define ("O_TRUNC", scm_from_int (O_TRUNC
));
1876 scm_c_define ("O_APPEND", scm_from_int (O_APPEND
));
1879 scm_c_define ("O_NONBLOCK", scm_from_int (O_NONBLOCK
));
1882 scm_c_define ("O_NDELAY", scm_from_int (O_NDELAY
));
1885 scm_c_define ("O_SYNC", scm_from_int (O_SYNC
));
1888 scm_c_define ("O_LARGEFILE", scm_from_int (O_LARGEFILE
));
1891 scm_c_define ("O_NOTRANS", scm_from_int (O_NOTRANS
));
1895 scm_c_define ("F_DUPFD", scm_from_int (F_DUPFD
));
1898 scm_c_define ("F_GETFD", scm_from_int (F_GETFD
));
1901 scm_c_define ("F_SETFD", scm_from_int (F_SETFD
));
1904 scm_c_define ("F_GETFL", scm_from_int (F_GETFL
));
1907 scm_c_define ("F_SETFL", scm_from_int (F_SETFL
));
1910 scm_c_define ("F_GETOWN", scm_from_int (F_GETOWN
));
1913 scm_c_define ("F_SETOWN", scm_from_int (F_SETOWN
));
1916 scm_c_define ("FD_CLOEXEC", scm_from_int (FD_CLOEXEC
));
1918 #endif /* HAVE_POSIX */
1920 /* `access' symbols. */
1921 scm_c_define ("R_OK", scm_from_int (R_OK
));
1922 scm_c_define ("W_OK", scm_from_int (W_OK
));
1923 scm_c_define ("X_OK", scm_from_int (X_OK
));
1924 scm_c_define ("F_OK", scm_from_int (F_OK
));
1926 scm_dot_string
= scm_from_locale_string (".");
1928 #include "libguile/filesys.x"