Ensure that shared substrings are handled properly when passed to
[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, 675 Mass Ave, Cambridge, MA 02139, USA.
16 *
17 * As a special exception, the Free Software Foundation gives permission
18 * for additional uses of the text contained in its release of GUILE.
19 *
20 * The exception is that, if you link the GUILE library with other files
21 * to produce an executable, this does not by itself cause the
22 * resulting executable to be covered by the GNU General Public License.
23 * Your use of that executable is in no way restricted on account of
24 * linking the GUILE library code into it.
25 *
26 * This exception does not however invalidate any other reasons why
27 * the executable file might be covered by the GNU General Public License.
28 *
29 * This exception applies only to the code released by the
30 * Free Software Foundation under the name GUILE. If you copy
31 * code from other Free Software Foundation releases into a copy of
32 * GUILE, as the General Public License permits, the exception does
33 * not apply to the code that you add in this way. To avoid misleading
34 * anyone as to the status of such modified files, you must delete
35 * this exception notice from them.
36 *
37 * If you write modifications of your own for GUILE, it is your choice
38 * whether to permit this exception to apply to your modifications.
39 * If you do not wish that, delete this exception notice.
40 */
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 fill_select_type SCM_P ((SELECT_TYPE * set, SCM list));
721
722 static void
723 fill_select_type (set, list)
724 SELECT_TYPE * set;
725 SCM list;
726 {
727 while (list != SCM_EOL)
728 {
729 if ( SCM_NIMP (SCM_CAR (list))
730 && (scm_tc16_fport == SCM_TYP16 (SCM_CAR (list)))
731 && SCM_OPPORTP (SCM_CAR (list)))
732 FD_SET (fileno ((FILE *)SCM_STREAM (SCM_CAR (list))), set);
733 else if (SCM_INUMP (SCM_CAR (list)))
734 FD_SET (SCM_INUM (SCM_CAR (list)), set);
735 list = SCM_CDR (list);
736 }
737 }
738
739
740 static SCM retrieve_select_type SCM_P ((SELECT_TYPE * set, SCM list));
741
742 static SCM
743 retrieve_select_type (set, list)
744 SELECT_TYPE * set;
745 SCM list;
746 {
747 SCM answer;
748 answer = SCM_EOL;
749 while (list != SCM_EOL)
750 {
751 if ( SCM_NIMP (SCM_CAR (list))
752 && (scm_tc16_fport == SCM_TYP16 (SCM_CAR (list)))
753 && SCM_OPPORTP (SCM_CAR (list)))
754 {
755 if (FD_ISSET (fileno ((FILE *)SCM_STREAM (SCM_CAR (list))), set))
756 answer = scm_cons (SCM_CAR (list), answer);
757 }
758 else if (SCM_INUMP (SCM_CAR (list)))
759 {
760 if (FD_ISSET (SCM_INUM (SCM_CAR (list)), set))
761 answer = scm_cons (SCM_CAR (list), answer);
762 }
763 list = SCM_CDR (list);
764 }
765 return answer;
766 }
767
768
769 /* {Checking for events}
770 */
771
772 SCM_PROC (s_select, "select", 3, 2, 0, scm_select);
773
774 SCM
775 scm_select (reads, writes, excepts, secs, msecs)
776 SCM reads;
777 SCM writes;
778 SCM excepts;
779 SCM secs;
780 SCM msecs;
781 {
782 #ifdef HAVE_SELECT
783 struct timeval timeout;
784 struct timeval * time_p;
785 SELECT_TYPE read_set;
786 SELECT_TYPE write_set;
787 SELECT_TYPE except_set;
788 int sreturn;
789
790 SCM_ASSERT (-1 < scm_ilength (reads), reads, SCM_ARG1, s_select);
791 SCM_ASSERT (-1 < scm_ilength (writes), reads, SCM_ARG1, s_select);
792 SCM_ASSERT (-1 < scm_ilength (excepts), reads, SCM_ARG1, s_select);
793
794 FD_ZERO (&read_set);
795 FD_ZERO (&write_set);
796 FD_ZERO (&except_set);
797
798 fill_select_type (&read_set, reads);
799 fill_select_type (&write_set, writes);
800 fill_select_type (&except_set, excepts);
801
802 if (SCM_UNBNDP (secs))
803 time_p = 0;
804 else
805 {
806 SCM_ASSERT (SCM_INUMP (secs), secs, SCM_ARG4, s_select);
807 if (SCM_UNBNDP (msecs))
808 msecs = SCM_INUM0;
809 else
810 SCM_ASSERT (SCM_INUMP (msecs), msecs, SCM_ARG5, s_select);
811
812 timeout.tv_sec = SCM_INUM (secs);
813 timeout.tv_usec = 1000 * SCM_INUM (msecs);
814 time_p = &timeout;
815 }
816
817 SCM_DEFER_INTS;
818 sreturn = select (SELECT_SET_SIZE,
819 &read_set, &write_set, &except_set, time_p);
820 if (sreturn < 0)
821 scm_syserror (s_select);
822 SCM_ALLOW_INTS;
823 return scm_listify (retrieve_select_type (&read_set, reads),
824 retrieve_select_type (&write_set, writes),
825 retrieve_select_type (&except_set, excepts),
826 SCM_UNDEFINED);
827 #else
828 scm_sysmissing (s_select);
829 /* not reached. */
830 return SCM_BOOL_F;
831 #endif
832 }
833
834 /* Check if FILE has characters waiting to be read. */
835
836 #ifdef __IBMC__
837 # define MSDOS
838 #endif
839 #ifdef MSDOS
840 # ifndef GO32
841 # include <io.h>
842 # include <conio.h>
843
844 int
845 scm_input_waiting_p (f, caller)
846 FILE *f;
847 char *caller;
848 {
849 if (feof (f))
850 return 1;
851 if (fileno (f) == fileno (stdin) && (isatty (fileno (stdin))))
852 return kbhit ();
853 return -1;
854 }
855
856 # endif
857 #else
858 # ifdef _DCC
859 # include <ioctl.h>
860 # else
861 # ifndef AMIGA
862 # ifndef vms
863 # ifdef MWC
864 # include <sys/io.h>
865 # else
866 # ifndef THINK_C
867 # ifndef ARM_ULIB
868 # include <sys/ioctl.h>
869 # endif
870 # endif
871 # endif
872 # endif
873 # endif
874 # endif
875
876 int
877 scm_input_waiting_p (f, caller)
878 FILE *f;
879 char *caller;
880 {
881 /* Can we return an end-of-file character? */
882 if (feof (f))
883 return 1;
884
885 /* Do we have characters in the stdio buffer? */
886 # ifdef FILE_CNT_FIELD
887 if (f->FILE_CNT_FIELD > 0)
888 return 1;
889 # else
890 # ifdef FILE_CNT_GPTR
891 if (f->_gptr != f->_egptr)
892 return 1;
893 # else
894 # ifdef FILE_CNT_READPTR
895 if (f->_IO_read_end != f->_IO_read_ptr)
896 return 1;
897 # else
898 Configure.in could not guess the name of the correct field in a FILE *.
899 This function needs to be ported to your system.
900 It should return zero iff no characters are waiting to be read.;
901 # endif
902 # endif
903 # endif
904
905 /* Is the file prepared to deliver input? */
906 # ifdef HAVE_SELECT
907 {
908 struct timeval timeout;
909 SELECT_TYPE read_set;
910 SELECT_TYPE write_set;
911 SELECT_TYPE except_set;
912 int fno = fileno ((FILE *)f);
913
914 FD_ZERO (&read_set);
915 FD_ZERO (&write_set);
916 FD_ZERO (&except_set);
917
918 FD_SET (fno, &read_set);
919
920 timeout.tv_sec = 0;
921 timeout.tv_usec = 0;
922
923 SCM_DEFER_INTS;
924 if (select (SELECT_SET_SIZE,
925 &read_set, &write_set, &except_set, &timeout)
926 < 0)
927 scm_syserror (caller);
928 SCM_ALLOW_INTS;
929 return FD_ISSET (fno, &read_set);
930 }
931 # else
932 # ifdef FIONREAD
933 {
934 long remir;
935 ioctl(fileno(f), FIONREAD, &remir);
936 return remir;
937 }
938 # else
939 scm_misc_error ("char-ready?", "Not fully implemented on this platform",
940 SCM_EOL);
941 # endif
942 # endif
943 }
944 #endif
945
946 \f
947
948 SCM_PROC (s_fcntl, "fcntl", 3, 0, 0, scm_fcntl);
949
950 SCM
951 scm_fcntl (port, cmd, value)
952 SCM port;
953 SCM cmd;
954 SCM value;
955 {
956 int rv;
957
958 SCM_ASSERT (SCM_OPFPORTP (port), port, SCM_ARG1, s_fcntl);
959 SCM_ASSERT (SCM_INUMP (cmd), cmd, SCM_ARG2, s_fcntl);
960 SCM_ASSERT (SCM_INUMP (value), value, SCM_ARG3, s_fcntl);
961
962 rv = fileno ((FILE *)SCM_STREAM (port));
963 if (rv != -1)
964 SCM_SYSCALL (rv = fcntl (rv, SCM_INUM (cmd), SCM_INUM (value)));
965 if (rv == -1)
966 scm_syserror (s_fcntl);
967 return SCM_MAKINUM (rv);
968 }
969 \f
970 /* {Symbolic Links}
971 */
972
973 SCM_PROC (s_symlink, "symlink", 2, 0, 0, scm_symlink);
974
975 SCM
976 scm_symlink(oldpath, newpath)
977 SCM oldpath;
978 SCM newpath;
979 {
980 #ifdef HAVE_SYMLINK
981 int val;
982
983 SCM_ASSERT (SCM_NIMP (oldpath) && SCM_ROSTRINGP (oldpath), oldpath, SCM_ARG1,
984 s_symlink);
985 SCM_ASSERT (SCM_NIMP (newpath) && SCM_ROSTRINGP (newpath), newpath, SCM_ARG2,
986 s_symlink);
987 SCM_COERCE_SUBSTR (oldpath);
988 SCM_COERCE_SUBSTR (newpath);
989 SCM_SYSCALL (val = symlink(SCM_ROCHARS(oldpath), SCM_ROCHARS(newpath)));
990 if (val != 0)
991 scm_syserror (s_symlink);
992 return SCM_UNSPECIFIED;
993 #else
994 scm_sysmissing (s_symlink);
995 /* not reached. */
996 return SCM_BOOL_F;
997 #endif
998 }
999
1000
1001 SCM_PROC (s_readlink, "readlink", 1, 0, 0, scm_readlink);
1002
1003 SCM
1004 scm_readlink(path)
1005 SCM path;
1006 {
1007 #ifdef HAVE_READLINK
1008 scm_sizet rv;
1009 scm_sizet size = 100;
1010 char *buf;
1011 SCM result;
1012 SCM_ASSERT (SCM_NIMP (path) && SCM_ROSTRINGP (path), path, (char *) SCM_ARG1,
1013 s_readlink);
1014 SCM_COERCE_SUBSTR (path);
1015 SCM_DEFER_INTS;
1016 buf = scm_must_malloc (size, s_readlink);
1017 while ((rv = readlink (SCM_ROCHARS (path), buf, (scm_sizet) size)) == size)
1018 {
1019 scm_must_free (buf);
1020 size *= 2;
1021 buf = scm_must_malloc (size, s_readlink);
1022 }
1023 if (rv == -1)
1024 scm_syserror (s_readlink);
1025 result = scm_makfromstr (buf, rv, 0);
1026 scm_must_free (buf);
1027 SCM_ALLOW_INTS;
1028 return result;
1029 #else
1030 scm_sysmissing (s_readlink);
1031 /* not reached. */
1032 return SCM_BOOL_F;
1033 #endif
1034 }
1035
1036
1037 SCM_PROC (s_lstat, "lstat", 1, 0, 0, scm_lstat);
1038
1039 SCM
1040 scm_lstat(str)
1041 SCM str;
1042 {
1043 #ifdef HAVE_LSTAT
1044 int rv;
1045 struct stat stat_temp;
1046
1047 SCM_ASSERT (SCM_NIMP (str) && SCM_ROSTRINGP (str), str, (char *) SCM_ARG1,
1048 s_lstat);
1049 SCM_COERCE_SUBSTR (str);
1050 SCM_SYSCALL(rv = lstat(SCM_ROCHARS(str), &stat_temp));
1051 if (rv != 0)
1052 {
1053 int en = errno;
1054
1055 scm_syserror_msg (s_lstat, "%s: %S",
1056 scm_listify (scm_makfrom0str (strerror (errno)),
1057 str,
1058 SCM_UNDEFINED),
1059 en);
1060 }
1061 return scm_stat2scm(&stat_temp);
1062 #else
1063 scm_sysmissing (s_lstat);
1064 /* not reached. */
1065 return SCM_BOOL_F;
1066 #endif
1067 }
1068
1069
1070 SCM_PROC (s_copy_file, "copy-file", 2, 0, 0, scm_copy_file);
1071
1072 SCM
1073 scm_copy_file (oldfile, newfile)
1074 SCM oldfile;
1075 SCM newfile;
1076 {
1077 int oldfd, newfd;
1078 int n;
1079 char buf[BUFSIZ]; /* this space could be shared. */
1080 struct stat oldstat;
1081
1082 SCM_ASSERT (SCM_NIMP (oldfile) && SCM_ROSTRINGP (oldfile), oldfile, SCM_ARG1, s_copy_file);
1083 if (SCM_SUBSTRP (oldfile))
1084 oldfile = scm_makfromstr (SCM_ROCHARS (oldfile), SCM_ROLENGTH (oldfile), 0);
1085 SCM_ASSERT (SCM_NIMP (newfile) && SCM_ROSTRINGP (newfile), newfile, SCM_ARG2, s_copy_file);
1086 if (SCM_SUBSTRP (newfile))
1087 newfile = scm_makfromstr (SCM_ROCHARS (newfile), SCM_ROLENGTH (newfile), 0);
1088 if (stat (SCM_ROCHARS (oldfile), &oldstat) == -1)
1089 scm_syserror (s_copy_file);
1090 SCM_DEFER_INTS;
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 SCM_ALLOW_INTS;
1112 return SCM_UNSPECIFIED;
1113 }
1114
1115 \f
1116
1117 void
1118 scm_init_filesys ()
1119 {
1120 scm_add_feature ("i/o-extensions");
1121
1122 scm_tc16_dir = scm_newsmob (&dir_smob);
1123
1124 #ifdef O_RDONLY
1125 scm_sysintern ("O_RDONLY", scm_long2num (O_RDONLY));
1126 #endif
1127 #ifdef O_WRONLY
1128 scm_sysintern ("O_WRONLY", scm_long2num (O_WRONLY));
1129 #endif
1130 #ifdef O_RDWR
1131 scm_sysintern ("O_RDWR", scm_long2num (O_RDWR));
1132 #endif
1133 #ifdef O_CREAT
1134 scm_sysintern ("O_CREAT", scm_long2num (O_CREAT));
1135 #endif
1136 #ifdef O_EXCL
1137 scm_sysintern ("O_EXCL", scm_long2num (O_EXCL));
1138 #endif
1139 #ifdef O_NOCTTY
1140 scm_sysintern ("O_NOCTTY", scm_long2num (O_NOCTTY));
1141 #endif
1142 #ifdef O_TRUNC
1143 scm_sysintern ("O_TRUNC", scm_long2num (O_TRUNC));
1144 #endif
1145 #ifdef O_APPEND
1146 scm_sysintern ("O_APPEND", scm_long2num (O_APPEND));
1147 #endif
1148 #ifdef O_NONBLO
1149 scm_sysintern ("O_NONBLOCK", scm_long2num (O_NONBLOCK));
1150 #endif
1151 #ifdef O_NDELAY
1152 scm_sysintern ("O_NDELAY", scm_long2num (O_NDELAY));
1153 #endif
1154 #ifdef O_SYNC
1155 scm_sysintern ("O_SYNC", scm_long2num (O_SYNC));
1156 #endif
1157
1158 #ifdef F_DUPFD
1159 scm_sysintern ("F_DUPFD", scm_long2num (F_DUPFD));
1160 #endif
1161 #ifdef F_GETFD
1162 scm_sysintern ("F_GETFD", scm_long2num (F_GETFD));
1163 #endif
1164 #ifdef F_SETFD
1165 scm_sysintern ("F_SETFD", scm_long2num (F_SETFD));
1166 #endif
1167 #ifdef F_GETFL
1168 scm_sysintern ("F_GETFL", scm_long2num (F_GETFL));
1169 #endif
1170 #ifdef F_SETFL
1171 scm_sysintern ("F_SETFL", scm_long2num (F_SETFL));
1172 #endif
1173 #ifdef F_GETOWN
1174 scm_sysintern ("F_GETOWN", scm_long2num (F_GETOWN));
1175 #endif
1176 #ifdef F_SETOWN
1177 scm_sysintern ("F_SETOWN", scm_long2num (F_SETOWN));
1178 #endif
1179 #ifdef FD_CLOEXEC
1180 scm_sysintern ("FD_CLOEXEC", scm_long2num (FD_CLOEXEC));
1181 #endif
1182
1183 #include "filesys.x"
1184 }