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