Fix problems with Windows file names that use backslashes.
[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
GH
773 "exceptional conditions on a collection of ports or file\n"
774 "descriptors, or waiting for a timeout to occur.\n"
775 "It also returns if interrupted by a signal.\n\n"
776 "@var{reads}, @var{writes} and @var{excepts} can be lists or\n"
777 "vectors, with each member a port or a file descriptor.\n"
778 "The value returned is a list of three corresponding\n"
779 "lists or vectors containing only the members which meet the\n"
780 "specified requirement. The ability of port buffers to\n"
781 "provide input or accept output is taken into account.\n"
782 "Ordering of the input lists or vectors is not preserved.\n\n"
783 "The optional arguments @var{secs} and @var{usecs} specify the\n"
784 "timeout. Either @var{secs} can be specified alone, as\n"
785 "either an integer or a real number, or both @var{secs} and\n"
786 "@var{usecs} can be specified as integers, in which case\n"
787 "@var{usecs} is an additional timeout expressed in\n"
788 "microseconds. If @var{secs} is omitted or is @code{#f} then\n"
789 "select will wait for as long as it takes for one of the other\n"
790 "conditions to be satisfied.\n\n"
791 "The scsh version of @code{select} differs as follows:\n"
792 "Only vectors are accepted for the first three arguments.\n"
793 "The @var{usecs} argument is not supported.\n"
794 "Multiple values are returned instead of a list.\n"
795 "Duplicates in the input vectors appear only once in output.\n"
9401323e 796 "An additional @code{select!} interface is provided.")
1bbd0b84 797#define FUNC_NAME s_scm_select
0f2d19dd 798{
0f2d19dd 799 struct timeval timeout;
28d77376 800 struct timeval * time_ptr;
ba59471e
AW
801 fd_set read_set;
802 fd_set write_set;
803 fd_set except_set;
28d77376
GH
804 int read_count;
805 int write_count;
806 int except_count;
807 /* these lists accumulate ports which are ready due to buffering.
808 their file descriptors don't need to be added to the select sets. */
809 SCM read_ports_ready = SCM_EOL;
810 SCM write_ports_ready = SCM_EOL;
811 int max_fd;
812
4057a3e0 813 if (scm_is_simple_vector (reads))
28d77376 814 {
4057a3e0 815 read_count = SCM_SIMPLE_VECTOR_LENGTH (reads);
28d77376
GH
816 }
817 else
818 {
819 read_count = scm_ilength (reads);
820 SCM_ASSERT (read_count >= 0, reads, SCM_ARG1, FUNC_NAME);
821 }
4057a3e0 822 if (scm_is_simple_vector (writes))
28d77376 823 {
4057a3e0 824 write_count = SCM_SIMPLE_VECTOR_LENGTH (writes);
28d77376
GH
825 }
826 else
827 {
828 write_count = scm_ilength (writes);
829 SCM_ASSERT (write_count >= 0, writes, SCM_ARG2, FUNC_NAME);
830 }
4057a3e0 831 if (scm_is_simple_vector (excepts))
28d77376 832 {
4057a3e0 833 except_count = SCM_SIMPLE_VECTOR_LENGTH (excepts);
28d77376
GH
834 }
835 else
836 {
837 except_count = scm_ilength (excepts);
838 SCM_ASSERT (except_count >= 0, excepts, SCM_ARG3, FUNC_NAME);
839 }
0f2d19dd
JB
840
841 FD_ZERO (&read_set);
842 FD_ZERO (&write_set);
843 FD_ZERO (&except_set);
844
28d77376
GH
845 max_fd = fill_select_type (&read_set, &read_ports_ready, reads, SCM_ARG1);
846
847 {
848 int write_max = fill_select_type (&write_set, &write_ports_ready,
849 writes, SCM_ARG2);
850 int except_max = fill_select_type (&except_set, NULL,
851 excepts, SCM_ARG3);
852
853 if (write_max > max_fd)
854 max_fd = write_max;
855 if (except_max > max_fd)
856 max_fd = except_max;
857 }
0f2d19dd 858
ae1b098b
GH
859 /* if there's a port with a ready buffer, don't block, just
860 check for ready file descriptors. */
d2e53ed6 861 if (!scm_is_null (read_ports_ready) || !scm_is_null (write_ports_ready))
ae1b098b
GH
862 {
863 timeout.tv_sec = 0;
864 timeout.tv_usec = 0;
865 time_ptr = &timeout;
866 }
7888309b 867 else if (SCM_UNBNDP (secs) || scm_is_false (secs))
28d77376 868 time_ptr = 0;
0f2d19dd
JB
869 else
870 {
a55c2b68 871 if (scm_is_unsigned_integer (secs, 0, ULONG_MAX))
a48a89bc 872 {
a55c2b68 873 timeout.tv_sec = scm_to_ulong (secs);
a48a89bc
GH
874 if (SCM_UNBNDP (usecs))
875 timeout.tv_usec = 0;
876 else
a55c2b68 877 timeout.tv_usec = scm_to_long (usecs);
a48a89bc 878 }
0f2d19dd 879 else
a48a89bc 880 {
d9a67fc4 881 double fl = scm_to_double (secs);
a48a89bc
GH
882
883 if (!SCM_UNBNDP (usecs))
c1bfcf60 884 SCM_WRONG_TYPE_ARG (4, secs);
a48a89bc 885 if (fl > LONG_MAX)
c1bfcf60 886 SCM_OUT_OF_RANGE (4, secs);
a48a89bc
GH
887 timeout.tv_sec = (long) fl;
888 timeout.tv_usec = (long) ((fl - timeout.tv_sec) * 1000000);
889 }
28d77376 890 time_ptr = &timeout;
0f2d19dd
JB
891 }
892
28d77376 893 {
ba59471e
AW
894 int rv = select (max_fd + 1,
895 &read_set, &write_set, &except_set,
896 time_ptr);
28d77376
GH
897 if (rv < 0)
898 SCM_SYSERROR;
899 }
1afff620
KN
900 return scm_list_3 (retrieve_select_type (&read_set, read_ports_ready, reads),
901 retrieve_select_type (&write_set, write_ports_ready, writes),
902 retrieve_select_type (&except_set, SCM_EOL, excepts));
0f2d19dd 903}
1bbd0b84 904#undef FUNC_NAME
0f2d19dd
JB
905
906\f
4c1feaa5 907
82893676 908#ifdef HAVE_FCNTL
af45e3b0 909SCM_DEFINE (scm_fcntl, "fcntl", 2, 1, 0,
1bbd0b84 910 (SCM object, SCM cmd, SCM value),
b7e64f8b 911 "Apply @var{cmd} to the specified file descriptor or the underlying\n"
d3818c29
MD
912 "file descriptor of the specified port. @var{value} is an optional\n"
913 "integer argument.\n\n"
b7e64f8b 914 "Values for @var{cmd} are:\n\n"
d3818c29
MD
915 "@table @code\n"
916 "@item F_DUPFD\n"
917 "Duplicate a file descriptor\n"
918 "@item F_GETFD\n"
919 "Get flags associated with the file descriptor.\n"
920 "@item F_SETFD\n"
921 "Set flags associated with the file descriptor to @var{value}.\n"
922 "@item F_GETFL\n"
923 "Get flags associated with the open file.\n"
924 "@item F_SETFL\n"
925 "Set flags associated with the open file to @var{value}\n"
926 "@item F_GETOWN\n"
927 "Get the process ID of a socket's owner, for @code{SIGIO} signals.\n"
928 "@item F_SETOWN\n"
929 "Set the process that owns a socket to @var{value}, for @code{SIGIO} signals.\n"
930 "@item FD_CLOEXEC\n"
55892d87
NJ
931 "The value used to indicate the \"close on exec\" flag with @code{F_GETFL} or\n"
932 "@code{F_SETFL}.\n"
a3c8b9fc 933 "@end table")
1bbd0b84 934#define FUNC_NAME s_scm_fcntl
4c1feaa5
JB
935{
936 int rv;
6afcd3b2
GH
937 int fdes;
938 int ivalue;
4c1feaa5 939
78446828
MV
940 object = SCM_COERCE_OUTPORT (object);
941
0c95b57d 942 if (SCM_OPFPORTP (object))
77a76b64 943 fdes = SCM_FPORT_FDES (object);
6afcd3b2 944 else
a55c2b68 945 fdes = scm_to_int (object);
af45e3b0 946
a55c2b68 947 if (SCM_UNBNDP (value))
6afcd3b2 948 ivalue = 0;
a55c2b68
MV
949 else
950 ivalue = scm_to_int (value);
af45e3b0 951
a55c2b68 952 SCM_SYSCALL (rv = fcntl (fdes, scm_to_int (cmd), ivalue));
77a76b64 953 if (rv == -1)
1bbd0b84 954 SCM_SYSERROR;
a55c2b68 955 return scm_from_int (rv);
4c1feaa5 956}
1bbd0b84 957#undef FUNC_NAME
82893676 958#endif /* HAVE_FCNTL */
6afcd3b2 959
a1ec6916 960SCM_DEFINE (scm_fsync, "fsync", 1, 0, 0,
1bbd0b84 961 (SCM object),
b7e64f8b
BT
962 "Copies any unwritten data for the specified output file\n"
963 "descriptor to disk. If @var{object} is a port, its buffer is\n"
964 "flushed before the underlying file descriptor is fsync'd.\n"
d3818c29 965 "The return value is unspecified.")
1bbd0b84 966#define FUNC_NAME s_scm_fsync
6afcd3b2
GH
967{
968 int fdes;
969
78446828
MV
970 object = SCM_COERCE_OUTPORT (object);
971
0c95b57d 972 if (SCM_OPFPORTP (object))
6afcd3b2 973 {
affc96b5 974 scm_flush (object);
77a76b64 975 fdes = SCM_FPORT_FDES (object);
6afcd3b2
GH
976 }
977 else
a55c2b68
MV
978 fdes = scm_to_int (object);
979
6afcd3b2 980 if (fsync (fdes) == -1)
1bbd0b84 981 SCM_SYSERROR;
6afcd3b2
GH
982 return SCM_UNSPECIFIED;
983}
1bbd0b84 984#undef FUNC_NAME
0f2d19dd 985
f25f761d 986#ifdef HAVE_SYMLINK
a1ec6916 987SCM_DEFINE (scm_symlink, "symlink", 2, 0, 0,
1bbd0b84 988 (SCM oldpath, SCM newpath),
b7e64f8b
BT
989 "Create a symbolic link named @var{oldpath} with the value\n"
990 "(i.e., pointing to) @var{newpath}. The return value is\n"
991 "unspecified.")
1bbd0b84 992#define FUNC_NAME s_scm_symlink
0f2d19dd 993{
0f2d19dd 994 int val;
02b754d3 995
1299a0f1
MV
996 STRING2_SYSCALL (oldpath, c_oldpath,
997 newpath, c_newpath,
998 val = symlink (c_oldpath, c_newpath));
02b754d3 999 if (val != 0)
1bbd0b84 1000 SCM_SYSERROR;
02b754d3 1001 return SCM_UNSPECIFIED;
0f2d19dd 1002}
1bbd0b84 1003#undef FUNC_NAME
f25f761d 1004#endif /* HAVE_SYMLINK */
0f2d19dd 1005
a1ec6916 1006SCM_DEFINE (scm_readlink, "readlink", 1, 0, 0,
1bbd0b84 1007 (SCM path),
1e6808ea
MG
1008 "Return the value of the symbolic link named by @var{path} (a\n"
1009 "string), i.e., the file that the link points to.")
1bbd0b84 1010#define FUNC_NAME s_scm_readlink
0f2d19dd 1011{
6a738a25
JB
1012 int rv;
1013 int size = 100;
0f2d19dd
JB
1014 char *buf;
1015 SCM result;
1299a0f1
MV
1016 char *c_path;
1017
661ae7ab 1018 scm_dynwind_begin (0);
1299a0f1
MV
1019
1020 c_path = scm_to_locale_string (path);
661ae7ab 1021 scm_dynwind_free (c_path);
1299a0f1 1022
4c9419ac 1023 buf = scm_malloc (size);
1299a0f1
MV
1024
1025 while ((rv = readlink (c_path, buf, size)) == size)
0f2d19dd 1026 {
4c9419ac 1027 free (buf);
0f2d19dd 1028 size *= 2;
4c9419ac 1029 buf = scm_malloc (size);
0f2d19dd 1030 }
02b754d3 1031 if (rv == -1)
11e1db06
KR
1032 {
1033 int save_errno = errno;
1034 free (buf);
1035 errno = save_errno;
1036 SCM_SYSERROR;
1037 }
1299a0f1
MV
1038 result = scm_take_locale_stringn (buf, rv);
1039
661ae7ab 1040 scm_dynwind_end ();
0f2d19dd 1041 return result;
0f2d19dd 1042}
1bbd0b84 1043#undef FUNC_NAME
0f2d19dd 1044
a1ec6916 1045SCM_DEFINE (scm_copy_file, "copy-file", 2, 0, 0,
1bbd0b84 1046 (SCM oldfile, SCM newfile),
b7e64f8b 1047 "Copy the file specified by @var{oldfile} to @var{newfile}.\n"
d3818c29 1048 "The return value is unspecified.")
1bbd0b84 1049#define FUNC_NAME s_scm_copy_file
0f2d19dd 1050{
1299a0f1 1051 char *c_oldfile, *c_newfile;
0f2d19dd 1052 int oldfd, newfd;
37eb673b 1053 int n, rv;
77a76b64 1054 char buf[BUFSIZ];
2b829bbb 1055 struct stat_or_stat64 oldstat;
0f2d19dd 1056
661ae7ab 1057 scm_dynwind_begin (0);
1299a0f1
MV
1058
1059 c_oldfile = scm_to_locale_string (oldfile);
661ae7ab 1060 scm_dynwind_free (c_oldfile);
1299a0f1 1061 c_newfile = scm_to_locale_string (newfile);
661ae7ab 1062 scm_dynwind_free (c_newfile);
37eb673b 1063
5a4a4454 1064 oldfd = open_or_open64 (c_oldfile, O_RDONLY | O_BINARY);
0f2d19dd 1065 if (oldfd == -1)
1bbd0b84 1066 SCM_SYSERROR;
02b754d3 1067
2b829bbb 1068 SCM_SYSCALL (rv = fstat_or_fstat64 (oldfd, &oldstat));
37eb673b
KR
1069 if (rv == -1)
1070 goto err_close_oldfd;
1071
02b754d3 1072 /* use POSIX flags instead of 07777?. */
2b829bbb
KR
1073 newfd = open_or_open64 (c_newfile, O_WRONLY | O_CREAT | O_TRUNC,
1074 oldstat.st_mode & 07777);
0f2d19dd 1075 if (newfd == -1)
01046395 1076 {
37eb673b 1077 err_close_oldfd:
01046395
KR
1078 close (oldfd);
1079 SCM_SYSERROR;
1080 }
02b754d3 1081
0f2d19dd
JB
1082 while ((n = read (oldfd, buf, sizeof buf)) > 0)
1083 if (write (newfd, buf, n) != n)
1084 {
1085 close (oldfd);
1086 close (newfd);
1bbd0b84 1087 SCM_SYSERROR;
0f2d19dd
JB
1088 }
1089 close (oldfd);
1090 if (close (newfd) == -1)
1bbd0b84 1091 SCM_SYSERROR;
1299a0f1 1092
661ae7ab 1093 scm_dynwind_end ();
02b754d3 1094 return SCM_UNSPECIFIED;
0f2d19dd 1095}
1bbd0b84 1096#undef FUNC_NAME
0f2d19dd 1097
fbac7c61
LC
1098SCM_DEFINE (scm_sendfile, "sendfile", 3, 1, 0,
1099 (SCM out, SCM in, SCM count, SCM offset),
1100 "Send @var{count} bytes from @var{in} to @var{out}, both of which "
e0886e07 1101 "must be either open file ports or file descriptors. When "
fbac7c61 1102 "@var{offset} is omitted, start reading from @var{in}'s current "
e0886e07
LC
1103 "position; otherwise, start reading at @var{offset}. Return "
1104 "the number of bytes actually sent.")
fbac7c61
LC
1105#define FUNC_NAME s_scm_sendfile
1106{
1107#define VALIDATE_FD_OR_PORT(cvar, svar, pos) \
1108 if (scm_is_integer (svar)) \
1109 cvar = scm_to_int (svar); \
1110 else \
1111 { \
1112 SCM_VALIDATE_OPFPORT (pos, svar); \
1113 scm_flush (svar); \
1114 cvar = SCM_FPORT_FDES (svar); \
1115 }
1116
e0886e07
LC
1117 ssize_t result SCM_UNUSED;
1118 size_t c_count, total = 0;
fbac7c61 1119 scm_t_off c_offset;
fbac7c61
LC
1120 int in_fd, out_fd;
1121
1122 VALIDATE_FD_OR_PORT (out_fd, out, 1);
1123 VALIDATE_FD_OR_PORT (in_fd, in, 2);
1124 c_count = scm_to_size_t (count);
1125 c_offset = SCM_UNBNDP (offset) ? 0 : scm_to_off_t (offset);
1126
11ed4277
LC
1127#if defined HAVE_SYS_SENDFILE_H && defined HAVE_SENDFILE
1128 /* The Linux-style sendfile(2), which is different from the BSD-style. */
1129
e0886e07
LC
1130 {
1131 off_t *offset_ptr;
1132
1133 offset_ptr = SCM_UNBNDP (offset) ? NULL : &c_offset;
1134
1135 /* On Linux, when OUT_FD is a file, everything is transferred at once and
1136 RESULT == C_COUNT. However, when OUT_FD is a pipe or other "slow"
1137 device, fewer bytes may be transferred, hence the loop. RESULT == 0
1138 means EOF on IN_FD, so leave the loop in that case. */
1139 do
1140 {
1141 result = sendfile_or_sendfile64 (out_fd, in_fd, offset_ptr,
1142 c_count - total);
1143 if (result > 0)
1144 /* At this point, either OFFSET_PTR is non-NULL and it has been
1145 updated to the current offset in IN_FD, or it is NULL and IN_FD's
1146 offset has been updated. */
1147 total += result;
1148 else if (result < 0 && (errno == EINTR || errno == EAGAIN))
1149 /* Keep going. */
7f3be1db 1150 result = 1;
e0886e07
LC
1151 }
1152 while (total < c_count && result > 0);
1153 }
fbac7c61
LC
1154
1155 /* Quoting the Linux man page: "In Linux kernels before 2.6.33, out_fd
1156 must refer to a socket. Since Linux 2.6.33 it can be any file."
1157 Fall back to read(2) and write(2) when such an error occurs. */
1158 if (result < 0 && errno != EINVAL && errno != ENOSYS)
1159 SCM_SYSERROR;
1160 else if (result < 0)
1161#endif
1162 {
1163 char buf[8192];
e0886e07 1164 size_t left;
7f3be1db 1165 int reached_eof = 0;
fbac7c61
LC
1166
1167 if (!SCM_UNBNDP (offset))
1168 {
1169 if (SCM_PORTP (in))
e0886e07 1170 scm_seek (in, scm_from_off_t (c_offset), scm_from_int (SEEK_SET));
fbac7c61 1171 else
f28885f4
LC
1172 {
1173 if (lseek_or_lseek64 (in_fd, c_offset, SEEK_SET) < 0)
1174 SCM_SYSERROR;
1175 }
fbac7c61
LC
1176 }
1177
7f3be1db 1178 for (total = 0, left = c_count; total < c_count && !reached_eof; )
fbac7c61 1179 {
7f3be1db 1180 size_t asked, obtained, written;
fbac7c61
LC
1181
1182 asked = SCM_MIN (sizeof buf, left);
1183 obtained = full_read (in_fd, buf, asked);
1184 if (obtained < asked)
7f3be1db
MW
1185 {
1186 if (errno == 0)
1187 reached_eof = 1;
1188 else
1189 SCM_SYSERROR;
1190 }
fbac7c61
LC
1191
1192 left -= obtained;
1193
7f3be1db
MW
1194 written = full_write (out_fd, buf, obtained);
1195 if (written < obtained)
fbac7c61
LC
1196 SCM_SYSERROR;
1197
7f3be1db 1198 total += written;
fbac7c61
LC
1199 }
1200
fbac7c61
LC
1201 }
1202
e0886e07 1203 return scm_from_size_t (total);
fbac7c61
LC
1204
1205#undef VALIDATE_FD_OR_PORT
1206}
1207#undef FUNC_NAME
1208
073167ef
LC
1209#endif /* HAVE_POSIX */
1210
1211\f
1212/* Essential procedures used in (system base compile). */
1213
1214#ifdef HAVE_GETCWD
1215SCM_DEFINE (scm_getcwd, "getcwd", 0, 0, 0,
1216 (),
1217 "Return the name of the current working directory.")
1218#define FUNC_NAME s_scm_getcwd
1219{
1220 char *rv;
1221 size_t size = 100;
1222 char *wd;
1223 SCM result;
1224
1225 wd = scm_malloc (size);
1226 while ((rv = getcwd (wd, size)) == 0 && errno == ERANGE)
1227 {
1228 free (wd);
1229 size *= 2;
1230 wd = scm_malloc (size);
1231 }
1232 if (rv == 0)
1233 {
1234 int save_errno = errno;
1235 free (wd);
1236 errno = save_errno;
1237 SCM_SYSERROR;
1238 }
9235f805
EZ
1239 /* On Windows, convert backslashes in current directory to forward
1240 slashes. */
1241 scm_i_mirror_backslashes (wd);
073167ef
LC
1242 result = scm_from_locale_stringn (wd, strlen (wd));
1243 free (wd);
1244 return result;
1245}
1246#undef FUNC_NAME
1247#endif /* HAVE_GETCWD */
1248
073167ef
LC
1249SCM_DEFINE (scm_mkdir, "mkdir", 1, 1, 0,
1250 (SCM path, SCM mode),
1251 "Create a new directory named by @var{path}. If @var{mode} is omitted\n"
1252 "then the permissions of the directory file are set using the current\n"
1253 "umask. Otherwise they are set to the decimal value specified with\n"
1254 "@var{mode}. The return value is unspecified.")
1255#define FUNC_NAME s_scm_mkdir
1256{
1257 int rv;
1258 mode_t mask;
1259
1260 if (SCM_UNBNDP (mode))
1261 {
1262 mask = umask (0);
1263 umask (mask);
1264 STRING_SYSCALL (path, c_path, rv = mkdir (c_path, 0777 ^ mask));
1265 }
1266 else
1267 {
1268 STRING_SYSCALL (path, c_path, rv = mkdir (c_path, scm_to_uint (mode)));
1269 }
1270 if (rv != 0)
1271 SCM_SYSERROR;
1272 return SCM_UNSPECIFIED;
1273}
1274#undef FUNC_NAME
073167ef 1275
073167ef
LC
1276SCM_DEFINE (scm_rmdir, "rmdir", 1, 0, 0,
1277 (SCM path),
1278 "Remove the existing directory named by @var{path}. The directory must\n"
1279 "be empty for this to succeed. The return value is unspecified.")
1280#define FUNC_NAME s_scm_rmdir
1281{
1282 int val;
1283
1284 STRING_SYSCALL (path, c_path, val = rmdir (c_path));
1285 if (val != 0)
1286 SCM_SYSERROR;
1287 return SCM_UNSPECIFIED;
1288}
1289#undef FUNC_NAME
073167ef
LC
1290
1291SCM_DEFINE (scm_rename, "rename-file", 2, 0, 0,
1292 (SCM oldname, SCM newname),
1293 "Renames the file specified by @var{oldname} to @var{newname}.\n"
1294 "The return value is unspecified.")
1295#define FUNC_NAME s_scm_rename
1296{
1297 int rv;
1298
1299 STRING2_SYSCALL (oldname, c_oldname,
1300 newname, c_newname,
ca6adcc6 1301 rv = rename (c_oldname, c_newname));
073167ef
LC
1302 if (rv != 0)
1303 SCM_SYSERROR;
1304 return SCM_UNSPECIFIED;
1305}
1306#undef FUNC_NAME
1307
1308
1309SCM_DEFINE (scm_delete_file, "delete-file", 1, 0, 0,
1310 (SCM str),
b7e64f8b 1311 "Deletes (or \"unlinks\") the file specified by @var{str}.")
073167ef
LC
1312#define FUNC_NAME s_scm_delete_file
1313{
1314 int ans;
1315 STRING_SYSCALL (str, c_str, ans = unlink (c_str));
1316 if (ans != 0)
1317 SCM_SYSERROR;
1318 return SCM_UNSPECIFIED;
1319}
1320#undef FUNC_NAME
1321
1322SCM_DEFINE (scm_access, "access?", 2, 0, 0,
1323 (SCM path, SCM how),
1324 "Test accessibility of a file under the real UID and GID of the\n"
1325 "calling process. The return is @code{#t} if @var{path} exists\n"
1326 "and the permissions requested by @var{how} are all allowed, or\n"
1327 "@code{#f} if not.\n"
1328 "\n"
1329 "@var{how} is an integer which is one of the following values,\n"
1330 "or a bitwise-OR (@code{logior}) of multiple values.\n"
1331 "\n"
1332 "@defvar R_OK\n"
1333 "Test for read permission.\n"
1334 "@end defvar\n"
1335 "@defvar W_OK\n"
1336 "Test for write permission.\n"
1337 "@end defvar\n"
1338 "@defvar X_OK\n"
1339 "Test for execute permission.\n"
1340 "@end defvar\n"
1341 "@defvar F_OK\n"
1342 "Test for existence of the file. This is implied by each of the\n"
1343 "other tests, so there's no need to combine it with them.\n"
1344 "@end defvar\n"
1345 "\n"
1346 "It's important to note that @code{access?} does not simply\n"
1347 "indicate what will happen on attempting to read or write a\n"
1348 "file. In normal circumstances it does, but in a set-UID or\n"
1349 "set-GID program it doesn't because @code{access?} tests the\n"
1350 "real ID, whereas an open or execute attempt uses the effective\n"
1351 "ID.\n"
1352 "\n"
1353 "A program which will never run set-UID/GID can ignore the\n"
1354 "difference between real and effective IDs, but for maximum\n"
1355 "generality, especially in library functions, it's best not to\n"
1356 "use @code{access?} to predict the result of an open or execute,\n"
1357 "instead simply attempt that and catch any exception.\n"
1358 "\n"
1359 "The main use for @code{access?} is to let a set-UID/GID program\n"
1360 "determine what the invoking user would have been allowed to do,\n"
1361 "without the greater (or perhaps lesser) privileges afforded by\n"
1362 "the effective ID. For more on this, see ``Testing File\n"
1363 "Access'' in The GNU C Library Reference Manual.")
1364#define FUNC_NAME s_scm_access
1365{
1366 int rv;
1367 char *c_path;
1368
1369 c_path = scm_to_locale_string (path);
1370 rv = access (c_path, scm_to_int (how));
1371 free (c_path);
1372
1373 return scm_from_bool (!rv);
1374}
1375#undef FUNC_NAME
1376
1377SCM_DEFINE (scm_chmod, "chmod", 2, 0, 0,
1378 (SCM object, SCM mode),
b7e64f8b
BT
1379 "Changes the permissions of the file referred to by\n"
1380 "@var{object}. @var{object} can be a string containing a file\n"
1381 "name or a port or integer file descriptor which is open on a\n"
1382 "file (in which case @code{fchmod} is used as the underlying\n"
1383 "system call). @var{mode} specifies the new permissions as a\n"
1384 "decimal number, e.g., @code{(chmod \"foo\" #o755)}.\n"
073167ef
LC
1385 "The return value is unspecified.")
1386#define FUNC_NAME s_scm_chmod
1387{
1388 int rv;
073167ef
LC
1389
1390 object = SCM_COERCE_OUTPORT (object);
1391
5558cdaa 1392#if HAVE_FCHMOD
073167ef
LC
1393 if (scm_is_integer (object) || SCM_OPFPORTP (object))
1394 {
5558cdaa 1395 int fdes;
073167ef
LC
1396 if (scm_is_integer (object))
1397 fdes = scm_to_int (object);
1398 else
1399 fdes = SCM_FPORT_FDES (object);
1400 SCM_SYSCALL (rv = fchmod (fdes, scm_to_int (mode)));
1401 }
1402 else
5558cdaa 1403#endif
073167ef
LC
1404 {
1405 STRING_SYSCALL (object, c_object,
1406 rv = chmod (c_object, scm_to_int (mode)));
1407 }
1408 if (rv == -1)
1409 SCM_SYSERROR;
1410 return SCM_UNSPECIFIED;
1411}
1412#undef FUNC_NAME
1413
1414SCM_DEFINE (scm_umask, "umask", 0, 1, 0,
1415 (SCM mode),
1416 "If @var{mode} is omitted, returns a decimal number representing the current\n"
1417 "file creation mask. Otherwise the file creation mask is set to\n"
1418 "@var{mode} and the previous value is returned.\n\n"
1419 "E.g., @code{(umask #o022)} sets the mask to octal 22, decimal 18.")
1420#define FUNC_NAME s_scm_umask
1421{
1422 mode_t mask;
1423 if (SCM_UNBNDP (mode))
1424 {
1425 mask = umask (0);
1426 umask (mask);
1427 }
1428 else
1429 {
1430 mask = umask (scm_to_uint (mode));
1431 }
1432 return scm_from_uint (mask);
1433}
1434#undef FUNC_NAME
1435
073167ef
LC
1436SCM_DEFINE (scm_mkstemp, "mkstemp!", 1, 0, 0,
1437 (SCM tmpl),
1438 "Create a new unique file in the file system and return a new\n"
1439 "buffered port open for reading and writing to the file.\n"
1440 "\n"
1441 "@var{tmpl} is a string specifying where the file should be\n"
1442 "created: it must end with @samp{XXXXXX} and those @samp{X}s\n"
1443 "will be changed in the string to return the name of the file.\n"
1444 "(@code{port-filename} on the port also gives the name.)\n"
1445 "\n"
1446 "POSIX doesn't specify the permissions mode of the file, on GNU\n"
1447 "and most systems it's @code{#o600}. An application can use\n"
1448 "@code{chmod} to relax that if desired. For example\n"
1449 "@code{#o666} less @code{umask}, which is usual for ordinary\n"
1450 "file creation,\n"
1451 "\n"
1452 "@example\n"
1453 "(let ((port (mkstemp! (string-copy \"/tmp/myfile-XXXXXX\"))))\n"
1454 " (chmod port (logand #o666 (lognot (umask))))\n"
1455 " ...)\n"
1456 "@end example")
1457#define FUNC_NAME s_scm_mkstemp
1458{
1459 char *c_tmpl;
1460 int rv;
1461
1462 scm_dynwind_begin (0);
1463
1464 c_tmpl = scm_to_locale_string (tmpl);
1465 scm_dynwind_free (c_tmpl);
1466
1467 SCM_SYSCALL (rv = mkstemp (c_tmpl));
1468 if (rv == -1)
1469 SCM_SYSERROR;
1470
1471 scm_substring_move_x (scm_from_locale_string (c_tmpl),
1472 SCM_INUM0, scm_string_length (tmpl),
1473 tmpl, SCM_INUM0);
1474
1475 scm_dynwind_end ();
1476 return scm_fdes_to_port (rv, "w+", tmpl);
1477}
1478#undef FUNC_NAME
1479
0f2d19dd 1480\f
6a738a25
JB
1481/* Filename manipulation */
1482
1483SCM scm_dot_string;
1484
9b6316ea
AW
1485#ifdef __MINGW32__
1486SCM_SYMBOL (sym_file_name_convention, "windows");
1487#else
1488SCM_SYMBOL (sym_file_name_convention, "posix");
1489#endif
1490
1491SCM_INTERNAL SCM scm_system_file_name_convention (void);
1492
1493SCM_DEFINE (scm_system_file_name_convention,
1494 "system-file-name-convention", 0, 0, 0, (void),
1495 "Return either @code{posix} or @code{windows}, depending on\n"
1496 "what kind of system this Guile is running on.")
1497#define FUNC_NAME s_scm_system_file_name_convention
1498{
1499 return sym_file_name_convention;
1500}
1501#undef FUNC_NAME
1502
a1ec6916 1503SCM_DEFINE (scm_dirname, "dirname", 1, 0, 0,
1bbd0b84 1504 (SCM filename),
fa6a543f
MG
1505 "Return the directory name component of the file name\n"
1506 "@var{filename}. If @var{filename} does not contain a directory\n"
1507 "component, @code{.} is returned.")
1bbd0b84 1508#define FUNC_NAME s_scm_dirname
6a738a25 1509{
9fd38a3d
DH
1510 long int i;
1511 unsigned long int len;
1512
34d19ef6 1513 SCM_VALIDATE_STRING (1, filename);
9fd38a3d 1514
cc95e00a 1515 len = scm_i_string_length (filename);
9fd38a3d 1516
6a738a25 1517 i = len - 1;
7a17979e
AW
1518
1519 while (i >= 0 && is_file_name_separator (scm_c_string_ref (filename, i)))
832bbc95 1520 --i;
7a17979e 1521 while (i >= 0 && !is_file_name_separator (scm_c_string_ref (filename, i)))
832bbc95 1522 --i;
7a17979e 1523 while (i >= 0 && is_file_name_separator (scm_c_string_ref (filename, i)))
832bbc95 1524 --i;
7a17979e 1525
6a738a25
JB
1526 if (i < 0)
1527 {
7a17979e 1528 if (len > 0 && is_file_name_separator (scm_c_string_ref (filename, 0)))
cc95e00a 1529 return scm_c_substring (filename, 0, 1);
6a738a25
JB
1530 else
1531 return scm_dot_string;
1532 }
1533 else
cc95e00a 1534 return scm_c_substring (filename, 0, i + 1);
6a738a25 1535}
1bbd0b84 1536#undef FUNC_NAME
6a738a25 1537
a1ec6916 1538SCM_DEFINE (scm_basename, "basename", 1, 1, 0,
1bbd0b84 1539 (SCM filename, SCM suffix),
fa6a543f
MG
1540 "Return the base name of the file name @var{filename}. The\n"
1541 "base name is the file name without any directory components.\n"
bb2c02f2 1542 "If @var{suffix} is provided, and is equal to the end of\n"
b7e64f8b 1543 "@var{filename}, it is removed also.")
1bbd0b84 1544#define FUNC_NAME s_scm_basename
6a738a25 1545{
6a738a25 1546 int i, j, len, end;
9fd38a3d 1547
34d19ef6 1548 SCM_VALIDATE_STRING (1, filename);
cc95e00a 1549 len = scm_i_string_length (filename);
9fd38a3d 1550
6a738a25
JB
1551 if (SCM_UNBNDP (suffix))
1552 j = -1;
1553 else
1554 {
9fd38a3d 1555 SCM_VALIDATE_STRING (2, suffix);
cc95e00a 1556 j = scm_i_string_length (suffix) - 1;
6a738a25 1557 }
6a738a25 1558 i = len - 1;
7a17979e 1559 while (i >= 0 && is_file_name_separator (scm_c_string_ref (filename, i)))
832bbc95 1560 --i;
6a738a25 1561 end = i;
832bbc95
MG
1562 while (i >= 0 && j >= 0
1563 && (scm_i_string_ref (filename, i)
1564 == scm_i_string_ref (suffix, j)))
1565 {
1566 --i;
1567 --j;
1568 }
6a738a25
JB
1569 if (j == -1)
1570 end = i;
7a17979e 1571 while (i >= 0 && !is_file_name_separator (scm_c_string_ref (filename, i)))
832bbc95 1572 --i;
6a738a25
JB
1573 if (i == end)
1574 {
7a17979e 1575 if (len > 0 && is_file_name_separator (scm_c_string_ref (filename, 0)))
832bbc95 1576 return scm_c_substring (filename, 0, 1);
6a738a25
JB
1577 else
1578 return scm_dot_string;
1579 }
1580 else
cc95e00a 1581 return scm_c_substring (filename, i+1, end+1);
6a738a25 1582}
1bbd0b84 1583#undef FUNC_NAME
6a738a25 1584
25b82b34
AW
1585SCM_DEFINE (scm_canonicalize_path, "canonicalize-path", 1, 0, 0,
1586 (SCM path),
1587 "Return the canonical path of @var{path}. A canonical path has\n"
1588 "no @code{.} or @code{..} components, nor any repeated path\n"
1589 "separators (@code{/}) nor symlinks.\n\n"
1590 "Raises an error if any component of @var{path} does not exist.")
1591#define FUNC_NAME s_scm_canonicalize_path
427c73b9
AW
1592{
1593 char *str, *canon;
25b82b34
AW
1594
1595 SCM_VALIDATE_STRING (1, path);
1596
1597 str = scm_to_locale_string (path);
1598 canon = canonicalize_file_name (str);
1599 free (str);
1600
1601 if (canon)
1602 return scm_take_locale_string (canon);
1603 else
1604 SCM_SYSERROR;
1605}
1606#undef FUNC_NAME
6a738a25 1607
22457d57
AW
1608SCM
1609scm_i_relativize_path (SCM path, SCM in_path)
1610{
1611 char *str, *canon;
1612 SCM scanon;
1613
1614 str = scm_to_locale_string (path);
1615 canon = canonicalize_file_name (str);
1616 free (str);
1617
1618 if (!canon)
1619 return SCM_BOOL_F;
1620
1621 scanon = scm_take_locale_string (canon);
1622
1623 for (; scm_is_pair (in_path); in_path = scm_cdr (in_path))
2ae7b7b6
LC
1624 {
1625 SCM dir = scm_car (in_path);
1626 size_t len = scm_c_string_length (dir);
1627
1628 /* When DIR is empty, it means "current working directory". We
1629 could set DIR to (getcwd) in that case, but then the
1630 canonicalization would depend on the current directory, which
1631 is not what we want in the context of `compile-file', for
1632 instance. */
1633 if (len > 0
1634 && scm_is_true (scm_string_prefix_p (dir, scanon,
1635 SCM_UNDEFINED, SCM_UNDEFINED,
1636 SCM_UNDEFINED, SCM_UNDEFINED)))
1637 {
1638 /* DIR either has a trailing delimiter or doesn't. SCANON
1639 will be delimited by single delimiters. When DIR does not
1640 have a trailing delimiter, add one to the length to strip
1641 off the delimiter within SCANON. */
7a17979e 1642 if (!is_file_name_separator (scm_c_string_ref (dir, len - 1)))
2ae7b7b6 1643 len++;
22457d57 1644
2ae7b7b6
LC
1645 if (scm_c_string_length (scanon) > len)
1646 return scm_substring (scanon, scm_from_size_t (len), SCM_UNDEFINED);
1647 else
1648 return SCM_BOOL_F;
1649 }
1650 }
22457d57
AW
1651
1652 return SCM_BOOL_F;
1653}
1654
d0476fa2
LC
1655\f
1656/* Examining directories. These procedures are used by `check-guile'
1657 and thus compiled unconditionally. */
1658
1659scm_t_bits scm_tc16_dir;
1660
1661
1662SCM_DEFINE (scm_directory_stream_p, "directory-stream?", 1, 0, 0,
1663 (SCM obj),
b7e64f8b 1664 "Return a boolean indicating whether @var{obj} is a directory\n"
d0476fa2
LC
1665 "stream as returned by @code{opendir}.")
1666#define FUNC_NAME s_scm_directory_stream_p
1667{
1668 return scm_from_bool (SCM_DIRP (obj));
1669}
1670#undef FUNC_NAME
1671
1672
1673SCM_DEFINE (scm_opendir, "opendir", 1, 0, 0,
1674 (SCM dirname),
b7e64f8b 1675 "Open the directory specified by @var{dirname} and return a directory\n"
d0476fa2
LC
1676 "stream.")
1677#define FUNC_NAME s_scm_opendir
1678{
1679 DIR *ds;
1680 STRING_SYSCALL (dirname, c_dirname, ds = opendir (c_dirname));
1681 if (ds == NULL)
1682 SCM_SYSERROR;
1683 SCM_RETURN_NEWSMOB (scm_tc16_dir | (SCM_DIR_FLAG_OPEN<<16), ds);
1684}
1685#undef FUNC_NAME
1686
1687
1688/* FIXME: The glibc manual has a portability note that readdir_r may not
1689 null-terminate its return string. The circumstances outlined for this
1690 are not clear, nor is it clear what should be done about it. Lets use
1691 NAMLEN and worry about what else should be done if/when someone can
1692 figure it out. */
1693
1694SCM_DEFINE (scm_readdir, "readdir", 1, 0, 0,
1695 (SCM port),
1696 "Return (as a string) the next directory entry from the directory stream\n"
b7e64f8b 1697 "@var{port}. If there is no remaining entry to be read then the\n"
d0476fa2
LC
1698 "end of file object is returned.")
1699#define FUNC_NAME s_scm_readdir
1700{
1701 struct dirent_or_dirent64 *rdent;
1702
1703 SCM_VALIDATE_DIR (1, port);
1704 if (!SCM_DIR_OPEN_P (port))
1705 SCM_MISC_ERROR ("Directory ~S is not open.", scm_list_1 (port));
1706
1707#if HAVE_READDIR_R
1708 /* As noted in the glibc manual, on various systems (such as Solaris) the
1709 d_name[] field is only 1 char and you're expected to size the dirent
1710 buffer for readdir_r based on NAME_MAX. The SCM_MAX expressions below
1711 effectively give either sizeof(d_name) or NAME_MAX+1, whichever is
1712 bigger.
1713
1714 On solaris 10 there's no NAME_MAX constant, it's necessary to use
1715 pathconf(). We prefer NAME_MAX though, since it should be a constant
1716 and will therefore save a system call. We also prefer it since dirfd()
1717 is not available everywhere.
1718
1719 An alternative to dirfd() would be to open() the directory and then use
1720 fdopendir(), if the latter is available. That'd let us hold the fd
1721 somewhere in the smob, or just the dirent size calculated once. */
1722 {
1723 struct dirent_or_dirent64 de; /* just for sizeof */
1724 DIR *ds = (DIR *) SCM_SMOB_DATA_1 (port);
1725#ifdef NAME_MAX
1726 char buf [SCM_MAX (sizeof (de),
1727 sizeof (de) - sizeof (de.d_name) + NAME_MAX + 1)];
1728#else
1729 char *buf;
1730 long name_max = fpathconf (dirfd (ds), _PC_NAME_MAX);
1731 if (name_max == -1)
1732 SCM_SYSERROR;
1733 buf = alloca (SCM_MAX (sizeof (de),
1734 sizeof (de) - sizeof (de.d_name) + name_max + 1));
1735#endif
1736
1737 errno = 0;
1738 SCM_SYSCALL (readdir_r_or_readdir64_r (ds, (struct dirent_or_dirent64 *) buf, &rdent));
1739 if (errno != 0)
1740 SCM_SYSERROR;
1741 if (! rdent)
1742 return SCM_EOF_VAL;
1743
1744 return (rdent ? scm_from_locale_stringn (rdent->d_name, NAMLEN (rdent))
1745 : SCM_EOF_VAL);
1746 }
1747#else
1748 {
1749 SCM ret;
1750 scm_dynwind_begin (0);
1751 scm_i_dynwind_pthread_mutex_lock (&scm_i_misc_mutex);
1752
1753 errno = 0;
1754 SCM_SYSCALL (rdent = readdir_or_readdir64 ((DIR *) SCM_SMOB_DATA_1 (port)));
1755 if (errno != 0)
1756 SCM_SYSERROR;
1757
1758 ret = (rdent ? scm_from_locale_stringn (rdent->d_name, NAMLEN (rdent))
1759 : SCM_EOF_VAL);
1760
1761 scm_dynwind_end ();
1762 return ret;
1763 }
1764#endif
1765}
1766#undef FUNC_NAME
1767
1768
1769SCM_DEFINE (scm_rewinddir, "rewinddir", 1, 0, 0,
1770 (SCM port),
b7e64f8b 1771 "Reset the directory port @var{port} so that the next call to\n"
d0476fa2
LC
1772 "@code{readdir} will return the first directory entry.")
1773#define FUNC_NAME s_scm_rewinddir
1774{
1775 SCM_VALIDATE_DIR (1, port);
1776 if (!SCM_DIR_OPEN_P (port))
1777 SCM_MISC_ERROR ("Directory ~S is not open.", scm_list_1 (port));
1778
1779 rewinddir ((DIR *) SCM_SMOB_DATA_1 (port));
1780
1781 return SCM_UNSPECIFIED;
1782}
1783#undef FUNC_NAME
1784
1785
1786SCM_DEFINE (scm_closedir, "closedir", 1, 0, 0,
1787 (SCM port),
b7e64f8b 1788 "Close the directory stream @var{port}.\n"
d0476fa2
LC
1789 "The return value is unspecified.")
1790#define FUNC_NAME s_scm_closedir
1791{
1792 SCM_VALIDATE_DIR (1, port);
1793
1794 if (SCM_DIR_OPEN_P (port))
1795 {
1796 int sts;
1797
1798 SCM_SYSCALL (sts = closedir ((DIR *) SCM_SMOB_DATA_1 (port)));
1799 if (sts != 0)
1800 SCM_SYSERROR;
1801
1802 SCM_SET_SMOB_DATA_0 (port, scm_tc16_dir);
1803 }
1804
1805 return SCM_UNSPECIFIED;
1806}
1807#undef FUNC_NAME
1808
1809
1fa54298 1810#ifdef HAVE_POSIX
d0476fa2
LC
1811static int
1812scm_dir_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
1813{
1814 scm_puts ("#<", port);
1815 if (!SCM_DIR_OPEN_P (exp))
1816 scm_puts ("closed: ", port);
1817 scm_puts ("directory stream ", port);
1818 scm_uintprint (SCM_SMOB_DATA_1 (exp), 16, port);
1819 scm_putc ('>', port);
1820 return 1;
1821}
1822
1823
1824static size_t
1825scm_dir_free (SCM p)
1826{
1827 if (SCM_DIR_OPEN_P (p))
1828 closedir ((DIR *) SCM_SMOB_DATA_1 (p));
1829 return 0;
1830}
1fa54298 1831#endif
6a738a25
JB
1832
1833\f
1cc91f1b 1834
0f2d19dd
JB
1835void
1836scm_init_filesys ()
0f2d19dd 1837{
073167ef 1838#ifdef HAVE_POSIX
e841c3e0
KN
1839 scm_tc16_dir = scm_make_smob_type ("directory", 0);
1840 scm_set_smob_free (scm_tc16_dir, scm_dir_free);
1841 scm_set_smob_print (scm_tc16_dir, scm_dir_print);
0f2d19dd 1842
3d8d56df 1843#ifdef O_RDONLY
23d72566 1844 scm_c_define ("O_RDONLY", scm_from_int (O_RDONLY));
3d8d56df
GH
1845#endif
1846#ifdef O_WRONLY
23d72566 1847 scm_c_define ("O_WRONLY", scm_from_int (O_WRONLY));
3d8d56df
GH
1848#endif
1849#ifdef O_RDWR
23d72566 1850 scm_c_define ("O_RDWR", scm_from_int (O_RDWR));
3d8d56df
GH
1851#endif
1852#ifdef O_CREAT
23d72566 1853 scm_c_define ("O_CREAT", scm_from_int (O_CREAT));
3d8d56df
GH
1854#endif
1855#ifdef O_EXCL
23d72566 1856 scm_c_define ("O_EXCL", scm_from_int (O_EXCL));
3d8d56df
GH
1857#endif
1858#ifdef O_NOCTTY
23d72566 1859 scm_c_define ("O_NOCTTY", scm_from_int (O_NOCTTY));
3d8d56df
GH
1860#endif
1861#ifdef O_TRUNC
23d72566 1862 scm_c_define ("O_TRUNC", scm_from_int (O_TRUNC));
3d8d56df
GH
1863#endif
1864#ifdef O_APPEND
23d72566 1865 scm_c_define ("O_APPEND", scm_from_int (O_APPEND));
3d8d56df 1866#endif
6afcd3b2 1867#ifdef O_NONBLOCK
23d72566 1868 scm_c_define ("O_NONBLOCK", scm_from_int (O_NONBLOCK));
3d8d56df
GH
1869#endif
1870#ifdef O_NDELAY
23d72566 1871 scm_c_define ("O_NDELAY", scm_from_int (O_NDELAY));
3d8d56df
GH
1872#endif
1873#ifdef O_SYNC
23d72566 1874 scm_c_define ("O_SYNC", scm_from_int (O_SYNC));
3d8d56df 1875#endif
23f2b9a3 1876#ifdef O_LARGEFILE
23d72566 1877 scm_c_define ("O_LARGEFILE", scm_from_int (O_LARGEFILE));
3565df45
LC
1878#endif
1879#ifdef O_NOTRANS
1880 scm_c_define ("O_NOTRANS", scm_from_int (O_NOTRANS));
1881#endif
3d8d56df 1882
4c1feaa5 1883#ifdef F_DUPFD
23d72566 1884 scm_c_define ("F_DUPFD", scm_from_int (F_DUPFD));
4c1feaa5
JB
1885#endif
1886#ifdef F_GETFD
23d72566 1887 scm_c_define ("F_GETFD", scm_from_int (F_GETFD));
4c1feaa5
JB
1888#endif
1889#ifdef F_SETFD
23d72566 1890 scm_c_define ("F_SETFD", scm_from_int (F_SETFD));
4c1feaa5
JB
1891#endif
1892#ifdef F_GETFL
23d72566 1893 scm_c_define ("F_GETFL", scm_from_int (F_GETFL));
4c1feaa5
JB
1894#endif
1895#ifdef F_SETFL
23d72566 1896 scm_c_define ("F_SETFL", scm_from_int (F_SETFL));
4c1feaa5
JB
1897#endif
1898#ifdef F_GETOWN
23d72566 1899 scm_c_define ("F_GETOWN", scm_from_int (F_GETOWN));
4c1feaa5
JB
1900#endif
1901#ifdef F_SETOWN
23d72566 1902 scm_c_define ("F_SETOWN", scm_from_int (F_SETOWN));
4c1feaa5
JB
1903#endif
1904#ifdef FD_CLOEXEC
23d72566 1905 scm_c_define ("FD_CLOEXEC", scm_from_int (FD_CLOEXEC));
bd9e24b3 1906#endif
073167ef
LC
1907#endif /* HAVE_POSIX */
1908
1909 /* `access' symbols. */
1910 scm_c_define ("R_OK", scm_from_int (R_OK));
1911 scm_c_define ("W_OK", scm_from_int (W_OK));
1912 scm_c_define ("X_OK", scm_from_int (X_OK));
1913 scm_c_define ("F_OK", scm_from_int (F_OK));
1914
1915 scm_dot_string = scm_from_locale_string (".");
3d8d56df 1916
a0599745 1917#include "libguile/filesys.x"
0f2d19dd 1918}
89e00824
ML
1919
1920/*
1921 Local Variables:
1922 c-file-style: "gnu"
1923 End:
1924*/