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