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