* configure.in: Check for <io.h>.
[bpt/guile.git] / libguile / filesys.c
CommitLineData
7dc6e754 1/* Copyright (C) 1996, 1997, 1998 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. */
0f2d19dd 41\f
3d8d56df 42#include <stdio.h>
0f2d19dd 43#include "_scm.h"
20e6290e
JB
44#include "genio.h"
45#include "smob.h"
52f4f4d6 46#include "feature.h"
3d8d56df 47#include "fports.h"
44e8413c 48#include "iselect.h"
0f2d19dd 49
20e6290e 50#include "filesys.h"
0f2d19dd
JB
51\f
52#ifdef TIME_WITH_SYS_TIME
53# include <sys/time.h>
54# include <time.h>
55#else
56# if HAVE_SYS_TIME_H
57# include <sys/time.h>
58# else
59# include <time.h>
60# endif
61#endif
62
63#ifdef HAVE_UNISTD_H
64#include <unistd.h>
65#endif
66
3594582b 67#ifdef LIBC_H_WITH_UNISTD_H
1f9e2226
JB
68#include <libc.h>
69#endif
70
0f2d19dd
JB
71#ifdef HAVE_SYS_SELECT_H
72#include <sys/select.h>
73#endif
74
1f9e2226
JB
75#ifdef HAVE_STRING_H
76#include <string.h>
77#endif
78
8cc71382 79#include <sys/types.h>
0f2d19dd
JB
80#include <sys/stat.h>
81#include <fcntl.h>
82
83#include <pwd.h>
84
85
0f2d19dd
JB
86#if HAVE_DIRENT_H
87# include <dirent.h>
88# define NAMLEN(dirent) strlen((dirent)->d_name)
89#else
90# define dirent direct
91# define NAMLEN(dirent) (dirent)->d_namlen
92# if HAVE_SYS_NDIR_H
93# include <sys/ndir.h>
94# endif
95# if HAVE_SYS_DIR_H
96# include <sys/dir.h>
97# endif
98# if HAVE_NDIR_H
99# include <ndir.h>
100# endif
101#endif
102
d7b8a21a
JB
103/* Ultrix has S_IFSOCK, but no S_ISSOCK. Ipe! */
104#if defined (S_IFSOCK) && ! defined (S_ISSOCK)
105#define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
106#endif
0f2d19dd
JB
107\f
108
0f2d19dd
JB
109
110\f
111
112/* {Permissions}
113 */
114
3d8d56df 115SCM_PROC (s_chown, "chown", 3, 0, 0, scm_chown);
1cc91f1b 116
0f2d19dd 117SCM
6afcd3b2
GH
118scm_chown (object, owner, group)
119 SCM object;
0f2d19dd
JB
120 SCM owner;
121 SCM group;
0f2d19dd 122{
6afcd3b2
GH
123 int rv;
124 int fdes;
02b754d3 125
78446828
MV
126 object = SCM_COERCE_OUTPORT (object);
127
3d8d56df
GH
128 SCM_ASSERT (SCM_INUMP (owner), owner, SCM_ARG2, s_chown);
129 SCM_ASSERT (SCM_INUMP (group), group, SCM_ARG3, s_chown);
6afcd3b2
GH
130 if (SCM_INUMP (object) || (SCM_NIMP (object) && SCM_OPFPORTP (object)))
131 {
132 if (SCM_INUMP (object))
133 fdes = SCM_INUM (object);
134 else
77a76b64 135 fdes = SCM_FPORT_FDES (object);
6afcd3b2
GH
136 SCM_SYSCALL (rv = fchown (fdes, SCM_INUM (owner), SCM_INUM (group)));
137 }
138 else
139 {
140 SCM_ASSERT (SCM_NIMP (object) && SCM_ROSTRINGP (object),
141 object, SCM_ARG1, s_chown);
142 SCM_COERCE_SUBSTR (object);
143 SCM_SYSCALL (rv = chown (SCM_ROCHARS (object),
144 SCM_INUM (owner), SCM_INUM (group)));
145 }
146 if (rv == -1)
3d8d56df 147 scm_syserror (s_chown);
02b754d3 148 return SCM_UNSPECIFIED;
0f2d19dd
JB
149}
150
151
3d8d56df 152SCM_PROC (s_chmod, "chmod", 2, 0, 0, scm_chmod);
1cc91f1b 153
0f2d19dd 154SCM
6afcd3b2
GH
155scm_chmod (object, mode)
156 SCM object;
0f2d19dd 157 SCM mode;
0f2d19dd
JB
158{
159 int rv;
6afcd3b2
GH
160 int fdes;
161
78446828
MV
162 object = SCM_COERCE_OUTPORT (object);
163
3d8d56df 164 SCM_ASSERT (SCM_INUMP (mode), mode, SCM_ARG2, s_chmod);
6afcd3b2 165 if (SCM_INUMP (object) || (SCM_NIMP (object) && SCM_OPFPORTP (object)))
89958ad0 166 {
6afcd3b2
GH
167 if (SCM_INUMP (object))
168 fdes = SCM_INUM (object);
169 else
77a76b64 170 fdes = SCM_FPORT_FDES (object);
6afcd3b2 171 SCM_SYSCALL (rv = fchmod (fdes, SCM_INUM (mode)));
89958ad0 172 }
0f2d19dd
JB
173 else
174 {
6afcd3b2
GH
175 SCM_ASSERT (SCM_NIMP (object) && SCM_ROSTRINGP (object),
176 object, SCM_ARG1, s_chmod);
177 SCM_COERCE_SUBSTR (object);
178 SCM_SYSCALL (rv = chmod (SCM_ROCHARS (object), SCM_INUM (mode)));
0f2d19dd 179 }
6afcd3b2 180 if (rv == -1)
3d8d56df 181 scm_syserror (s_chmod);
02b754d3 182 return SCM_UNSPECIFIED;
0f2d19dd
JB
183}
184
185SCM_PROC (s_umask, "umask", 0, 1, 0, scm_umask);
1cc91f1b 186
0f2d19dd
JB
187SCM
188scm_umask (mode)
189 SCM mode;
0f2d19dd
JB
190{
191 mode_t mask;
192 if (SCM_UNBNDP (mode))
193 {
194 mask = umask (0);
195 umask (mask);
196 }
197 else
198 {
199 SCM_ASSERT (SCM_INUMP (mode), mode, SCM_ARG1, s_umask);
200 mask = umask (SCM_INUM (mode));
201 }
202 return SCM_MAKINUM (mask);
203}
204
205\f
0f2d19dd 206
6afcd3b2 207SCM_PROC (s_open_fdes, "open-fdes", 2, 1, 0, scm_open_fdes);
0f2d19dd 208SCM
6afcd3b2 209scm_open_fdes (SCM path, SCM flags, SCM mode)
0f2d19dd
JB
210{
211 int fd;
3d8d56df 212 int iflags;
6afcd3b2 213 int imode;
0f2d19dd 214
6afcd3b2
GH
215 SCM_ASSERT (SCM_NIMP (path) && SCM_ROSTRINGP (path), path, SCM_ARG1,
216 s_open_fdes);
217 SCM_COERCE_SUBSTR (path);
218 iflags = scm_num2long (flags, (char *) SCM_ARG2, s_open_fdes);
0f2d19dd 219
3d8d56df 220 if (SCM_UNBNDP (mode))
6afcd3b2 221 imode = 0666;
0f2d19dd
JB
222 else
223 {
6afcd3b2
GH
224 SCM_ASSERT (SCM_INUMP (mode), mode, SCM_ARG3, s_open_fdes);
225 imode = SCM_INUM (mode);
0f2d19dd 226 }
6afcd3b2 227 SCM_SYSCALL (fd = open (SCM_ROCHARS (path), iflags, imode));
3d8d56df 228 if (fd == -1)
6afcd3b2 229 scm_syserror (s_open_fdes);
6afcd3b2
GH
230 return SCM_MAKINUM (fd);
231}
232
233SCM_PROC (s_open, "open", 2, 1, 0, scm_open);
234SCM
235scm_open (SCM path, SCM flags, SCM mode)
236{
237 SCM newpt;
238 char *port_mode;
239 int fd;
6afcd3b2
GH
240 int iflags;
241
242 fd = SCM_INUM (scm_open_fdes (path, flags, mode));
243 iflags = scm_num2long (flags, (char *) SCM_ARG2, s_open_fdes);
3d8d56df 244 if (iflags & O_RDWR)
77a76b64
JB
245 {
246 if (iflags & O_APPEND)
247 port_mode = "a+";
248 else if (iflags & O_CREAT)
249 port_mode = "w+";
250 else
251 port_mode = "r+";
252 }
3d8d56df 253 else {
77a76b64
JB
254 if (iflags & O_APPEND)
255 port_mode = "a";
256 else if (iflags & O_WRONLY)
3d8d56df
GH
257 port_mode = "w";
258 else
259 port_mode = "r";
260 }
77a76b64 261 newpt = scm_fdes_to_port (fd, port_mode, path);
3d8d56df 262 return newpt;
0f2d19dd
JB
263}
264
eadd48de
GH
265SCM_PROC (s_close, "close", 1, 0, 0, scm_close);
266SCM
267scm_close (SCM fd_or_port)
268{
269 int rv;
270 int fd;
271
78446828
MV
272 fd_or_port = SCM_COERCE_OUTPORT (fd_or_port);
273
eadd48de
GH
274 if (SCM_NIMP (fd_or_port) && SCM_PORTP (fd_or_port))
275 return scm_close_port (fd_or_port);
276 SCM_ASSERT (SCM_INUMP (fd_or_port), fd_or_port, SCM_ARG1, s_close);
277 fd = SCM_INUM (fd_or_port);
eadd48de 278 scm_evict_ports (fd); /* see scsh manual. */
a9488d12 279 SCM_SYSCALL (rv = close (fd));
eadd48de
GH
280 /* following scsh, closing an already closed file descriptor is
281 not an error. */
282 if (rv < 0 && errno != EBADF)
283 scm_syserror (s_close);
eadd48de
GH
284 return (rv < 0) ? SCM_BOOL_F : SCM_BOOL_T;
285}
286
0f2d19dd
JB
287\f
288/* {Files}
289 */
1cc91f1b 290
ae5253c5
GH
291SCM_SYMBOL (scm_sym_regular, "regular");
292SCM_SYMBOL (scm_sym_directory, "directory");
293SCM_SYMBOL (scm_sym_symlink, "symlink");
294SCM_SYMBOL (scm_sym_block_special, "block-special");
295SCM_SYMBOL (scm_sym_char_special, "char-special");
296SCM_SYMBOL (scm_sym_fifo, "fifo");
297SCM_SYMBOL (scm_sym_sock, "socket");
298SCM_SYMBOL (scm_sym_unknown, "unknown");
299
1cc91f1b
JB
300static SCM scm_stat2scm SCM_P ((struct stat *stat_temp));
301
0f2d19dd
JB
302static SCM
303scm_stat2scm (stat_temp)
304 struct stat *stat_temp;
0f2d19dd 305{
a8741caa 306 SCM ans = scm_make_vector (SCM_MAKINUM (15), SCM_UNSPECIFIED);
0f2d19dd 307 SCM *ve = SCM_VELTS (ans);
ae5253c5 308
0f2d19dd
JB
309 ve[0] = scm_ulong2num ((unsigned long) stat_temp->st_dev);
310 ve[1] = scm_ulong2num ((unsigned long) stat_temp->st_ino);
311 ve[2] = scm_ulong2num ((unsigned long) stat_temp->st_mode);
312 ve[3] = scm_ulong2num ((unsigned long) stat_temp->st_nlink);
313 ve[4] = scm_ulong2num ((unsigned long) stat_temp->st_uid);
314 ve[5] = scm_ulong2num ((unsigned long) stat_temp->st_gid);
315#ifdef HAVE_ST_RDEV
316 ve[6] = scm_ulong2num ((unsigned long) stat_temp->st_rdev);
317#else
318 ve[6] = SCM_BOOL_F;
319#endif
320 ve[7] = scm_ulong2num ((unsigned long) stat_temp->st_size);
321 ve[8] = scm_ulong2num ((unsigned long) stat_temp->st_atime);
322 ve[9] = scm_ulong2num ((unsigned long) stat_temp->st_mtime);
323 ve[10] = scm_ulong2num ((unsigned long) stat_temp->st_ctime);
324#ifdef HAVE_ST_BLKSIZE
325 ve[11] = scm_ulong2num ((unsigned long) stat_temp->st_blksize);
326#else
327 ve[11] = scm_ulong2num (4096L);
328#endif
329#ifdef HAVE_ST_BLOCKS
330 ve[12] = scm_ulong2num ((unsigned long) stat_temp->st_blocks);
331#else
332 ve[12] = SCM_BOOL_F;
333#endif
ae5253c5
GH
334 {
335 int mode = stat_temp->st_mode;
336
337 if (S_ISREG (mode))
338 ve[13] = scm_sym_regular;
339 else if (S_ISDIR (mode))
340 ve[13] = scm_sym_directory;
341 else if (S_ISLNK (mode))
342 ve[13] = scm_sym_symlink;
343 else if (S_ISBLK (mode))
344 ve[13] = scm_sym_block_special;
345 else if (S_ISCHR (mode))
346 ve[13] = scm_sym_char_special;
347 else if (S_ISFIFO (mode))
348 ve[13] = scm_sym_fifo;
349 else if (S_ISSOCK (mode))
350 ve[13] = scm_sym_sock;
351 else
352 ve[13] = scm_sym_unknown;
353
354 ve[14] = SCM_MAKINUM ((~S_IFMT) & mode);
355
356 /* the layout of the bits in ve[14] is intended to be portable.
357 If there are systems that don't follow the usual convention,
358 the following could be used:
359
360 tmp = 0;
361 if (S_ISUID & mode) tmp += 1;
362 tmp <<= 1;
363 if (S_IRGRP & mode) tmp += 1;
364 tmp <<= 1;
365 if (S_ISVTX & mode) tmp += 1;
366 tmp <<= 1;
367 if (S_IRUSR & mode) tmp += 1;
368 tmp <<= 1;
369 if (S_IWUSR & mode) tmp += 1;
370 tmp <<= 1;
371 if (S_IXUSR & mode) tmp += 1;
372 tmp <<= 1;
373 if (S_IWGRP & mode) tmp += 1;
374 tmp <<= 1;
375 if (S_IXGRP & mode) tmp += 1;
376 tmp <<= 1;
377 if (S_IROTH & mode) tmp += 1;
378 tmp <<= 1;
379 if (S_IWOTH & mode) tmp += 1;
380 tmp <<= 1;
381 if (S_IXOTH & mode) tmp += 1;
382
383 ve[14] = SCM_MAKINUM (tmp);
384
385 */
386 }
0f2d19dd
JB
387
388 return ans;
389}
390
3d8d56df 391SCM_PROC (s_stat, "stat", 1, 0, 0, scm_stat);
1cc91f1b 392
0f2d19dd 393SCM
6afcd3b2
GH
394scm_stat (object)
395 SCM object;
0f2d19dd 396{
6afcd3b2
GH
397 int rv;
398 int fdes;
0f2d19dd
JB
399 struct stat stat_temp;
400
1ea47048
MD
401 if (SCM_INUMP (object))
402 SCM_SYSCALL (rv = fstat (SCM_INUM (object), &stat_temp));
403 else
0f2d19dd 404 {
1ea47048
MD
405 SCM_ASSERT (SCM_NIMP (object), object, SCM_ARG1, s_stat);
406 if (SCM_ROSTRINGP (object))
407 {
408 SCM_COERCE_SUBSTR (object);
409 SCM_SYSCALL (rv = stat (SCM_ROCHARS (object), &stat_temp));
410 }
c0ebd8c5 411 else
0f2d19dd 412 {
1ea47048
MD
413 object = SCM_COERCE_OUTPORT (object);
414 SCM_ASSERT (SCM_OPFPORTP (object), object, SCM_ARG1, s_stat);
77a76b64 415 fdes = SCM_FPORT_FDES (object);
1ea47048 416 SCM_SYSCALL (rv = fstat (fdes, &stat_temp));
0f2d19dd 417 }
6afcd3b2
GH
418 }
419 if (rv == -1)
3d8d56df
GH
420 {
421 int en = errno;
422
423 scm_syserror_msg (s_stat, "%s: %S",
424 scm_listify (scm_makfrom0str (strerror (errno)),
6afcd3b2 425 object,
3d8d56df
GH
426 SCM_UNDEFINED),
427 en);
428 }
02b754d3 429 return scm_stat2scm (&stat_temp);
0f2d19dd
JB
430}
431
0f2d19dd
JB
432\f
433/* {Modifying Directories}
434 */
435
3d8d56df 436SCM_PROC (s_link, "link", 2, 0, 0, scm_link);
1cc91f1b 437
0f2d19dd 438SCM
3d8d56df 439scm_link (oldpath, newpath)
0f2d19dd
JB
440 SCM oldpath;
441 SCM newpath;
0f2d19dd
JB
442{
443 int val;
02b754d3 444
6afcd3b2
GH
445 SCM_ASSERT (SCM_NIMP (oldpath) && SCM_ROSTRINGP (oldpath), oldpath,
446 SCM_ARG1, s_link);
0f2d19dd 447 if (SCM_SUBSTRP (oldpath))
6afcd3b2
GH
448 oldpath = scm_makfromstr (SCM_ROCHARS (oldpath),
449 SCM_ROLENGTH (oldpath), 0);
450 SCM_ASSERT (SCM_NIMP (newpath) && SCM_ROSTRINGP (newpath), newpath,
451 SCM_ARG2, s_link);
0f2d19dd 452 if (SCM_SUBSTRP (newpath))
6afcd3b2
GH
453 newpath = scm_makfromstr (SCM_ROCHARS (newpath),
454 SCM_ROLENGTH (newpath), 0);
0f2d19dd 455 SCM_SYSCALL (val = link (SCM_ROCHARS (oldpath), SCM_ROCHARS (newpath)));
02b754d3 456 if (val != 0)
3d8d56df 457 scm_syserror (s_link);
02b754d3 458 return SCM_UNSPECIFIED;
0f2d19dd
JB
459}
460
461
462
3d8d56df 463SCM_PROC (s_rename, "rename-file", 2, 0, 0, scm_rename);
1cc91f1b 464
0f2d19dd 465SCM
3d8d56df 466scm_rename (oldname, newname)
0f2d19dd
JB
467 SCM oldname;
468 SCM newname;
0f2d19dd
JB
469{
470 int rv;
89958ad0
JB
471 SCM_ASSERT (SCM_NIMP (oldname) && SCM_ROSTRINGP (oldname), oldname, SCM_ARG1,
472 s_rename);
473 SCM_ASSERT (SCM_NIMP (newname) && SCM_ROSTRINGP (newname), newname, SCM_ARG2,
474 s_rename);
475 SCM_COERCE_SUBSTR (oldname);
476 SCM_COERCE_SUBSTR (newname);
0f2d19dd 477#ifdef HAVE_RENAME
89958ad0 478 SCM_SYSCALL (rv = rename (SCM_ROCHARS (oldname), SCM_ROCHARS (newname)));
0f2d19dd 479#else
89958ad0 480 SCM_SYSCALL (rv = link (SCM_ROCHARS (oldname), SCM_ROCHARS (newname)));
02b754d3 481 if (rv == 0)
0f2d19dd 482 {
89958ad0 483 SCM_SYSCALL (rv = unlink (SCM_ROCHARS (oldname)));;
02b754d3 484 if (rv != 0)
0f2d19dd 485 /* unlink failed. remove new name */
89958ad0 486 SCM_SYSCALL (unlink (SCM_ROCHARS (newname)));
0f2d19dd 487 }
6afcd3b2 488#endif
02b754d3 489 if (rv != 0)
3d8d56df 490 scm_syserror (s_rename);
02b754d3 491 return SCM_UNSPECIFIED;
0f2d19dd
JB
492}
493
494
3d8d56df 495SCM_PROC(s_delete_file, "delete-file", 1, 0, 0, scm_delete_file);
1cc91f1b 496
2f3ed1ba 497SCM
3d8d56df 498scm_delete_file (str)
2f3ed1ba 499 SCM str;
2f3ed1ba
JB
500{
501 int ans;
6afcd3b2
GH
502 SCM_ASSERT (SCM_NIMP (str) && SCM_ROSTRINGP (str), str, SCM_ARG1,
503 s_delete_file);
89958ad0
JB
504 SCM_COERCE_SUBSTR (str);
505 SCM_SYSCALL (ans = unlink (SCM_ROCHARS (str)));
2f3ed1ba 506 if (ans != 0)
3d8d56df 507 scm_syserror (s_delete_file);
2f3ed1ba
JB
508 return SCM_UNSPECIFIED;
509}
510
6afcd3b2
GH
511SCM_PROC (s_truncate_file, "truncate-file", 2, 0, 0, scm_truncate_file);
512SCM
513scm_truncate_file (SCM object, SCM size)
514{
515 int rv;
516 scm_sizet csize;
517 int fdes;
518
78446828
MV
519 object = SCM_COERCE_OUTPORT (object);
520
6afcd3b2 521 csize = (scm_sizet) scm_num2long (size, (char *) SCM_ARG2, s_truncate_file);
6afcd3b2
GH
522 if (SCM_INUMP (object) || (SCM_NIMP (object) && SCM_OPFPORTP (object)))
523 {
524 if (SCM_INUMP (object))
525 fdes = SCM_INUM (object);
526 else
77a76b64 527 fdes = SCM_FPORT_FDES (object);
6afcd3b2
GH
528 SCM_SYSCALL (rv = ftruncate (fdes, csize));
529 }
530 else
531 {
532 SCM_ASSERT (SCM_NIMP (object) && SCM_ROSTRINGP (object),
533 object, SCM_ARG1, s_chown);
534 SCM_COERCE_SUBSTR (object);
535 SCM_SYSCALL (rv = truncate (SCM_ROCHARS (object), csize));
536 }
537 if (rv == -1)
538 scm_syserror (s_truncate_file);
6afcd3b2
GH
539 return SCM_UNSPECIFIED;
540}
0f2d19dd 541
3d8d56df 542SCM_PROC (s_mkdir, "mkdir", 1, 1, 0, scm_mkdir);
1cc91f1b 543
0f2d19dd 544SCM
3d8d56df 545scm_mkdir (path, mode)
0f2d19dd
JB
546 SCM path;
547 SCM mode;
0f2d19dd
JB
548{
549#ifdef HAVE_MKDIR
550 int rv;
551 mode_t mask;
89958ad0
JB
552 SCM_ASSERT (SCM_NIMP (path) && SCM_ROSTRINGP (path), path, SCM_ARG1,
553 s_mkdir);
554 SCM_COERCE_SUBSTR (path);
0f2d19dd
JB
555 if (SCM_UNBNDP (mode))
556 {
557 mask = umask (0);
558 umask (mask);
89958ad0 559 SCM_SYSCALL (rv = mkdir (SCM_ROCHARS (path), 0777 ^ mask));
0f2d19dd
JB
560 }
561 else
562 {
3d8d56df 563 SCM_ASSERT (SCM_INUMP (mode), mode, SCM_ARG2, s_mkdir);
89958ad0 564 SCM_SYSCALL (rv = mkdir (SCM_ROCHARS (path), SCM_INUM (mode)));
0f2d19dd 565 }
02b754d3 566 if (rv != 0)
3d8d56df 567 scm_syserror (s_mkdir);
02b754d3 568 return SCM_UNSPECIFIED;
0f2d19dd 569#else
3d8d56df 570 scm_sysmissing (s_mkdir);
02b754d3
GH
571 /* not reached. */
572 return SCM_BOOL_F;
0f2d19dd
JB
573#endif
574}
575
576
3d8d56df 577SCM_PROC (s_rmdir, "rmdir", 1, 0, 0, scm_rmdir);
1cc91f1b 578
0f2d19dd 579SCM
3d8d56df 580scm_rmdir (path)
0f2d19dd 581 SCM path;
0f2d19dd
JB
582{
583#ifdef HAVE_RMDIR
584 int val;
02b754d3 585
89958ad0
JB
586 SCM_ASSERT (SCM_NIMP (path) && SCM_ROSTRINGP (path), path, SCM_ARG1,
587 s_rmdir);
588 SCM_COERCE_SUBSTR (path);
589 SCM_SYSCALL (val = rmdir (SCM_ROCHARS (path)));
02b754d3 590 if (val != 0)
3d8d56df 591 scm_syserror (s_rmdir);
02b754d3 592 return SCM_UNSPECIFIED;
0f2d19dd 593#else
3d8d56df 594 scm_sysmissing (s_rmdir);
02b754d3
GH
595 /* not reached. */
596 return SCM_BOOL_F;
0f2d19dd
JB
597#endif
598}
599
600\f
601/* {Examining Directories}
602 */
603
604long scm_tc16_dir;
605
3d8d56df 606SCM_PROC (s_opendir, "opendir", 1, 0, 0, scm_opendir);
1cc91f1b 607
0f2d19dd 608SCM
3d8d56df 609scm_opendir (dirname)
0f2d19dd 610 SCM dirname;
0f2d19dd
JB
611{
612 DIR *ds;
89958ad0
JB
613 SCM_ASSERT (SCM_NIMP (dirname) && SCM_ROSTRINGP (dirname), dirname, SCM_ARG1,
614 s_opendir);
615 SCM_COERCE_SUBSTR (dirname);
89958ad0 616 SCM_SYSCALL (ds = opendir (SCM_ROCHARS (dirname)));
02b754d3 617 if (ds == NULL)
3d8d56df 618 scm_syserror (s_opendir);
23a62151 619 SCM_RETURN_NEWSMOB (scm_tc16_dir | SCM_OPN, ds);
0f2d19dd
JB
620}
621
622
3d8d56df 623SCM_PROC (s_readdir, "readdir", 1, 0, 0, scm_readdir);
1cc91f1b 624
0f2d19dd 625SCM
3d8d56df 626scm_readdir (port)
0f2d19dd 627 SCM port;
0f2d19dd
JB
628{
629 struct dirent *rdent;
3d8d56df 630 SCM_ASSERT (SCM_NIMP (port) && SCM_OPDIRP (port), port, SCM_ARG1, s_readdir);
0f2d19dd
JB
631 errno = 0;
632 SCM_SYSCALL (rdent = readdir ((DIR *) SCM_CDR (port)));
02b754d3 633 if (errno != 0)
3d8d56df 634 scm_syserror (s_readdir);
02b754d3
GH
635 return (rdent ? scm_makfromstr (rdent->d_name, NAMLEN (rdent), 0)
636 : SCM_EOF_VAL);
0f2d19dd
JB
637}
638
639
640
641SCM_PROC (s_rewinddir, "rewinddir", 1, 0, 0, scm_rewinddir);
1cc91f1b 642
0f2d19dd
JB
643SCM
644scm_rewinddir (port)
645 SCM port;
0f2d19dd
JB
646{
647 SCM_ASSERT (SCM_NIMP (port) && SCM_OPDIRP (port), port, SCM_ARG1, s_rewinddir);
648 rewinddir ((DIR *) SCM_CDR (port));
649 return SCM_UNSPECIFIED;
650}
651
652
653
3d8d56df 654SCM_PROC (s_closedir, "closedir", 1, 0, 0, scm_closedir);
1cc91f1b 655
0f2d19dd 656SCM
3d8d56df 657scm_closedir (port)
0f2d19dd 658 SCM port;
0f2d19dd
JB
659{
660 int sts;
02b754d3 661
3d8d56df 662 SCM_ASSERT (SCM_NIMP (port) && SCM_DIRP (port), port, SCM_ARG1, s_closedir);
0f2d19dd
JB
663 if (SCM_CLOSEDP (port))
664 {
02b754d3 665 return SCM_UNSPECIFIED;
0f2d19dd
JB
666 }
667 SCM_SYSCALL (sts = closedir ((DIR *) SCM_CDR (port)));
02b754d3 668 if (sts != 0)
3d8d56df 669 scm_syserror (s_closedir);
a6c64c3c 670 SCM_SETCAR (port, scm_tc16_dir);
02b754d3 671 return SCM_UNSPECIFIED;
0f2d19dd
JB
672}
673
674
675
1cc91f1b
JB
676
677static int scm_dir_print SCM_P ((SCM sexp, SCM port, scm_print_state *pstate));
678
0f2d19dd 679static int
9882ea19 680scm_dir_print (sexp, port, pstate)
0f2d19dd
JB
681 SCM sexp;
682 SCM port;
9882ea19 683 scm_print_state *pstate;
0f2d19dd
JB
684{
685 scm_prinport (sexp, port, "directory");
686 return 1;
687}
688
1cc91f1b
JB
689
690static scm_sizet scm_dir_free SCM_P ((SCM p));
691
0f2d19dd
JB
692static scm_sizet
693scm_dir_free (p)
694 SCM p;
0f2d19dd
JB
695{
696 if (SCM_OPENP (p))
697 closedir ((DIR *) SCM_CDR (p));
698 return 0;
699}
700
0f2d19dd
JB
701\f
702/* {Navigating Directories}
703 */
704
705
3d8d56df 706SCM_PROC (s_chdir, "chdir", 1, 0, 0, scm_chdir);
1cc91f1b 707
0f2d19dd 708SCM
3d8d56df 709scm_chdir (str)
0f2d19dd 710 SCM str;
0f2d19dd
JB
711{
712 int ans;
02b754d3 713
89958ad0
JB
714 SCM_ASSERT (SCM_NIMP (str) && SCM_ROSTRINGP (str), str, SCM_ARG1, s_chdir);
715 SCM_COERCE_SUBSTR (str);
716 SCM_SYSCALL (ans = chdir (SCM_ROCHARS (str)));
02b754d3 717 if (ans != 0)
3d8d56df 718 scm_syserror (s_chdir);
02b754d3 719 return SCM_UNSPECIFIED;
0f2d19dd
JB
720}
721
722
723
3d8d56df 724SCM_PROC (s_getcwd, "getcwd", 0, 0, 0, scm_getcwd);
1cc91f1b 725
0f2d19dd 726SCM
3d8d56df 727scm_getcwd ()
0f2d19dd
JB
728{
729#ifdef HAVE_GETCWD
730 char *rv;
731
732 scm_sizet size = 100;
733 char *wd;
734 SCM result;
735
3d8d56df 736 wd = scm_must_malloc (size, s_getcwd);
0f2d19dd
JB
737 while ((rv = getcwd (wd, size)) == 0 && errno == ERANGE)
738 {
739 scm_must_free (wd);
740 size *= 2;
3d8d56df 741 wd = scm_must_malloc (size, s_getcwd);
0f2d19dd 742 }
02b754d3 743 if (rv == 0)
3d8d56df 744 scm_syserror (s_getcwd);
02b754d3 745 result = scm_makfromstr (wd, strlen (wd), 0);
0f2d19dd 746 scm_must_free (wd);
0f2d19dd
JB
747 return result;
748#else
3d8d56df 749 scm_sysmissing (s_getcwd);
02b754d3
GH
750 /* not reached. */
751 return SCM_BOOL_F;
0f2d19dd
JB
752#endif
753}
754
755\f
756
cafc12ff
MD
757SCM_PROC (s_select, "select", 3, 2, 0, scm_select);
758
1cc91f1b 759
cafc12ff
MD
760static int
761set_element (SELECT_TYPE *set, SCM element, int arg)
a48a89bc 762{
cafc12ff 763 int fd;
78446828 764 element = SCM_COERCE_OUTPORT (element);
77a76b64
JB
765 if (SCM_NIMP (element) && SCM_OPFPORTP (element))
766 fd = SCM_FPORT_FDES (element);
cafc12ff
MD
767 else {
768 SCM_ASSERT (SCM_INUMP (element), element, arg, s_select);
769 fd = SCM_INUM (element);
770 }
771 FD_SET (fd, set);
772 return fd;
a48a89bc 773}
1cc91f1b 774
cafc12ff
MD
775static int
776fill_select_type (SELECT_TYPE *set, SCM list, int arg)
0f2d19dd 777{
cafc12ff 778 int max_fd = 0, fd;
a48a89bc 779 if (SCM_NIMP (list) && SCM_VECTORP (list))
0f2d19dd 780 {
a48a89bc
GH
781 int len = SCM_LENGTH (list);
782 SCM *ve = SCM_VELTS (list);
783
784 while (len > 0)
785 {
cafc12ff
MD
786 fd = set_element (set, ve[len - 1], arg);
787 if (fd > max_fd)
788 max_fd = fd;
a48a89bc
GH
789 len--;
790 }
791 }
792 else
793 {
794 while (list != SCM_EOL)
795 {
cafc12ff
MD
796 fd = set_element (set, SCM_CAR (list), arg);
797 if (fd > max_fd)
798 max_fd = fd;
a48a89bc
GH
799 list = SCM_CDR (list);
800 }
0f2d19dd 801 }
cafc12ff
MD
802
803 return max_fd;
0f2d19dd
JB
804}
805
a48a89bc
GH
806static SCM
807get_element (SELECT_TYPE *set, SCM element, SCM list)
808{
78446828 809 element = SCM_COERCE_OUTPORT (element);
77a76b64 810 if (SCM_NIMP (element) && SCM_OPFPORTP (element))
a48a89bc 811 {
77a76b64 812 if (FD_ISSET (SCM_FPORT_FDES (element), set))
a48a89bc
GH
813 list = scm_cons (element, list);
814 }
815 else if (SCM_INUMP (element))
816 {
817 if (FD_ISSET (SCM_INUM (element), set))
818 list = scm_cons (element, list);
819 }
820 return list;
821}
1cc91f1b 822
0f2d19dd 823static SCM
a48a89bc 824retrieve_select_type (SELECT_TYPE *set, SCM list)
0f2d19dd 825{
a48a89bc
GH
826 SCM answer_list = SCM_EOL;
827
828 if (SCM_NIMP (list) && SCM_VECTORP (list))
0f2d19dd 829 {
a48a89bc
GH
830 int len = SCM_LENGTH (list);
831 SCM *ve = SCM_VELTS (list);
832
833 while (len > 0)
0f2d19dd 834 {
a48a89bc
GH
835 answer_list = get_element (set, ve[len - 1], answer_list);
836 len--;
0f2d19dd 837 }
a48a89bc
GH
838 return scm_vector (answer_list);
839 }
840 else
841 {
842 /* list is a list. */
843 while (list != SCM_EOL)
0f2d19dd 844 {
a48a89bc
GH
845 answer_list = get_element (set, SCM_CAR (list), answer_list);
846 list = SCM_CDR (list);
0f2d19dd 847 }
a48a89bc 848 return answer_list;
0f2d19dd 849 }
0f2d19dd
JB
850}
851
852
0f2d19dd 853SCM
a48a89bc 854scm_select (reads, writes, excepts, secs, usecs)
0f2d19dd
JB
855 SCM reads;
856 SCM writes;
857 SCM excepts;
858 SCM secs;
a48a89bc 859 SCM usecs;
0f2d19dd
JB
860{
861#ifdef HAVE_SELECT
862 struct timeval timeout;
863 struct timeval * time_p;
864 SELECT_TYPE read_set;
865 SELECT_TYPE write_set;
866 SELECT_TYPE except_set;
cafc12ff 867 int max_fd, fd;
0f2d19dd
JB
868 int sreturn;
869
a48a89bc
GH
870#define assert_set(x, arg) \
871 SCM_ASSERT (scm_ilength (x) > -1 || (SCM_NIMP (x) && SCM_VECTORP (x)), \
872 x, arg, s_select)
873 assert_set (reads, SCM_ARG1);
874 assert_set (writes, SCM_ARG2);
875 assert_set (excepts, SCM_ARG3);
876#undef assert_set
0f2d19dd
JB
877
878 FD_ZERO (&read_set);
879 FD_ZERO (&write_set);
880 FD_ZERO (&except_set);
881
cafc12ff
MD
882 max_fd = fill_select_type (&read_set, reads, SCM_ARG1);
883 fd = fill_select_type (&write_set, writes, SCM_ARG2);
884 if (fd > max_fd)
885 max_fd = fd;
886 fd = fill_select_type (&except_set, excepts, SCM_ARG3);
887 if (fd > max_fd)
888 max_fd = fd;
0f2d19dd 889
a48a89bc 890 if (SCM_UNBNDP (secs) || SCM_FALSEP (secs))
0f2d19dd
JB
891 time_p = 0;
892 else
893 {
a48a89bc
GH
894 if (SCM_INUMP (secs))
895 {
896 timeout.tv_sec = SCM_INUM (secs);
897 if (SCM_UNBNDP (usecs))
898 timeout.tv_usec = 0;
899 else
900 {
901 SCM_ASSERT (SCM_INUMP (usecs), usecs, SCM_ARG5, s_select);
902
903 timeout.tv_usec = SCM_INUM (usecs);
904 }
905 }
0f2d19dd 906 else
a48a89bc
GH
907 {
908 double fl = scm_num2dbl (secs, s_select);
909
910 if (!SCM_UNBNDP (usecs))
911 scm_wrong_type_arg (s_select, 4, secs);
912 if (fl > LONG_MAX)
913 scm_out_of_range (s_select, secs);
914 timeout.tv_sec = (long) fl;
915 timeout.tv_usec = (long) ((fl - timeout.tv_sec) * 1000000);
916 }
0f2d19dd
JB
917 time_p = &timeout;
918 }
919
44e8413c 920#ifdef GUILE_ISELECT
cafc12ff 921 sreturn = scm_internal_select (max_fd + 1,
44e8413c
MD
922 &read_set, &write_set, &except_set, time_p);
923#else
cafc12ff 924 sreturn = select (max_fd + 1,
0f2d19dd 925 &read_set, &write_set, &except_set, time_p);
44e8413c 926#endif
0f2d19dd 927 if (sreturn < 0)
3d8d56df 928 scm_syserror (s_select);
02b754d3
GH
929 return scm_listify (retrieve_select_type (&read_set, reads),
930 retrieve_select_type (&write_set, writes),
931 retrieve_select_type (&except_set, excepts),
932 SCM_UNDEFINED);
0f2d19dd 933#else
3d8d56df 934 scm_sysmissing (s_select);
02b754d3
GH
935 /* not reached. */
936 return SCM_BOOL_F;
0f2d19dd
JB
937#endif
938}
939
940\f
4c1feaa5 941
6afcd3b2 942SCM_PROC (s_fcntl, "fcntl", 2, 0, 1, scm_fcntl);
4c1feaa5 943SCM
6afcd3b2 944scm_fcntl (SCM object, SCM cmd, SCM value)
4c1feaa5
JB
945{
946 int rv;
6afcd3b2
GH
947 int fdes;
948 int ivalue;
4c1feaa5 949
78446828
MV
950 object = SCM_COERCE_OUTPORT (object);
951
4c1feaa5 952 SCM_ASSERT (SCM_INUMP (cmd), cmd, SCM_ARG2, s_fcntl);
6afcd3b2 953 if (SCM_NIMP (object) && SCM_OPFPORTP (object))
77a76b64 954 fdes = SCM_FPORT_FDES (object);
6afcd3b2
GH
955 else
956 {
957 SCM_ASSERT (SCM_INUMP (object), object, SCM_ARG1, s_fcntl);
958 fdes = SCM_INUM (object);
959 }
960 if (SCM_NULLP (value))
961 ivalue = 0;
962 else
963 {
964 SCM_ASSERT (SCM_INUMP (SCM_CAR (value)), value, SCM_ARG3, s_fcntl);
965 ivalue = SCM_INUM (SCM_CAR (value));
966 }
77a76b64
JB
967 SCM_SYSCALL (rv = fcntl (fdes, SCM_INUM (cmd), ivalue));
968 if (rv == -1)
4c1feaa5
JB
969 scm_syserror (s_fcntl);
970 return SCM_MAKINUM (rv);
971}
6afcd3b2
GH
972
973SCM_PROC (s_fsync, "fsync", 1, 0, 0, scm_fsync);
974SCM
975scm_fsync (SCM object)
976{
977 int fdes;
978
78446828
MV
979 object = SCM_COERCE_OUTPORT (object);
980
6afcd3b2
GH
981 if (SCM_NIMP (object) && SCM_OPFPORTP (object))
982 {
77a76b64
JB
983 scm_fflush (object);
984 fdes = SCM_FPORT_FDES (object);
6afcd3b2
GH
985 }
986 else
987 {
988 SCM_ASSERT (SCM_INUMP (object), object, SCM_ARG1, s_fsync);
989 fdes = SCM_INUM (object);
990 }
991 if (fsync (fdes) == -1)
992 scm_syserror (s_fsync);
6afcd3b2
GH
993 return SCM_UNSPECIFIED;
994}
0f2d19dd 995
3d8d56df 996SCM_PROC (s_symlink, "symlink", 2, 0, 0, scm_symlink);
1cc91f1b 997
0f2d19dd 998SCM
3d8d56df 999scm_symlink(oldpath, newpath)
0f2d19dd
JB
1000 SCM oldpath;
1001 SCM newpath;
0f2d19dd
JB
1002{
1003#ifdef HAVE_SYMLINK
1004 int val;
02b754d3 1005
89958ad0
JB
1006 SCM_ASSERT (SCM_NIMP (oldpath) && SCM_ROSTRINGP (oldpath), oldpath, SCM_ARG1,
1007 s_symlink);
1008 SCM_ASSERT (SCM_NIMP (newpath) && SCM_ROSTRINGP (newpath), newpath, SCM_ARG2,
1009 s_symlink);
1010 SCM_COERCE_SUBSTR (oldpath);
1011 SCM_COERCE_SUBSTR (newpath);
1012 SCM_SYSCALL (val = symlink(SCM_ROCHARS(oldpath), SCM_ROCHARS(newpath)));
02b754d3 1013 if (val != 0)
3d8d56df 1014 scm_syserror (s_symlink);
02b754d3 1015 return SCM_UNSPECIFIED;
0f2d19dd 1016#else
3d8d56df 1017 scm_sysmissing (s_symlink);
02b754d3
GH
1018 /* not reached. */
1019 return SCM_BOOL_F;
0f2d19dd
JB
1020#endif
1021}
1022
1023
3d8d56df 1024SCM_PROC (s_readlink, "readlink", 1, 0, 0, scm_readlink);
1cc91f1b 1025
0f2d19dd 1026SCM
3d8d56df 1027scm_readlink(path)
0f2d19dd 1028 SCM path;
0f2d19dd
JB
1029{
1030#ifdef HAVE_READLINK
6a738a25
JB
1031 int rv;
1032 int size = 100;
0f2d19dd
JB
1033 char *buf;
1034 SCM result;
89958ad0
JB
1035 SCM_ASSERT (SCM_NIMP (path) && SCM_ROSTRINGP (path), path, (char *) SCM_ARG1,
1036 s_readlink);
1037 SCM_COERCE_SUBSTR (path);
3d8d56df 1038 buf = scm_must_malloc (size, s_readlink);
6a738a25 1039 while ((rv = readlink (SCM_ROCHARS (path), buf, size)) == size)
0f2d19dd
JB
1040 {
1041 scm_must_free (buf);
1042 size *= 2;
3d8d56df 1043 buf = scm_must_malloc (size, s_readlink);
0f2d19dd 1044 }
02b754d3 1045 if (rv == -1)
3d8d56df 1046 scm_syserror (s_readlink);
02b754d3 1047 result = scm_makfromstr (buf, rv, 0);
0f2d19dd 1048 scm_must_free (buf);
0f2d19dd
JB
1049 return result;
1050#else
3d8d56df 1051 scm_sysmissing (s_readlink);
02b754d3
GH
1052 /* not reached. */
1053 return SCM_BOOL_F;
0f2d19dd
JB
1054#endif
1055}
1056
1057
3d8d56df 1058SCM_PROC (s_lstat, "lstat", 1, 0, 0, scm_lstat);
1cc91f1b 1059
0f2d19dd 1060SCM
3d8d56df 1061scm_lstat(str)
0f2d19dd 1062 SCM str;
0f2d19dd 1063{
02b754d3
GH
1064#ifdef HAVE_LSTAT
1065 int rv;
0f2d19dd 1066 struct stat stat_temp;
02b754d3 1067
89958ad0
JB
1068 SCM_ASSERT (SCM_NIMP (str) && SCM_ROSTRINGP (str), str, (char *) SCM_ARG1,
1069 s_lstat);
1070 SCM_COERCE_SUBSTR (str);
1071 SCM_SYSCALL(rv = lstat(SCM_ROCHARS(str), &stat_temp));
02b754d3 1072 if (rv != 0)
3d8d56df
GH
1073 {
1074 int en = errno;
1075
1076 scm_syserror_msg (s_lstat, "%s: %S",
1077 scm_listify (scm_makfrom0str (strerror (errno)),
1078 str,
1079 SCM_UNDEFINED),
1080 en);
1081 }
02b754d3 1082 return scm_stat2scm(&stat_temp);
0f2d19dd 1083#else
3d8d56df 1084 scm_sysmissing (s_lstat);
02b754d3
GH
1085 /* not reached. */
1086 return SCM_BOOL_F;
0f2d19dd
JB
1087#endif
1088}
1089
1090
3d8d56df 1091SCM_PROC (s_copy_file, "copy-file", 2, 0, 0, scm_copy_file);
1cc91f1b 1092
0f2d19dd 1093SCM
3d8d56df 1094scm_copy_file (oldfile, newfile)
0f2d19dd
JB
1095 SCM oldfile;
1096 SCM newfile;
0f2d19dd
JB
1097{
1098 int oldfd, newfd;
1099 int n;
77a76b64 1100 char buf[BUFSIZ];
0f2d19dd
JB
1101 struct stat oldstat;
1102
3d8d56df 1103 SCM_ASSERT (SCM_NIMP (oldfile) && SCM_ROSTRINGP (oldfile), oldfile, SCM_ARG1, s_copy_file);
0f2d19dd
JB
1104 if (SCM_SUBSTRP (oldfile))
1105 oldfile = scm_makfromstr (SCM_ROCHARS (oldfile), SCM_ROLENGTH (oldfile), 0);
3d8d56df 1106 SCM_ASSERT (SCM_NIMP (newfile) && SCM_ROSTRINGP (newfile), newfile, SCM_ARG2, s_copy_file);
0f2d19dd
JB
1107 if (SCM_SUBSTRP (newfile))
1108 newfile = scm_makfromstr (SCM_ROCHARS (newfile), SCM_ROLENGTH (newfile), 0);
1109 if (stat (SCM_ROCHARS (oldfile), &oldstat) == -1)
3d8d56df 1110 scm_syserror (s_copy_file);
0f2d19dd
JB
1111 oldfd = open (SCM_ROCHARS (oldfile), O_RDONLY);
1112 if (oldfd == -1)
3d8d56df 1113 scm_syserror (s_copy_file);
02b754d3
GH
1114
1115 /* use POSIX flags instead of 07777?. */
0f2d19dd
JB
1116 newfd = open (SCM_ROCHARS (newfile), O_WRONLY | O_CREAT | O_TRUNC,
1117 oldstat.st_mode & 07777);
1118 if (newfd == -1)
3d8d56df 1119 scm_syserror (s_copy_file);
02b754d3 1120
0f2d19dd
JB
1121 while ((n = read (oldfd, buf, sizeof buf)) > 0)
1122 if (write (newfd, buf, n) != n)
1123 {
1124 close (oldfd);
1125 close (newfd);
3d8d56df 1126 scm_syserror (s_copy_file);
0f2d19dd
JB
1127 }
1128 close (oldfd);
1129 if (close (newfd) == -1)
3d8d56df 1130 scm_syserror (s_copy_file);
02b754d3 1131 return SCM_UNSPECIFIED;
0f2d19dd
JB
1132}
1133
1134\f
6a738a25
JB
1135/* Filename manipulation */
1136
1137SCM scm_dot_string;
1138
1139SCM_PROC (s_dirname, "dirname", 1, 0, 0, scm_dirname);
1140
1141SCM
1142scm_dirname (SCM filename)
1143{
1144 char *s;
1145 int i, len;
1146 SCM_ASSERT (SCM_NIMP (filename) && SCM_ROSTRINGP (filename),
1147 filename,
1148 SCM_ARG1,
1149 s_dirname);
1150 s = SCM_ROCHARS (filename);
1151 len = SCM_LENGTH (filename);
1152 i = len - 1;
1153 while (i >= 0 && s[i] == '/') --i;
1154 while (i >= 0 && s[i] != '/') --i;
1155 while (i >= 0 && s[i] == '/') --i;
1156 if (i < 0)
1157 {
1158 if (len > 0 && s[0] == '/')
1159 return scm_make_shared_substring (filename, SCM_INUM0, SCM_MAKINUM (1));
1160 else
1161 return scm_dot_string;
1162 }
1163 else
1164 return scm_make_shared_substring (filename, SCM_INUM0, SCM_MAKINUM (i + 1));
1165}
1166
1167SCM_PROC (s_basename, "basename", 1, 1, 0, scm_basename);
1168
1169SCM
1170scm_basename (SCM filename, SCM suffix)
1171{
1172 char *f, *s = 0;
1173 int i, j, len, end;
1174 SCM_ASSERT (SCM_NIMP (filename) && SCM_ROSTRINGP (filename),
1175 filename,
1176 SCM_ARG1,
1177 s_basename);
1178 SCM_ASSERT (SCM_UNBNDP (suffix)
1179 || (SCM_NIMP (suffix) && SCM_ROSTRINGP (suffix)),
1180 suffix,
1181 SCM_ARG2,
1182 s_basename);
1183 f = SCM_ROCHARS (filename);
1184 if (SCM_UNBNDP (suffix))
1185 j = -1;
1186 else
1187 {
1188 s = SCM_ROCHARS (suffix);
1189 j = SCM_LENGTH (suffix) - 1;
1190 }
1191 len = SCM_LENGTH (filename);
1192 i = len - 1;
1193 while (i >= 0 && f[i] == '/') --i;
1194 end = i;
1195 while (i >= 0 && j >= 0 && f[i] == s[j]) --i, --j;
1196 if (j == -1)
1197 end = i;
1198 while (i >= 0 && f[i] != '/') --i;
1199 if (i == end)
1200 {
1201 if (len > 0 && f[0] == '/')
1202 return scm_make_shared_substring (filename, SCM_INUM0, SCM_MAKINUM (1));
1203 else
1204 return scm_dot_string;
1205 }
1206 else
1207 return scm_make_shared_substring (filename,
1208 SCM_MAKINUM (i + 1),
1209 SCM_MAKINUM (end + 1));
1210}
1211
1212
1213
1214\f
1cc91f1b 1215
0f2d19dd
JB
1216void
1217scm_init_filesys ()
0f2d19dd 1218{
52f4f4d6 1219 scm_add_feature ("i/o-extensions");
0f2d19dd 1220
23a62151
MD
1221 scm_tc16_dir = scm_make_smob_type_mfpe ("directory", 0,
1222 NULL, scm_dir_free,scm_dir_print, NULL);
0f2d19dd 1223
a163dda9
MD
1224 scm_dot_string = scm_permanent_object (scm_makfrom0str ("."));
1225
3d8d56df
GH
1226#ifdef O_RDONLY
1227scm_sysintern ("O_RDONLY", scm_long2num (O_RDONLY));
1228#endif
1229#ifdef O_WRONLY
1230scm_sysintern ("O_WRONLY", scm_long2num (O_WRONLY));
1231#endif
1232#ifdef O_RDWR
1233scm_sysintern ("O_RDWR", scm_long2num (O_RDWR));
1234#endif
1235#ifdef O_CREAT
1236scm_sysintern ("O_CREAT", scm_long2num (O_CREAT));
1237#endif
1238#ifdef O_EXCL
1239scm_sysintern ("O_EXCL", scm_long2num (O_EXCL));
1240#endif
1241#ifdef O_NOCTTY
1242scm_sysintern ("O_NOCTTY", scm_long2num (O_NOCTTY));
1243#endif
1244#ifdef O_TRUNC
1245scm_sysintern ("O_TRUNC", scm_long2num (O_TRUNC));
1246#endif
1247#ifdef O_APPEND
1248scm_sysintern ("O_APPEND", scm_long2num (O_APPEND));
1249#endif
6afcd3b2 1250#ifdef O_NONBLOCK
3d8d56df
GH
1251scm_sysintern ("O_NONBLOCK", scm_long2num (O_NONBLOCK));
1252#endif
1253#ifdef O_NDELAY
1254scm_sysintern ("O_NDELAY", scm_long2num (O_NDELAY));
1255#endif
1256#ifdef O_SYNC
1257scm_sysintern ("O_SYNC", scm_long2num (O_SYNC));
1258#endif
1259
4c1feaa5
JB
1260#ifdef F_DUPFD
1261scm_sysintern ("F_DUPFD", scm_long2num (F_DUPFD));
1262#endif
1263#ifdef F_GETFD
1264scm_sysintern ("F_GETFD", scm_long2num (F_GETFD));
1265#endif
1266#ifdef F_SETFD
1267scm_sysintern ("F_SETFD", scm_long2num (F_SETFD));
1268#endif
1269#ifdef F_GETFL
1270scm_sysintern ("F_GETFL", scm_long2num (F_GETFL));
1271#endif
1272#ifdef F_SETFL
1273scm_sysintern ("F_SETFL", scm_long2num (F_SETFL));
1274#endif
1275#ifdef F_GETOWN
1276scm_sysintern ("F_GETOWN", scm_long2num (F_GETOWN));
1277#endif
1278#ifdef F_SETOWN
1279scm_sysintern ("F_SETOWN", scm_long2num (F_SETOWN));
1280#endif
1281#ifdef FD_CLOEXEC
1282scm_sysintern ("FD_CLOEXEC", scm_long2num (FD_CLOEXEC));
1283#endif
3d8d56df 1284
0f2d19dd
JB
1285#include "filesys.x"
1286}