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