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