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