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