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