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