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