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