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