Merge commit '29776e85da637ec4d44b2b2822d6934a50c0084b' into boehm-demers-weiser-gc
[bpt/guile.git] / libguile / filesys.c
1 /* Copyright (C) 1996,1997,1998,1999,2000,2001, 2002, 2004, 2006 Free Software Foundation, Inc.
2 *
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.
7 *
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.
12 *
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
18
19 \f
20
21 /* See stime.c for comments on why _POSIX_C_SOURCE is not always defined. */
22 #define _GNU_SOURCE /* ask glibc for everything */
23 #define _LARGEFILE64_SOURCE /* ask for stat64 etc */
24 #ifdef __hpux
25 #define _POSIX_C_SOURCE 199506L /* for readdir_r */
26 #endif
27
28 #if HAVE_CONFIG_H
29 # include <config.h>
30 #endif
31
32 #include <alloca.h>
33
34 #include <stdio.h>
35 #include <errno.h>
36
37 #include "libguile/_scm.h"
38 #include "libguile/smob.h"
39 #include "libguile/feature.h"
40 #include "libguile/fports.h"
41 #include "libguile/private-gc.h" /* for SCM_MAX */
42 #include "libguile/iselect.h"
43 #include "libguile/strings.h"
44 #include "libguile/vectors.h"
45 #include "libguile/lang.h"
46 #include "libguile/dynwind.h"
47
48 #include "libguile/validate.h"
49 #include "libguile/filesys.h"
50
51 \f
52 #ifdef HAVE_IO_H
53 #include <io.h>
54 #endif
55
56 #ifdef HAVE_DIRECT_H
57 #include <direct.h>
58 #endif
59
60 #ifdef TIME_WITH_SYS_TIME
61 # include <sys/time.h>
62 # include <time.h>
63 #else
64 # if HAVE_SYS_TIME_H
65 # include <sys/time.h>
66 # else
67 # include <time.h>
68 # endif
69 #endif
70
71 #ifdef HAVE_UNISTD_H
72 #include <unistd.h>
73 #endif
74
75 #ifdef LIBC_H_WITH_UNISTD_H
76 #include <libc.h>
77 #endif
78
79 #ifdef HAVE_SYS_SELECT_H
80 #include <sys/select.h>
81 #endif
82
83 #ifdef HAVE_STRING_H
84 #include <string.h>
85 #endif
86
87 #include <sys/types.h>
88 #include <sys/stat.h>
89 #include <fcntl.h>
90
91 #ifdef HAVE_PWD_H
92 #include <pwd.h>
93 #endif
94
95
96 #if defined (__MINGW32__) || defined (_MSC_VER) || defined (__BORLANDC__)
97 # include "win32-dirent.h"
98 # define NAMLEN(dirent) strlen((dirent)->d_name)
99 /* The following bits are per AC_HEADER_DIRENT doco in the autoconf manual */
100 #elif HAVE_DIRENT_H
101 # include <dirent.h>
102 # define NAMLEN(dirent) strlen((dirent)->d_name)
103 #else
104 # define dirent direct
105 # define NAMLEN(dirent) (dirent)->d_namlen
106 # if HAVE_SYS_NDIR_H
107 # include <sys/ndir.h>
108 # endif
109 # if HAVE_SYS_DIR_H
110 # include <sys/dir.h>
111 # endif
112 # if HAVE_NDIR_H
113 # include <ndir.h>
114 # endif
115 #endif
116
117 /* Ultrix has S_IFSOCK, but no S_ISSOCK. Ipe! */
118 #if defined (S_IFSOCK) && ! defined (S_ISSOCK)
119 #define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
120 #endif
121
122 /* The MinGW gcc does not define the S_ISSOCK macro. Any other native Windows
123 compiler like BorlandC or MSVC has none of these macros defined. */
124 #ifdef __MINGW32__
125
126 # ifdef _S_IFIFO
127 # undef _S_IFIFO
128 # endif
129 # ifdef _S_IFCHR
130 # undef _S_IFCHR
131 # endif
132 # ifdef _S_IFBLK
133 # undef _S_IFBLK
134 # endif
135 # ifdef _S_IFDIR
136 # undef _S_IFDIR
137 # endif
138 # ifdef _S_IFREG
139 # undef _S_IFREG
140 # endif
141 # ifdef _S_IFSOCK
142 # undef _S_IFSOCK
143 # endif
144
145 # define _S_IFIFO 0x1000 /* FIFO */
146 # define _S_IFCHR 0x2000 /* Character */
147 # define _S_IFBLK 0x3000 /* Block */
148 # define _S_IFDIR 0x4000 /* Directory */
149 # define _S_IFREG 0x8000 /* Regular */
150 # define _S_IFSOCK 0xC000 /* Socket */
151
152 # ifdef S_ISBLK
153 # undef S_ISBLK
154 # endif
155 # ifdef S_ISFIFO
156 # undef S_ISFIFO
157 # endif
158 # ifdef S_ISCHR
159 # undef S_ISCHR
160 # endif
161 # ifdef S_ISDIR
162 # undef S_ISDIR
163 # endif
164 # ifdef S_ISREG
165 # undef S_ISREG
166 # endif
167 # ifdef S_ISSOCK
168 # undef S_ISSOCK
169 # endif
170
171 # define S_ISBLK(mode) (((mode) & _S_IFMT) == _S_IFBLK)
172 # define S_ISFIFO(mode) (((mode) & _S_IFMT) == _S_IFIFO)
173 # define S_ISCHR(mode) (((mode) & _S_IFMT) == _S_IFCHR)
174 # define S_ISDIR(mode) (((mode) & _S_IFMT) == _S_IFDIR)
175 # define S_ISREG(mode) (((mode) & _S_IFMT) == _S_IFREG)
176 # define S_ISSOCK(mode) (((mode) & _S_IFMT) == _S_IFSOCK)
177
178 #endif /* __MINGW32__ */
179
180 /* Some more definitions for the native Windows port. */
181 #ifdef __MINGW32__
182 # define mkdir(path, mode) mkdir (path)
183 # define fsync(fd) _commit (fd)
184 # define fchmod(fd, mode) (-1)
185 #endif /* __MINGW32__ */
186
187 /* dirfd() returns the file descriptor underlying a "DIR*" directory stream.
188 Found on MacOS X for instance. The following definition is for Solaris
189 10, it's probably not right elsewhere, but that's ok, it shouldn't be
190 used elsewhere. Crib note: If we need more then gnulib has a dirfd.m4
191 figuring out how to get the fd (dirfd function, dirfd macro, dd_fd field,
192 or d_fd field). */
193 #ifndef dirfd
194 #define dirfd(dirstream) ((dirstream)->dd_fd)
195 #endif
196
197 \f
198
199 /* Two helper macros for an often used pattern */
200
201 #define STRING_SYSCALL(str,cstr,code) \
202 do { \
203 int eno; \
204 char *cstr = scm_to_locale_string (str); \
205 SCM_SYSCALL (code); \
206 eno = errno; free (cstr); errno = eno; \
207 } while (0)
208
209 #define STRING2_SYSCALL(str1,cstr1,str2,cstr2,code) \
210 do { \
211 int eno; \
212 char *cstr1, *cstr2; \
213 scm_dynwind_begin (0); \
214 cstr1 = scm_to_locale_string (str1); \
215 scm_dynwind_free (cstr1); \
216 cstr2 = scm_to_locale_string (str2); \
217 scm_dynwind_free (cstr2); \
218 SCM_SYSCALL (code); \
219 eno = errno; scm_dynwind_end (); errno = eno; \
220 } while (0)
221
222 \f
223
224 /* {Permissions}
225 */
226
227 #ifdef HAVE_CHOWN
228 SCM_DEFINE (scm_chown, "chown", 3, 0, 0,
229 (SCM object, SCM owner, SCM group),
230 "Change the ownership and group of the file referred to by @var{object} to\n"
231 "the integer values @var{owner} and @var{group}. @var{object} can be\n"
232 "a string containing a file name or, if the platform\n"
233 "supports fchown, a port or integer file descriptor\n"
234 "which is open on the file. The return value\n"
235 "is unspecified.\n\n"
236 "If @var{object} is a symbolic link, either the\n"
237 "ownership of the link or the ownership of the referenced file will be\n"
238 "changed depending on the operating system (lchown is\n"
239 "unsupported at present). If @var{owner} or @var{group} is specified\n"
240 "as @code{-1}, then that ID is not changed.")
241 #define FUNC_NAME s_scm_chown
242 {
243 int rv;
244
245 object = SCM_COERCE_OUTPORT (object);
246
247 #ifdef HAVE_FCHOWN
248 if (scm_is_integer (object) || (SCM_OPFPORTP (object)))
249 {
250 int fdes = (SCM_OPFPORTP (object)?
251 SCM_FPORT_FDES (object) : scm_to_int (object));
252
253 SCM_SYSCALL (rv = fchown (fdes, scm_to_int (owner), scm_to_int (group)));
254 }
255 else
256 #endif
257 {
258 STRING_SYSCALL (object, c_object,
259 rv = chown (c_object,
260 scm_to_int (owner), scm_to_int (group)));
261 }
262 if (rv == -1)
263 SCM_SYSERROR;
264 return SCM_UNSPECIFIED;
265 }
266 #undef FUNC_NAME
267 #endif /* HAVE_CHOWN */
268
269
270 SCM_DEFINE (scm_chmod, "chmod", 2, 0, 0,
271 (SCM object, SCM mode),
272 "Changes the permissions of the file referred to by @var{obj}.\n"
273 "@var{obj} can be a string containing a file name or a port or integer file\n"
274 "descriptor which is open on a file (in which case @code{fchmod} is used\n"
275 "as the underlying system call).\n"
276 "@var{mode} specifies\n"
277 "the new permissions as a decimal number, e.g., @code{(chmod \"foo\" #o755)}.\n"
278 "The return value is unspecified.")
279 #define FUNC_NAME s_scm_chmod
280 {
281 int rv;
282 int fdes;
283
284 object = SCM_COERCE_OUTPORT (object);
285
286 if (scm_is_integer (object) || SCM_OPFPORTP (object))
287 {
288 if (scm_is_integer (object))
289 fdes = scm_to_int (object);
290 else
291 fdes = SCM_FPORT_FDES (object);
292 SCM_SYSCALL (rv = fchmod (fdes, scm_to_int (mode)));
293 }
294 else
295 {
296 STRING_SYSCALL (object, c_object,
297 rv = chmod (c_object, scm_to_int (mode)));
298 }
299 if (rv == -1)
300 SCM_SYSERROR;
301 return SCM_UNSPECIFIED;
302 }
303 #undef FUNC_NAME
304
305 SCM_DEFINE (scm_umask, "umask", 0, 1, 0,
306 (SCM mode),
307 "If @var{mode} is omitted, returns a decimal number representing the current\n"
308 "file creation mask. Otherwise the file creation mask is set to\n"
309 "@var{mode} and the previous value is returned.\n\n"
310 "E.g., @code{(umask #o022)} sets the mask to octal 22, decimal 18.")
311 #define FUNC_NAME s_scm_umask
312 {
313 mode_t mask;
314 if (SCM_UNBNDP (mode))
315 {
316 mask = umask (0);
317 umask (mask);
318 }
319 else
320 {
321 mask = umask (scm_to_uint (mode));
322 }
323 return scm_from_uint (mask);
324 }
325 #undef FUNC_NAME
326
327 \f
328
329 SCM_DEFINE (scm_open_fdes, "open-fdes", 2, 1, 0,
330 (SCM path, SCM flags, SCM mode),
331 "Similar to @code{open} but return a file descriptor instead of\n"
332 "a port.")
333 #define FUNC_NAME s_scm_open_fdes
334 {
335 int fd;
336 int iflags;
337 int imode;
338
339 iflags = SCM_NUM2INT (2, flags);
340 imode = SCM_NUM2INT_DEF (3, mode, 0666);
341 STRING_SYSCALL (path, c_path, fd = open_or_open64 (c_path, iflags, imode));
342 if (fd == -1)
343 SCM_SYSERROR;
344 return scm_from_int (fd);
345 }
346 #undef FUNC_NAME
347
348 SCM_DEFINE (scm_open, "open", 2, 1, 0,
349 (SCM path, SCM flags, SCM mode),
350 "Open the file named by @var{path} for reading and/or writing.\n"
351 "@var{flags} is an integer specifying how the file should be opened.\n"
352 "@var{mode} is an integer specifying the permission bits of the file, if\n"
353 "it needs to be created, before the umask is applied. The default is 666\n"
354 "(Unix itself has no default).\n\n"
355 "@var{flags} can be constructed by combining variables using @code{logior}.\n"
356 "Basic flags are:\n\n"
357 "@defvar O_RDONLY\n"
358 "Open the file read-only.\n"
359 "@end defvar\n"
360 "@defvar O_WRONLY\n"
361 "Open the file write-only.\n"
362 "@end defvar\n"
363 "@defvar O_RDWR\n"
364 "Open the file read/write.\n"
365 "@end defvar\n"
366 "@defvar O_APPEND\n"
367 "Append to the file instead of truncating.\n"
368 "@end defvar\n"
369 "@defvar O_CREAT\n"
370 "Create the file if it does not already exist.\n"
371 "@end defvar\n\n"
372 "See the Unix documentation of the @code{open} system call\n"
373 "for additional flags.")
374 #define FUNC_NAME s_scm_open
375 {
376 SCM newpt;
377 char *port_mode;
378 int fd;
379 int iflags;
380
381 fd = scm_to_int (scm_open_fdes (path, flags, mode));
382 iflags = SCM_NUM2INT (2, flags);
383 if (iflags & O_RDWR)
384 {
385 if (iflags & O_APPEND)
386 port_mode = "a+";
387 else if (iflags & O_CREAT)
388 port_mode = "w+";
389 else
390 port_mode = "r+";
391 }
392 else {
393 if (iflags & O_APPEND)
394 port_mode = "a";
395 else if (iflags & O_WRONLY)
396 port_mode = "w";
397 else
398 port_mode = "r";
399 }
400 newpt = scm_fdes_to_port (fd, port_mode, path);
401 return newpt;
402 }
403 #undef FUNC_NAME
404
405 SCM_DEFINE (scm_close, "close", 1, 0, 0,
406 (SCM fd_or_port),
407 "Similar to close-port (@pxref{Closing, close-port}),\n"
408 "but also works on file descriptors. A side\n"
409 "effect of closing a file descriptor is that any ports using that file\n"
410 "descriptor are moved to a different file descriptor and have\n"
411 "their revealed counts set to zero.")
412 #define FUNC_NAME s_scm_close
413 {
414 int rv;
415 int fd;
416
417 fd_or_port = SCM_COERCE_OUTPORT (fd_or_port);
418
419 if (SCM_PORTP (fd_or_port))
420 return scm_close_port (fd_or_port);
421 fd = scm_to_int (fd_or_port);
422 scm_evict_ports (fd); /* see scsh manual. */
423 SCM_SYSCALL (rv = close (fd));
424 /* following scsh, closing an already closed file descriptor is
425 not an error. */
426 if (rv < 0 && errno != EBADF)
427 SCM_SYSERROR;
428 return scm_from_bool (rv >= 0);
429 }
430 #undef FUNC_NAME
431
432 SCM_DEFINE (scm_close_fdes, "close-fdes", 1, 0, 0,
433 (SCM fd),
434 "A simple wrapper for the @code{close} system call.\n"
435 "Close file descriptor @var{fd}, which must be an integer.\n"
436 "Unlike close (@pxref{Ports and File Descriptors, close}),\n"
437 "the file descriptor will be closed even if a port is using it.\n"
438 "The return value is unspecified.")
439 #define FUNC_NAME s_scm_close_fdes
440 {
441 int c_fd;
442 int rv;
443
444 c_fd = scm_to_int (fd);
445 SCM_SYSCALL (rv = close (c_fd));
446 if (rv < 0)
447 SCM_SYSERROR;
448 return SCM_UNSPECIFIED;
449 }
450 #undef FUNC_NAME
451
452 \f
453 /* {Files}
454 */
455
456 SCM_SYMBOL (scm_sym_regular, "regular");
457 SCM_SYMBOL (scm_sym_directory, "directory");
458 #ifdef S_ISLNK
459 SCM_SYMBOL (scm_sym_symlink, "symlink");
460 #endif
461 SCM_SYMBOL (scm_sym_block_special, "block-special");
462 SCM_SYMBOL (scm_sym_char_special, "char-special");
463 SCM_SYMBOL (scm_sym_fifo, "fifo");
464 SCM_SYMBOL (scm_sym_sock, "socket");
465 SCM_SYMBOL (scm_sym_unknown, "unknown");
466
467 static SCM
468 scm_stat2scm (struct stat_or_stat64 *stat_temp)
469 {
470 SCM ans = scm_c_make_vector (15, SCM_UNSPECIFIED);
471
472 SCM_SIMPLE_VECTOR_SET(ans, 0, scm_from_ulong (stat_temp->st_dev));
473 SCM_SIMPLE_VECTOR_SET(ans, 1, scm_from_ino_t_or_ino64_t (stat_temp->st_ino));
474 SCM_SIMPLE_VECTOR_SET(ans, 2, scm_from_ulong (stat_temp->st_mode));
475 SCM_SIMPLE_VECTOR_SET(ans, 3, scm_from_ulong (stat_temp->st_nlink));
476 SCM_SIMPLE_VECTOR_SET(ans, 4, scm_from_ulong (stat_temp->st_uid));
477 SCM_SIMPLE_VECTOR_SET(ans, 5, scm_from_ulong (stat_temp->st_gid));
478 #ifdef HAVE_STRUCT_STAT_ST_RDEV
479 SCM_SIMPLE_VECTOR_SET(ans, 6, scm_from_ulong (stat_temp->st_rdev));
480 #else
481 SCM_SIMPLE_VECTOR_SET(ans, 6, SCM_BOOL_F);
482 #endif
483 SCM_SIMPLE_VECTOR_SET(ans, 7, scm_from_off_t_or_off64_t (stat_temp->st_size));
484 SCM_SIMPLE_VECTOR_SET(ans, 8, scm_from_ulong (stat_temp->st_atime));
485 SCM_SIMPLE_VECTOR_SET(ans, 9, scm_from_ulong (stat_temp->st_mtime));
486 SCM_SIMPLE_VECTOR_SET(ans, 10, scm_from_ulong (stat_temp->st_ctime));
487 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
488 SCM_SIMPLE_VECTOR_SET(ans, 11, scm_from_ulong (stat_temp->st_blksize));
489 #else
490 SCM_SIMPLE_VECTOR_SET(ans, 11, scm_from_ulong (4096L));
491 #endif
492 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
493 SCM_SIMPLE_VECTOR_SET(ans, 12, scm_from_blkcnt_t_or_blkcnt64_t (stat_temp->st_blocks));
494 #else
495 SCM_SIMPLE_VECTOR_SET(ans, 12, SCM_BOOL_F);
496 #endif
497 {
498 int mode = stat_temp->st_mode;
499
500 if (S_ISREG (mode))
501 SCM_SIMPLE_VECTOR_SET(ans, 13, scm_sym_regular);
502 else if (S_ISDIR (mode))
503 SCM_SIMPLE_VECTOR_SET(ans, 13, scm_sym_directory);
504 #ifdef S_ISLNK
505 /* systems without symlinks probably don't have S_ISLNK */
506 else if (S_ISLNK (mode))
507 SCM_SIMPLE_VECTOR_SET(ans, 13, scm_sym_symlink);
508 #endif
509 else if (S_ISBLK (mode))
510 SCM_SIMPLE_VECTOR_SET(ans, 13, scm_sym_block_special);
511 else if (S_ISCHR (mode))
512 SCM_SIMPLE_VECTOR_SET(ans, 13, scm_sym_char_special);
513 else if (S_ISFIFO (mode))
514 SCM_SIMPLE_VECTOR_SET(ans, 13, scm_sym_fifo);
515 #ifdef S_ISSOCK
516 else if (S_ISSOCK (mode))
517 SCM_SIMPLE_VECTOR_SET(ans, 13, scm_sym_sock);
518 #endif
519 else
520 SCM_SIMPLE_VECTOR_SET(ans, 13, scm_sym_unknown);
521
522 SCM_SIMPLE_VECTOR_SET(ans, 14, scm_from_int ((~S_IFMT) & mode));
523
524 /* the layout of the bits in ve[14] is intended to be portable.
525 If there are systems that don't follow the usual convention,
526 the following could be used:
527
528 tmp = 0;
529 if (S_ISUID & mode) tmp += 1;
530 tmp <<= 1;
531 if (S_IRGRP & mode) tmp += 1;
532 tmp <<= 1;
533 if (S_ISVTX & mode) tmp += 1;
534 tmp <<= 1;
535 if (S_IRUSR & mode) tmp += 1;
536 tmp <<= 1;
537 if (S_IWUSR & mode) tmp += 1;
538 tmp <<= 1;
539 if (S_IXUSR & mode) tmp += 1;
540 tmp <<= 1;
541 if (S_IWGRP & mode) tmp += 1;
542 tmp <<= 1;
543 if (S_IXGRP & mode) tmp += 1;
544 tmp <<= 1;
545 if (S_IROTH & mode) tmp += 1;
546 tmp <<= 1;
547 if (S_IWOTH & mode) tmp += 1;
548 tmp <<= 1;
549 if (S_IXOTH & mode) tmp += 1;
550
551 SCM_SIMPLE_VECTOR_SET(ans, 14, scm_from_int (tmp));
552
553 */
554 }
555
556 return ans;
557 }
558
559 #ifdef __MINGW32__
560 /*
561 * Try getting the appropiate stat buffer for a given file descriptor
562 * under Windows. It differentiates between file, pipe and socket
563 * descriptors.
564 */
565 static int fstat_Win32 (int fdes, struct stat *buf)
566 {
567 int error, optlen = sizeof (int);
568
569 memset (buf, 0, sizeof (struct stat));
570
571 /* Is this a socket ? */
572 if (getsockopt (fdes, SOL_SOCKET, SO_ERROR, (void *) &error, &optlen) >= 0)
573 {
574 buf->st_mode = _S_IFSOCK | _S_IREAD | _S_IWRITE | _S_IEXEC;
575 buf->st_nlink = 1;
576 buf->st_atime = buf->st_ctime = buf->st_mtime = time (NULL);
577 return 0;
578 }
579 /* Maybe a regular file or pipe ? */
580 return fstat (fdes, buf);
581 }
582 #endif /* __MINGW32__ */
583
584 SCM_DEFINE (scm_stat, "stat", 1, 0, 0,
585 (SCM object),
586 "Return an object containing various information about the file\n"
587 "determined by @var{obj}. @var{obj} can be a string containing\n"
588 "a file name or a port or integer file descriptor which is open\n"
589 "on a file (in which case @code{fstat} is used as the underlying\n"
590 "system call).\n"
591 "\n"
592 "The object returned by @code{stat} can be passed as a single\n"
593 "parameter to the following procedures, all of which return\n"
594 "integers:\n"
595 "\n"
596 "@table @code\n"
597 "@item stat:dev\n"
598 "The device containing the file.\n"
599 "@item stat:ino\n"
600 "The file serial number, which distinguishes this file from all\n"
601 "other files on the same device.\n"
602 "@item stat:mode\n"
603 "The mode of the file. This includes file type information and\n"
604 "the file permission bits. See @code{stat:type} and\n"
605 "@code{stat:perms} below.\n"
606 "@item stat:nlink\n"
607 "The number of hard links to the file.\n"
608 "@item stat:uid\n"
609 "The user ID of the file's owner.\n"
610 "@item stat:gid\n"
611 "The group ID of the file.\n"
612 "@item stat:rdev\n"
613 "Device ID; this entry is defined only for character or block\n"
614 "special files.\n"
615 "@item stat:size\n"
616 "The size of a regular file in bytes.\n"
617 "@item stat:atime\n"
618 "The last access time for the file.\n"
619 "@item stat:mtime\n"
620 "The last modification time for the file.\n"
621 "@item stat:ctime\n"
622 "The last modification time for the attributes of the file.\n"
623 "@item stat:blksize\n"
624 "The optimal block size for reading or writing the file, in\n"
625 "bytes.\n"
626 "@item stat:blocks\n"
627 "The amount of disk space that the file occupies measured in\n"
628 "units of 512 byte blocks.\n"
629 "@end table\n"
630 "\n"
631 "In addition, the following procedures return the information\n"
632 "from stat:mode in a more convenient form:\n"
633 "\n"
634 "@table @code\n"
635 "@item stat:type\n"
636 "A symbol representing the type of file. Possible values are\n"
637 "regular, directory, symlink, block-special, char-special, fifo,\n"
638 "socket and unknown\n"
639 "@item stat:perms\n"
640 "An integer representing the access permission bits.\n"
641 "@end table")
642 #define FUNC_NAME s_scm_stat
643 {
644 int rv;
645 int fdes;
646 struct stat_or_stat64 stat_temp;
647
648 if (scm_is_integer (object))
649 {
650 #ifdef __MINGW32__
651 SCM_SYSCALL (rv = fstat_Win32 (scm_to_int (object), &stat_temp));
652 #else
653 SCM_SYSCALL (rv = fstat_or_fstat64 (scm_to_int (object), &stat_temp));
654 #endif
655 }
656 else if (scm_is_string (object))
657 {
658 char *file = scm_to_locale_string (object);
659 #ifdef __MINGW32__
660 char *p;
661 p = file + strlen (file) - 1;
662 while (p > file && (*p == '/' || *p == '\\'))
663 *p-- = '\0';
664 #endif
665 SCM_SYSCALL (rv = stat_or_stat64 (file, &stat_temp));
666 free (file);
667 }
668 else
669 {
670 object = SCM_COERCE_OUTPORT (object);
671 SCM_VALIDATE_OPFPORT (1, object);
672 fdes = SCM_FPORT_FDES (object);
673 #ifdef __MINGW32__
674 SCM_SYSCALL (rv = fstat_Win32 (fdes, &stat_temp));
675 #else
676 SCM_SYSCALL (rv = fstat_or_fstat64 (fdes, &stat_temp));
677 #endif
678 }
679
680 if (rv == -1)
681 {
682 int en = errno;
683
684 SCM_SYSERROR_MSG ("~A: ~S",
685 scm_list_2 (scm_strerror (scm_from_int (en)),
686 object),
687 en);
688 }
689 return scm_stat2scm (&stat_temp);
690 }
691 #undef FUNC_NAME
692
693 \f
694 /* {Modifying Directories}
695 */
696
697 #ifdef HAVE_LINK
698 SCM_DEFINE (scm_link, "link", 2, 0, 0,
699 (SCM oldpath, SCM newpath),
700 "Creates a new name @var{newpath} in the file system for the\n"
701 "file named by @var{oldpath}. If @var{oldpath} is a symbolic\n"
702 "link, the link may or may not be followed depending on the\n"
703 "system.")
704 #define FUNC_NAME s_scm_link
705 {
706 int val;
707
708 STRING2_SYSCALL (oldpath, c_oldpath,
709 newpath, c_newpath,
710 val = link (c_oldpath, c_newpath));
711 if (val != 0)
712 SCM_SYSERROR;
713 return SCM_UNSPECIFIED;
714 }
715 #undef FUNC_NAME
716 #endif /* HAVE_LINK */
717
718 #ifdef HAVE_RENAME
719 #define my_rename rename
720 #else
721 static int
722 my_rename (const char *oldname, const char *newname)
723 {
724 int rv;
725
726 SCM_SYSCALL (rv = link (oldname, newname));
727 if (rv == 0)
728 {
729 SCM_SYSCALL (rv = unlink (oldname));
730 if (rv != 0)
731 /* unlink failed. remove new name */
732 SCM_SYSCALL (unlink (newname));
733 }
734 return rv;
735 }
736 #endif
737
738 SCM_DEFINE (scm_rename, "rename-file", 2, 0, 0,
739 (SCM oldname, SCM newname),
740 "Renames the file specified by @var{oldname} to @var{newname}.\n"
741 "The return value is unspecified.")
742 #define FUNC_NAME s_scm_rename
743 {
744 int rv;
745
746 STRING2_SYSCALL (oldname, c_oldname,
747 newname, c_newname,
748 rv = my_rename (c_oldname, c_newname));
749 if (rv != 0)
750 SCM_SYSERROR;
751 return SCM_UNSPECIFIED;
752 }
753 #undef FUNC_NAME
754
755
756 SCM_DEFINE (scm_delete_file, "delete-file", 1, 0, 0,
757 (SCM str),
758 "Deletes (or \"unlinks\") the file specified by @var{path}.")
759 #define FUNC_NAME s_scm_delete_file
760 {
761 int ans;
762 STRING_SYSCALL (str, c_str, ans = unlink (c_str));
763 if (ans != 0)
764 SCM_SYSERROR;
765 return SCM_UNSPECIFIED;
766 }
767 #undef FUNC_NAME
768
769 #ifdef HAVE_MKDIR
770 SCM_DEFINE (scm_mkdir, "mkdir", 1, 1, 0,
771 (SCM path, SCM mode),
772 "Create a new directory named by @var{path}. If @var{mode} is omitted\n"
773 "then the permissions of the directory file are set using the current\n"
774 "umask. Otherwise they are set to the decimal value specified with\n"
775 "@var{mode}. The return value is unspecified.")
776 #define FUNC_NAME s_scm_mkdir
777 {
778 int rv;
779 mode_t mask;
780
781 if (SCM_UNBNDP (mode))
782 {
783 mask = umask (0);
784 umask (mask);
785 STRING_SYSCALL (path, c_path, rv = mkdir (c_path, 0777 ^ mask));
786 }
787 else
788 {
789 STRING_SYSCALL (path, c_path, rv = mkdir (c_path, scm_to_uint (mode)));
790 }
791 if (rv != 0)
792 SCM_SYSERROR;
793 return SCM_UNSPECIFIED;
794 }
795 #undef FUNC_NAME
796 #endif /* HAVE_MKDIR */
797
798 #ifdef HAVE_RMDIR
799 SCM_DEFINE (scm_rmdir, "rmdir", 1, 0, 0,
800 (SCM path),
801 "Remove the existing directory named by @var{path}. The directory must\n"
802 "be empty for this to succeed. The return value is unspecified.")
803 #define FUNC_NAME s_scm_rmdir
804 {
805 int val;
806
807 STRING_SYSCALL (path, c_path, val = rmdir (c_path));
808 if (val != 0)
809 SCM_SYSERROR;
810 return SCM_UNSPECIFIED;
811 }
812 #undef FUNC_NAME
813 #endif
814
815 \f
816
817 /* {Examining Directories}
818 */
819
820 scm_t_bits scm_tc16_dir;
821
822
823 SCM_DEFINE (scm_directory_stream_p, "directory-stream?", 1, 0, 0,
824 (SCM obj),
825 "Return a boolean indicating whether @var{object} is a directory\n"
826 "stream as returned by @code{opendir}.")
827 #define FUNC_NAME s_scm_directory_stream_p
828 {
829 return scm_from_bool (SCM_DIRP (obj));
830 }
831 #undef FUNC_NAME
832
833
834 SCM_DEFINE (scm_opendir, "opendir", 1, 0, 0,
835 (SCM dirname),
836 "Open the directory specified by @var{path} and return a directory\n"
837 "stream.")
838 #define FUNC_NAME s_scm_opendir
839 {
840 DIR *ds;
841 STRING_SYSCALL (dirname, c_dirname, ds = opendir (c_dirname));
842 if (ds == NULL)
843 SCM_SYSERROR;
844 SCM_RETURN_NEWSMOB (scm_tc16_dir | SCM_DIR_FLAG_OPEN, ds);
845 }
846 #undef FUNC_NAME
847
848
849 /* FIXME: The glibc manual has a portability note that readdir_r may not
850 null-terminate its return string. The circumstances outlined for this
851 are not clear, nor is it clear what should be done about it. Lets use
852 NAMLEN and worry about what else should be done if/when someone can
853 figure it out. */
854
855 SCM_DEFINE (scm_readdir, "readdir", 1, 0, 0,
856 (SCM port),
857 "Return (as a string) the next directory entry from the directory stream\n"
858 "@var{stream}. If there is no remaining entry to be read then the\n"
859 "end of file object is returned.")
860 #define FUNC_NAME s_scm_readdir
861 {
862 struct dirent_or_dirent64 *rdent;
863
864 SCM_VALIDATE_DIR (1, port);
865 if (!SCM_DIR_OPEN_P (port))
866 SCM_MISC_ERROR ("Directory ~S is not open.", scm_list_1 (port));
867
868 #if HAVE_READDIR_R
869 /* As noted in the glibc manual, on various systems (such as Solaris) the
870 d_name[] field is only 1 char and you're expected to size the dirent
871 buffer for readdir_r based on NAME_MAX. The SCM_MAX expressions below
872 effectively give either sizeof(d_name) or NAME_MAX+1, whichever is
873 bigger.
874
875 On solaris 10 there's no NAME_MAX constant, it's necessary to use
876 pathconf(). We prefer NAME_MAX though, since it should be a constant
877 and will therefore save a system call. We also prefer it since dirfd()
878 is not available everywhere.
879
880 An alternative to dirfd() would be to open() the directory and then use
881 fdopendir(), if the latter is available. That'd let us hold the fd
882 somewhere in the smob, or just the dirent size calculated once. */
883 {
884 struct dirent_or_dirent64 de; /* just for sizeof */
885 DIR *ds = (DIR *) SCM_CELL_WORD_1 (port);
886 size_t namlen;
887 #ifdef NAME_MAX
888 char buf [SCM_MAX (sizeof (de),
889 sizeof (de) - sizeof (de.d_name) + NAME_MAX + 1)];
890 #else
891 char *buf;
892 long name_max = fpathconf (dirfd (ds), _PC_NAME_MAX);
893 if (name_max == -1)
894 SCM_SYSERROR;
895 buf = alloca (SCM_MAX (sizeof (de),
896 sizeof (de) - sizeof (de.d_name) + name_max + 1));
897 #endif
898
899 errno = 0;
900 SCM_SYSCALL (readdir_r_or_readdir64_r (ds, (struct dirent_or_dirent64 *) buf, &rdent));
901 if (errno != 0)
902 SCM_SYSERROR;
903 if (! rdent)
904 return SCM_EOF_VAL;
905
906 namlen = NAMLEN (rdent);
907
908 return (rdent ? scm_from_locale_stringn (rdent->d_name, NAMLEN (rdent))
909 : SCM_EOF_VAL);
910 }
911 #else
912 {
913 SCM ret;
914 scm_dynwind_begin (0);
915 scm_i_dynwind_pthread_mutex_lock (&scm_i_misc_mutex);
916
917 errno = 0;
918 SCM_SYSCALL (rdent = readdir_or_readdir64 ((DIR *) SCM_CELL_WORD_1 (port)));
919 if (errno != 0)
920 SCM_SYSERROR;
921
922 ret = (rdent ? scm_from_locale_stringn (rdent->d_name, NAMLEN (rdent))
923 : SCM_EOF_VAL);
924
925 scm_dynwind_end ();
926 return ret;
927 }
928 #endif
929 }
930 #undef FUNC_NAME
931
932
933 SCM_DEFINE (scm_rewinddir, "rewinddir", 1, 0, 0,
934 (SCM port),
935 "Reset the directory port @var{stream} so that the next call to\n"
936 "@code{readdir} will return the first directory entry.")
937 #define FUNC_NAME s_scm_rewinddir
938 {
939 SCM_VALIDATE_DIR (1, port);
940 if (!SCM_DIR_OPEN_P (port))
941 SCM_MISC_ERROR ("Directory ~S is not open.", scm_list_1 (port));
942
943 rewinddir ((DIR *) SCM_CELL_WORD_1 (port));
944
945 return SCM_UNSPECIFIED;
946 }
947 #undef FUNC_NAME
948
949
950 SCM_DEFINE (scm_closedir, "closedir", 1, 0, 0,
951 (SCM port),
952 "Close the directory stream @var{stream}.\n"
953 "The return value is unspecified.")
954 #define FUNC_NAME s_scm_closedir
955 {
956 SCM_VALIDATE_DIR (1, port);
957
958 if (SCM_DIR_OPEN_P (port))
959 {
960 int sts;
961
962 SCM_SYSCALL (sts = closedir ((DIR *) SCM_CELL_WORD_1 (port)));
963 if (sts != 0)
964 SCM_SYSERROR;
965
966 SCM_SET_CELL_WORD_0 (port, scm_tc16_dir);
967 }
968
969 return SCM_UNSPECIFIED;
970 }
971 #undef FUNC_NAME
972
973
974 static int
975 scm_dir_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
976 {
977 scm_puts ("#<", port);
978 if (!SCM_DIR_OPEN_P (exp))
979 scm_puts ("closed: ", port);
980 scm_puts ("directory stream ", port);
981 scm_uintprint (SCM_CELL_WORD_1 (exp), 16, port);
982 scm_putc ('>', port);
983 return 1;
984 }
985
986
987 static size_t
988 scm_dir_free (SCM p)
989 {
990 if (SCM_DIR_OPEN_P (p))
991 closedir ((DIR *) SCM_CELL_WORD_1 (p));
992 return 0;
993 }
994
995 \f
996 /* {Navigating Directories}
997 */
998
999
1000 SCM_DEFINE (scm_chdir, "chdir", 1, 0, 0,
1001 (SCM str),
1002 "Change the current working directory to @var{path}.\n"
1003 "The return value is unspecified.")
1004 #define FUNC_NAME s_scm_chdir
1005 {
1006 int ans;
1007
1008 STRING_SYSCALL (str, c_str, ans = chdir (c_str));
1009 if (ans != 0)
1010 SCM_SYSERROR;
1011 return SCM_UNSPECIFIED;
1012 }
1013 #undef FUNC_NAME
1014
1015 #ifdef HAVE_GETCWD
1016 SCM_DEFINE (scm_getcwd, "getcwd", 0, 0, 0,
1017 (),
1018 "Return the name of the current working directory.")
1019 #define FUNC_NAME s_scm_getcwd
1020 {
1021 char *rv;
1022 size_t size = 100;
1023 char *wd;
1024 SCM result;
1025
1026 wd = scm_malloc (size);
1027 while ((rv = getcwd (wd, size)) == 0 && errno == ERANGE)
1028 {
1029 free (wd);
1030 size *= 2;
1031 wd = scm_malloc (size);
1032 }
1033 if (rv == 0)
1034 {
1035 int save_errno = errno;
1036 free (wd);
1037 errno = save_errno;
1038 SCM_SYSERROR;
1039 }
1040 result = scm_from_locale_stringn (wd, strlen (wd));
1041 free (wd);
1042 return result;
1043 }
1044 #undef FUNC_NAME
1045 #endif /* HAVE_GETCWD */
1046
1047 \f
1048
1049 #ifdef HAVE_SELECT
1050
1051 /* check that element is a port or file descriptor. if it's a port
1052 and its buffer is ready for use, add it to the ports_ready list.
1053 otherwise add its file descriptor to *set. the type of list can be
1054 determined from pos: SCM_ARG1 for reads, SCM_ARG2 for writes,
1055 SCM_ARG3 for excepts. */
1056 static int
1057 set_element (SELECT_TYPE *set, SCM *ports_ready, SCM element, int pos)
1058 {
1059 int fd;
1060
1061 if (scm_is_integer (element))
1062 {
1063 fd = scm_to_int (element);
1064 }
1065 else
1066 {
1067 int use_buf = 0;
1068
1069 element = SCM_COERCE_OUTPORT (element);
1070 SCM_ASSERT (SCM_OPFPORTP (element), element, pos, "select");
1071 if (pos == SCM_ARG1)
1072 {
1073 /* check whether port has buffered input. */
1074 scm_t_port *pt = SCM_PTAB_ENTRY (element);
1075
1076 if (pt->read_pos < pt->read_end)
1077 use_buf = 1;
1078 }
1079 else if (pos == SCM_ARG2)
1080 {
1081 /* check whether port's output buffer has room. */
1082 scm_t_port *pt = SCM_PTAB_ENTRY (element);
1083
1084 /* > 1 since writing the last byte in the buffer causes flush. */
1085 if (pt->write_end - pt->write_pos > 1)
1086 use_buf = 1;
1087 }
1088 fd = use_buf ? -1 : SCM_FPORT_FDES (element);
1089 }
1090 if (fd == -1)
1091 *ports_ready = scm_cons (element, *ports_ready);
1092 else
1093 FD_SET (fd, set);
1094 return fd;
1095 }
1096
1097 /* check list_or_vec, a list or vector of ports or file descriptors,
1098 adding each member to either the ports_ready list (if it's a port
1099 with a usable buffer) or to *set. the kind of list_or_vec can be
1100 determined from pos: SCM_ARG1 for reads, SCM_ARG2 for writes,
1101 SCM_ARG3 for excepts. */
1102 static int
1103 fill_select_type (SELECT_TYPE *set, SCM *ports_ready, SCM list_or_vec, int pos)
1104 {
1105 int max_fd = 0;
1106
1107 if (scm_is_simple_vector (list_or_vec))
1108 {
1109 int i = SCM_SIMPLE_VECTOR_LENGTH (list_or_vec);
1110
1111 while (--i >= 0)
1112 {
1113 int fd = set_element (set, ports_ready,
1114 SCM_SIMPLE_VECTOR_REF (list_or_vec, i), pos);
1115
1116 if (fd > max_fd)
1117 max_fd = fd;
1118 }
1119 }
1120 else
1121 {
1122 while (!SCM_NULL_OR_NIL_P (list_or_vec))
1123 {
1124 int fd = set_element (set, ports_ready, SCM_CAR (list_or_vec), pos);
1125
1126 if (fd > max_fd)
1127 max_fd = fd;
1128 list_or_vec = SCM_CDR (list_or_vec);
1129 }
1130 }
1131
1132 return max_fd;
1133 }
1134
1135 /* if element (a file descriptor or port) appears in *set, cons it to
1136 list. return list. */
1137 static SCM
1138 get_element (SELECT_TYPE *set, SCM element, SCM list)
1139 {
1140 int fd;
1141
1142 if (scm_is_integer (element))
1143 {
1144 fd = scm_to_int (element);
1145 }
1146 else
1147 {
1148 fd = SCM_FPORT_FDES (SCM_COERCE_OUTPORT (element));
1149 }
1150 if (FD_ISSET (fd, set))
1151 list = scm_cons (element, list);
1152 return list;
1153 }
1154
1155 /* construct component of scm_select return value.
1156 set: pointer to set of file descriptors found by select to be ready
1157 ports_ready: ports ready due to buffering
1158 list_or_vec: original list/vector handed to scm_select.
1159 the return value is a list/vector of ready ports/file descriptors.
1160 works by finding the objects in list which correspond to members of
1161 *set and appending them to ports_ready. result is converted to a
1162 vector if list_or_vec is a vector. */
1163 static SCM
1164 retrieve_select_type (SELECT_TYPE *set, SCM ports_ready, SCM list_or_vec)
1165 {
1166 SCM answer_list = ports_ready;
1167
1168 if (scm_is_simple_vector (list_or_vec))
1169 {
1170 int i = SCM_SIMPLE_VECTOR_LENGTH (list_or_vec);
1171
1172 while (--i >= 0)
1173 {
1174 answer_list = get_element (set,
1175 SCM_SIMPLE_VECTOR_REF (list_or_vec, i),
1176 answer_list);
1177 }
1178 return scm_vector (answer_list);
1179 }
1180 else
1181 {
1182 /* list_or_vec must be a list. */
1183 while (!SCM_NULL_OR_NIL_P (list_or_vec))
1184 {
1185 answer_list = get_element (set, SCM_CAR (list_or_vec), answer_list);
1186 list_or_vec = SCM_CDR (list_or_vec);
1187 }
1188 return answer_list;
1189 }
1190 }
1191
1192 /* Static helper functions above refer to s_scm_select directly as s_select */
1193 SCM_DEFINE (scm_select, "select", 3, 2, 0,
1194 (SCM reads, SCM writes, SCM excepts, SCM secs, SCM usecs),
1195 "This procedure has a variety of uses: waiting for the ability\n"
1196 "to provide input, accept output, or the existence of\n"
1197 "exceptional conditions on a collection of ports or file\n"
1198 "descriptors, or waiting for a timeout to occur.\n"
1199 "It also returns if interrupted by a signal.\n\n"
1200 "@var{reads}, @var{writes} and @var{excepts} can be lists or\n"
1201 "vectors, with each member a port or a file descriptor.\n"
1202 "The value returned is a list of three corresponding\n"
1203 "lists or vectors containing only the members which meet the\n"
1204 "specified requirement. The ability of port buffers to\n"
1205 "provide input or accept output is taken into account.\n"
1206 "Ordering of the input lists or vectors is not preserved.\n\n"
1207 "The optional arguments @var{secs} and @var{usecs} specify the\n"
1208 "timeout. Either @var{secs} can be specified alone, as\n"
1209 "either an integer or a real number, or both @var{secs} and\n"
1210 "@var{usecs} can be specified as integers, in which case\n"
1211 "@var{usecs} is an additional timeout expressed in\n"
1212 "microseconds. If @var{secs} is omitted or is @code{#f} then\n"
1213 "select will wait for as long as it takes for one of the other\n"
1214 "conditions to be satisfied.\n\n"
1215 "The scsh version of @code{select} differs as follows:\n"
1216 "Only vectors are accepted for the first three arguments.\n"
1217 "The @var{usecs} argument is not supported.\n"
1218 "Multiple values are returned instead of a list.\n"
1219 "Duplicates in the input vectors appear only once in output.\n"
1220 "An additional @code{select!} interface is provided.")
1221 #define FUNC_NAME s_scm_select
1222 {
1223 struct timeval timeout;
1224 struct timeval * time_ptr;
1225 SELECT_TYPE read_set;
1226 SELECT_TYPE write_set;
1227 SELECT_TYPE except_set;
1228 int read_count;
1229 int write_count;
1230 int except_count;
1231 /* these lists accumulate ports which are ready due to buffering.
1232 their file descriptors don't need to be added to the select sets. */
1233 SCM read_ports_ready = SCM_EOL;
1234 SCM write_ports_ready = SCM_EOL;
1235 int max_fd;
1236
1237 if (scm_is_simple_vector (reads))
1238 {
1239 read_count = SCM_SIMPLE_VECTOR_LENGTH (reads);
1240 }
1241 else
1242 {
1243 read_count = scm_ilength (reads);
1244 SCM_ASSERT (read_count >= 0, reads, SCM_ARG1, FUNC_NAME);
1245 }
1246 if (scm_is_simple_vector (writes))
1247 {
1248 write_count = SCM_SIMPLE_VECTOR_LENGTH (writes);
1249 }
1250 else
1251 {
1252 write_count = scm_ilength (writes);
1253 SCM_ASSERT (write_count >= 0, writes, SCM_ARG2, FUNC_NAME);
1254 }
1255 if (scm_is_simple_vector (excepts))
1256 {
1257 except_count = SCM_SIMPLE_VECTOR_LENGTH (excepts);
1258 }
1259 else
1260 {
1261 except_count = scm_ilength (excepts);
1262 SCM_ASSERT (except_count >= 0, excepts, SCM_ARG3, FUNC_NAME);
1263 }
1264
1265 FD_ZERO (&read_set);
1266 FD_ZERO (&write_set);
1267 FD_ZERO (&except_set);
1268
1269 max_fd = fill_select_type (&read_set, &read_ports_ready, reads, SCM_ARG1);
1270
1271 {
1272 int write_max = fill_select_type (&write_set, &write_ports_ready,
1273 writes, SCM_ARG2);
1274 int except_max = fill_select_type (&except_set, NULL,
1275 excepts, SCM_ARG3);
1276
1277 if (write_max > max_fd)
1278 max_fd = write_max;
1279 if (except_max > max_fd)
1280 max_fd = except_max;
1281 }
1282
1283 /* if there's a port with a ready buffer, don't block, just
1284 check for ready file descriptors. */
1285 if (!scm_is_null (read_ports_ready) || !scm_is_null (write_ports_ready))
1286 {
1287 timeout.tv_sec = 0;
1288 timeout.tv_usec = 0;
1289 time_ptr = &timeout;
1290 }
1291 else if (SCM_UNBNDP (secs) || scm_is_false (secs))
1292 time_ptr = 0;
1293 else
1294 {
1295 if (scm_is_unsigned_integer (secs, 0, ULONG_MAX))
1296 {
1297 timeout.tv_sec = scm_to_ulong (secs);
1298 if (SCM_UNBNDP (usecs))
1299 timeout.tv_usec = 0;
1300 else
1301 timeout.tv_usec = scm_to_long (usecs);
1302 }
1303 else
1304 {
1305 double fl = scm_to_double (secs);
1306
1307 if (!SCM_UNBNDP (usecs))
1308 SCM_WRONG_TYPE_ARG (4, secs);
1309 if (fl > LONG_MAX)
1310 SCM_OUT_OF_RANGE (4, secs);
1311 timeout.tv_sec = (long) fl;
1312 timeout.tv_usec = (long) ((fl - timeout.tv_sec) * 1000000);
1313 }
1314 time_ptr = &timeout;
1315 }
1316
1317 {
1318 int rv = scm_std_select (max_fd + 1,
1319 &read_set, &write_set, &except_set,
1320 time_ptr);
1321 if (rv < 0)
1322 SCM_SYSERROR;
1323 }
1324 return scm_list_3 (retrieve_select_type (&read_set, read_ports_ready, reads),
1325 retrieve_select_type (&write_set, write_ports_ready, writes),
1326 retrieve_select_type (&except_set, SCM_EOL, excepts));
1327 }
1328 #undef FUNC_NAME
1329 #endif /* HAVE_SELECT */
1330
1331 \f
1332
1333 #ifdef HAVE_FCNTL
1334 SCM_DEFINE (scm_fcntl, "fcntl", 2, 1, 0,
1335 (SCM object, SCM cmd, SCM value),
1336 "Apply @var{command} to the specified file descriptor or the underlying\n"
1337 "file descriptor of the specified port. @var{value} is an optional\n"
1338 "integer argument.\n\n"
1339 "Values for @var{command} are:\n\n"
1340 "@table @code\n"
1341 "@item F_DUPFD\n"
1342 "Duplicate a file descriptor\n"
1343 "@item F_GETFD\n"
1344 "Get flags associated with the file descriptor.\n"
1345 "@item F_SETFD\n"
1346 "Set flags associated with the file descriptor to @var{value}.\n"
1347 "@item F_GETFL\n"
1348 "Get flags associated with the open file.\n"
1349 "@item F_SETFL\n"
1350 "Set flags associated with the open file to @var{value}\n"
1351 "@item F_GETOWN\n"
1352 "Get the process ID of a socket's owner, for @code{SIGIO} signals.\n"
1353 "@item F_SETOWN\n"
1354 "Set the process that owns a socket to @var{value}, for @code{SIGIO} signals.\n"
1355 "@item FD_CLOEXEC\n"
1356 "The value used to indicate the \"close on exec\" flag with @code{F_GETFL} or\n"
1357 "@code{F_SETFL}.\n"
1358 "@end table")
1359 #define FUNC_NAME s_scm_fcntl
1360 {
1361 int rv;
1362 int fdes;
1363 int ivalue;
1364
1365 object = SCM_COERCE_OUTPORT (object);
1366
1367 if (SCM_OPFPORTP (object))
1368 fdes = SCM_FPORT_FDES (object);
1369 else
1370 fdes = scm_to_int (object);
1371
1372 if (SCM_UNBNDP (value))
1373 ivalue = 0;
1374 else
1375 ivalue = scm_to_int (value);
1376
1377 SCM_SYSCALL (rv = fcntl (fdes, scm_to_int (cmd), ivalue));
1378 if (rv == -1)
1379 SCM_SYSERROR;
1380 return scm_from_int (rv);
1381 }
1382 #undef FUNC_NAME
1383 #endif /* HAVE_FCNTL */
1384
1385 SCM_DEFINE (scm_fsync, "fsync", 1, 0, 0,
1386 (SCM object),
1387 "Copies any unwritten data for the specified output file descriptor to disk.\n"
1388 "If @var{port/fd} is a port, its buffer is flushed before the underlying\n"
1389 "file descriptor is fsync'd.\n"
1390 "The return value is unspecified.")
1391 #define FUNC_NAME s_scm_fsync
1392 {
1393 int fdes;
1394
1395 object = SCM_COERCE_OUTPORT (object);
1396
1397 if (SCM_OPFPORTP (object))
1398 {
1399 scm_flush (object);
1400 fdes = SCM_FPORT_FDES (object);
1401 }
1402 else
1403 fdes = scm_to_int (object);
1404
1405 if (fsync (fdes) == -1)
1406 SCM_SYSERROR;
1407 return SCM_UNSPECIFIED;
1408 }
1409 #undef FUNC_NAME
1410
1411 #ifdef HAVE_SYMLINK
1412 SCM_DEFINE (scm_symlink, "symlink", 2, 0, 0,
1413 (SCM oldpath, SCM newpath),
1414 "Create a symbolic link named @var{path-to} with the value (i.e., pointing to)\n"
1415 "@var{path-from}. The return value is unspecified.")
1416 #define FUNC_NAME s_scm_symlink
1417 {
1418 int val;
1419
1420 STRING2_SYSCALL (oldpath, c_oldpath,
1421 newpath, c_newpath,
1422 val = symlink (c_oldpath, c_newpath));
1423 if (val != 0)
1424 SCM_SYSERROR;
1425 return SCM_UNSPECIFIED;
1426 }
1427 #undef FUNC_NAME
1428 #endif /* HAVE_SYMLINK */
1429
1430 #ifdef HAVE_READLINK
1431 SCM_DEFINE (scm_readlink, "readlink", 1, 0, 0,
1432 (SCM path),
1433 "Return the value of the symbolic link named by @var{path} (a\n"
1434 "string), i.e., the file that the link points to.")
1435 #define FUNC_NAME s_scm_readlink
1436 {
1437 int rv;
1438 int size = 100;
1439 char *buf;
1440 SCM result;
1441 char *c_path;
1442
1443 scm_dynwind_begin (0);
1444
1445 c_path = scm_to_locale_string (path);
1446 scm_dynwind_free (c_path);
1447
1448 buf = scm_malloc (size);
1449
1450 while ((rv = readlink (c_path, buf, size)) == size)
1451 {
1452 free (buf);
1453 size *= 2;
1454 buf = scm_malloc (size);
1455 }
1456 if (rv == -1)
1457 {
1458 int save_errno = errno;
1459 free (buf);
1460 errno = save_errno;
1461 SCM_SYSERROR;
1462 }
1463 result = scm_take_locale_stringn (buf, rv);
1464
1465 scm_dynwind_end ();
1466 return result;
1467 }
1468 #undef FUNC_NAME
1469 #endif /* HAVE_READLINK */
1470
1471 #ifdef HAVE_LSTAT
1472 SCM_DEFINE (scm_lstat, "lstat", 1, 0, 0,
1473 (SCM str),
1474 "Similar to @code{stat}, but does not follow symbolic links, i.e.,\n"
1475 "it will return information about a symbolic link itself, not the\n"
1476 "file it points to. @var{path} must be a string.")
1477 #define FUNC_NAME s_scm_lstat
1478 {
1479 int rv;
1480 struct stat_or_stat64 stat_temp;
1481
1482 STRING_SYSCALL (str, c_str, rv = lstat_or_lstat64 (c_str, &stat_temp));
1483 if (rv != 0)
1484 {
1485 int en = errno;
1486
1487 SCM_SYSERROR_MSG ("~A: ~S",
1488 scm_list_2 (scm_strerror (scm_from_int (en)), str),
1489 en);
1490 }
1491 return scm_stat2scm (&stat_temp);
1492 }
1493 #undef FUNC_NAME
1494 #endif /* HAVE_LSTAT */
1495
1496 SCM_DEFINE (scm_copy_file, "copy-file", 2, 0, 0,
1497 (SCM oldfile, SCM newfile),
1498 "Copy the file specified by @var{path-from} to @var{path-to}.\n"
1499 "The return value is unspecified.")
1500 #define FUNC_NAME s_scm_copy_file
1501 {
1502 char *c_oldfile, *c_newfile;
1503 int oldfd, newfd;
1504 int n, rv;
1505 char buf[BUFSIZ];
1506 struct stat_or_stat64 oldstat;
1507
1508 scm_dynwind_begin (0);
1509
1510 c_oldfile = scm_to_locale_string (oldfile);
1511 scm_dynwind_free (c_oldfile);
1512 c_newfile = scm_to_locale_string (newfile);
1513 scm_dynwind_free (c_newfile);
1514
1515 oldfd = open_or_open64 (c_oldfile, O_RDONLY);
1516 if (oldfd == -1)
1517 SCM_SYSERROR;
1518
1519 #ifdef __MINGW32__
1520 SCM_SYSCALL (rv = fstat_Win32 (oldfd, &oldstat));
1521 #else
1522 SCM_SYSCALL (rv = fstat_or_fstat64 (oldfd, &oldstat));
1523 #endif
1524 if (rv == -1)
1525 goto err_close_oldfd;
1526
1527 /* use POSIX flags instead of 07777?. */
1528 newfd = open_or_open64 (c_newfile, O_WRONLY | O_CREAT | O_TRUNC,
1529 oldstat.st_mode & 07777);
1530 if (newfd == -1)
1531 {
1532 err_close_oldfd:
1533 close (oldfd);
1534 SCM_SYSERROR;
1535 }
1536
1537 while ((n = read (oldfd, buf, sizeof buf)) > 0)
1538 if (write (newfd, buf, n) != n)
1539 {
1540 close (oldfd);
1541 close (newfd);
1542 SCM_SYSERROR;
1543 }
1544 close (oldfd);
1545 if (close (newfd) == -1)
1546 SCM_SYSERROR;
1547
1548 scm_dynwind_end ();
1549 return SCM_UNSPECIFIED;
1550 }
1551 #undef FUNC_NAME
1552
1553 \f
1554 /* Filename manipulation */
1555
1556 SCM scm_dot_string;
1557
1558 SCM_DEFINE (scm_dirname, "dirname", 1, 0, 0,
1559 (SCM filename),
1560 "Return the directory name component of the file name\n"
1561 "@var{filename}. If @var{filename} does not contain a directory\n"
1562 "component, @code{.} is returned.")
1563 #define FUNC_NAME s_scm_dirname
1564 {
1565 const char *s;
1566 long int i;
1567 unsigned long int len;
1568
1569 SCM_VALIDATE_STRING (1, filename);
1570
1571 s = scm_i_string_chars (filename);
1572 len = scm_i_string_length (filename);
1573
1574 i = len - 1;
1575 #ifdef __MINGW32__
1576 while (i >= 0 && (s[i] == '/' || s[i] == '\\')) --i;
1577 while (i >= 0 && (s[i] != '/' && s[i] != '\\')) --i;
1578 while (i >= 0 && (s[i] == '/' || s[i] == '\\')) --i;
1579 #else
1580 while (i >= 0 && s[i] == '/') --i;
1581 while (i >= 0 && s[i] != '/') --i;
1582 while (i >= 0 && s[i] == '/') --i;
1583 #endif /* ndef __MINGW32__ */
1584 if (i < 0)
1585 {
1586 #ifdef __MINGW32__
1587 if (len > 0 && (s[0] == '/' || s[0] == '\\'))
1588 #else
1589 if (len > 0 && s[0] == '/')
1590 #endif /* ndef __MINGW32__ */
1591 return scm_c_substring (filename, 0, 1);
1592 else
1593 return scm_dot_string;
1594 }
1595 else
1596 return scm_c_substring (filename, 0, i + 1);
1597 }
1598 #undef FUNC_NAME
1599
1600 SCM_DEFINE (scm_basename, "basename", 1, 1, 0,
1601 (SCM filename, SCM suffix),
1602 "Return the base name of the file name @var{filename}. The\n"
1603 "base name is the file name without any directory components.\n"
1604 "If @var{suffix} is provided, and is equal to the end of\n"
1605 "@var{basename}, it is removed also.")
1606 #define FUNC_NAME s_scm_basename
1607 {
1608 const char *f, *s = 0;
1609 int i, j, len, end;
1610
1611 SCM_VALIDATE_STRING (1, filename);
1612 f = scm_i_string_chars (filename);
1613 len = scm_i_string_length (filename);
1614
1615 if (SCM_UNBNDP (suffix))
1616 j = -1;
1617 else
1618 {
1619 SCM_VALIDATE_STRING (2, suffix);
1620 s = scm_i_string_chars (suffix);
1621 j = scm_i_string_length (suffix) - 1;
1622 }
1623 i = len - 1;
1624 #ifdef __MINGW32__
1625 while (i >= 0 && (f[i] == '/' || f[i] == '\\')) --i;
1626 #else
1627 while (i >= 0 && f[i] == '/') --i;
1628 #endif /* ndef __MINGW32__ */
1629 end = i;
1630 while (i >= 0 && j >= 0 && f[i] == s[j]) --i, --j;
1631 if (j == -1)
1632 end = i;
1633 #ifdef __MINGW32__
1634 while (i >= 0 && f[i] != '/' && f[i] != '\\') --i;
1635 #else
1636 while (i >= 0 && f[i] != '/') --i;
1637 #endif /* ndef __MINGW32__ */
1638 if (i == end)
1639 {
1640 #ifdef __MINGW32__
1641 if (len > 0 && (f[0] == '/' || f[0] == '\\'))
1642 #else
1643 if (len > 0 && f[0] == '/')
1644 #endif /* ndef __MINGW32__ */
1645 return scm_c_substring (filename, 0, 1);
1646 else
1647 return scm_dot_string;
1648 }
1649 else
1650 return scm_c_substring (filename, i+1, end+1);
1651 }
1652 #undef FUNC_NAME
1653
1654
1655
1656 \f
1657
1658 void
1659 scm_init_filesys ()
1660 {
1661 scm_tc16_dir = scm_make_smob_type ("directory", 0);
1662 scm_set_smob_free (scm_tc16_dir, scm_dir_free);
1663 scm_set_smob_print (scm_tc16_dir, scm_dir_print);
1664
1665 scm_dot_string = scm_permanent_object (scm_from_locale_string ("."));
1666
1667 #ifdef O_RDONLY
1668 scm_c_define ("O_RDONLY", scm_from_int (O_RDONLY));
1669 #endif
1670 #ifdef O_WRONLY
1671 scm_c_define ("O_WRONLY", scm_from_int (O_WRONLY));
1672 #endif
1673 #ifdef O_RDWR
1674 scm_c_define ("O_RDWR", scm_from_int (O_RDWR));
1675 #endif
1676 #ifdef O_CREAT
1677 scm_c_define ("O_CREAT", scm_from_int (O_CREAT));
1678 #endif
1679 #ifdef O_EXCL
1680 scm_c_define ("O_EXCL", scm_from_int (O_EXCL));
1681 #endif
1682 #ifdef O_NOCTTY
1683 scm_c_define ("O_NOCTTY", scm_from_int (O_NOCTTY));
1684 #endif
1685 #ifdef O_TRUNC
1686 scm_c_define ("O_TRUNC", scm_from_int (O_TRUNC));
1687 #endif
1688 #ifdef O_APPEND
1689 scm_c_define ("O_APPEND", scm_from_int (O_APPEND));
1690 #endif
1691 #ifdef O_NONBLOCK
1692 scm_c_define ("O_NONBLOCK", scm_from_int (O_NONBLOCK));
1693 #endif
1694 #ifdef O_NDELAY
1695 scm_c_define ("O_NDELAY", scm_from_int (O_NDELAY));
1696 #endif
1697 #ifdef O_SYNC
1698 scm_c_define ("O_SYNC", scm_from_int (O_SYNC));
1699 #endif
1700 #ifdef O_LARGEFILE
1701 scm_c_define ("O_LARGEFILE", scm_from_int (O_LARGEFILE));
1702 #endif
1703
1704 #ifdef F_DUPFD
1705 scm_c_define ("F_DUPFD", scm_from_int (F_DUPFD));
1706 #endif
1707 #ifdef F_GETFD
1708 scm_c_define ("F_GETFD", scm_from_int (F_GETFD));
1709 #endif
1710 #ifdef F_SETFD
1711 scm_c_define ("F_SETFD", scm_from_int (F_SETFD));
1712 #endif
1713 #ifdef F_GETFL
1714 scm_c_define ("F_GETFL", scm_from_int (F_GETFL));
1715 #endif
1716 #ifdef F_SETFL
1717 scm_c_define ("F_SETFL", scm_from_int (F_SETFL));
1718 #endif
1719 #ifdef F_GETOWN
1720 scm_c_define ("F_GETOWN", scm_from_int (F_GETOWN));
1721 #endif
1722 #ifdef F_SETOWN
1723 scm_c_define ("F_SETOWN", scm_from_int (F_SETOWN));
1724 #endif
1725 #ifdef FD_CLOEXEC
1726 scm_c_define ("FD_CLOEXEC", scm_from_int (FD_CLOEXEC));
1727 #endif
1728
1729 #include "libguile/filesys.x"
1730 }
1731
1732 /*
1733 Local Variables:
1734 c-file-style: "gnu"
1735 End:
1736 */