* alist.c, chars.c, debug.c, dynl.c, dynwind.c, error.c, eval.c,
[bpt/guile.git] / libguile / filesys.c
CommitLineData
def804a3 1/* Copyright (C) 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
0f2d19dd
JB
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program 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
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
82892bed
JB
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
0f2d19dd
JB
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
37 *
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
82892bed 40 * If you do not wish that, delete this exception notice. */
1bbd0b84
GB
41
42/* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
44
0f2d19dd 45\f
3d8d56df 46#include <stdio.h>
0f2d19dd 47#include "_scm.h"
20e6290e
JB
48#include "genio.h"
49#include "smob.h"
52f4f4d6 50#include "feature.h"
3d8d56df 51#include "fports.h"
44e8413c 52#include "iselect.h"
0f2d19dd 53
1bbd0b84 54#include "scm_validate.h"
20e6290e 55#include "filesys.h"
def804a3 56
0f2d19dd 57\f
def804a3
JB
58#ifdef HAVE_IO_H
59#include <io.h>
60#endif
61
0f2d19dd
JB
62#ifdef TIME_WITH_SYS_TIME
63# include <sys/time.h>
64# include <time.h>
65#else
66# if HAVE_SYS_TIME_H
67# include <sys/time.h>
68# else
69# include <time.h>
70# endif
71#endif
72
73#ifdef HAVE_UNISTD_H
74#include <unistd.h>
75#endif
76
3594582b 77#ifdef LIBC_H_WITH_UNISTD_H
1f9e2226
JB
78#include <libc.h>
79#endif
80
0f2d19dd
JB
81#ifdef HAVE_SYS_SELECT_H
82#include <sys/select.h>
83#endif
84
1f9e2226
JB
85#ifdef HAVE_STRING_H
86#include <string.h>
87#endif
88
8cc71382 89#include <sys/types.h>
0f2d19dd
JB
90#include <sys/stat.h>
91#include <fcntl.h>
92
93#include <pwd.h>
94
95
0f2d19dd
JB
96#if HAVE_DIRENT_H
97# include <dirent.h>
98# define NAMLEN(dirent) strlen((dirent)->d_name)
99#else
100# define dirent direct
101# define NAMLEN(dirent) (dirent)->d_namlen
102# if HAVE_SYS_NDIR_H
103# include <sys/ndir.h>
104# endif
105# if HAVE_SYS_DIR_H
106# include <sys/dir.h>
107# endif
108# if HAVE_NDIR_H
109# include <ndir.h>
110# endif
111#endif
112
d7b8a21a
JB
113/* Ultrix has S_IFSOCK, but no S_ISSOCK. Ipe! */
114#if defined (S_IFSOCK) && ! defined (S_ISSOCK)
115#define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
116#endif
0f2d19dd
JB
117\f
118
0f2d19dd
JB
119
120\f
121
122/* {Permissions}
123 */
124
a1ec6916 125SCM_DEFINE (scm_chown, "chown", 3, 0, 0,
1bbd0b84 126 (SCM object, SCM owner, SCM group),
4079f87e
GB
127"Change the ownership and group of the file referred to by @var{obj} to
128the integer userid values @var{owner} and @var{group}. @var{obj} can be
129a string containing a file name or a port or integer file descriptor
130which is open on the file (in which case fchown is used as the underlying
131system call). The return value
132is unspecified.
133
134If @var{obj} is a symbolic link, either the
135ownership of the link or the ownership of the referenced file will be
136changed depending on the operating system (lchown is
137unsupported at present). If @var{owner} or @var{group} is specified
138as @code{-1}, then that ID is not changed.")
1bbd0b84 139#define FUNC_NAME s_scm_chown
0f2d19dd 140{
6afcd3b2
GH
141 int rv;
142 int fdes;
02b754d3 143
78446828
MV
144 object = SCM_COERCE_OUTPORT (object);
145
3b3b36dd
GB
146 SCM_VALIDATE_INUM (2,owner);
147 SCM_VALIDATE_INUM (3,group);
0c95b57d 148 if (SCM_INUMP (object) || (SCM_OPFPORTP (object)))
6afcd3b2
GH
149 {
150 if (SCM_INUMP (object))
151 fdes = SCM_INUM (object);
152 else
77a76b64 153 fdes = SCM_FPORT_FDES (object);
6afcd3b2
GH
154 SCM_SYSCALL (rv = fchown (fdes, SCM_INUM (owner), SCM_INUM (group)));
155 }
156 else
157 {
c1bfcf60 158 SCM_VALIDATE_ROSTRING(1,object);
6afcd3b2
GH
159 SCM_COERCE_SUBSTR (object);
160 SCM_SYSCALL (rv = chown (SCM_ROCHARS (object),
161 SCM_INUM (owner), SCM_INUM (group)));
162 }
163 if (rv == -1)
1bbd0b84 164 SCM_SYSERROR;
02b754d3 165 return SCM_UNSPECIFIED;
0f2d19dd 166}
1bbd0b84 167#undef FUNC_NAME
0f2d19dd
JB
168
169
a1ec6916 170SCM_DEFINE (scm_chmod, "chmod", 2, 0, 0,
1bbd0b84 171 (SCM object, SCM mode),
4079f87e
GB
172"Changes the permissions of the file referred to by @var{obj}.
173@var{obj} can be a string containing a file name or a port or integer file
174descriptor which is open on a file (in which case @code{fchmod} is used
175as the underlying system call).
176@var{mode} specifies
177the new permissions as a decimal number, e.g., @code{(chmod "foo" #o755)}.
178The return value is unspecified.")
1bbd0b84 179#define FUNC_NAME s_scm_chmod
0f2d19dd
JB
180{
181 int rv;
6afcd3b2
GH
182 int fdes;
183
78446828
MV
184 object = SCM_COERCE_OUTPORT (object);
185
3b3b36dd 186 SCM_VALIDATE_INUM (2,mode);
0c95b57d 187 if (SCM_INUMP (object) || SCM_OPFPORTP (object))
89958ad0 188 {
6afcd3b2
GH
189 if (SCM_INUMP (object))
190 fdes = SCM_INUM (object);
191 else
77a76b64 192 fdes = SCM_FPORT_FDES (object);
6afcd3b2 193 SCM_SYSCALL (rv = fchmod (fdes, SCM_INUM (mode)));
89958ad0 194 }
0f2d19dd
JB
195 else
196 {
3b3b36dd 197 SCM_VALIDATE_ROSTRING (1,object);
6afcd3b2
GH
198 SCM_COERCE_SUBSTR (object);
199 SCM_SYSCALL (rv = chmod (SCM_ROCHARS (object), SCM_INUM (mode)));
0f2d19dd 200 }
6afcd3b2 201 if (rv == -1)
1bbd0b84 202 SCM_SYSERROR;
02b754d3 203 return SCM_UNSPECIFIED;
0f2d19dd 204}
1bbd0b84 205#undef FUNC_NAME
0f2d19dd 206
a1ec6916 207SCM_DEFINE (scm_umask, "umask", 0, 1, 0,
1bbd0b84 208 (SCM mode),
4079f87e
GB
209"If @var{mode} is omitted, retuns a decimal number representing the current
210file creation mask. Otherwise the file creation mask is set to
211@var{mode} and the previous value is returned.
212
213E.g., @code{(umask #o022)} sets the mask to octal 22, decimal 18.")
1bbd0b84 214#define FUNC_NAME s_scm_umask
0f2d19dd
JB
215{
216 mode_t mask;
217 if (SCM_UNBNDP (mode))
218 {
219 mask = umask (0);
220 umask (mask);
221 }
222 else
223 {
3b3b36dd 224 SCM_VALIDATE_INUM (1,mode);
0f2d19dd
JB
225 mask = umask (SCM_INUM (mode));
226 }
227 return SCM_MAKINUM (mask);
228}
1bbd0b84 229#undef FUNC_NAME
0f2d19dd
JB
230
231\f
0f2d19dd 232
a1ec6916 233SCM_DEFINE (scm_open_fdes, "open-fdes", 2, 1, 0,
1bbd0b84 234 (SCM path, SCM flags, SCM mode),
4079f87e
GB
235"Similar to @code{open} but returns a file descriptor instead of a
236port.")
1bbd0b84 237#define FUNC_NAME s_scm_open_fdes
0f2d19dd
JB
238{
239 int fd;
3d8d56df 240 int iflags;
6afcd3b2 241 int imode;
0f2d19dd 242
3b3b36dd 243 SCM_VALIDATE_ROSTRING (1,path);
6afcd3b2 244 SCM_COERCE_SUBSTR (path);
c1bfcf60
GB
245 iflags = SCM_NUM2LONG(2,flags);
246 imode = SCM_NUM2LONG_DEF(3,mode,0666);
6afcd3b2 247 SCM_SYSCALL (fd = open (SCM_ROCHARS (path), iflags, imode));
3d8d56df 248 if (fd == -1)
1bbd0b84 249 SCM_SYSERROR;
6afcd3b2
GH
250 return SCM_MAKINUM (fd);
251}
1bbd0b84 252#undef FUNC_NAME
6afcd3b2 253
a1ec6916 254SCM_DEFINE (scm_open, "open", 2, 1, 0,
1bbd0b84 255 (SCM path, SCM flags, SCM mode),
4079f87e
GB
256"Open the file named by @var{path} for reading and/or writing.
257@var{flags} is an integer specifying how the file should be opened.
258@var{mode} is an integer specifying the permission bits of the file, if
259it needs to be created, before the umask is applied. The default is 666
260(Unix itself has no default).
261
262@var{flags} can be constructed by combining variables using @code{logior}.
263Basic flags are:
264
265@defvar O_RDONLY
266Open the file read-only.
267@end defvar
268@defvar O_WRONLY
269Open the file write-only.
270@end defvar
271@defvar O_RDWR
272Open the file read/write.
273@end defvar
274@defvar O_APPEND
275Append to the file instead of truncating.
276@end defvar
277@defvar O_CREAT
278Create the file if it does not already exist.
279@end defvar
280
281See the Unix documentation of the @code{open} system call
282for additional flags.")
1bbd0b84 283#define FUNC_NAME s_scm_open
6afcd3b2
GH
284{
285 SCM newpt;
286 char *port_mode;
287 int fd;
6afcd3b2
GH
288 int iflags;
289
290 fd = SCM_INUM (scm_open_fdes (path, flags, mode));
c1bfcf60 291 iflags = SCM_NUM2LONG (2,flags);
3d8d56df 292 if (iflags & O_RDWR)
77a76b64
JB
293 {
294 if (iflags & O_APPEND)
295 port_mode = "a+";
296 else if (iflags & O_CREAT)
297 port_mode = "w+";
298 else
299 port_mode = "r+";
300 }
3d8d56df 301 else {
77a76b64
JB
302 if (iflags & O_APPEND)
303 port_mode = "a";
304 else if (iflags & O_WRONLY)
3d8d56df
GH
305 port_mode = "w";
306 else
307 port_mode = "r";
308 }
77a76b64 309 newpt = scm_fdes_to_port (fd, port_mode, path);
3d8d56df 310 return newpt;
0f2d19dd 311}
1bbd0b84 312#undef FUNC_NAME
0f2d19dd 313
a1ec6916 314SCM_DEFINE (scm_close, "close", 1, 0, 0,
1bbd0b84 315 (SCM fd_or_port),
4079f87e
GB
316"Similar to close-port (@pxref{Generic Port Operations, close-port}),
317but also works on file descriptors. A side
318effect of closing a file descriptor is that any ports using that file
319descriptor are moved to a different file descriptor and have
320their revealed counts set to zero.")
1bbd0b84 321#define FUNC_NAME s_scm_close
eadd48de
GH
322{
323 int rv;
324 int fd;
325
78446828
MV
326 fd_or_port = SCM_COERCE_OUTPORT (fd_or_port);
327
0c95b57d 328 if (SCM_PORTP (fd_or_port))
eadd48de 329 return scm_close_port (fd_or_port);
3b3b36dd 330 SCM_VALIDATE_INUM (1,fd_or_port);
eadd48de 331 fd = SCM_INUM (fd_or_port);
eadd48de 332 scm_evict_ports (fd); /* see scsh manual. */
a9488d12 333 SCM_SYSCALL (rv = close (fd));
eadd48de
GH
334 /* following scsh, closing an already closed file descriptor is
335 not an error. */
336 if (rv < 0 && errno != EBADF)
1bbd0b84
GB
337 SCM_SYSERROR;
338 return SCM_NEGATE_BOOL(rv < 0);
eadd48de 339}
1bbd0b84 340#undef FUNC_NAME
eadd48de 341
0f2d19dd
JB
342\f
343/* {Files}
344 */
1cc91f1b 345
ae5253c5
GH
346SCM_SYMBOL (scm_sym_regular, "regular");
347SCM_SYMBOL (scm_sym_directory, "directory");
f326ecf3 348#ifdef HAVE_S_ISLNK
ae5253c5 349SCM_SYMBOL (scm_sym_symlink, "symlink");
f326ecf3 350#endif
ae5253c5
GH
351SCM_SYMBOL (scm_sym_block_special, "block-special");
352SCM_SYMBOL (scm_sym_char_special, "char-special");
353SCM_SYMBOL (scm_sym_fifo, "fifo");
354SCM_SYMBOL (scm_sym_sock, "socket");
355SCM_SYMBOL (scm_sym_unknown, "unknown");
356
0f2d19dd 357static SCM
1bbd0b84 358scm_stat2scm (struct stat *stat_temp)
0f2d19dd 359{
a8741caa 360 SCM ans = scm_make_vector (SCM_MAKINUM (15), SCM_UNSPECIFIED);
0f2d19dd 361 SCM *ve = SCM_VELTS (ans);
ae5253c5 362
0f2d19dd
JB
363 ve[0] = scm_ulong2num ((unsigned long) stat_temp->st_dev);
364 ve[1] = scm_ulong2num ((unsigned long) stat_temp->st_ino);
365 ve[2] = scm_ulong2num ((unsigned long) stat_temp->st_mode);
366 ve[3] = scm_ulong2num ((unsigned long) stat_temp->st_nlink);
367 ve[4] = scm_ulong2num ((unsigned long) stat_temp->st_uid);
368 ve[5] = scm_ulong2num ((unsigned long) stat_temp->st_gid);
369#ifdef HAVE_ST_RDEV
370 ve[6] = scm_ulong2num ((unsigned long) stat_temp->st_rdev);
371#else
372 ve[6] = SCM_BOOL_F;
373#endif
374 ve[7] = scm_ulong2num ((unsigned long) stat_temp->st_size);
375 ve[8] = scm_ulong2num ((unsigned long) stat_temp->st_atime);
376 ve[9] = scm_ulong2num ((unsigned long) stat_temp->st_mtime);
377 ve[10] = scm_ulong2num ((unsigned long) stat_temp->st_ctime);
378#ifdef HAVE_ST_BLKSIZE
379 ve[11] = scm_ulong2num ((unsigned long) stat_temp->st_blksize);
380#else
381 ve[11] = scm_ulong2num (4096L);
382#endif
383#ifdef HAVE_ST_BLOCKS
384 ve[12] = scm_ulong2num ((unsigned long) stat_temp->st_blocks);
385#else
386 ve[12] = SCM_BOOL_F;
387#endif
ae5253c5
GH
388 {
389 int mode = stat_temp->st_mode;
390
391 if (S_ISREG (mode))
392 ve[13] = scm_sym_regular;
393 else if (S_ISDIR (mode))
394 ve[13] = scm_sym_directory;
f326ecf3 395#ifdef HAVE_S_ISLNK
ae5253c5
GH
396 else if (S_ISLNK (mode))
397 ve[13] = scm_sym_symlink;
f326ecf3 398#endif
ae5253c5
GH
399 else if (S_ISBLK (mode))
400 ve[13] = scm_sym_block_special;
401 else if (S_ISCHR (mode))
402 ve[13] = scm_sym_char_special;
403 else if (S_ISFIFO (mode))
404 ve[13] = scm_sym_fifo;
405 else if (S_ISSOCK (mode))
406 ve[13] = scm_sym_sock;
407 else
408 ve[13] = scm_sym_unknown;
409
410 ve[14] = SCM_MAKINUM ((~S_IFMT) & mode);
411
412 /* the layout of the bits in ve[14] is intended to be portable.
413 If there are systems that don't follow the usual convention,
414 the following could be used:
415
416 tmp = 0;
417 if (S_ISUID & mode) tmp += 1;
418 tmp <<= 1;
419 if (S_IRGRP & mode) tmp += 1;
420 tmp <<= 1;
421 if (S_ISVTX & mode) tmp += 1;
422 tmp <<= 1;
423 if (S_IRUSR & mode) tmp += 1;
424 tmp <<= 1;
425 if (S_IWUSR & mode) tmp += 1;
426 tmp <<= 1;
427 if (S_IXUSR & mode) tmp += 1;
428 tmp <<= 1;
429 if (S_IWGRP & mode) tmp += 1;
430 tmp <<= 1;
431 if (S_IXGRP & mode) tmp += 1;
432 tmp <<= 1;
433 if (S_IROTH & mode) tmp += 1;
434 tmp <<= 1;
435 if (S_IWOTH & mode) tmp += 1;
436 tmp <<= 1;
437 if (S_IXOTH & mode) tmp += 1;
438
439 ve[14] = SCM_MAKINUM (tmp);
440
441 */
442 }
0f2d19dd
JB
443
444 return ans;
445}
446
a1ec6916 447SCM_DEFINE (scm_stat, "stat", 1, 0, 0,
1bbd0b84 448 (SCM object),
4079f87e
GB
449"Returns an object containing various information
450about the file determined by @var{obj}.
451@var{obj} can be a string containing a file name or a port or integer file
452descriptor which is open on a file (in which case @code{fstat} is used
453as the underlying system call).
454
455The object returned by @code{stat} can be passed as a single parameter
456to the following procedures, all of which return integers:
457
458@table @code
459@item stat:dev
460The device containing the file.
461@item stat:ino
462The file serial number, which distinguishes this file from all other
463files on the same device.
464@item stat:mode
465The mode of the file. This includes file type information
466and the file permission bits. See @code{stat:type} and @code{stat:perms}
467below.
468@item stat:nlink
469The number of hard links to the file.
470@item stat:uid
471The user ID of the file's owner.
472@item stat:gid
473The group ID of the file.
474@item stat:rdev
475Device ID; this entry is defined only for character or block
476special files.
477@item stat:size
478The size of a regular file in bytes.
479@item stat:atime
480The last access time for the file.
481@item stat:mtime
482The last modification time for the file.
483@item stat:ctime
484The last modification time for the attributes of the file.
485@item stat:blksize
486The optimal block size for reading or writing the file, in bytes.
487@item stat:blocks
488The amount of disk space that the file occupies measured in units of
489512 byte blocks.
490@end table
491
492In addition, the following procedures return the information
493from stat:mode in a more convenient form:
494
495@table @code
496@item stat:type
497A symbol representing the type of file. Possible values are
498regular, directory, symlink, block-special, char-special,
499fifo, socket and unknown
500@item stat:perms
501An integer representing the access permission bits.
502@end table")
1bbd0b84 503#define FUNC_NAME s_scm_stat
0f2d19dd 504{
6afcd3b2
GH
505 int rv;
506 int fdes;
0f2d19dd
JB
507 struct stat stat_temp;
508
1ea47048
MD
509 if (SCM_INUMP (object))
510 SCM_SYSCALL (rv = fstat (SCM_INUM (object), &stat_temp));
511 else
0f2d19dd 512 {
6b5a304f 513 SCM_VALIDATE_NIM (1,object);
1ea47048
MD
514 if (SCM_ROSTRINGP (object))
515 {
516 SCM_COERCE_SUBSTR (object);
517 SCM_SYSCALL (rv = stat (SCM_ROCHARS (object), &stat_temp));
518 }
c0ebd8c5 519 else
0f2d19dd 520 {
1ea47048 521 object = SCM_COERCE_OUTPORT (object);
c1bfcf60 522 SCM_VALIDATE_OPFPORT(1,object);
77a76b64 523 fdes = SCM_FPORT_FDES (object);
1ea47048 524 SCM_SYSCALL (rv = fstat (fdes, &stat_temp));
0f2d19dd 525 }
6afcd3b2
GH
526 }
527 if (rv == -1)
3d8d56df
GH
528 {
529 int en = errno;
530
5d2d2ffc 531 SCM_SYSERROR_MSG ("~A: ~S",
3d8d56df 532 scm_listify (scm_makfrom0str (strerror (errno)),
6afcd3b2 533 object,
5d2d2ffc 534 SCM_UNDEFINED), en);
3d8d56df 535 }
02b754d3 536 return scm_stat2scm (&stat_temp);
0f2d19dd 537}
1bbd0b84 538#undef FUNC_NAME
0f2d19dd 539
0f2d19dd
JB
540\f
541/* {Modifying Directories}
542 */
543
a1ec6916 544SCM_DEFINE (scm_link, "link", 2, 0, 0,
1bbd0b84 545 (SCM oldpath, SCM newpath),
4079f87e
GB
546"Creates a new name @var{path-to} in the file system for the file
547named by @var{path-from}. If @var{path-from} is a symbolic link, the
548link may or may not be followed depending on the system.")
1bbd0b84 549#define FUNC_NAME s_scm_link
0f2d19dd
JB
550{
551 int val;
02b754d3 552
3b3b36dd 553 SCM_VALIDATE_ROSTRING (1,oldpath);
0f2d19dd 554 if (SCM_SUBSTRP (oldpath))
6afcd3b2
GH
555 oldpath = scm_makfromstr (SCM_ROCHARS (oldpath),
556 SCM_ROLENGTH (oldpath), 0);
3b3b36dd 557 SCM_VALIDATE_ROSTRING (2,newpath);
0f2d19dd 558 if (SCM_SUBSTRP (newpath))
6afcd3b2
GH
559 newpath = scm_makfromstr (SCM_ROCHARS (newpath),
560 SCM_ROLENGTH (newpath), 0);
0f2d19dd 561 SCM_SYSCALL (val = link (SCM_ROCHARS (oldpath), SCM_ROCHARS (newpath)));
02b754d3 562 if (val != 0)
1bbd0b84 563 SCM_SYSERROR;
02b754d3 564 return SCM_UNSPECIFIED;
0f2d19dd 565}
1bbd0b84 566#undef FUNC_NAME
0f2d19dd
JB
567
568
569
a1ec6916 570SCM_DEFINE (scm_rename, "rename-file", 2, 0, 0,
1bbd0b84 571 (SCM oldname, SCM newname),
4079f87e
GB
572"Renames the file specified by @var{path-from} to @var{path-to}.
573The return value is unspecified.")
1bbd0b84 574#define FUNC_NAME s_scm_rename
0f2d19dd
JB
575{
576 int rv;
3b3b36dd
GB
577 SCM_VALIDATE_ROSTRING (1,oldname);
578 SCM_VALIDATE_ROSTRING (2,newname);
89958ad0
JB
579 SCM_COERCE_SUBSTR (oldname);
580 SCM_COERCE_SUBSTR (newname);
0f2d19dd 581#ifdef HAVE_RENAME
89958ad0 582 SCM_SYSCALL (rv = rename (SCM_ROCHARS (oldname), SCM_ROCHARS (newname)));
0f2d19dd 583#else
89958ad0 584 SCM_SYSCALL (rv = link (SCM_ROCHARS (oldname), SCM_ROCHARS (newname)));
02b754d3 585 if (rv == 0)
0f2d19dd 586 {
89958ad0 587 SCM_SYSCALL (rv = unlink (SCM_ROCHARS (oldname)));;
02b754d3 588 if (rv != 0)
0f2d19dd 589 /* unlink failed. remove new name */
89958ad0 590 SCM_SYSCALL (unlink (SCM_ROCHARS (newname)));
0f2d19dd 591 }
6afcd3b2 592#endif
02b754d3 593 if (rv != 0)
1bbd0b84 594 SCM_SYSERROR;
02b754d3 595 return SCM_UNSPECIFIED;
0f2d19dd 596}
1bbd0b84 597#undef FUNC_NAME
0f2d19dd
JB
598
599
3b3b36dd 600SCM_DEFINE (scm_delete_file, "delete-file", 1, 0, 0,
1bbd0b84 601 (SCM str),
4079f87e 602"Deletes (or \"unlinks\") the file specified by @var{path}.")
1bbd0b84 603#define FUNC_NAME s_scm_delete_file
2f3ed1ba
JB
604{
605 int ans;
3b3b36dd 606 SCM_VALIDATE_ROSTRING (1,str);
89958ad0
JB
607 SCM_COERCE_SUBSTR (str);
608 SCM_SYSCALL (ans = unlink (SCM_ROCHARS (str)));
2f3ed1ba 609 if (ans != 0)
1bbd0b84 610 SCM_SYSERROR;
2f3ed1ba
JB
611 return SCM_UNSPECIFIED;
612}
1bbd0b84 613#undef FUNC_NAME
2f3ed1ba 614
f25f761d 615#ifdef HAVE_MKDIR
a1ec6916 616SCM_DEFINE (scm_mkdir, "mkdir", 1, 1, 0,
1bbd0b84 617 (SCM path, SCM mode),
4079f87e
GB
618"Create a new directory named by @var{path}. If @var{mode} is omitted
619then the permissions of the directory file are set using the current
620umask. Otherwise they are set to the decimal value specified with
621@var{mode}. The return value is unspecified.")
1bbd0b84 622#define FUNC_NAME s_scm_mkdir
0f2d19dd 623{
0f2d19dd
JB
624 int rv;
625 mode_t mask;
3b3b36dd 626 SCM_VALIDATE_ROSTRING (1,path);
89958ad0 627 SCM_COERCE_SUBSTR (path);
0f2d19dd
JB
628 if (SCM_UNBNDP (mode))
629 {
630 mask = umask (0);
631 umask (mask);
89958ad0 632 SCM_SYSCALL (rv = mkdir (SCM_ROCHARS (path), 0777 ^ mask));
0f2d19dd
JB
633 }
634 else
635 {
3b3b36dd 636 SCM_VALIDATE_INUM (2,mode);
89958ad0 637 SCM_SYSCALL (rv = mkdir (SCM_ROCHARS (path), SCM_INUM (mode)));
0f2d19dd 638 }
02b754d3 639 if (rv != 0)
1bbd0b84 640 SCM_SYSERROR;
02b754d3 641 return SCM_UNSPECIFIED;
0f2d19dd 642}
1bbd0b84 643#undef FUNC_NAME
f25f761d 644#endif /* HAVE_MKDIR */
0f2d19dd 645
f25f761d 646#ifdef HAVE_RMDIR
a1ec6916 647SCM_DEFINE (scm_rmdir, "rmdir", 1, 0, 0,
1bbd0b84 648 (SCM path),
4079f87e
GB
649"Remove the existing directory named by @var{path}. The directory must
650be empty for this to succeed. The return value is unspecified.")
1bbd0b84 651#define FUNC_NAME s_scm_rmdir
0f2d19dd 652{
0f2d19dd 653 int val;
02b754d3 654
3b3b36dd 655 SCM_VALIDATE_ROSTRING (1,path);
89958ad0
JB
656 SCM_COERCE_SUBSTR (path);
657 SCM_SYSCALL (val = rmdir (SCM_ROCHARS (path)));
02b754d3 658 if (val != 0)
1bbd0b84 659 SCM_SYSERROR;
02b754d3 660 return SCM_UNSPECIFIED;
0f2d19dd 661}
1bbd0b84 662#undef FUNC_NAME
f25f761d 663#endif
0f2d19dd
JB
664
665\f
666/* {Examining Directories}
667 */
668
669long scm_tc16_dir;
670
a1ec6916 671SCM_DEFINE (scm_directory_stream_p, "directory-stream?", 1, 0, 0,
1bbd0b84 672 (SCM obj),
4079f87e
GB
673"Returns a boolean indicating whether @var{object} is a directory stream
674as returned by @code{opendir}.")
1bbd0b84 675#define FUNC_NAME s_scm_directory_stream_p
77242ff9 676{
0c95b57d 677 return SCM_BOOL(SCM_DIRP (obj));
77242ff9 678}
1bbd0b84 679#undef FUNC_NAME
77242ff9 680
a1ec6916 681SCM_DEFINE (scm_opendir, "opendir", 1, 0, 0,
1bbd0b84 682 (SCM dirname),
4079f87e
GB
683"Open the directory specified by @var{path} and return a directory
684stream.")
1bbd0b84 685#define FUNC_NAME s_scm_opendir
0f2d19dd
JB
686{
687 DIR *ds;
3b3b36dd 688 SCM_VALIDATE_ROSTRING (1,dirname);
89958ad0 689 SCM_COERCE_SUBSTR (dirname);
89958ad0 690 SCM_SYSCALL (ds = opendir (SCM_ROCHARS (dirname)));
02b754d3 691 if (ds == NULL)
1bbd0b84 692 SCM_SYSERROR;
23a62151 693 SCM_RETURN_NEWSMOB (scm_tc16_dir | SCM_OPN, ds);
0f2d19dd 694}
1bbd0b84 695#undef FUNC_NAME
0f2d19dd
JB
696
697
a1ec6916 698SCM_DEFINE (scm_readdir, "readdir", 1, 0, 0,
1bbd0b84 699 (SCM port),
4079f87e
GB
700"Return (as a string) the next directory entry from the directory stream
701@var{stream}. If there is no remaining entry to be read then the
702end of file object is returned.")
1bbd0b84 703#define FUNC_NAME s_scm_readdir
0f2d19dd
JB
704{
705 struct dirent *rdent;
3b3b36dd 706 SCM_VALIDATE_OPDIR (1,port);
0f2d19dd
JB
707 errno = 0;
708 SCM_SYSCALL (rdent = readdir ((DIR *) SCM_CDR (port)));
02b754d3 709 if (errno != 0)
1bbd0b84 710 SCM_SYSERROR;
02b754d3
GH
711 return (rdent ? scm_makfromstr (rdent->d_name, NAMLEN (rdent), 0)
712 : SCM_EOF_VAL);
0f2d19dd 713}
1bbd0b84 714#undef FUNC_NAME
0f2d19dd
JB
715
716
717
a1ec6916 718SCM_DEFINE (scm_rewinddir, "rewinddir", 1, 0, 0,
1bbd0b84 719 (SCM port),
4079f87e
GB
720"Reset the directory port @var{stream} so that the next call to
721@code{readdir} will return the first directory entry.")
1bbd0b84 722#define FUNC_NAME s_scm_rewinddir
0f2d19dd 723{
3b3b36dd 724 SCM_VALIDATE_OPDIR (1,port);
0f2d19dd
JB
725 rewinddir ((DIR *) SCM_CDR (port));
726 return SCM_UNSPECIFIED;
727}
1bbd0b84 728#undef FUNC_NAME
0f2d19dd
JB
729
730
731
a1ec6916 732SCM_DEFINE (scm_closedir, "closedir", 1, 0, 0,
1bbd0b84 733 (SCM port),
4079f87e
GB
734"Close the directory stream @var{stream}.
735The return value is unspecified.")
1bbd0b84 736#define FUNC_NAME s_scm_closedir
0f2d19dd
JB
737{
738 int sts;
02b754d3 739
3b3b36dd 740 SCM_VALIDATE_DIR (1,port);
0f2d19dd
JB
741 if (SCM_CLOSEDP (port))
742 {
02b754d3 743 return SCM_UNSPECIFIED;
0f2d19dd
JB
744 }
745 SCM_SYSCALL (sts = closedir ((DIR *) SCM_CDR (port)));
02b754d3 746 if (sts != 0)
1bbd0b84 747 SCM_SYSERROR;
a6c64c3c 748 SCM_SETCAR (port, scm_tc16_dir);
02b754d3 749 return SCM_UNSPECIFIED;
0f2d19dd 750}
1bbd0b84 751#undef FUNC_NAME
0f2d19dd
JB
752
753
754
1cc91f1b 755
0f2d19dd 756static int
f8b16091 757scm_dir_print (SCM exp, SCM port, scm_print_state *pstate)
0f2d19dd 758{
f8b16091
MD
759 scm_puts ("#<", port);
760 if (SCM_CLOSEDP (exp))
761 scm_puts ("closed: ", port);
0d03da62 762 scm_puts ("directory stream ", port);
f8b16091
MD
763 scm_intprint (SCM_CDR (exp), 16, port);
764 scm_putc ('>', port);
0f2d19dd
JB
765 return 1;
766}
767
1cc91f1b 768
0f2d19dd 769static scm_sizet
1bbd0b84 770scm_dir_free (SCM p)
0f2d19dd
JB
771{
772 if (SCM_OPENP (p))
773 closedir ((DIR *) SCM_CDR (p));
774 return 0;
775}
776
0f2d19dd
JB
777\f
778/* {Navigating Directories}
779 */
780
781
a1ec6916 782SCM_DEFINE (scm_chdir, "chdir", 1, 0, 0,
1bbd0b84 783 (SCM str),
4079f87e
GB
784"Change the current working directory to @var{path}.
785The return value is unspecified.")
1bbd0b84 786#define FUNC_NAME s_scm_chdir
0f2d19dd
JB
787{
788 int ans;
02b754d3 789
3b3b36dd 790 SCM_VALIDATE_ROSTRING (1,str);
89958ad0
JB
791 SCM_COERCE_SUBSTR (str);
792 SCM_SYSCALL (ans = chdir (SCM_ROCHARS (str)));
02b754d3 793 if (ans != 0)
1bbd0b84 794 SCM_SYSERROR;
02b754d3 795 return SCM_UNSPECIFIED;
0f2d19dd 796}
1bbd0b84 797#undef FUNC_NAME
0f2d19dd 798
f25f761d 799#ifdef HAVE_GETCWD
a1ec6916 800SCM_DEFINE (scm_getcwd, "getcwd", 0, 0, 0,
1bbd0b84 801 (),
4079f87e 802"Returns the name of the current working directory.")
1bbd0b84 803#define FUNC_NAME s_scm_getcwd
0f2d19dd 804{
0f2d19dd
JB
805 char *rv;
806
807 scm_sizet size = 100;
808 char *wd;
809 SCM result;
810
1bbd0b84 811 wd = scm_must_malloc (size, FUNC_NAME);
0f2d19dd
JB
812 while ((rv = getcwd (wd, size)) == 0 && errno == ERANGE)
813 {
814 scm_must_free (wd);
815 size *= 2;
1bbd0b84 816 wd = scm_must_malloc (size, FUNC_NAME);
0f2d19dd 817 }
02b754d3 818 if (rv == 0)
1bbd0b84 819 SCM_SYSERROR;
02b754d3 820 result = scm_makfromstr (wd, strlen (wd), 0);
0f2d19dd 821 scm_must_free (wd);
0f2d19dd 822 return result;
0f2d19dd 823}
1bbd0b84 824#undef FUNC_NAME
f25f761d 825#endif /* HAVE_GETCWD */
0f2d19dd
JB
826
827\f
828
cafc12ff
MD
829static int
830set_element (SELECT_TYPE *set, SCM element, int arg)
a48a89bc 831{
cafc12ff 832 int fd;
78446828 833 element = SCM_COERCE_OUTPORT (element);
0c95b57d 834 if (SCM_OPFPORTP (element))
77a76b64 835 fd = SCM_FPORT_FDES (element);
cafc12ff 836 else {
1bbd0b84 837 SCM_ASSERT (SCM_INUMP (element), element, arg, "select");
cafc12ff
MD
838 fd = SCM_INUM (element);
839 }
840 FD_SET (fd, set);
841 return fd;
a48a89bc 842}
1cc91f1b 843
cafc12ff
MD
844static int
845fill_select_type (SELECT_TYPE *set, SCM list, int arg)
0f2d19dd 846{
cafc12ff 847 int max_fd = 0, fd;
0c95b57d 848 if (SCM_VECTORP (list))
0f2d19dd 849 {
a48a89bc
GH
850 int len = SCM_LENGTH (list);
851 SCM *ve = SCM_VELTS (list);
852
853 while (len > 0)
854 {
cafc12ff
MD
855 fd = set_element (set, ve[len - 1], arg);
856 if (fd > max_fd)
857 max_fd = fd;
a48a89bc
GH
858 len--;
859 }
860 }
861 else
862 {
863 while (list != SCM_EOL)
864 {
cafc12ff
MD
865 fd = set_element (set, SCM_CAR (list), arg);
866 if (fd > max_fd)
867 max_fd = fd;
a48a89bc
GH
868 list = SCM_CDR (list);
869 }
0f2d19dd 870 }
cafc12ff
MD
871
872 return max_fd;
0f2d19dd
JB
873}
874
a48a89bc
GH
875static SCM
876get_element (SELECT_TYPE *set, SCM element, SCM list)
877{
78446828 878 element = SCM_COERCE_OUTPORT (element);
0c95b57d 879 if (SCM_OPFPORTP (element))
a48a89bc 880 {
77a76b64 881 if (FD_ISSET (SCM_FPORT_FDES (element), set))
a48a89bc
GH
882 list = scm_cons (element, list);
883 }
884 else if (SCM_INUMP (element))
885 {
886 if (FD_ISSET (SCM_INUM (element), set))
887 list = scm_cons (element, list);
888 }
889 return list;
890}
1cc91f1b 891
0f2d19dd 892static SCM
a48a89bc 893retrieve_select_type (SELECT_TYPE *set, SCM list)
0f2d19dd 894{
a48a89bc
GH
895 SCM answer_list = SCM_EOL;
896
0c95b57d 897 if (SCM_VECTORP (list))
0f2d19dd 898 {
a48a89bc
GH
899 int len = SCM_LENGTH (list);
900 SCM *ve = SCM_VELTS (list);
901
902 while (len > 0)
0f2d19dd 903 {
a48a89bc
GH
904 answer_list = get_element (set, ve[len - 1], answer_list);
905 len--;
0f2d19dd 906 }
a48a89bc
GH
907 return scm_vector (answer_list);
908 }
909 else
910 {
911 /* list is a list. */
912 while (list != SCM_EOL)
0f2d19dd 913 {
a48a89bc
GH
914 answer_list = get_element (set, SCM_CAR (list), answer_list);
915 list = SCM_CDR (list);
0f2d19dd 916 }
a48a89bc 917 return answer_list;
0f2d19dd 918 }
0f2d19dd
JB
919}
920
f25f761d 921#ifdef HAVE_SELECT
1bbd0b84 922/* Static helper functions above refer to s_scm_select directly as s_select */
a1ec6916 923SCM_DEFINE (scm_select, "select", 3, 2, 0,
1bbd0b84 924 (SCM reads, SCM writes, SCM excepts, SCM secs, SCM usecs),
4079f87e
GB
925"@var{reads}, @var{writes} and @var{excepts} can be lists or vectors: it
926doesn't matter which, but the corresponding object returned will be
927of the same type.
928Each element is a port or file descriptor on which to wait for
929readability, writeability
930or exceptional conditions respectively. @var{secs} and @var{usecs}
931optionally specify a timeout: @var{secs} can be specified alone, as
932either an integer or a real number, or both @var{secs} and @var{usecs}
933can be specified as integers, in which case @var{usecs} is an additional
934timeout expressed in microseconds.
935
936Buffered input or output data is (currently, but this may change)
937ignored: select uses the underlying file descriptor of a port
938(@code{char-ready?} will check input buffers, output buffers are
939problematic).
940
941The return value is a list of subsets of the input lists or vectors for
942which the requested condition has been met.
943
944It is not quite compatible with scsh's select: scsh checks port buffers,
945doesn't accept input lists or a microsecond timeout, returns multiple
946values instead of a list and has an additional select! interface.
947")
1bbd0b84 948#define FUNC_NAME s_scm_select
0f2d19dd 949{
0f2d19dd
JB
950 struct timeval timeout;
951 struct timeval * time_p;
952 SELECT_TYPE read_set;
953 SELECT_TYPE write_set;
954 SELECT_TYPE except_set;
cafc12ff 955 int max_fd, fd;
0f2d19dd
JB
956 int sreturn;
957
a48a89bc 958#define assert_set(x, arg) \
0c95b57d 959 SCM_ASSERT (scm_ilength (x) >= 0 || (SCM_VECTORP (x)), \
1bbd0b84 960 x, arg, FUNC_NAME)
a48a89bc
GH
961 assert_set (reads, SCM_ARG1);
962 assert_set (writes, SCM_ARG2);
963 assert_set (excepts, SCM_ARG3);
964#undef assert_set
0f2d19dd
JB
965
966 FD_ZERO (&read_set);
967 FD_ZERO (&write_set);
968 FD_ZERO (&except_set);
969
cafc12ff
MD
970 max_fd = fill_select_type (&read_set, reads, SCM_ARG1);
971 fd = fill_select_type (&write_set, writes, SCM_ARG2);
972 if (fd > max_fd)
973 max_fd = fd;
974 fd = fill_select_type (&except_set, excepts, SCM_ARG3);
975 if (fd > max_fd)
976 max_fd = fd;
0f2d19dd 977
a48a89bc 978 if (SCM_UNBNDP (secs) || SCM_FALSEP (secs))
0f2d19dd
JB
979 time_p = 0;
980 else
981 {
a48a89bc
GH
982 if (SCM_INUMP (secs))
983 {
984 timeout.tv_sec = SCM_INUM (secs);
985 if (SCM_UNBNDP (usecs))
986 timeout.tv_usec = 0;
987 else
988 {
3b3b36dd 989 SCM_VALIDATE_INUM (5,usecs);
a48a89bc
GH
990 timeout.tv_usec = SCM_INUM (usecs);
991 }
992 }
0f2d19dd 993 else
a48a89bc 994 {
1bbd0b84 995 double fl = scm_num2dbl (secs, FUNC_NAME);
a48a89bc
GH
996
997 if (!SCM_UNBNDP (usecs))
c1bfcf60 998 SCM_WRONG_TYPE_ARG (4, secs);
a48a89bc 999 if (fl > LONG_MAX)
c1bfcf60 1000 SCM_OUT_OF_RANGE (4, secs);
a48a89bc
GH
1001 timeout.tv_sec = (long) fl;
1002 timeout.tv_usec = (long) ((fl - timeout.tv_sec) * 1000000);
1003 }
0f2d19dd
JB
1004 time_p = &timeout;
1005 }
1006
44e8413c 1007#ifdef GUILE_ISELECT
cafc12ff 1008 sreturn = scm_internal_select (max_fd + 1,
44e8413c
MD
1009 &read_set, &write_set, &except_set, time_p);
1010#else
cafc12ff 1011 sreturn = select (max_fd + 1,
0f2d19dd 1012 &read_set, &write_set, &except_set, time_p);
44e8413c 1013#endif
0f2d19dd 1014 if (sreturn < 0)
1bbd0b84 1015 SCM_SYSERROR;
02b754d3
GH
1016 return scm_listify (retrieve_select_type (&read_set, reads),
1017 retrieve_select_type (&write_set, writes),
1018 retrieve_select_type (&except_set, excepts),
1019 SCM_UNDEFINED);
0f2d19dd 1020}
1bbd0b84 1021#undef FUNC_NAME
f25f761d 1022#endif /* HAVE_SELECT */
0f2d19dd
JB
1023
1024\f
4c1feaa5 1025
a1ec6916 1026SCM_DEFINE (scm_fcntl, "fcntl", 2, 0, 1,
1bbd0b84 1027 (SCM object, SCM cmd, SCM value),
4079f87e
GB
1028"Apply @var{command} to the specified file descriptor or the underlying
1029file descriptor of the specified port. @var{value} is an optional
1030integer argument.
1031
1032Values for @var{command} are:
1033
1034@table @code
1035@item F_DUPFD
1036Duplicate a file descriptor
1037@item F_GETFD
1038Get flags associated with the file descriptor.
1039@item F_SETFD
1040Set flags associated with the file descriptor to @var{value}.
1041@item F_GETFL
1042Get flags associated with the open file.
1043@item F_SETFL
1044Set flags associated with the open file to @var{value}
1045@item F_GETOWN
1046Get the process ID of a socket's owner, for @code{SIGIO} signals.
1047@item F_SETOWN
1048Set the process that owns a socket to @var{value}, for @code{SIGIO} signals.
1049@item FD_CLOEXEC
1050The value used to indicate the "close on exec" flag with @code{F_GETFL} or
1051@code{F_SETFL}.
1052@end table")
1bbd0b84 1053#define FUNC_NAME s_scm_fcntl
4c1feaa5
JB
1054{
1055 int rv;
6afcd3b2
GH
1056 int fdes;
1057 int ivalue;
4c1feaa5 1058
78446828
MV
1059 object = SCM_COERCE_OUTPORT (object);
1060
3b3b36dd 1061 SCM_VALIDATE_INUM (2,cmd);
0c95b57d 1062 if (SCM_OPFPORTP (object))
77a76b64 1063 fdes = SCM_FPORT_FDES (object);
6afcd3b2
GH
1064 else
1065 {
3b3b36dd 1066 SCM_VALIDATE_INUM (1,object);
6afcd3b2
GH
1067 fdes = SCM_INUM (object);
1068 }
1069 if (SCM_NULLP (value))
1070 ivalue = 0;
1071 else
1072 {
1bbd0b84 1073 SCM_ASSERT (SCM_INUMP (SCM_CAR (value)), value, SCM_ARG3, FUNC_NAME);
6afcd3b2
GH
1074 ivalue = SCM_INUM (SCM_CAR (value));
1075 }
77a76b64
JB
1076 SCM_SYSCALL (rv = fcntl (fdes, SCM_INUM (cmd), ivalue));
1077 if (rv == -1)
1bbd0b84 1078 SCM_SYSERROR;
4c1feaa5
JB
1079 return SCM_MAKINUM (rv);
1080}
1bbd0b84 1081#undef FUNC_NAME
6afcd3b2 1082
a1ec6916 1083SCM_DEFINE (scm_fsync, "fsync", 1, 0, 0,
1bbd0b84 1084 (SCM object),
4079f87e
GB
1085"Copies any unwritten data for the specified output file descriptor to disk.
1086If @var{port/fd} is a port, its buffer is flushed before the underlying
1087file descriptor is fsync'd.
1088The return value is unspecified.")
1bbd0b84 1089#define FUNC_NAME s_scm_fsync
6afcd3b2
GH
1090{
1091 int fdes;
1092
78446828
MV
1093 object = SCM_COERCE_OUTPORT (object);
1094
0c95b57d 1095 if (SCM_OPFPORTP (object))
6afcd3b2 1096 {
affc96b5 1097 scm_flush (object);
77a76b64 1098 fdes = SCM_FPORT_FDES (object);
6afcd3b2
GH
1099 }
1100 else
1101 {
3b3b36dd 1102 SCM_VALIDATE_INUM (1,object);
6afcd3b2
GH
1103 fdes = SCM_INUM (object);
1104 }
1105 if (fsync (fdes) == -1)
1bbd0b84 1106 SCM_SYSERROR;
6afcd3b2
GH
1107 return SCM_UNSPECIFIED;
1108}
1bbd0b84 1109#undef FUNC_NAME
0f2d19dd 1110
f25f761d 1111#ifdef HAVE_SYMLINK
a1ec6916 1112SCM_DEFINE (scm_symlink, "symlink", 2, 0, 0,
1bbd0b84 1113 (SCM oldpath, SCM newpath),
4079f87e
GB
1114"Create a symbolic link named @var{path-to} with the value (i.e., pointing to)
1115@var{path-from}. The return value is unspecified.")
1bbd0b84 1116#define FUNC_NAME s_scm_symlink
0f2d19dd 1117{
0f2d19dd 1118 int val;
02b754d3 1119
3b3b36dd
GB
1120 SCM_VALIDATE_ROSTRING (1,oldpath);
1121 SCM_VALIDATE_ROSTRING (2,newpath);
89958ad0
JB
1122 SCM_COERCE_SUBSTR (oldpath);
1123 SCM_COERCE_SUBSTR (newpath);
1124 SCM_SYSCALL (val = symlink(SCM_ROCHARS(oldpath), SCM_ROCHARS(newpath)));
02b754d3 1125 if (val != 0)
1bbd0b84 1126 SCM_SYSERROR;
02b754d3 1127 return SCM_UNSPECIFIED;
0f2d19dd 1128}
1bbd0b84 1129#undef FUNC_NAME
f25f761d 1130#endif /* HAVE_SYMLINK */
0f2d19dd 1131
f25f761d 1132#ifdef HAVE_READLINK
a1ec6916 1133SCM_DEFINE (scm_readlink, "readlink", 1, 0, 0,
1bbd0b84 1134 (SCM path),
4079f87e
GB
1135"Returns the value of the symbolic link named by
1136@var{path} (a string), i.e., the
1137file that the link points to.")
1bbd0b84 1138#define FUNC_NAME s_scm_readlink
0f2d19dd 1139{
6a738a25
JB
1140 int rv;
1141 int size = 100;
0f2d19dd
JB
1142 char *buf;
1143 SCM result;
3b3b36dd 1144 SCM_VALIDATE_ROSTRING (1,path);
89958ad0 1145 SCM_COERCE_SUBSTR (path);
1bbd0b84 1146 buf = scm_must_malloc (size, FUNC_NAME);
6a738a25 1147 while ((rv = readlink (SCM_ROCHARS (path), buf, size)) == size)
0f2d19dd
JB
1148 {
1149 scm_must_free (buf);
1150 size *= 2;
1bbd0b84 1151 buf = scm_must_malloc (size, FUNC_NAME);
0f2d19dd 1152 }
02b754d3 1153 if (rv == -1)
1bbd0b84 1154 SCM_SYSERROR;
02b754d3 1155 result = scm_makfromstr (buf, rv, 0);
0f2d19dd 1156 scm_must_free (buf);
0f2d19dd 1157 return result;
0f2d19dd 1158}
1bbd0b84 1159#undef FUNC_NAME
f25f761d 1160#endif /* HAVE_READLINK */
0f2d19dd 1161
f25f761d 1162#ifdef HAVE_LSTAT
a1ec6916 1163SCM_DEFINE (scm_lstat, "lstat", 1, 0, 0,
1bbd0b84 1164 (SCM str),
4079f87e
GB
1165"Similar to @code{stat}, but does not follow symbolic links, i.e.,
1166it will return information about a symbolic link itself, not the
1167file it points to. @var{path} must be a string.")
1bbd0b84 1168#define FUNC_NAME s_scm_lstat
0f2d19dd 1169{
02b754d3 1170 int rv;
0f2d19dd 1171 struct stat stat_temp;
02b754d3 1172
3b3b36dd 1173 SCM_VALIDATE_ROSTRING (1,str);
89958ad0
JB
1174 SCM_COERCE_SUBSTR (str);
1175 SCM_SYSCALL(rv = lstat(SCM_ROCHARS(str), &stat_temp));
02b754d3 1176 if (rv != 0)
3d8d56df
GH
1177 {
1178 int en = errno;
1179
5d2d2ffc 1180 SCM_SYSERROR_MSG ("~A: ~S",
3d8d56df
GH
1181 scm_listify (scm_makfrom0str (strerror (errno)),
1182 str,
5d2d2ffc 1183 SCM_UNDEFINED), en);
3d8d56df 1184 }
02b754d3 1185 return scm_stat2scm(&stat_temp);
0f2d19dd 1186}
1bbd0b84 1187#undef FUNC_NAME
f25f761d 1188#endif /* HAVE_LSTAT */
0f2d19dd 1189
a1ec6916 1190SCM_DEFINE (scm_copy_file, "copy-file", 2, 0, 0,
1bbd0b84 1191 (SCM oldfile, SCM newfile),
4079f87e
GB
1192"Copy the file specified by @var{path-from} to @var{path-to}.
1193The return value is unspecified.")
1bbd0b84 1194#define FUNC_NAME s_scm_copy_file
0f2d19dd
JB
1195{
1196 int oldfd, newfd;
1197 int n;
77a76b64 1198 char buf[BUFSIZ];
0f2d19dd
JB
1199 struct stat oldstat;
1200
3b3b36dd 1201 SCM_VALIDATE_ROSTRING (1,oldfile);
0f2d19dd
JB
1202 if (SCM_SUBSTRP (oldfile))
1203 oldfile = scm_makfromstr (SCM_ROCHARS (oldfile), SCM_ROLENGTH (oldfile), 0);
3b3b36dd 1204 SCM_VALIDATE_ROSTRING (2,newfile);
0f2d19dd
JB
1205 if (SCM_SUBSTRP (newfile))
1206 newfile = scm_makfromstr (SCM_ROCHARS (newfile), SCM_ROLENGTH (newfile), 0);
1207 if (stat (SCM_ROCHARS (oldfile), &oldstat) == -1)
1bbd0b84 1208 SCM_SYSERROR;
0f2d19dd
JB
1209 oldfd = open (SCM_ROCHARS (oldfile), O_RDONLY);
1210 if (oldfd == -1)
1bbd0b84 1211 SCM_SYSERROR;
02b754d3
GH
1212
1213 /* use POSIX flags instead of 07777?. */
0f2d19dd
JB
1214 newfd = open (SCM_ROCHARS (newfile), O_WRONLY | O_CREAT | O_TRUNC,
1215 oldstat.st_mode & 07777);
1216 if (newfd == -1)
1bbd0b84 1217 SCM_SYSERROR;
02b754d3 1218
0f2d19dd
JB
1219 while ((n = read (oldfd, buf, sizeof buf)) > 0)
1220 if (write (newfd, buf, n) != n)
1221 {
1222 close (oldfd);
1223 close (newfd);
1bbd0b84 1224 SCM_SYSERROR;
0f2d19dd
JB
1225 }
1226 close (oldfd);
1227 if (close (newfd) == -1)
1bbd0b84 1228 SCM_SYSERROR;
02b754d3 1229 return SCM_UNSPECIFIED;
0f2d19dd 1230}
1bbd0b84 1231#undef FUNC_NAME
0f2d19dd
JB
1232
1233\f
6a738a25
JB
1234/* Filename manipulation */
1235
1236SCM scm_dot_string;
1237
a1ec6916 1238SCM_DEFINE (scm_dirname, "dirname", 1, 0, 0,
1bbd0b84
GB
1239 (SCM filename),
1240"")
1241#define FUNC_NAME s_scm_dirname
6a738a25
JB
1242{
1243 char *s;
1244 int i, len;
3b3b36dd 1245 SCM_VALIDATE_ROSTRING (1,filename);
6a738a25
JB
1246 s = SCM_ROCHARS (filename);
1247 len = SCM_LENGTH (filename);
1248 i = len - 1;
1249 while (i >= 0 && s[i] == '/') --i;
1250 while (i >= 0 && s[i] != '/') --i;
1251 while (i >= 0 && s[i] == '/') --i;
1252 if (i < 0)
1253 {
1254 if (len > 0 && s[0] == '/')
1255 return scm_make_shared_substring (filename, SCM_INUM0, SCM_MAKINUM (1));
1256 else
1257 return scm_dot_string;
1258 }
1259 else
1260 return scm_make_shared_substring (filename, SCM_INUM0, SCM_MAKINUM (i + 1));
1261}
1bbd0b84 1262#undef FUNC_NAME
6a738a25 1263
a1ec6916 1264SCM_DEFINE (scm_basename, "basename", 1, 1, 0,
1bbd0b84
GB
1265 (SCM filename, SCM suffix),
1266"")
1267#define FUNC_NAME s_scm_basename
6a738a25
JB
1268{
1269 char *f, *s = 0;
1270 int i, j, len, end;
3b3b36dd 1271 SCM_VALIDATE_ROSTRING (1,filename);
6a738a25 1272 SCM_ASSERT (SCM_UNBNDP (suffix)
0c95b57d 1273 || (SCM_ROSTRINGP (suffix)),
6a738a25
JB
1274 suffix,
1275 SCM_ARG2,
1bbd0b84 1276 FUNC_NAME);
6a738a25
JB
1277 f = SCM_ROCHARS (filename);
1278 if (SCM_UNBNDP (suffix))
1279 j = -1;
1280 else
1281 {
1282 s = SCM_ROCHARS (suffix);
1283 j = SCM_LENGTH (suffix) - 1;
1284 }
1285 len = SCM_LENGTH (filename);
1286 i = len - 1;
1287 while (i >= 0 && f[i] == '/') --i;
1288 end = i;
1289 while (i >= 0 && j >= 0 && f[i] == s[j]) --i, --j;
1290 if (j == -1)
1291 end = i;
1292 while (i >= 0 && f[i] != '/') --i;
1293 if (i == end)
1294 {
1295 if (len > 0 && f[0] == '/')
1296 return scm_make_shared_substring (filename, SCM_INUM0, SCM_MAKINUM (1));
1297 else
1298 return scm_dot_string;
1299 }
1300 else
1301 return scm_make_shared_substring (filename,
1302 SCM_MAKINUM (i + 1),
1303 SCM_MAKINUM (end + 1));
1304}
1bbd0b84 1305#undef FUNC_NAME
6a738a25
JB
1306
1307
1308
1309\f
1cc91f1b 1310
0f2d19dd
JB
1311void
1312scm_init_filesys ()
0f2d19dd 1313{
23a62151
MD
1314 scm_tc16_dir = scm_make_smob_type_mfpe ("directory", 0,
1315 NULL, scm_dir_free,scm_dir_print, NULL);
0f2d19dd 1316
a163dda9
MD
1317 scm_dot_string = scm_permanent_object (scm_makfrom0str ("."));
1318
3d8d56df
GH
1319#ifdef O_RDONLY
1320scm_sysintern ("O_RDONLY", scm_long2num (O_RDONLY));
1321#endif
1322#ifdef O_WRONLY
1323scm_sysintern ("O_WRONLY", scm_long2num (O_WRONLY));
1324#endif
1325#ifdef O_RDWR
1326scm_sysintern ("O_RDWR", scm_long2num (O_RDWR));
1327#endif
1328#ifdef O_CREAT
1329scm_sysintern ("O_CREAT", scm_long2num (O_CREAT));
1330#endif
1331#ifdef O_EXCL
1332scm_sysintern ("O_EXCL", scm_long2num (O_EXCL));
1333#endif
1334#ifdef O_NOCTTY
1335scm_sysintern ("O_NOCTTY", scm_long2num (O_NOCTTY));
1336#endif
1337#ifdef O_TRUNC
1338scm_sysintern ("O_TRUNC", scm_long2num (O_TRUNC));
1339#endif
1340#ifdef O_APPEND
1341scm_sysintern ("O_APPEND", scm_long2num (O_APPEND));
1342#endif
6afcd3b2 1343#ifdef O_NONBLOCK
3d8d56df
GH
1344scm_sysintern ("O_NONBLOCK", scm_long2num (O_NONBLOCK));
1345#endif
1346#ifdef O_NDELAY
1347scm_sysintern ("O_NDELAY", scm_long2num (O_NDELAY));
1348#endif
1349#ifdef O_SYNC
1350scm_sysintern ("O_SYNC", scm_long2num (O_SYNC));
1351#endif
1352
4c1feaa5
JB
1353#ifdef F_DUPFD
1354scm_sysintern ("F_DUPFD", scm_long2num (F_DUPFD));
1355#endif
1356#ifdef F_GETFD
1357scm_sysintern ("F_GETFD", scm_long2num (F_GETFD));
1358#endif
1359#ifdef F_SETFD
1360scm_sysintern ("F_SETFD", scm_long2num (F_SETFD));
1361#endif
1362#ifdef F_GETFL
1363scm_sysintern ("F_GETFL", scm_long2num (F_GETFL));
1364#endif
1365#ifdef F_SETFL
1366scm_sysintern ("F_SETFL", scm_long2num (F_SETFL));
1367#endif
1368#ifdef F_GETOWN
1369scm_sysintern ("F_GETOWN", scm_long2num (F_GETOWN));
1370#endif
1371#ifdef F_SETOWN
1372scm_sysintern ("F_SETOWN", scm_long2num (F_SETOWN));
1373#endif
1374#ifdef FD_CLOEXEC
1375scm_sysintern ("FD_CLOEXEC", scm_long2num (FD_CLOEXEC));
1376#endif
3d8d56df 1377
0f2d19dd
JB
1378#include "filesys.x"
1379}