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