1 /* Copyright (C) 1996,1997,1998,1999,2000,2001, 2002, 2004 Free Software Foundation, Inc.
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
8 * This library 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 GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #define _GNU_SOURCE /* ask glibc for everything */
21 #define _POSIX_C_SOURCE 199506L /* for readdir_r elsewhere */
30 #include "libguile/_scm.h"
31 #include "libguile/smob.h"
32 #include "libguile/feature.h"
33 #include "libguile/fports.h"
34 #include "libguile/iselect.h"
35 #include "libguile/strings.h"
36 #include "libguile/vectors.h"
37 #include "libguile/lang.h"
39 #include "libguile/validate.h"
40 #include "libguile/filesys.h"
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 #if defined (__MINGW32__) || defined (_MSC_VER) || defined (__BORLANDC__)
88 # include "win32-dirent.h"
89 # define NAMLEN(dirent) strlen((dirent)->d_name)
92 # define NAMLEN(dirent) strlen((dirent)->d_name)
94 # define dirent direct
95 # define NAMLEN(dirent) (dirent)->d_namlen
97 # include <sys/ndir.h>
100 # include <sys/dir.h>
107 /* Ultrix has S_IFSOCK, but no S_ISSOCK. Ipe! */
108 #if defined (S_IFSOCK) && ! defined (S_ISSOCK)
109 #define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
112 /* The MinGW gcc does not define the S_ISSOCK macro. Any other native Windows
113 compiler like BorlandC or MSVC has none of these macros defined. */
135 # define _S_IFIFO 0x1000 /* FIFO */
136 # define _S_IFCHR 0x2000 /* Character */
137 # define _S_IFBLK 0x3000 /* Block */
138 # define _S_IFDIR 0x4000 /* Directory */
139 # define _S_IFREG 0x8000 /* Regular */
140 # define _S_IFSOCK 0xC000 /* Socket */
161 # define S_ISBLK(mode) (((mode) & _S_IFMT) == _S_IFBLK)
162 # define S_ISFIFO(mode) (((mode) & _S_IFMT) == _S_IFIFO)
163 # define S_ISCHR(mode) (((mode) & _S_IFMT) == _S_IFCHR)
164 # define S_ISDIR(mode) (((mode) & _S_IFMT) == _S_IFDIR)
165 # define S_ISREG(mode) (((mode) & _S_IFMT) == _S_IFREG)
166 # define S_ISSOCK(mode) (((mode) & _S_IFMT) == _S_IFSOCK)
168 #endif /* __MINGW32__ */
170 /* Some more definitions for the native Windows port. */
172 # define mkdir(path, mode) mkdir (path)
173 # define fsync(fd) _commit (fd)
174 # define fchmod(fd, mode) (-1)
175 #endif /* __MINGW32__ */
185 SCM_DEFINE (scm_chown
, "chown", 3, 0, 0,
186 (SCM object
, SCM owner
, SCM group
),
187 "Change the ownership and group of the file referred to by @var{object} to\n"
188 "the integer values @var{owner} and @var{group}. @var{object} can be\n"
189 "a string containing a file name or, if the platform\n"
190 "supports fchown, a port or integer file descriptor\n"
191 "which is open on the file. The return value\n"
192 "is unspecified.\n\n"
193 "If @var{object} is a symbolic link, either the\n"
194 "ownership of the link or the ownership of the referenced file will be\n"
195 "changed depending on the operating system (lchown is\n"
196 "unsupported at present). If @var{owner} or @var{group} is specified\n"
197 "as @code{-1}, then that ID is not changed.")
198 #define FUNC_NAME s_scm_chown
202 object
= SCM_COERCE_OUTPORT (object
);
204 SCM_VALIDATE_INUM (2, owner
);
205 SCM_VALIDATE_INUM (3, group
);
207 if (SCM_INUMP (object
) || (SCM_OPFPORTP (object
)))
209 int fdes
= SCM_INUMP (object
) ? SCM_INUM (object
)
210 : SCM_FPORT_FDES (object
);
212 SCM_SYSCALL (rv
= fchown (fdes
, SCM_INUM (owner
), SCM_INUM (group
)));
217 SCM_VALIDATE_STRING (1, object
);
218 SCM_SYSCALL (rv
= chown (SCM_STRING_CHARS (object
),
219 SCM_INUM (owner
), SCM_INUM (group
)));
223 return SCM_UNSPECIFIED
;
226 #endif /* HAVE_CHOWN */
229 SCM_DEFINE (scm_chmod
, "chmod", 2, 0, 0,
230 (SCM object
, SCM mode
),
231 "Changes the permissions of the file referred to by @var{obj}.\n"
232 "@var{obj} can be a string containing a file name or a port or integer file\n"
233 "descriptor which is open on a file (in which case @code{fchmod} is used\n"
234 "as the underlying system call).\n"
235 "@var{mode} specifies\n"
236 "the new permissions as a decimal number, e.g., @code{(chmod \"foo\" #o755)}.\n"
237 "The return value is unspecified.")
238 #define FUNC_NAME s_scm_chmod
243 object
= SCM_COERCE_OUTPORT (object
);
245 SCM_VALIDATE_INUM (2, mode
);
246 if (SCM_INUMP (object
) || SCM_OPFPORTP (object
))
248 if (SCM_INUMP (object
))
249 fdes
= SCM_INUM (object
);
251 fdes
= SCM_FPORT_FDES (object
);
252 SCM_SYSCALL (rv
= fchmod (fdes
, SCM_INUM (mode
)));
256 SCM_VALIDATE_STRING (1, object
);
257 SCM_SYSCALL (rv
= chmod (SCM_STRING_CHARS (object
), SCM_INUM (mode
)));
261 return SCM_UNSPECIFIED
;
265 SCM_DEFINE (scm_umask
, "umask", 0, 1, 0,
267 "If @var{mode} is omitted, returns a decimal number representing the current\n"
268 "file creation mask. Otherwise the file creation mask is set to\n"
269 "@var{mode} and the previous value is returned.\n\n"
270 "E.g., @code{(umask #o022)} sets the mask to octal 22, decimal 18.")
271 #define FUNC_NAME s_scm_umask
274 if (SCM_UNBNDP (mode
))
281 SCM_VALIDATE_INUM (1, mode
);
282 mask
= umask (SCM_INUM (mode
));
284 return SCM_MAKINUM (mask
);
290 SCM_DEFINE (scm_open_fdes
, "open-fdes", 2, 1, 0,
291 (SCM path
, SCM flags
, SCM mode
),
292 "Similar to @code{open} but return a file descriptor instead of\n"
294 #define FUNC_NAME s_scm_open_fdes
300 SCM_VALIDATE_STRING (1, path
);
301 iflags
= SCM_NUM2INT (2, flags
);
302 imode
= SCM_NUM2INT_DEF (3, mode
, 0666);
303 SCM_SYSCALL (fd
= open (SCM_STRING_CHARS (path
), iflags
, imode
));
306 return SCM_MAKINUM (fd
);
310 SCM_DEFINE (scm_open
, "open", 2, 1, 0,
311 (SCM path
, SCM flags
, SCM mode
),
312 "Open the file named by @var{path} for reading and/or writing.\n"
313 "@var{flags} is an integer specifying how the file should be opened.\n"
314 "@var{mode} is an integer specifying the permission bits of the file, if\n"
315 "it needs to be created, before the umask is applied. The default is 666\n"
316 "(Unix itself has no default).\n\n"
317 "@var{flags} can be constructed by combining variables using @code{logior}.\n"
318 "Basic flags are:\n\n"
320 "Open the file read-only.\n"
323 "Open the file write-only.\n"
326 "Open the file read/write.\n"
329 "Append to the file instead of truncating.\n"
332 "Create the file if it does not already exist.\n"
334 "See the Unix documentation of the @code{open} system call\n"
335 "for additional flags.")
336 #define FUNC_NAME s_scm_open
343 fd
= SCM_INUM (scm_open_fdes (path
, flags
, mode
));
344 iflags
= SCM_NUM2INT (2, flags
);
347 if (iflags
& O_APPEND
)
349 else if (iflags
& O_CREAT
)
355 if (iflags
& O_APPEND
)
357 else if (iflags
& O_WRONLY
)
362 newpt
= scm_fdes_to_port (fd
, port_mode
, path
);
367 SCM_DEFINE (scm_close
, "close", 1, 0, 0,
369 "Similar to close-port (@pxref{Closing, close-port}),\n"
370 "but also works on file descriptors. A side\n"
371 "effect of closing a file descriptor is that any ports using that file\n"
372 "descriptor are moved to a different file descriptor and have\n"
373 "their revealed counts set to zero.")
374 #define FUNC_NAME s_scm_close
379 fd_or_port
= SCM_COERCE_OUTPORT (fd_or_port
);
381 if (SCM_PORTP (fd_or_port
))
382 return scm_close_port (fd_or_port
);
383 SCM_VALIDATE_INUM (1, fd_or_port
);
384 fd
= SCM_INUM (fd_or_port
);
385 scm_evict_ports (fd
); /* see scsh manual. */
386 SCM_SYSCALL (rv
= close (fd
));
387 /* following scsh, closing an already closed file descriptor is
389 if (rv
< 0 && errno
!= EBADF
)
391 return SCM_BOOL (rv
>= 0);
395 SCM_DEFINE (scm_close_fdes
, "close-fdes", 1, 0, 0,
397 "A simple wrapper for the @code{close} system call.\n"
398 "Close file descriptor @var{fd}, which must be an integer.\n"
399 "Unlike close (@pxref{Ports and File Descriptors, close}),\n"
400 "the file descriptor will be closed even if a port is using it.\n"
401 "The return value is unspecified.")
402 #define FUNC_NAME s_scm_close_fdes
407 SCM_VALIDATE_INUM_COPY (1, fd
, c_fd
);
408 SCM_SYSCALL (rv
= close (c_fd
));
411 return SCM_UNSPECIFIED
;
419 SCM_SYMBOL (scm_sym_regular
, "regular");
420 SCM_SYMBOL (scm_sym_directory
, "directory");
422 SCM_SYMBOL (scm_sym_symlink
, "symlink");
424 SCM_SYMBOL (scm_sym_block_special
, "block-special");
425 SCM_SYMBOL (scm_sym_char_special
, "char-special");
426 SCM_SYMBOL (scm_sym_fifo
, "fifo");
427 SCM_SYMBOL (scm_sym_sock
, "socket");
428 SCM_SYMBOL (scm_sym_unknown
, "unknown");
431 scm_stat2scm (struct stat
*stat_temp
)
433 SCM ans
= scm_c_make_vector (15, SCM_UNSPECIFIED
);
435 SCM_VECTOR_SET(ans
, 0, scm_ulong2num ((unsigned long) stat_temp
->st_dev
));
436 SCM_VECTOR_SET(ans
, 1, scm_ulong2num ((unsigned long) stat_temp
->st_ino
));
437 SCM_VECTOR_SET(ans
, 2, scm_ulong2num ((unsigned long) stat_temp
->st_mode
));
438 SCM_VECTOR_SET(ans
, 3, scm_ulong2num ((unsigned long) stat_temp
->st_nlink
));
439 SCM_VECTOR_SET(ans
, 4, scm_ulong2num ((unsigned long) stat_temp
->st_uid
));
440 SCM_VECTOR_SET(ans
, 5, scm_ulong2num ((unsigned long) stat_temp
->st_gid
));
441 #ifdef HAVE_STRUCT_STAT_ST_RDEV
442 SCM_VECTOR_SET(ans
, 6, scm_ulong2num ((unsigned long) stat_temp
->st_rdev
));
444 SCM_VECTOR_SET(ans
, 6, SCM_BOOL_F
);
446 SCM_VECTOR_SET(ans
, 7, scm_ulong2num ((unsigned long) stat_temp
->st_size
));
447 SCM_VECTOR_SET(ans
, 8, scm_ulong2num ((unsigned long) stat_temp
->st_atime
));
448 SCM_VECTOR_SET(ans
, 9, scm_ulong2num ((unsigned long) stat_temp
->st_mtime
));
449 SCM_VECTOR_SET(ans
, 10, scm_ulong2num ((unsigned long) stat_temp
->st_ctime
));
450 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
451 SCM_VECTOR_SET(ans
, 11, scm_ulong2num ((unsigned long) stat_temp
->st_blksize
));
453 SCM_VECTOR_SET(ans
, 11, scm_ulong2num (4096L));
455 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
456 SCM_VECTOR_SET(ans
, 12, scm_ulong2num ((unsigned long) stat_temp
->st_blocks
));
458 SCM_VECTOR_SET(ans
, 12, SCM_BOOL_F
);
461 int mode
= stat_temp
->st_mode
;
464 SCM_VECTOR_SET(ans
, 13, scm_sym_regular
);
465 else if (S_ISDIR (mode
))
466 SCM_VECTOR_SET(ans
, 13, scm_sym_directory
);
468 else if (S_ISLNK (mode
))
469 SCM_VECTOR_SET(ans
, 13, scm_sym_symlink
);
471 else if (S_ISBLK (mode
))
472 SCM_VECTOR_SET(ans
, 13, scm_sym_block_special
);
473 else if (S_ISCHR (mode
))
474 SCM_VECTOR_SET(ans
, 13, scm_sym_char_special
);
475 else if (S_ISFIFO (mode
))
476 SCM_VECTOR_SET(ans
, 13, scm_sym_fifo
);
478 else if (S_ISSOCK (mode
))
479 SCM_VECTOR_SET(ans
, 13, scm_sym_sock
);
482 SCM_VECTOR_SET(ans
, 13, scm_sym_unknown
);
484 SCM_VECTOR_SET(ans
, 14, SCM_MAKINUM ((~S_IFMT
) & mode
));
486 /* the layout of the bits in ve[14] is intended to be portable.
487 If there are systems that don't follow the usual convention,
488 the following could be used:
491 if (S_ISUID & mode) tmp += 1;
493 if (S_IRGRP & mode) tmp += 1;
495 if (S_ISVTX & mode) tmp += 1;
497 if (S_IRUSR & mode) tmp += 1;
499 if (S_IWUSR & mode) tmp += 1;
501 if (S_IXUSR & mode) tmp += 1;
503 if (S_IWGRP & mode) tmp += 1;
505 if (S_IXGRP & mode) tmp += 1;
507 if (S_IROTH & mode) tmp += 1;
509 if (S_IWOTH & mode) tmp += 1;
511 if (S_IXOTH & mode) tmp += 1;
513 SCM_VECTOR_SET(ans, 14, SCM_MAKINUM (tmp));
523 * Try getting the appropiate stat buffer for a given file descriptor
524 * under Windows. It differentiates between file, pipe and socket
527 static int fstat_Win32 (int fdes
, struct stat
*buf
)
529 int error
, optlen
= sizeof (int);
531 memset (buf
, 0, sizeof (struct stat
));
533 /* Is this a socket ? */
534 if (getsockopt (fdes
, SOL_SOCKET
, SO_ERROR
, (void *) &error
, &optlen
) >= 0)
536 buf
->st_mode
= _S_IFSOCK
| _S_IREAD
| _S_IWRITE
| _S_IEXEC
;
538 buf
->st_atime
= buf
->st_ctime
= buf
->st_mtime
= time (NULL
);
541 /* Maybe a regular file or pipe ? */
542 return fstat (fdes
, buf
);
544 #endif /* __MINGW32__ */
546 SCM_DEFINE (scm_stat
, "stat", 1, 0, 0,
548 "Return an object containing various information about the file\n"
549 "determined by @var{obj}. @var{obj} can be a string containing\n"
550 "a file name or a port or integer file descriptor which is open\n"
551 "on a file (in which case @code{fstat} is used as the underlying\n"
554 "The object returned by @code{stat} can be passed as a single\n"
555 "parameter to the following procedures, all of which return\n"
560 "The device containing the file.\n"
562 "The file serial number, which distinguishes this file from all\n"
563 "other files on the same device.\n"
565 "The mode of the file. This includes file type information and\n"
566 "the file permission bits. See @code{stat:type} and\n"
567 "@code{stat:perms} below.\n"
569 "The number of hard links to the file.\n"
571 "The user ID of the file's owner.\n"
573 "The group ID of the file.\n"
575 "Device ID; this entry is defined only for character or block\n"
578 "The size of a regular file in bytes.\n"
580 "The last access time for the file.\n"
582 "The last modification time for the file.\n"
584 "The last modification time for the attributes of the file.\n"
585 "@item stat:blksize\n"
586 "The optimal block size for reading or writing the file, in\n"
588 "@item stat:blocks\n"
589 "The amount of disk space that the file occupies measured in\n"
590 "units of 512 byte blocks.\n"
593 "In addition, the following procedures return the information\n"
594 "from stat:mode in a more convenient form:\n"
598 "A symbol representing the type of file. Possible values are\n"
599 "regular, directory, symlink, block-special, char-special, fifo,\n"
600 "socket and unknown\n"
602 "An integer representing the access permission bits.\n"
604 #define FUNC_NAME s_scm_stat
608 struct stat stat_temp
;
610 if (SCM_INUMP (object
))
613 SCM_SYSCALL (rv
= fstat_Win32 (SCM_INUM (object
), &stat_temp
));
615 SCM_SYSCALL (rv
= fstat (SCM_INUM (object
), &stat_temp
));
618 else if (SCM_STRINGP (object
))
621 char *p
, *file
= strdup (SCM_STRING_CHARS (object
));
622 p
= file
+ strlen (file
) - 1;
623 while (p
> file
&& (*p
== '/' || *p
== '\\'))
625 SCM_SYSCALL (rv
= stat (file
, &stat_temp
));
628 SCM_SYSCALL (rv
= stat (SCM_STRING_CHARS (object
), &stat_temp
));
633 object
= SCM_COERCE_OUTPORT (object
);
634 SCM_VALIDATE_OPFPORT (1, object
);
635 fdes
= SCM_FPORT_FDES (object
);
637 SCM_SYSCALL (rv
= fstat_Win32 (fdes
, &stat_temp
));
639 SCM_SYSCALL (rv
= fstat (fdes
, &stat_temp
));
647 SCM_SYSERROR_MSG ("~A: ~S",
648 scm_list_2 (scm_makfrom0str (strerror (errno
)),
652 return scm_stat2scm (&stat_temp
);
657 /* {Modifying Directories}
661 SCM_DEFINE (scm_link
, "link", 2, 0, 0,
662 (SCM oldpath
, SCM newpath
),
663 "Creates a new name @var{newpath} in the file system for the\n"
664 "file named by @var{oldpath}. If @var{oldpath} is a symbolic\n"
665 "link, the link may or may not be followed depending on the\n"
667 #define FUNC_NAME s_scm_link
671 SCM_VALIDATE_STRING (1, oldpath
);
672 SCM_VALIDATE_STRING (2, newpath
);
673 SCM_SYSCALL (val
= link (SCM_STRING_CHARS (oldpath
),
674 SCM_STRING_CHARS (newpath
)));
677 return SCM_UNSPECIFIED
;
680 #endif /* HAVE_LINK */
684 SCM_DEFINE (scm_rename
, "rename-file", 2, 0, 0,
685 (SCM oldname
, SCM newname
),
686 "Renames the file specified by @var{oldname} to @var{newname}.\n"
687 "The return value is unspecified.")
688 #define FUNC_NAME s_scm_rename
691 SCM_VALIDATE_STRING (1, oldname
);
692 SCM_VALIDATE_STRING (2, newname
);
694 SCM_SYSCALL (rv
= rename (SCM_STRING_CHARS (oldname
), SCM_STRING_CHARS (newname
)));
696 SCM_SYSCALL (rv
= link (SCM_STRING_CHARS (oldname
), SCM_STRING_CHARS (newname
)));
699 SCM_SYSCALL (rv
= unlink (SCM_STRING_CHARS (oldname
)));;
701 /* unlink failed. remove new name */
702 SCM_SYSCALL (unlink (SCM_STRING_CHARS (newname
)));
707 return SCM_UNSPECIFIED
;
712 SCM_DEFINE (scm_delete_file
, "delete-file", 1, 0, 0,
714 "Deletes (or \"unlinks\") the file specified by @var{path}.")
715 #define FUNC_NAME s_scm_delete_file
718 SCM_VALIDATE_STRING (1, str
);
719 SCM_SYSCALL (ans
= unlink (SCM_STRING_CHARS (str
)));
722 return SCM_UNSPECIFIED
;
727 SCM_DEFINE (scm_mkdir
, "mkdir", 1, 1, 0,
728 (SCM path
, SCM mode
),
729 "Create a new directory named by @var{path}. If @var{mode} is omitted\n"
730 "then the permissions of the directory file are set using the current\n"
731 "umask. Otherwise they are set to the decimal value specified with\n"
732 "@var{mode}. The return value is unspecified.")
733 #define FUNC_NAME s_scm_mkdir
737 SCM_VALIDATE_STRING (1, path
);
738 if (SCM_UNBNDP (mode
))
742 SCM_SYSCALL (rv
= mkdir (SCM_STRING_CHARS (path
), 0777 ^ mask
));
746 SCM_VALIDATE_INUM (2, mode
);
747 SCM_SYSCALL (rv
= mkdir (SCM_STRING_CHARS (path
), SCM_INUM (mode
)));
751 return SCM_UNSPECIFIED
;
754 #endif /* HAVE_MKDIR */
757 SCM_DEFINE (scm_rmdir
, "rmdir", 1, 0, 0,
759 "Remove the existing directory named by @var{path}. The directory must\n"
760 "be empty for this to succeed. The return value is unspecified.")
761 #define FUNC_NAME s_scm_rmdir
765 SCM_VALIDATE_STRING (1, path
);
766 SCM_SYSCALL (val
= rmdir (SCM_STRING_CHARS (path
)));
769 return SCM_UNSPECIFIED
;
776 /* {Examining Directories}
779 scm_t_bits scm_tc16_dir
;
782 SCM_DEFINE (scm_directory_stream_p
, "directory-stream?", 1, 0, 0,
784 "Return a boolean indicating whether @var{object} is a directory\n"
785 "stream as returned by @code{opendir}.")
786 #define FUNC_NAME s_scm_directory_stream_p
788 return SCM_BOOL (SCM_DIRP (obj
));
793 SCM_DEFINE (scm_opendir
, "opendir", 1, 0, 0,
795 "Open the directory specified by @var{path} and return a directory\n"
797 #define FUNC_NAME s_scm_opendir
800 SCM_VALIDATE_STRING (1, dirname
);
801 SCM_SYSCALL (ds
= opendir (SCM_STRING_CHARS (dirname
)));
804 SCM_RETURN_NEWSMOB (scm_tc16_dir
| SCM_DIR_FLAG_OPEN
, ds
);
809 /* FIXME: The glibc manual has a portability note that readdir_r may not
810 null-terminate its return string. The circumstances outlined for this
811 are not clear, nor is it clear what should be done about it. Lets worry
812 about this if/when someone can figure it out. */
814 SCM_DEFINE (scm_readdir
, "readdir", 1, 0, 0,
816 "Return (as a string) the next directory entry from the directory stream\n"
817 "@var{stream}. If there is no remaining entry to be read then the\n"
818 "end of file object is returned.")
819 #define FUNC_NAME s_scm_readdir
821 struct dirent
*rdent
;
823 SCM_VALIDATE_DIR (1, port
);
824 if (!SCM_DIR_OPEN_P (port
))
825 SCM_MISC_ERROR ("Directory ~S is not open.", scm_list_1 (port
));
830 /* On Solaris 2.7, struct dirent only contains "char d_name[1]" and one is
831 expected to provide a buffer of "sizeof(struct dirent) + NAME_MAX"
832 bytes. The glibc 2.3.2 manual notes this sort of thing too, and
833 advises "offsetof(struct dirent,d_name) + NAME_MAX + 1". Either should
834 suffice, we give both to be certain. */
837 char pad1
[sizeof(struct dirent
) + NAME_MAX
];
838 char pad2
[offsetof (struct dirent
, d_name
) + NAME_MAX
+ 1];
840 SCM_SYSCALL (readdir_r ((DIR *) SCM_CELL_WORD_1 (port
), &u
.ent
, &rdent
));
842 SCM_SYSCALL (rdent
= readdir ((DIR *) SCM_CELL_WORD_1 (port
)));
847 return (rdent
? scm_mem2string (rdent
->d_name
, NAMLEN (rdent
))
854 SCM_DEFINE (scm_rewinddir
, "rewinddir", 1, 0, 0,
856 "Reset the directory port @var{stream} so that the next call to\n"
857 "@code{readdir} will return the first directory entry.")
858 #define FUNC_NAME s_scm_rewinddir
860 SCM_VALIDATE_DIR (1, port
);
861 if (!SCM_DIR_OPEN_P (port
))
862 SCM_MISC_ERROR ("Directory ~S is not open.", scm_list_1 (port
));
864 rewinddir ((DIR *) SCM_CELL_WORD_1 (port
));
866 return SCM_UNSPECIFIED
;
871 SCM_DEFINE (scm_closedir
, "closedir", 1, 0, 0,
873 "Close the directory stream @var{stream}.\n"
874 "The return value is unspecified.")
875 #define FUNC_NAME s_scm_closedir
877 SCM_VALIDATE_DIR (1, port
);
879 if (SCM_DIR_OPEN_P (port
))
883 SCM_SYSCALL (sts
= closedir ((DIR *) SCM_CELL_WORD_1 (port
)));
887 SCM_SET_CELL_WORD_0 (port
, scm_tc16_dir
);
890 return SCM_UNSPECIFIED
;
896 scm_dir_print (SCM exp
, SCM port
, scm_print_state
*pstate SCM_UNUSED
)
898 scm_puts ("#<", port
);
899 if (!SCM_DIR_OPEN_P (exp
))
900 scm_puts ("closed: ", port
);
901 scm_puts ("directory stream ", port
);
902 scm_intprint (SCM_CELL_WORD_1 (exp
), 16, port
);
903 scm_putc ('>', port
);
911 if (SCM_DIR_OPEN_P (p
))
912 closedir ((DIR *) SCM_CELL_WORD_1 (p
));
917 /* {Navigating Directories}
921 SCM_DEFINE (scm_chdir
, "chdir", 1, 0, 0,
923 "Change the current working directory to @var{path}.\n"
924 "The return value is unspecified.")
925 #define FUNC_NAME s_scm_chdir
929 SCM_VALIDATE_STRING (1, str
);
930 SCM_SYSCALL (ans
= chdir (SCM_STRING_CHARS (str
)));
933 return SCM_UNSPECIFIED
;
938 SCM_DEFINE (scm_getcwd
, "getcwd", 0, 0, 0,
940 "Return the name of the current working directory.")
941 #define FUNC_NAME s_scm_getcwd
948 wd
= scm_malloc (size
);
949 while ((rv
= getcwd (wd
, size
)) == 0 && errno
== ERANGE
)
953 wd
= scm_malloc (size
);
957 int save_errno
= errno
;
962 result
= scm_mem2string (wd
, strlen (wd
));
967 #endif /* HAVE_GETCWD */
973 /* check that element is a port or file descriptor. if it's a port
974 and its buffer is ready for use, add it to the ports_ready list.
975 otherwise add its file descriptor to *set. the type of list can be
976 determined from pos: SCM_ARG1 for reads, SCM_ARG2 for writes,
977 SCM_ARG3 for excepts. */
979 set_element (SELECT_TYPE
*set
, SCM
*ports_ready
, SCM element
, int pos
)
983 if (SCM_INUMP (element
))
985 fd
= SCM_INUM (element
);
991 element
= SCM_COERCE_OUTPORT (element
);
992 SCM_ASSERT (SCM_OPFPORTP (element
), element
, pos
, "select");
995 /* check whether port has buffered input. */
996 scm_t_port
*pt
= SCM_PTAB_ENTRY (element
);
998 if (pt
->read_pos
< pt
->read_end
)
1001 else if (pos
== SCM_ARG2
)
1003 /* check whether port's output buffer has room. */
1004 scm_t_port
*pt
= SCM_PTAB_ENTRY (element
);
1006 /* > 1 since writing the last byte in the buffer causes flush. */
1007 if (pt
->write_end
- pt
->write_pos
> 1)
1010 fd
= use_buf
? -1 : SCM_FPORT_FDES (element
);
1013 *ports_ready
= scm_cons (element
, *ports_ready
);
1019 /* check list_or_vec, a list or vector of ports or file descriptors,
1020 adding each member to either the ports_ready list (if it's a port
1021 with a usable buffer) or to *set. the kind of list_or_vec can be
1022 determined from pos: SCM_ARG1 for reads, SCM_ARG2 for writes,
1023 SCM_ARG3 for excepts. */
1025 fill_select_type (SELECT_TYPE
*set
, SCM
*ports_ready
, SCM list_or_vec
, int pos
)
1029 if (SCM_VECTORP (list_or_vec
))
1031 int i
= SCM_VECTOR_LENGTH (list_or_vec
);
1032 SCM
const *ve
= SCM_VELTS (list_or_vec
);
1036 int fd
= set_element (set
, ports_ready
, ve
[i
], pos
);
1044 while (!SCM_NULL_OR_NIL_P (list_or_vec
))
1046 int fd
= set_element (set
, ports_ready
, SCM_CAR (list_or_vec
), pos
);
1050 list_or_vec
= SCM_CDR (list_or_vec
);
1057 /* if element (a file descriptor or port) appears in *set, cons it to
1058 list. return list. */
1060 get_element (SELECT_TYPE
*set
, SCM element
, SCM list
)
1064 if (SCM_INUMP (element
))
1066 fd
= SCM_INUM (element
);
1070 fd
= SCM_FPORT_FDES (SCM_COERCE_OUTPORT (element
));
1072 if (FD_ISSET (fd
, set
))
1073 list
= scm_cons (element
, list
);
1077 /* construct component of scm_select return value.
1078 set: pointer to set of file descriptors found by select to be ready
1079 ports_ready: ports ready due to buffering
1080 list_or_vec: original list/vector handed to scm_select.
1081 the return value is a list/vector of ready ports/file descriptors.
1082 works by finding the objects in list which correspond to members of
1083 *set and appending them to ports_ready. result is converted to a
1084 vector if list_or_vec is a vector. */
1086 retrieve_select_type (SELECT_TYPE
*set
, SCM ports_ready
, SCM list_or_vec
)
1088 SCM answer_list
= ports_ready
;
1090 if (SCM_VECTORP (list_or_vec
))
1092 int i
= SCM_VECTOR_LENGTH (list_or_vec
);
1093 SCM
const *ve
= SCM_VELTS (list_or_vec
);
1097 answer_list
= get_element (set
, ve
[i
], answer_list
);
1099 return scm_vector (answer_list
);
1103 /* list_or_vec must be a list. */
1104 while (!SCM_NULL_OR_NIL_P (list_or_vec
))
1106 answer_list
= get_element (set
, SCM_CAR (list_or_vec
), answer_list
);
1107 list_or_vec
= SCM_CDR (list_or_vec
);
1113 /* Static helper functions above refer to s_scm_select directly as s_select */
1114 SCM_DEFINE (scm_select
, "select", 3, 2, 0,
1115 (SCM reads
, SCM writes
, SCM excepts
, SCM secs
, SCM usecs
),
1116 "This procedure has a variety of uses: waiting for the ability\n"
1117 "to provide input, accept output, or the existence of\n"
1118 "exceptional conditions on a collection of ports or file\n"
1119 "descriptors, or waiting for a timeout to occur.\n"
1120 "It also returns if interrupted by a signal.\n\n"
1121 "@var{reads}, @var{writes} and @var{excepts} can be lists or\n"
1122 "vectors, with each member a port or a file descriptor.\n"
1123 "The value returned is a list of three corresponding\n"
1124 "lists or vectors containing only the members which meet the\n"
1125 "specified requirement. The ability of port buffers to\n"
1126 "provide input or accept output is taken into account.\n"
1127 "Ordering of the input lists or vectors is not preserved.\n\n"
1128 "The optional arguments @var{secs} and @var{usecs} specify the\n"
1129 "timeout. Either @var{secs} can be specified alone, as\n"
1130 "either an integer or a real number, or both @var{secs} and\n"
1131 "@var{usecs} can be specified as integers, in which case\n"
1132 "@var{usecs} is an additional timeout expressed in\n"
1133 "microseconds. If @var{secs} is omitted or is @code{#f} then\n"
1134 "select will wait for as long as it takes for one of the other\n"
1135 "conditions to be satisfied.\n\n"
1136 "The scsh version of @code{select} differs as follows:\n"
1137 "Only vectors are accepted for the first three arguments.\n"
1138 "The @var{usecs} argument is not supported.\n"
1139 "Multiple values are returned instead of a list.\n"
1140 "Duplicates in the input vectors appear only once in output.\n"
1141 "An additional @code{select!} interface is provided.")
1142 #define FUNC_NAME s_scm_select
1144 struct timeval timeout
;
1145 struct timeval
* time_ptr
;
1146 SELECT_TYPE read_set
;
1147 SELECT_TYPE write_set
;
1148 SELECT_TYPE except_set
;
1152 /* these lists accumulate ports which are ready due to buffering.
1153 their file descriptors don't need to be added to the select sets. */
1154 SCM read_ports_ready
= SCM_EOL
;
1155 SCM write_ports_ready
= SCM_EOL
;
1158 if (SCM_VECTORP (reads
))
1160 read_count
= SCM_VECTOR_LENGTH (reads
);
1164 read_count
= scm_ilength (reads
);
1165 SCM_ASSERT (read_count
>= 0, reads
, SCM_ARG1
, FUNC_NAME
);
1167 if (SCM_VECTORP (writes
))
1169 write_count
= SCM_VECTOR_LENGTH (writes
);
1173 write_count
= scm_ilength (writes
);
1174 SCM_ASSERT (write_count
>= 0, writes
, SCM_ARG2
, FUNC_NAME
);
1176 if (SCM_VECTORP (excepts
))
1178 except_count
= SCM_VECTOR_LENGTH (excepts
);
1182 except_count
= scm_ilength (excepts
);
1183 SCM_ASSERT (except_count
>= 0, excepts
, SCM_ARG3
, FUNC_NAME
);
1186 FD_ZERO (&read_set
);
1187 FD_ZERO (&write_set
);
1188 FD_ZERO (&except_set
);
1190 max_fd
= fill_select_type (&read_set
, &read_ports_ready
, reads
, SCM_ARG1
);
1193 int write_max
= fill_select_type (&write_set
, &write_ports_ready
,
1195 int except_max
= fill_select_type (&except_set
, NULL
,
1198 if (write_max
> max_fd
)
1200 if (except_max
> max_fd
)
1201 max_fd
= except_max
;
1204 /* if there's a port with a ready buffer, don't block, just
1205 check for ready file descriptors. */
1206 if (!SCM_NULLP (read_ports_ready
) || !SCM_NULLP (write_ports_ready
))
1209 timeout
.tv_usec
= 0;
1210 time_ptr
= &timeout
;
1212 else if (SCM_UNBNDP (secs
) || SCM_FALSEP (secs
))
1216 if (SCM_INUMP (secs
))
1218 timeout
.tv_sec
= SCM_INUM (secs
);
1219 if (SCM_UNBNDP (usecs
))
1220 timeout
.tv_usec
= 0;
1223 SCM_VALIDATE_INUM (5, usecs
);
1224 timeout
.tv_usec
= SCM_INUM (usecs
);
1229 double fl
= scm_num2dbl (secs
, FUNC_NAME
);
1231 if (!SCM_UNBNDP (usecs
))
1232 SCM_WRONG_TYPE_ARG (4, secs
);
1234 SCM_OUT_OF_RANGE (4, secs
);
1235 timeout
.tv_sec
= (long) fl
;
1236 timeout
.tv_usec
= (long) ((fl
- timeout
.tv_sec
) * 1000000);
1238 time_ptr
= &timeout
;
1242 int rv
= scm_internal_select (max_fd
+ 1,
1243 &read_set
, &write_set
, &except_set
,
1248 return scm_list_3 (retrieve_select_type (&read_set
, read_ports_ready
, reads
),
1249 retrieve_select_type (&write_set
, write_ports_ready
, writes
),
1250 retrieve_select_type (&except_set
, SCM_EOL
, excepts
));
1253 #endif /* HAVE_SELECT */
1258 SCM_DEFINE (scm_fcntl
, "fcntl", 2, 1, 0,
1259 (SCM object
, SCM cmd
, SCM value
),
1260 "Apply @var{command} to the specified file descriptor or the underlying\n"
1261 "file descriptor of the specified port. @var{value} is an optional\n"
1262 "integer argument.\n\n"
1263 "Values for @var{command} are:\n\n"
1266 "Duplicate a file descriptor\n"
1268 "Get flags associated with the file descriptor.\n"
1270 "Set flags associated with the file descriptor to @var{value}.\n"
1272 "Get flags associated with the open file.\n"
1274 "Set flags associated with the open file to @var{value}\n"
1276 "Get the process ID of a socket's owner, for @code{SIGIO} signals.\n"
1278 "Set the process that owns a socket to @var{value}, for @code{SIGIO} signals.\n"
1279 "@item FD_CLOEXEC\n"
1280 "The value used to indicate the \"close on exec\" flag with @code{F_GETFL} or\n"
1283 #define FUNC_NAME s_scm_fcntl
1289 object
= SCM_COERCE_OUTPORT (object
);
1291 SCM_VALIDATE_INUM (2, cmd
);
1292 if (SCM_OPFPORTP (object
))
1293 fdes
= SCM_FPORT_FDES (object
);
1296 SCM_VALIDATE_INUM (1, object
);
1297 fdes
= SCM_INUM (object
);
1300 if (SCM_UNBNDP (value
)) {
1303 SCM_VALIDATE_INUM_COPY (SCM_ARG3
, value
, ivalue
);
1306 SCM_SYSCALL (rv
= fcntl (fdes
, SCM_INUM (cmd
), ivalue
));
1309 return SCM_MAKINUM (rv
);
1312 #endif /* HAVE_FCNTL */
1314 SCM_DEFINE (scm_fsync
, "fsync", 1, 0, 0,
1316 "Copies any unwritten data for the specified output file descriptor to disk.\n"
1317 "If @var{port/fd} is a port, its buffer is flushed before the underlying\n"
1318 "file descriptor is fsync'd.\n"
1319 "The return value is unspecified.")
1320 #define FUNC_NAME s_scm_fsync
1324 object
= SCM_COERCE_OUTPORT (object
);
1326 if (SCM_OPFPORTP (object
))
1329 fdes
= SCM_FPORT_FDES (object
);
1333 SCM_VALIDATE_INUM (1, object
);
1334 fdes
= SCM_INUM (object
);
1336 if (fsync (fdes
) == -1)
1338 return SCM_UNSPECIFIED
;
1343 SCM_DEFINE (scm_symlink
, "symlink", 2, 0, 0,
1344 (SCM oldpath
, SCM newpath
),
1345 "Create a symbolic link named @var{path-to} with the value (i.e., pointing to)\n"
1346 "@var{path-from}. The return value is unspecified.")
1347 #define FUNC_NAME s_scm_symlink
1351 SCM_VALIDATE_STRING (1, oldpath
);
1352 SCM_VALIDATE_STRING (2, newpath
);
1353 SCM_SYSCALL (val
= symlink (SCM_STRING_CHARS (oldpath
), SCM_STRING_CHARS (newpath
)));
1356 return SCM_UNSPECIFIED
;
1359 #endif /* HAVE_SYMLINK */
1361 #ifdef HAVE_READLINK
1362 SCM_DEFINE (scm_readlink
, "readlink", 1, 0, 0,
1364 "Return the value of the symbolic link named by @var{path} (a\n"
1365 "string), i.e., the file that the link points to.")
1366 #define FUNC_NAME s_scm_readlink
1372 SCM_VALIDATE_STRING (1, path
);
1373 buf
= scm_malloc (size
);
1374 while ((rv
= readlink (SCM_STRING_CHARS (path
), buf
, size
)) == size
)
1378 buf
= scm_malloc (size
);
1382 int save_errno
= errno
;
1387 result
= scm_mem2string (buf
, rv
);
1392 #endif /* HAVE_READLINK */
1395 SCM_DEFINE (scm_lstat
, "lstat", 1, 0, 0,
1397 "Similar to @code{stat}, but does not follow symbolic links, i.e.,\n"
1398 "it will return information about a symbolic link itself, not the\n"
1399 "file it points to. @var{path} must be a string.")
1400 #define FUNC_NAME s_scm_lstat
1403 struct stat stat_temp
;
1405 SCM_VALIDATE_STRING (1, str
);
1406 SCM_SYSCALL (rv
= lstat (SCM_STRING_CHARS (str
), &stat_temp
));
1411 SCM_SYSERROR_MSG ("~A: ~S",
1412 scm_list_2 (scm_makfrom0str (strerror (errno
)), str
),
1415 return scm_stat2scm(&stat_temp
);
1418 #endif /* HAVE_LSTAT */
1420 SCM_DEFINE (scm_copy_file
, "copy-file", 2, 0, 0,
1421 (SCM oldfile
, SCM newfile
),
1422 "Copy the file specified by @var{path-from} to @var{path-to}.\n"
1423 "The return value is unspecified.")
1424 #define FUNC_NAME s_scm_copy_file
1429 struct stat oldstat
;
1431 SCM_VALIDATE_STRING (1, oldfile
);
1432 SCM_VALIDATE_STRING (2, newfile
);
1433 if (stat (SCM_STRING_CHARS (oldfile
), &oldstat
) == -1)
1435 oldfd
= open (SCM_STRING_CHARS (oldfile
), O_RDONLY
);
1439 /* use POSIX flags instead of 07777?. */
1440 newfd
= open (SCM_STRING_CHARS (newfile
), O_WRONLY
| O_CREAT
| O_TRUNC
,
1441 oldstat
.st_mode
& 07777);
1445 while ((n
= read (oldfd
, buf
, sizeof buf
)) > 0)
1446 if (write (newfd
, buf
, n
) != n
)
1453 if (close (newfd
) == -1)
1455 return SCM_UNSPECIFIED
;
1460 /* Filename manipulation */
1464 SCM_DEFINE (scm_dirname
, "dirname", 1, 0, 0,
1466 "Return the directory name component of the file name\n"
1467 "@var{filename}. If @var{filename} does not contain a directory\n"
1468 "component, @code{.} is returned.")
1469 #define FUNC_NAME s_scm_dirname
1473 unsigned long int len
;
1475 SCM_VALIDATE_STRING (1, filename
);
1477 s
= SCM_STRING_CHARS (filename
);
1478 len
= SCM_STRING_LENGTH (filename
);
1482 while (i
>= 0 && (s
[i
] == '/' || s
[i
] == '\\')) --i
;
1483 while (i
>= 0 && (s
[i
] != '/' && s
[i
] != '\\')) --i
;
1484 while (i
>= 0 && (s
[i
] == '/' || s
[i
] == '\\')) --i
;
1486 while (i
>= 0 && s
[i
] == '/') --i
;
1487 while (i
>= 0 && s
[i
] != '/') --i
;
1488 while (i
>= 0 && s
[i
] == '/') --i
;
1489 #endif /* ndef __MINGW32__ */
1493 if (len
> 0 && (s
[0] == '/' || s
[0] == '\\'))
1495 if (len
> 0 && s
[0] == '/')
1496 #endif /* ndef __MINGW32__ */
1497 return scm_substring (filename
, SCM_INUM0
, SCM_MAKINUM (1));
1499 return scm_dot_string
;
1502 return scm_substring (filename
, SCM_INUM0
, SCM_MAKINUM (i
+ 1));
1506 SCM_DEFINE (scm_basename
, "basename", 1, 1, 0,
1507 (SCM filename
, SCM suffix
),
1508 "Return the base name of the file name @var{filename}. The\n"
1509 "base name is the file name without any directory components.\n"
1510 "If @var{suffix} is provided, and is equal to the end of\n"
1511 "@var{basename}, it is removed also.")
1512 #define FUNC_NAME s_scm_basename
1517 SCM_VALIDATE_STRING (1, filename
);
1518 f
= SCM_STRING_CHARS (filename
);
1519 len
= SCM_STRING_LENGTH (filename
);
1521 if (SCM_UNBNDP (suffix
))
1525 SCM_VALIDATE_STRING (2, suffix
);
1526 s
= SCM_STRING_CHARS (suffix
);
1527 j
= SCM_STRING_LENGTH (suffix
) - 1;
1531 while (i
>= 0 && (f
[i
] == '/' || f
[i
] == '\\')) --i
;
1533 while (i
>= 0 && f
[i
] == '/') --i
;
1534 #endif /* ndef __MINGW32__ */
1536 while (i
>= 0 && j
>= 0 && f
[i
] == s
[j
]) --i
, --j
;
1540 while (i
>= 0 && f
[i
] != '/' && f
[i
] != '\\') --i
;
1542 while (i
>= 0 && f
[i
] != '/') --i
;
1543 #endif /* ndef __MINGW32__ */
1547 if (len
> 0 && (f
[0] == '/' || f
[0] == '\\'))
1549 if (len
> 0 && f
[0] == '/')
1550 #endif /* ndef __MINGW32__ */
1551 return scm_substring (filename
, SCM_INUM0
, SCM_MAKINUM (1));
1553 return scm_dot_string
;
1556 return scm_substring (filename
, SCM_MAKINUM (i
+ 1), SCM_MAKINUM (end
+ 1));
1567 scm_tc16_dir
= scm_make_smob_type ("directory", 0);
1568 scm_set_smob_free (scm_tc16_dir
, scm_dir_free
);
1569 scm_set_smob_print (scm_tc16_dir
, scm_dir_print
);
1571 scm_dot_string
= scm_permanent_object (scm_makfrom0str ("."));
1574 scm_c_define ("O_RDONLY", scm_long2num (O_RDONLY
));
1577 scm_c_define ("O_WRONLY", scm_long2num (O_WRONLY
));
1580 scm_c_define ("O_RDWR", scm_long2num (O_RDWR
));
1583 scm_c_define ("O_CREAT", scm_long2num (O_CREAT
));
1586 scm_c_define ("O_EXCL", scm_long2num (O_EXCL
));
1589 scm_c_define ("O_NOCTTY", scm_long2num (O_NOCTTY
));
1592 scm_c_define ("O_TRUNC", scm_long2num (O_TRUNC
));
1595 scm_c_define ("O_APPEND", scm_long2num (O_APPEND
));
1598 scm_c_define ("O_NONBLOCK", scm_long2num (O_NONBLOCK
));
1601 scm_c_define ("O_NDELAY", scm_long2num (O_NDELAY
));
1604 scm_c_define ("O_SYNC", scm_long2num (O_SYNC
));
1608 scm_c_define ("F_DUPFD", scm_long2num (F_DUPFD
));
1611 scm_c_define ("F_GETFD", scm_long2num (F_GETFD
));
1614 scm_c_define ("F_SETFD", scm_long2num (F_SETFD
));
1617 scm_c_define ("F_GETFL", scm_long2num (F_GETFL
));
1620 scm_c_define ("F_SETFL", scm_long2num (F_SETFL
));
1623 scm_c_define ("F_GETOWN", scm_long2num (F_GETOWN
));
1626 scm_c_define ("F_SETOWN", scm_long2num (F_SETOWN
));
1629 scm_c_define ("FD_CLOEXEC", scm_long2num (FD_CLOEXEC
));
1632 #include "libguile/filesys.x"