Commit | Line | Data |
---|---|---|
0f2d19dd JB |
1 | /* Copyright (C) 1996 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 "_scm.h" | |
20e6290e JB |
43 | #include "genio.h" |
44 | #include "smob.h" | |
52f4f4d6 | 45 | #include "feature.h" |
0f2d19dd | 46 | |
20e6290e | 47 | #include "filesys.h" |
0f2d19dd JB |
48 | \f |
49 | #ifdef TIME_WITH_SYS_TIME | |
50 | # include <sys/time.h> | |
51 | # include <time.h> | |
52 | #else | |
53 | # if HAVE_SYS_TIME_H | |
54 | # include <sys/time.h> | |
55 | # else | |
56 | # include <time.h> | |
57 | # endif | |
58 | #endif | |
59 | ||
60 | #ifdef HAVE_UNISTD_H | |
61 | #include <unistd.h> | |
62 | #endif | |
63 | ||
1f9e2226 JB |
64 | #ifdef HAVE_LIBC_H |
65 | #include <libc.h> | |
66 | #endif | |
67 | ||
0f2d19dd JB |
68 | #ifdef HAVE_SYS_SELECT_H |
69 | #include <sys/select.h> | |
70 | #endif | |
71 | ||
1f9e2226 JB |
72 | #ifdef HAVE_STRING_H |
73 | #include <string.h> | |
74 | #endif | |
75 | ||
8cc71382 | 76 | #include <sys/types.h> |
0f2d19dd JB |
77 | #include <sys/stat.h> |
78 | #include <fcntl.h> | |
79 | ||
80 | #include <pwd.h> | |
81 | ||
82 | ||
83 | #ifdef FD_SET | |
84 | ||
85 | #define SELECT_TYPE fd_set | |
86 | #define SELECT_SET_SIZE FD_SETSIZE | |
87 | ||
88 | #else /* no FD_SET */ | |
89 | ||
90 | /* Define the macros to access a single-int bitmap of descriptors. */ | |
91 | #define SELECT_SET_SIZE 32 | |
92 | #define SELECT_TYPE int | |
93 | #define FD_SET(n, p) (*(p) |= (1 << (n))) | |
94 | #define FD_CLR(n, p) (*(p) &= ~(1 << (n))) | |
95 | #define FD_ISSET(n, p) (*(p) & (1 << (n))) | |
96 | #define FD_ZERO(p) (*(p) = 0) | |
97 | ||
98 | #endif /* no FD_SET */ | |
99 | ||
100 | #if HAVE_DIRENT_H | |
101 | # include <dirent.h> | |
102 | # define NAMLEN(dirent) strlen((dirent)->d_name) | |
103 | #else | |
104 | # define dirent direct | |
105 | # define NAMLEN(dirent) (dirent)->d_namlen | |
106 | # if HAVE_SYS_NDIR_H | |
107 | # include <sys/ndir.h> | |
108 | # endif | |
109 | # if HAVE_SYS_DIR_H | |
110 | # include <sys/dir.h> | |
111 | # endif | |
112 | # if HAVE_NDIR_H | |
113 | # include <ndir.h> | |
114 | # endif | |
115 | #endif | |
116 | ||
117 | \f | |
118 | ||
119 | #ifdef O_CREAT | |
120 | SCM_CONST_LONG (scm_O_CREAT, "O_CREAT", O_CREAT); | |
121 | #endif | |
122 | ||
123 | #ifdef O_EXCL | |
124 | SCM_CONST_LONG (scm_O_EXCL, "O_EXCL", O_EXCL); | |
125 | #endif | |
126 | ||
127 | #ifdef O_NOCTTY | |
128 | SCM_CONST_LONG (scm_O_NOCTTY, "O_NOCTTY", O_NOCTTY); | |
129 | #endif | |
130 | ||
131 | #ifdef O_TRUNC | |
132 | SCM_CONST_LONG (scm_O_TRUNC, "O_TRUNC", O_TRUNC); | |
133 | #endif | |
134 | ||
135 | #ifdef O_APPEND | |
136 | SCM_CONST_LONG (scm_O_APPEND, "O_APPEND", O_APPEND); | |
137 | #endif | |
138 | ||
139 | #ifdef O_NONBLOCK | |
140 | SCM_CONST_LONG (scm_O_NONBLOCK, "O_NONBLOCK", O_NONBLOCK); | |
141 | #endif | |
142 | ||
143 | #ifdef O_NDELAY | |
144 | SCM_CONST_LONG (scm_O_NDELAY, "O_NDELAY", O_NDELAY); | |
145 | #endif | |
146 | ||
147 | #ifdef O_SYNC | |
148 | SCM_CONST_LONG (scm_O_SYNC, "O_SYNC", O_SYNC); | |
149 | #endif | |
150 | ||
151 | ||
152 | ||
153 | \f | |
154 | ||
155 | /* {Permissions} | |
156 | */ | |
157 | ||
02b754d3 | 158 | SCM_PROC (s_sys_chown, "chown", 3, 0, 0, scm_sys_chown); |
1cc91f1b | 159 | |
0f2d19dd JB |
160 | SCM |
161 | scm_sys_chown (path, owner, group) | |
162 | SCM path; | |
163 | SCM owner; | |
164 | SCM group; | |
0f2d19dd JB |
165 | { |
166 | int val; | |
02b754d3 | 167 | |
0f2d19dd JB |
168 | SCM_ASSERT (SCM_NIMP (path) && SCM_ROSTRINGP (path), path, SCM_ARG1, s_sys_chown); |
169 | if (SCM_SUBSTRP (path)) | |
170 | path = scm_makfromstr (SCM_ROCHARS (path), SCM_ROLENGTH (path), 0); | |
171 | SCM_ASSERT (SCM_INUMP (owner), owner, SCM_ARG2, s_sys_chown); | |
172 | SCM_ASSERT (SCM_INUMP (group), group, SCM_ARG3, s_sys_chown); | |
02b754d3 GH |
173 | SCM_SYSCALL (val = chown (SCM_ROCHARS (path), |
174 | SCM_INUM (owner), SCM_INUM (group))); | |
175 | if (val != 0) | |
52859adf | 176 | scm_syserror (s_sys_chown); |
02b754d3 | 177 | return SCM_UNSPECIFIED; |
0f2d19dd JB |
178 | } |
179 | ||
180 | ||
02b754d3 | 181 | SCM_PROC (s_sys_chmod, "chmod", 2, 0, 0, scm_sys_chmod); |
1cc91f1b | 182 | |
0f2d19dd JB |
183 | SCM |
184 | scm_sys_chmod (port_or_path, mode) | |
185 | SCM port_or_path; | |
186 | SCM mode; | |
0f2d19dd JB |
187 | { |
188 | int rv; | |
189 | SCM_ASSERT (SCM_INUMP (mode), mode, SCM_ARG2, s_sys_chmod); | |
190 | SCM_ASSERT (SCM_NIMP (port_or_path), port_or_path, SCM_ARG1, s_sys_chmod); | |
191 | if (SCM_STRINGP (port_or_path)) | |
192 | SCM_SYSCALL (rv = chmod (SCM_CHARS (port_or_path), SCM_INUM (mode))); | |
193 | else | |
194 | { | |
195 | SCM_ASSERT (SCM_OPFPORTP (port_or_path), port_or_path, SCM_ARG1, s_sys_chmod); | |
196 | rv = fileno ((FILE *)SCM_STREAM (port_or_path)); | |
197 | if (rv != -1) | |
198 | SCM_SYSCALL (rv = fchmod (rv, SCM_INUM (mode))); | |
199 | } | |
02b754d3 | 200 | if (rv != 0) |
52859adf | 201 | scm_syserror (s_sys_chmod); |
02b754d3 | 202 | return SCM_UNSPECIFIED; |
0f2d19dd JB |
203 | } |
204 | ||
205 | SCM_PROC (s_umask, "umask", 0, 1, 0, scm_umask); | |
1cc91f1b | 206 | |
0f2d19dd JB |
207 | SCM |
208 | scm_umask (mode) | |
209 | SCM mode; | |
0f2d19dd JB |
210 | { |
211 | mode_t mask; | |
212 | if (SCM_UNBNDP (mode)) | |
213 | { | |
214 | mask = umask (0); | |
215 | umask (mask); | |
216 | } | |
217 | else | |
218 | { | |
219 | SCM_ASSERT (SCM_INUMP (mode), mode, SCM_ARG1, s_umask); | |
220 | mask = umask (SCM_INUM (mode)); | |
221 | } | |
222 | return SCM_MAKINUM (mask); | |
223 | } | |
224 | ||
225 | \f | |
226 | /* {File Descriptors} | |
227 | */ | |
228 | long scm_tc16_fd; | |
229 | ||
1cc91f1b JB |
230 | |
231 | static int scm_fd_print SCM_P ((SCM sexp, SCM port, scm_print_state *pstate)); | |
232 | ||
0f2d19dd | 233 | static int |
9882ea19 | 234 | scm_fd_print (sexp, port, pstate) |
0f2d19dd JB |
235 | SCM sexp; |
236 | SCM port; | |
9882ea19 | 237 | scm_print_state *pstate; |
0f2d19dd JB |
238 | { |
239 | scm_gen_puts (scm_regular_string, "#<fd ", port); | |
240 | scm_intprint (SCM_CDR (sexp), 10, port); | |
241 | scm_gen_puts (scm_regular_string, ">", port); | |
242 | return 1; | |
243 | } | |
244 | ||
1cc91f1b JB |
245 | |
246 | static scm_sizet scm_fd_free SCM_P ((SCM p)); | |
247 | ||
0f2d19dd JB |
248 | static scm_sizet |
249 | scm_fd_free (p) | |
250 | SCM p; | |
0f2d19dd JB |
251 | { |
252 | SCM flags; | |
253 | ||
254 | flags = SCM_FD_FLAGS (p); | |
255 | if ((scm_close_fd_on_gc & flags) && (scm_fd_is_open & flags)) | |
256 | { | |
257 | SCM_SYSCALL( close (SCM_FD (p)) ); | |
258 | } | |
259 | return 0; | |
260 | } | |
261 | ||
262 | static scm_smobfuns fd_smob = {scm_mark0, scm_fd_free, scm_fd_print, 0}; | |
263 | ||
1cc91f1b | 264 | |
0f2d19dd JB |
265 | SCM |
266 | scm_intern_fd (fd, flags) | |
267 | int fd; | |
268 | int flags; | |
0f2d19dd JB |
269 | { |
270 | SCM it; | |
271 | SCM_NEWCELL (it); | |
272 | SCM_REDEFER_INTS; | |
273 | SCM_SETCAR (it, (scm_tc16_fd | (flags << 16))); | |
274 | SCM_SETCDR (it, (SCM)fd); | |
275 | SCM_REALLOW_INTS; | |
276 | return it; | |
277 | } | |
278 | ||
279 | \f | |
280 | ||
02b754d3 | 281 | SCM_PROC (s_sys_open, "open", 3, 0, 0, scm_sys_open); |
1cc91f1b | 282 | |
0f2d19dd JB |
283 | SCM |
284 | scm_sys_open (path, flags, mode) | |
285 | SCM path; | |
286 | SCM flags; | |
287 | SCM mode; | |
0f2d19dd JB |
288 | { |
289 | int fd; | |
290 | SCM sfd; | |
291 | ||
292 | SCM_ASSERT (SCM_NIMP (path) && SCM_ROSTRINGP (path), path, SCM_ARG1, s_sys_open); | |
293 | SCM_ASSERT (SCM_INUMP (flags), flags, SCM_ARG2, s_sys_open); | |
294 | SCM_ASSERT (SCM_INUMP (mode), mode, SCM_ARG3, s_sys_open); | |
295 | ||
296 | if (SCM_SUBSTRP (path)) | |
297 | path = scm_makfromstr (SCM_ROCHARS (path), SCM_ROLENGTH (path), 0); | |
298 | ||
299 | SCM_DEFER_INTS; | |
300 | SCM_SYSCALL ( fd = open (SCM_ROCHARS (path), SCM_INUM (flags), SCM_INUM (mode)) ); | |
301 | if (fd == -1) | |
52859adf | 302 | scm_syserror (s_sys_open); |
02b754d3 | 303 | sfd = scm_intern_fd (fd, scm_fd_is_open | scm_close_fd_on_gc); |
0f2d19dd JB |
304 | SCM_ALLOW_INTS; |
305 | ||
306 | return scm_return_first (sfd, path); | |
307 | } | |
308 | ||
309 | ||
02b754d3 | 310 | SCM_PROC (s_sys_create, "create", 2, 0, 0, scm_sys_create); |
1cc91f1b | 311 | |
0f2d19dd JB |
312 | SCM |
313 | scm_sys_create (path, mode) | |
314 | SCM path; | |
315 | SCM mode; | |
0f2d19dd JB |
316 | { |
317 | int fd; | |
318 | SCM sfd; | |
319 | ||
320 | SCM_ASSERT (SCM_NIMP (path) && SCM_ROSTRINGP (path), path, SCM_ARG1, s_sys_create); | |
321 | SCM_ASSERT (SCM_INUMP (mode), mode, SCM_ARG2, s_sys_create); | |
322 | ||
323 | if (SCM_SUBSTRP (path)) | |
324 | path = scm_makfromstr (SCM_ROCHARS (path), SCM_ROLENGTH (path), 0); | |
325 | ||
326 | SCM_DEFER_INTS; | |
327 | SCM_SYSCALL ( fd = creat (SCM_ROCHARS (path), SCM_INUM (mode)) ); | |
328 | if (fd == -1) | |
52859adf | 329 | scm_syserror (s_sys_create); |
02b754d3 | 330 | sfd = scm_intern_fd (fd, scm_fd_is_open | scm_close_fd_on_gc); |
0f2d19dd JB |
331 | SCM_ALLOW_INTS; |
332 | ||
333 | return scm_return_first (sfd, path); | |
334 | } | |
335 | ||
336 | ||
02b754d3 | 337 | SCM_PROC (s_sys_close, "close", 1, 0, 0, scm_sys_close); |
1cc91f1b | 338 | |
0f2d19dd JB |
339 | SCM |
340 | scm_sys_close (sfd) | |
341 | SCM sfd; | |
0f2d19dd JB |
342 | { |
343 | int fd; | |
344 | int got; | |
345 | SCM_ASSERT (SCM_NIMP (sfd) && SCM_FD_P (sfd), sfd, SCM_ARG1, s_sys_close); | |
346 | fd = SCM_FD (sfd); | |
347 | ||
348 | SCM_DEFER_INTS; | |
349 | got = close (fd); | |
350 | SCM_SETCAR (sfd, scm_tc16_fd); | |
351 | SCM_ALLOW_INTS; | |
02b754d3 | 352 | if (got == -1) |
52859adf | 353 | scm_syserror (s_sys_close); |
02b754d3 | 354 | return SCM_UNSPECIFIED; |
0f2d19dd JB |
355 | } |
356 | ||
357 | ||
02b754d3 | 358 | SCM_PROC (s_sys_write_fd, "write-fd", 2, 0, 0, scm_sys_write_fd); |
1cc91f1b | 359 | |
0f2d19dd JB |
360 | SCM |
361 | scm_sys_write_fd (sfd, buf) | |
362 | SCM sfd; | |
363 | SCM buf; | |
0f2d19dd JB |
364 | { |
365 | SCM answer; | |
366 | int fd; | |
367 | size_t written; | |
368 | SCM_ASSERT (SCM_NIMP (sfd) && SCM_FD_P (sfd), sfd, SCM_ARG1, s_sys_write_fd); | |
369 | SCM_ASSERT (SCM_NIMP (buf) && SCM_ROSTRINGP (buf), buf, SCM_ARG2, s_sys_write_fd); | |
370 | fd = SCM_FD (sfd); | |
371 | SCM_DEFER_INTS; | |
372 | written = write (fd, SCM_ROCHARS (buf), SCM_ROLENGTH (buf)); | |
373 | if (written == -1) | |
52859adf | 374 | scm_syserror (s_sys_write_fd); |
02b754d3 | 375 | answer = scm_long2num (written); |
0f2d19dd JB |
376 | SCM_ALLOW_INTS; |
377 | return scm_return_first (answer, buf); | |
378 | } | |
379 | ||
380 | ||
02b754d3 | 381 | SCM_PROC (s_sys_read_fd, "read-fd", 2, 2, 0, scm_sys_read_fd); |
1cc91f1b | 382 | |
0f2d19dd JB |
383 | SCM |
384 | scm_sys_read_fd (sfd, buf, offset, length) | |
385 | SCM sfd; | |
386 | SCM buf; | |
387 | SCM offset; | |
388 | SCM length; | |
0f2d19dd JB |
389 | { |
390 | SCM answer; | |
391 | int fd; | |
392 | char * bytes; | |
393 | int off; | |
394 | int len; | |
395 | size_t got; | |
396 | ||
397 | SCM_ASSERT (SCM_NIMP (sfd) && SCM_FD_P (sfd), sfd, SCM_ARG1, s_sys_read_fd); | |
398 | fd = SCM_FD (sfd); | |
399 | ||
400 | SCM_ASSERT (SCM_NIMP (buf) && SCM_STRINGP (buf), buf, SCM_ARG2, s_sys_read_fd); | |
401 | bytes = SCM_CHARS (buf); | |
402 | ||
403 | if (SCM_UNBNDP (offset)) | |
404 | off = 0; | |
405 | else | |
406 | { | |
407 | SCM_ASSERT (SCM_INUMP (offset), offset, SCM_ARG3, s_sys_read_fd); | |
408 | off = SCM_INUM (offset); | |
409 | } | |
410 | ||
411 | if (SCM_UNBNDP (length)) | |
412 | len = SCM_LENGTH (buf); | |
413 | else | |
414 | { | |
415 | SCM_ASSERT (SCM_INUMP (length), length, SCM_ARG3, s_sys_read_fd); | |
416 | len = SCM_INUM (length); | |
417 | } | |
418 | ||
419 | SCM_DEFER_INTS; | |
420 | got = read (fd, bytes + off, len); | |
421 | if (got == -1) | |
52859adf | 422 | scm_syserror (s_sys_read_fd); |
02b754d3 | 423 | answer = scm_long2num (got); |
0f2d19dd JB |
424 | SCM_ALLOW_INTS; |
425 | return scm_return_first (answer, buf); | |
426 | } | |
427 | ||
02b754d3 | 428 | SCM_PROC (s_sys_lseek, "lseek", 2, 1, 0, scm_sys_lseek); |
1cc91f1b | 429 | |
0f2d19dd JB |
430 | SCM |
431 | scm_sys_lseek (sfd, offset, whence) | |
432 | SCM sfd; | |
433 | SCM offset; | |
434 | SCM whence; | |
0f2d19dd JB |
435 | { |
436 | SCM answer; | |
437 | int fd; | |
438 | long off; | |
439 | int wh; | |
440 | long got; | |
441 | ||
442 | SCM_ASSERT (SCM_NIMP (sfd) && SCM_FD_P (sfd), sfd, SCM_ARG1, s_sys_lseek); | |
443 | fd = SCM_FD (sfd); | |
444 | ||
445 | off = scm_num2long (offset, (char *)SCM_ARG2, s_sys_lseek); | |
446 | if (SCM_UNBNDP (whence)) | |
447 | wh = SEEK_SET; | |
448 | else | |
449 | { | |
450 | SCM_ASSERT (SCM_INUMP (whence), whence, SCM_ARG3, s_sys_lseek); | |
451 | wh = SCM_INUM (whence); | |
452 | } | |
453 | ||
454 | SCM_DEFER_INTS; | |
455 | SCM_SYSCALL (got = lseek (fd, off, wh)); | |
456 | if (got == -1) | |
52859adf | 457 | scm_syserror (s_sys_lseek); |
02b754d3 | 458 | answer = scm_long2num (got); |
0f2d19dd JB |
459 | SCM_ALLOW_INTS; |
460 | return answer; | |
461 | } | |
462 | ||
463 | ||
02b754d3 | 464 | SCM_PROC (s_sys_dup, "dup", 1, 1, 0, scm_sys_dup); |
1cc91f1b | 465 | |
0f2d19dd JB |
466 | SCM |
467 | scm_sys_dup (oldfd, newfd) | |
468 | SCM oldfd; | |
469 | SCM newfd; | |
0f2d19dd JB |
470 | { |
471 | SCM answer; | |
472 | int fd; | |
473 | int nfd; | |
474 | int (*fn)(); | |
475 | ||
476 | SCM_ASSERT (SCM_NIMP (oldfd) && SCM_FD_P (oldfd), oldfd, SCM_ARG1, s_sys_dup); | |
477 | SCM_ASSERT (SCM_UNBNDP (newfd) || SCM_INUMP (newfd), newfd, SCM_ARG2, s_sys_dup); | |
478 | fd = SCM_FD (oldfd); | |
479 | nfd = (SCM_INUMP (newfd) ? SCM_INUM (newfd) : -1); | |
480 | ||
481 | SCM_DEFER_INTS; | |
482 | fn = ((nfd == -1) ? (int (*)())dup : (int (*)())dup2); | |
483 | nfd = fn (fd, nfd); | |
02b754d3 | 484 | if (nfd == -1) |
52859adf | 485 | scm_syserror (s_sys_dup); |
02b754d3 | 486 | answer = SCM_MAKINUM (nfd); |
0f2d19dd JB |
487 | SCM_ALLOW_INTS; |
488 | return answer; | |
489 | } | |
490 | ||
491 | ||
492 | \f | |
493 | /* {Files} | |
494 | */ | |
1cc91f1b JB |
495 | |
496 | static SCM scm_stat2scm SCM_P ((struct stat *stat_temp)); | |
497 | ||
0f2d19dd JB |
498 | static SCM |
499 | scm_stat2scm (stat_temp) | |
500 | struct stat *stat_temp; | |
0f2d19dd JB |
501 | { |
502 | SCM ans = scm_make_vector (SCM_MAKINUM (13), SCM_UNSPECIFIED, SCM_BOOL_F); | |
503 | SCM *ve = SCM_VELTS (ans); | |
504 | ve[0] = scm_ulong2num ((unsigned long) stat_temp->st_dev); | |
505 | ve[1] = scm_ulong2num ((unsigned long) stat_temp->st_ino); | |
506 | ve[2] = scm_ulong2num ((unsigned long) stat_temp->st_mode); | |
507 | ve[3] = scm_ulong2num ((unsigned long) stat_temp->st_nlink); | |
508 | ve[4] = scm_ulong2num ((unsigned long) stat_temp->st_uid); | |
509 | ve[5] = scm_ulong2num ((unsigned long) stat_temp->st_gid); | |
510 | #ifdef HAVE_ST_RDEV | |
511 | ve[6] = scm_ulong2num ((unsigned long) stat_temp->st_rdev); | |
512 | #else | |
513 | ve[6] = SCM_BOOL_F; | |
514 | #endif | |
515 | ve[7] = scm_ulong2num ((unsigned long) stat_temp->st_size); | |
516 | ve[8] = scm_ulong2num ((unsigned long) stat_temp->st_atime); | |
517 | ve[9] = scm_ulong2num ((unsigned long) stat_temp->st_mtime); | |
518 | ve[10] = scm_ulong2num ((unsigned long) stat_temp->st_ctime); | |
519 | #ifdef HAVE_ST_BLKSIZE | |
520 | ve[11] = scm_ulong2num ((unsigned long) stat_temp->st_blksize); | |
521 | #else | |
522 | ve[11] = scm_ulong2num (4096L); | |
523 | #endif | |
524 | #ifdef HAVE_ST_BLOCKS | |
525 | ve[12] = scm_ulong2num ((unsigned long) stat_temp->st_blocks); | |
526 | #else | |
527 | ve[12] = SCM_BOOL_F; | |
528 | #endif | |
529 | ||
530 | return ans; | |
531 | } | |
532 | ||
02b754d3 | 533 | SCM_PROC (s_sys_stat, "stat", 1, 0, 0, scm_sys_stat); |
1cc91f1b | 534 | |
0f2d19dd JB |
535 | SCM |
536 | scm_sys_stat (fd_or_path) | |
537 | SCM fd_or_path; | |
0f2d19dd | 538 | { |
657c49b3 | 539 | int rv = 1; |
0f2d19dd JB |
540 | struct stat stat_temp; |
541 | ||
542 | if (SCM_INUMP (fd_or_path)) | |
543 | { | |
0f2d19dd JB |
544 | rv = SCM_INUM (fd_or_path); |
545 | SCM_SYSCALL (rv = fstat (rv, &stat_temp)); | |
546 | } | |
547 | else if (SCM_NIMP (fd_or_path) && SCM_FD_P (fd_or_path)) | |
548 | { | |
549 | rv = SCM_FD (fd_or_path); | |
550 | SCM_SYSCALL (rv = fstat (rv, &stat_temp)); | |
551 | } | |
552 | else | |
553 | { | |
554 | SCM_ASSERT (SCM_NIMP (fd_or_path), fd_or_path, SCM_ARG1, s_sys_stat); | |
555 | SCM_ASSERT (SCM_ROSTRINGP (fd_or_path), fd_or_path, SCM_ARG1, s_sys_stat); | |
556 | if (SCM_ROSTRINGP (fd_or_path)) | |
557 | { | |
558 | if (SCM_SUBSTRP (fd_or_path)) | |
559 | fd_or_path = scm_makfromstr (SCM_ROCHARS (fd_or_path), SCM_ROLENGTH (fd_or_path), 0); | |
560 | SCM_SYSCALL (rv = stat (SCM_CHARS (fd_or_path), &stat_temp)); | |
561 | } | |
562 | ||
563 | } | |
02b754d3 | 564 | if (rv != 0) |
dbece3a2 GH |
565 | scm_syserror_msg (s_sys_stat, "%s: %S", |
566 | scm_listify (scm_makfrom0str (strerror (errno)), | |
567 | fd_or_path, | |
568 | SCM_UNDEFINED)); | |
02b754d3 | 569 | return scm_stat2scm (&stat_temp); |
0f2d19dd JB |
570 | } |
571 | ||
572 | ||
573 | \f | |
574 | /* {Modifying Directories} | |
575 | */ | |
576 | ||
02b754d3 | 577 | SCM_PROC (s_sys_link, "link", 2, 0, 0, scm_sys_link); |
1cc91f1b | 578 | |
0f2d19dd JB |
579 | SCM |
580 | scm_sys_link (oldpath, newpath) | |
581 | SCM oldpath; | |
582 | SCM newpath; | |
0f2d19dd JB |
583 | { |
584 | int val; | |
02b754d3 | 585 | |
0f2d19dd JB |
586 | SCM_ASSERT (SCM_NIMP (oldpath) && SCM_ROSTRINGP (oldpath), oldpath, SCM_ARG1, s_sys_link); |
587 | if (SCM_SUBSTRP (oldpath)) | |
588 | oldpath = scm_makfromstr (SCM_ROCHARS (oldpath), SCM_ROLENGTH (oldpath), 0); | |
589 | SCM_ASSERT (SCM_NIMP (newpath) && SCM_ROSTRINGP (newpath), newpath, SCM_ARG2, s_sys_link); | |
590 | if (SCM_SUBSTRP (newpath)) | |
591 | newpath = scm_makfromstr (SCM_ROCHARS (newpath), SCM_ROLENGTH (newpath), 0); | |
592 | SCM_SYSCALL (val = link (SCM_ROCHARS (oldpath), SCM_ROCHARS (newpath))); | |
02b754d3 | 593 | if (val != 0) |
52859adf | 594 | scm_syserror (s_sys_link); |
02b754d3 | 595 | return SCM_UNSPECIFIED; |
0f2d19dd JB |
596 | } |
597 | ||
598 | ||
599 | ||
02b754d3 | 600 | SCM_PROC (s_sys_rename, "rename-file", 2, 0, 0, scm_sys_rename); |
1cc91f1b | 601 | |
0f2d19dd JB |
602 | SCM |
603 | scm_sys_rename (oldname, newname) | |
604 | SCM oldname; | |
605 | SCM newname; | |
0f2d19dd JB |
606 | { |
607 | int rv; | |
608 | SCM_ASSERT (SCM_NIMP (oldname) && SCM_STRINGP (oldname), oldname, SCM_ARG1, s_sys_rename); | |
609 | SCM_ASSERT (SCM_NIMP (newname) && SCM_STRINGP (newname), newname, SCM_ARG2, s_sys_rename); | |
610 | #ifdef HAVE_RENAME | |
611 | SCM_SYSCALL (rv = rename (SCM_CHARS (oldname), SCM_CHARS (newname))); | |
02b754d3 | 612 | if (rv != 0) |
52859adf | 613 | scm_syserror (s_sys_rename); |
02b754d3 | 614 | return SCM_UNSPECIFIED; |
0f2d19dd JB |
615 | #else |
616 | SCM_DEFER_INTS; | |
617 | SCM_SYSCALL (rv = link (SCM_CHARS (oldname), SCM_CHARS (newname))); | |
02b754d3 | 618 | if (rv == 0) |
0f2d19dd JB |
619 | { |
620 | SCM_SYSCALL (rv = unlink (SCM_CHARS (oldname)));; | |
02b754d3 | 621 | if (rv != 0) |
0f2d19dd JB |
622 | /* unlink failed. remove new name */ |
623 | SCM_SYSCALL (unlink (SCM_CHARS (newname))); | |
624 | } | |
625 | SCM_ALLOW_INTS; | |
02b754d3 | 626 | if (rv != 0) |
52859adf | 627 | scm_syserror (s_sys_rename); |
02b754d3 | 628 | return SCM_UNSPECIFIED; |
0f2d19dd JB |
629 | #endif |
630 | } | |
631 | ||
632 | ||
2f3ed1ba | 633 | SCM_PROC(s_sys_delete_file, "delete-file", 1, 0, 0, scm_sys_delete_file); |
1cc91f1b | 634 | |
2f3ed1ba JB |
635 | SCM |
636 | scm_sys_delete_file (str) | |
637 | SCM str; | |
2f3ed1ba JB |
638 | { |
639 | int ans; | |
640 | SCM_ASSERT (SCM_NIMP (str) && SCM_STRINGP (str), str, SCM_ARG1, s_sys_delete_file); | |
641 | SCM_SYSCALL (ans = unlink (SCM_CHARS (str))); | |
642 | if (ans != 0) | |
52859adf | 643 | scm_syserror (s_sys_delete_file); |
2f3ed1ba JB |
644 | return SCM_UNSPECIFIED; |
645 | } | |
646 | ||
0f2d19dd | 647 | |
02b754d3 | 648 | SCM_PROC (s_sys_mkdir, "mkdir", 1, 1, 0, scm_sys_mkdir); |
1cc91f1b | 649 | |
0f2d19dd JB |
650 | SCM |
651 | scm_sys_mkdir (path, mode) | |
652 | SCM path; | |
653 | SCM mode; | |
0f2d19dd JB |
654 | { |
655 | #ifdef HAVE_MKDIR | |
656 | int rv; | |
657 | mode_t mask; | |
658 | SCM_ASSERT (SCM_NIMP (path) && SCM_STRINGP (path), path, SCM_ARG1, s_sys_mkdir); | |
659 | if (SCM_UNBNDP (mode)) | |
660 | { | |
661 | mask = umask (0); | |
662 | umask (mask); | |
663 | SCM_SYSCALL (rv = mkdir (SCM_CHARS (path), 0777 ^ mask)); | |
664 | } | |
665 | else | |
666 | { | |
667 | SCM_ASSERT (SCM_INUMP (mode), mode, SCM_ARG2, s_sys_mkdir); | |
668 | SCM_SYSCALL (rv = mkdir (SCM_CHARS (path), SCM_INUM (mode))); | |
669 | } | |
02b754d3 | 670 | if (rv != 0) |
52859adf | 671 | scm_syserror (s_sys_mkdir); |
02b754d3 | 672 | return SCM_UNSPECIFIED; |
0f2d19dd | 673 | #else |
52859adf | 674 | scm_sysmissing (s_sys_mkdir); |
02b754d3 GH |
675 | /* not reached. */ |
676 | return SCM_BOOL_F; | |
0f2d19dd JB |
677 | #endif |
678 | } | |
679 | ||
680 | ||
02b754d3 | 681 | SCM_PROC (s_sys_rmdir, "rmdir", 1, 0, 0, scm_sys_rmdir); |
1cc91f1b | 682 | |
0f2d19dd JB |
683 | SCM |
684 | scm_sys_rmdir (path) | |
685 | SCM path; | |
0f2d19dd JB |
686 | { |
687 | #ifdef HAVE_RMDIR | |
688 | int val; | |
02b754d3 | 689 | |
0f2d19dd JB |
690 | SCM_ASSERT (SCM_NIMP (path) && SCM_STRINGP (path), path, SCM_ARG1, s_sys_rmdir); |
691 | SCM_SYSCALL (val = rmdir (SCM_CHARS (path))); | |
02b754d3 | 692 | if (val != 0) |
52859adf | 693 | scm_syserror (s_sys_rmdir); |
02b754d3 | 694 | return SCM_UNSPECIFIED; |
0f2d19dd | 695 | #else |
52859adf | 696 | scm_sysmissing (s_sys_rmdir); |
02b754d3 GH |
697 | /* not reached. */ |
698 | return SCM_BOOL_F; | |
0f2d19dd JB |
699 | #endif |
700 | } | |
701 | ||
702 | \f | |
703 | /* {Examining Directories} | |
704 | */ | |
705 | ||
706 | long scm_tc16_dir; | |
707 | ||
02b754d3 | 708 | SCM_PROC (s_sys_opendir, "opendir", 1, 0, 0, scm_sys_opendir); |
1cc91f1b | 709 | |
0f2d19dd JB |
710 | SCM |
711 | scm_sys_opendir (dirname) | |
712 | SCM dirname; | |
0f2d19dd JB |
713 | { |
714 | DIR *ds; | |
715 | SCM dir; | |
716 | SCM_ASSERT (SCM_NIMP (dirname) && SCM_STRINGP (dirname), dirname, SCM_ARG1, s_sys_opendir); | |
717 | SCM_NEWCELL (dir); | |
718 | SCM_DEFER_INTS; | |
719 | SCM_SYSCALL (ds = opendir (SCM_CHARS (dirname))); | |
02b754d3 | 720 | if (ds == NULL) |
52859adf | 721 | scm_syserror (s_sys_opendir); |
0f2d19dd JB |
722 | SCM_CAR (dir) = scm_tc16_dir | SCM_OPN; |
723 | SCM_SETCDR (dir, ds); | |
724 | SCM_ALLOW_INTS; | |
725 | return dir; | |
726 | } | |
727 | ||
728 | ||
02b754d3 | 729 | SCM_PROC (s_sys_readdir, "readdir", 1, 0, 0, scm_sys_readdir); |
1cc91f1b | 730 | |
0f2d19dd JB |
731 | SCM |
732 | scm_sys_readdir (port) | |
733 | SCM port; | |
0f2d19dd JB |
734 | { |
735 | struct dirent *rdent; | |
736 | SCM_DEFER_INTS; | |
737 | SCM_ASSERT (SCM_NIMP (port) && SCM_OPDIRP (port), port, SCM_ARG1, s_sys_readdir); | |
738 | errno = 0; | |
739 | SCM_SYSCALL (rdent = readdir ((DIR *) SCM_CDR (port))); | |
740 | SCM_ALLOW_INTS; | |
02b754d3 | 741 | if (errno != 0) |
52859adf | 742 | scm_syserror (s_sys_readdir); |
02b754d3 GH |
743 | return (rdent ? scm_makfromstr (rdent->d_name, NAMLEN (rdent), 0) |
744 | : SCM_EOF_VAL); | |
0f2d19dd JB |
745 | } |
746 | ||
747 | ||
748 | ||
749 | SCM_PROC (s_rewinddir, "rewinddir", 1, 0, 0, scm_rewinddir); | |
1cc91f1b | 750 | |
0f2d19dd JB |
751 | SCM |
752 | scm_rewinddir (port) | |
753 | SCM port; | |
0f2d19dd JB |
754 | { |
755 | SCM_ASSERT (SCM_NIMP (port) && SCM_OPDIRP (port), port, SCM_ARG1, s_rewinddir); | |
756 | rewinddir ((DIR *) SCM_CDR (port)); | |
757 | return SCM_UNSPECIFIED; | |
758 | } | |
759 | ||
760 | ||
761 | ||
02b754d3 | 762 | SCM_PROC (s_sys_closedir, "closedir", 1, 0, 0, scm_sys_closedir); |
1cc91f1b | 763 | |
0f2d19dd JB |
764 | SCM |
765 | scm_sys_closedir (port) | |
766 | SCM port; | |
0f2d19dd JB |
767 | { |
768 | int sts; | |
02b754d3 | 769 | |
0f2d19dd JB |
770 | SCM_ASSERT (SCM_NIMP (port) && SCM_DIRP (port), port, SCM_ARG1, s_sys_closedir); |
771 | SCM_DEFER_INTS; | |
772 | if (SCM_CLOSEDP (port)) | |
773 | { | |
774 | SCM_ALLOW_INTS; | |
02b754d3 | 775 | return SCM_UNSPECIFIED; |
0f2d19dd JB |
776 | } |
777 | SCM_SYSCALL (sts = closedir ((DIR *) SCM_CDR (port))); | |
02b754d3 | 778 | if (sts != 0) |
52859adf | 779 | scm_syserror (s_sys_closedir); |
0f2d19dd JB |
780 | SCM_CAR (port) = scm_tc16_dir; |
781 | SCM_ALLOW_INTS; | |
02b754d3 | 782 | return SCM_UNSPECIFIED; |
0f2d19dd JB |
783 | } |
784 | ||
785 | ||
786 | ||
1cc91f1b JB |
787 | |
788 | static int scm_dir_print SCM_P ((SCM sexp, SCM port, scm_print_state *pstate)); | |
789 | ||
0f2d19dd | 790 | static int |
9882ea19 | 791 | scm_dir_print (sexp, port, pstate) |
0f2d19dd JB |
792 | SCM sexp; |
793 | SCM port; | |
9882ea19 | 794 | scm_print_state *pstate; |
0f2d19dd JB |
795 | { |
796 | scm_prinport (sexp, port, "directory"); | |
797 | return 1; | |
798 | } | |
799 | ||
1cc91f1b JB |
800 | |
801 | static scm_sizet scm_dir_free SCM_P ((SCM p)); | |
802 | ||
0f2d19dd JB |
803 | static scm_sizet |
804 | scm_dir_free (p) | |
805 | SCM p; | |
0f2d19dd JB |
806 | { |
807 | if (SCM_OPENP (p)) | |
808 | closedir ((DIR *) SCM_CDR (p)); | |
809 | return 0; | |
810 | } | |
811 | ||
812 | static scm_smobfuns dir_smob = {scm_mark0, scm_dir_free, scm_dir_print, 0}; | |
813 | ||
814 | \f | |
815 | /* {Navigating Directories} | |
816 | */ | |
817 | ||
818 | ||
02b754d3 | 819 | SCM_PROC (s_sys_chdir, "chdir", 1, 0, 0, scm_sys_chdir); |
1cc91f1b | 820 | |
0f2d19dd JB |
821 | SCM |
822 | scm_sys_chdir (str) | |
823 | SCM str; | |
0f2d19dd JB |
824 | { |
825 | int ans; | |
02b754d3 | 826 | |
0f2d19dd JB |
827 | SCM_ASSERT (SCM_NIMP (str) && SCM_STRINGP (str), str, SCM_ARG1, s_sys_chdir); |
828 | SCM_SYSCALL (ans = chdir (SCM_CHARS (str))); | |
02b754d3 | 829 | if (ans != 0) |
52859adf | 830 | scm_syserror (s_sys_chdir); |
02b754d3 | 831 | return SCM_UNSPECIFIED; |
0f2d19dd JB |
832 | } |
833 | ||
834 | ||
835 | ||
02b754d3 | 836 | SCM_PROC (s_sys_getcwd, "getcwd", 0, 0, 0, scm_sys_getcwd); |
1cc91f1b | 837 | |
0f2d19dd JB |
838 | SCM |
839 | scm_sys_getcwd () | |
0f2d19dd JB |
840 | { |
841 | #ifdef HAVE_GETCWD | |
842 | char *rv; | |
843 | ||
844 | scm_sizet size = 100; | |
845 | char *wd; | |
846 | SCM result; | |
847 | ||
848 | SCM_DEFER_INTS; | |
849 | wd = scm_must_malloc (size, s_sys_getcwd); | |
850 | while ((rv = getcwd (wd, size)) == 0 && errno == ERANGE) | |
851 | { | |
852 | scm_must_free (wd); | |
853 | size *= 2; | |
854 | wd = scm_must_malloc (size, s_sys_getcwd); | |
855 | } | |
02b754d3 | 856 | if (rv == 0) |
52859adf | 857 | scm_syserror (s_sys_getcwd); |
02b754d3 | 858 | result = scm_makfromstr (wd, strlen (wd), 0); |
0f2d19dd JB |
859 | scm_must_free (wd); |
860 | SCM_ALLOW_INTS; | |
861 | return result; | |
862 | #else | |
52859adf | 863 | scm_sysmissing (s_sys_getcwd); |
02b754d3 GH |
864 | /* not reached. */ |
865 | return SCM_BOOL_F; | |
0f2d19dd JB |
866 | #endif |
867 | } | |
868 | ||
869 | \f | |
870 | ||
1cc91f1b JB |
871 | |
872 | static void fill_select_type SCM_P ((SELECT_TYPE * set, SCM list)); | |
873 | ||
0f2d19dd JB |
874 | static void |
875 | fill_select_type (set, list) | |
876 | SELECT_TYPE * set; | |
877 | SCM list; | |
0f2d19dd JB |
878 | { |
879 | while (list != SCM_EOL) | |
880 | { | |
881 | if ( SCM_NIMP (SCM_CAR (list)) | |
882 | && (scm_tc16_fport == SCM_TYP16 (SCM_CAR (list))) | |
883 | && SCM_OPPORTP (SCM_CAR (list))) | |
884 | FD_SET (fileno ((FILE *)SCM_STREAM (SCM_CAR (list))), set); | |
885 | else if (SCM_INUMP (SCM_CAR (list))) | |
886 | FD_SET (SCM_INUM (SCM_CAR (list)), set); | |
887 | else if (SCM_NIMP (SCM_CAR (list)) && SCM_FD_P (SCM_CAR (list))) | |
888 | FD_SET (SCM_FD (SCM_CAR (list)), set); | |
889 | list = SCM_CDR (list); | |
890 | } | |
891 | } | |
892 | ||
1cc91f1b JB |
893 | |
894 | static SCM retrieve_select_type SCM_P ((SELECT_TYPE * set, SCM list)); | |
895 | ||
0f2d19dd JB |
896 | static SCM |
897 | retrieve_select_type (set, list) | |
898 | SELECT_TYPE * set; | |
899 | SCM list; | |
0f2d19dd JB |
900 | { |
901 | SCM answer; | |
902 | answer = SCM_EOL; | |
903 | while (list != SCM_EOL) | |
904 | { | |
905 | if ( SCM_NIMP (SCM_CAR (list)) | |
906 | && (scm_tc16_fport == SCM_TYP16 (SCM_CAR (list))) | |
907 | && SCM_OPPORTP (SCM_CAR (list))) | |
908 | { | |
909 | if (FD_ISSET (fileno ((FILE *)SCM_STREAM (SCM_CAR (list))), set)) | |
910 | answer = scm_cons (SCM_CAR (list), answer); | |
911 | } | |
912 | else if (SCM_INUMP (SCM_CAR (list))) | |
913 | { | |
914 | if (FD_ISSET (SCM_INUM (SCM_CAR (list)), set)) | |
915 | answer = scm_cons (SCM_CAR (list), answer); | |
916 | } | |
917 | else if (SCM_NIMP (SCM_CAR (list)) && SCM_FD_P (SCM_CAR (list))) | |
918 | { | |
919 | if (FD_ISSET (SCM_FD (SCM_CAR (list)), set)) | |
920 | answer = scm_cons (SCM_CAR (list), answer); | |
921 | } | |
922 | list = SCM_CDR (list); | |
923 | } | |
924 | return answer; | |
925 | } | |
926 | ||
927 | ||
02b754d3 | 928 | SCM_PROC (s_sys_select, "select", 3, 2, 0, scm_sys_select); |
1cc91f1b | 929 | |
0f2d19dd JB |
930 | SCM |
931 | scm_sys_select (reads, writes, excepts, secs, msecs) | |
932 | SCM reads; | |
933 | SCM writes; | |
934 | SCM excepts; | |
935 | SCM secs; | |
936 | SCM msecs; | |
0f2d19dd JB |
937 | { |
938 | #ifdef HAVE_SELECT | |
939 | struct timeval timeout; | |
940 | struct timeval * time_p; | |
941 | SELECT_TYPE read_set; | |
942 | SELECT_TYPE write_set; | |
943 | SELECT_TYPE except_set; | |
944 | int sreturn; | |
945 | ||
946 | SCM_ASSERT (-1 < scm_ilength (reads), reads, SCM_ARG1, s_sys_select); | |
947 | SCM_ASSERT (-1 < scm_ilength (writes), reads, SCM_ARG1, s_sys_select); | |
948 | SCM_ASSERT (-1 < scm_ilength (excepts), reads, SCM_ARG1, s_sys_select); | |
949 | ||
950 | FD_ZERO (&read_set); | |
951 | FD_ZERO (&write_set); | |
952 | FD_ZERO (&except_set); | |
953 | ||
954 | fill_select_type (&read_set, reads); | |
955 | fill_select_type (&write_set, writes); | |
956 | fill_select_type (&except_set, excepts); | |
957 | ||
958 | if (SCM_UNBNDP (secs)) | |
959 | time_p = 0; | |
960 | else | |
961 | { | |
962 | SCM_ASSERT (SCM_INUMP (secs), secs, SCM_ARG4, s_sys_select); | |
963 | if (SCM_UNBNDP (msecs)) | |
964 | msecs = SCM_INUM0; | |
965 | else | |
966 | SCM_ASSERT (SCM_INUMP (msecs), msecs, SCM_ARG5, s_sys_select); | |
967 | ||
968 | timeout.tv_sec = SCM_INUM (secs); | |
969 | timeout.tv_usec = 1000 * SCM_INUM (msecs); | |
970 | time_p = &timeout; | |
971 | } | |
972 | ||
973 | SCM_DEFER_INTS; | |
974 | sreturn = select (SELECT_SET_SIZE, | |
975 | &read_set, &write_set, &except_set, time_p); | |
0f2d19dd | 976 | if (sreturn < 0) |
52859adf | 977 | scm_syserror (s_sys_select); |
52f4f4d6 | 978 | SCM_ALLOW_INTS; |
02b754d3 GH |
979 | return scm_listify (retrieve_select_type (&read_set, reads), |
980 | retrieve_select_type (&write_set, writes), | |
981 | retrieve_select_type (&except_set, excepts), | |
982 | SCM_UNDEFINED); | |
0f2d19dd | 983 | #else |
52859adf | 984 | scm_sysmissing (s_sys_select); |
02b754d3 GH |
985 | /* not reached. */ |
986 | return SCM_BOOL_F; | |
0f2d19dd JB |
987 | #endif |
988 | } | |
989 | ||
990 | \f | |
991 | /* {Symbolic Links} | |
992 | */ | |
993 | ||
02b754d3 | 994 | SCM_PROC (s_sys_symlink, "symlink", 2, 0, 0, scm_sys_symlink); |
1cc91f1b | 995 | |
0f2d19dd JB |
996 | SCM |
997 | scm_sys_symlink(oldpath, newpath) | |
998 | SCM oldpath; | |
999 | SCM newpath; | |
0f2d19dd JB |
1000 | { |
1001 | #ifdef HAVE_SYMLINK | |
1002 | int val; | |
02b754d3 | 1003 | |
0f2d19dd JB |
1004 | SCM_ASSERT(SCM_NIMP(oldpath) && SCM_STRINGP(oldpath), oldpath, SCM_ARG1, s_sys_symlink); |
1005 | SCM_ASSERT(SCM_NIMP(newpath) && SCM_STRINGP(newpath), newpath, SCM_ARG2, s_sys_symlink); | |
02b754d3 GH |
1006 | SCM_SYSCALL (val = symlink(SCM_CHARS(oldpath), SCM_CHARS(newpath))); |
1007 | if (val != 0) | |
52859adf | 1008 | scm_syserror (s_sys_symlink); |
02b754d3 | 1009 | return SCM_UNSPECIFIED; |
0f2d19dd | 1010 | #else |
52859adf | 1011 | scm_sysmissing (s_sys_symlink); |
02b754d3 GH |
1012 | /* not reached. */ |
1013 | return SCM_BOOL_F; | |
0f2d19dd JB |
1014 | #endif |
1015 | } | |
1016 | ||
1017 | ||
02b754d3 | 1018 | SCM_PROC (s_sys_readlink, "readlink", 1, 0, 0, scm_sys_readlink); |
1cc91f1b | 1019 | |
0f2d19dd JB |
1020 | SCM |
1021 | scm_sys_readlink(path) | |
1022 | SCM path; | |
0f2d19dd JB |
1023 | { |
1024 | #ifdef HAVE_READLINK | |
1025 | scm_sizet rv; | |
1026 | scm_sizet size = 100; | |
1027 | char *buf; | |
1028 | SCM result; | |
1029 | SCM_ASSERT (SCM_NIMP (path) && SCM_STRINGP (path), path, (char *) SCM_ARG1, s_sys_readlink); | |
1030 | SCM_DEFER_INTS; | |
1031 | buf = scm_must_malloc (size, s_sys_readlink); | |
1032 | while ((rv = readlink (SCM_CHARS (path), buf, (scm_sizet) size)) == size) | |
1033 | { | |
1034 | scm_must_free (buf); | |
1035 | size *= 2; | |
1036 | buf = scm_must_malloc (size, s_sys_readlink); | |
1037 | } | |
02b754d3 | 1038 | if (rv == -1) |
52859adf | 1039 | scm_syserror (s_sys_readlink); |
02b754d3 | 1040 | result = scm_makfromstr (buf, rv, 0); |
0f2d19dd JB |
1041 | scm_must_free (buf); |
1042 | SCM_ALLOW_INTS; | |
1043 | return result; | |
1044 | #else | |
52859adf | 1045 | scm_sysmissing (s_sys_readlink); |
02b754d3 GH |
1046 | /* not reached. */ |
1047 | return SCM_BOOL_F; | |
0f2d19dd JB |
1048 | #endif |
1049 | } | |
1050 | ||
1051 | ||
02b754d3 | 1052 | SCM_PROC (s_sys_lstat, "lstat", 1, 0, 0, scm_sys_lstat); |
1cc91f1b | 1053 | |
0f2d19dd JB |
1054 | SCM |
1055 | scm_sys_lstat(str) | |
1056 | SCM str; | |
0f2d19dd | 1057 | { |
02b754d3 GH |
1058 | #ifdef HAVE_LSTAT |
1059 | int rv; | |
0f2d19dd | 1060 | struct stat stat_temp; |
02b754d3 | 1061 | |
0f2d19dd | 1062 | SCM_ASSERT(SCM_NIMP(str) && SCM_STRINGP(str), str, (char *)SCM_ARG1, s_sys_lstat); |
02b754d3 GH |
1063 | SCM_SYSCALL(rv = lstat(SCM_CHARS(str), &stat_temp)); |
1064 | if (rv != 0) | |
dbece3a2 GH |
1065 | scm_syserror_msg (s_sys_lstat, "%s: %S", |
1066 | scm_listify (scm_makfrom0str (strerror (errno)), | |
1067 | str, | |
1068 | SCM_UNDEFINED)); | |
02b754d3 | 1069 | return scm_stat2scm(&stat_temp); |
0f2d19dd | 1070 | #else |
52859adf | 1071 | scm_sysmissing (s_sys_lstat); |
02b754d3 GH |
1072 | /* not reached. */ |
1073 | return SCM_BOOL_F; | |
0f2d19dd JB |
1074 | #endif |
1075 | } | |
1076 | ||
1077 | ||
02b754d3 | 1078 | SCM_PROC (s_sys_copy_file, "copy-file", 2, 0, 0, scm_sys_copy_file); |
1cc91f1b | 1079 | |
0f2d19dd JB |
1080 | SCM |
1081 | scm_sys_copy_file (oldfile, newfile) | |
1082 | SCM oldfile; | |
1083 | SCM newfile; | |
0f2d19dd JB |
1084 | { |
1085 | int oldfd, newfd; | |
1086 | int n; | |
1087 | char buf[BUFSIZ]; /* this space could be shared. */ | |
1088 | struct stat oldstat; | |
1089 | ||
1090 | SCM_ASSERT (SCM_NIMP (oldfile) && SCM_ROSTRINGP (oldfile), oldfile, SCM_ARG1, s_sys_copy_file); | |
1091 | if (SCM_SUBSTRP (oldfile)) | |
1092 | oldfile = scm_makfromstr (SCM_ROCHARS (oldfile), SCM_ROLENGTH (oldfile), 0); | |
1093 | SCM_ASSERT (SCM_NIMP (newfile) && SCM_ROSTRINGP (newfile), newfile, SCM_ARG2, s_sys_copy_file); | |
1094 | if (SCM_SUBSTRP (newfile)) | |
1095 | newfile = scm_makfromstr (SCM_ROCHARS (newfile), SCM_ROLENGTH (newfile), 0); | |
1096 | if (stat (SCM_ROCHARS (oldfile), &oldstat) == -1) | |
52859adf | 1097 | scm_syserror (s_sys_copy_file); |
0f2d19dd JB |
1098 | SCM_DEFER_INTS; |
1099 | oldfd = open (SCM_ROCHARS (oldfile), O_RDONLY); | |
1100 | if (oldfd == -1) | |
52859adf | 1101 | scm_syserror (s_sys_copy_file); |
02b754d3 GH |
1102 | |
1103 | /* use POSIX flags instead of 07777?. */ | |
0f2d19dd JB |
1104 | newfd = open (SCM_ROCHARS (newfile), O_WRONLY | O_CREAT | O_TRUNC, |
1105 | oldstat.st_mode & 07777); | |
1106 | if (newfd == -1) | |
52859adf | 1107 | scm_syserror (s_sys_copy_file); |
02b754d3 | 1108 | |
0f2d19dd JB |
1109 | while ((n = read (oldfd, buf, sizeof buf)) > 0) |
1110 | if (write (newfd, buf, n) != n) | |
1111 | { | |
1112 | close (oldfd); | |
1113 | close (newfd); | |
52859adf | 1114 | scm_syserror (s_sys_copy_file); |
0f2d19dd JB |
1115 | } |
1116 | close (oldfd); | |
1117 | if (close (newfd) == -1) | |
52859adf | 1118 | scm_syserror (s_sys_copy_file); |
0f2d19dd | 1119 | SCM_ALLOW_INTS; |
02b754d3 | 1120 | return SCM_UNSPECIFIED; |
0f2d19dd JB |
1121 | } |
1122 | ||
1123 | \f | |
1cc91f1b | 1124 | |
0f2d19dd JB |
1125 | void |
1126 | scm_init_filesys () | |
0f2d19dd | 1127 | { |
52f4f4d6 | 1128 | scm_add_feature ("i/o-extensions"); |
0f2d19dd JB |
1129 | /* File type/permission bits. */ |
1130 | #ifdef S_IRUSR | |
1131 | scm_sysintern ("S_IRUSR", SCM_MAKINUM (S_IRUSR)); | |
1132 | #endif | |
1133 | #ifdef S_IWUSR | |
1134 | scm_sysintern ("S_IWUSR", SCM_MAKINUM (S_IWUSR)); | |
1135 | #endif | |
1136 | #ifdef S_IXUSR | |
1137 | scm_sysintern ("S_IXUSR", SCM_MAKINUM (S_IXUSR)); | |
1138 | #endif | |
1139 | #ifdef S_IRWXU | |
1140 | scm_sysintern ("S_IRWXU", SCM_MAKINUM (S_IRWXU)); | |
1141 | #endif | |
1142 | ||
1143 | #ifdef S_IRGRP | |
1144 | scm_sysintern ("S_IRGRP", SCM_MAKINUM (S_IRGRP)); | |
1145 | #endif | |
1146 | #ifdef S_IWGRP | |
1147 | scm_sysintern ("S_IWGRP", SCM_MAKINUM (S_IWGRP)); | |
1148 | #endif | |
1149 | #ifdef S_IXGRP | |
1150 | scm_sysintern ("S_IXGRP", SCM_MAKINUM (S_IXGRP)); | |
1151 | #endif | |
1152 | #ifdef S_IRWXG | |
1153 | scm_sysintern ("S_IRWXG", SCM_MAKINUM (S_IRWXG)); | |
1154 | #endif | |
1155 | ||
1156 | #ifdef S_IROTH | |
1157 | scm_sysintern ("S_IROTH", SCM_MAKINUM (S_IROTH)); | |
1158 | #endif | |
1159 | #ifdef S_IWOTH | |
1160 | scm_sysintern ("S_IWOTH", SCM_MAKINUM (S_IWOTH)); | |
1161 | #endif | |
1162 | #ifdef S_IXOTH | |
1163 | scm_sysintern ("S_IXOTH", SCM_MAKINUM (S_IXOTH)); | |
1164 | #endif | |
1165 | #ifdef S_IRWXO | |
1166 | scm_sysintern ("S_IRWXO", SCM_MAKINUM (S_IRWXO)); | |
1167 | #endif | |
1168 | ||
1169 | #ifdef S_ISUID | |
1170 | scm_sysintern ("S_ISUID", SCM_MAKINUM (S_ISUID)); | |
1171 | #endif | |
1172 | #ifdef S_ISGID | |
1173 | scm_sysintern ("S_ISGID", SCM_MAKINUM (S_ISGID)); | |
1174 | #endif | |
1175 | #ifdef S_ISVTX | |
1176 | scm_sysintern ("S_ISVTX", SCM_MAKINUM (S_ISVTX)); | |
1177 | #endif | |
1178 | ||
1179 | #ifdef S_IFMT | |
1180 | scm_sysintern ("S_IFMT", SCM_MAKINUM (S_IFMT)); | |
1181 | #endif | |
1182 | #ifdef S_IFDIR | |
1183 | scm_sysintern ("S_IFDIR", SCM_MAKINUM (S_IFDIR)); | |
1184 | #endif | |
1185 | #ifdef S_IFCHR | |
1186 | scm_sysintern ("S_IFCHR", SCM_MAKINUM (S_IFCHR)); | |
1187 | #endif | |
1188 | #ifdef S_IFBLK | |
1189 | scm_sysintern ("S_IFBLK", SCM_MAKINUM (S_IFBLK)); | |
1190 | #endif | |
1191 | #ifdef S_IFREG | |
1192 | scm_sysintern ("S_IFREG", SCM_MAKINUM (S_IFREG)); | |
1193 | #endif | |
1194 | #ifdef S_IFLNK | |
1195 | scm_sysintern ("S_IFLNK", SCM_MAKINUM (S_IFLNK)); | |
1196 | #endif | |
1197 | #ifdef S_IFSOCK | |
1198 | scm_sysintern ("S_IFSOCK", SCM_MAKINUM (S_IFSOCK)); | |
1199 | #endif | |
1200 | #ifdef S_IFIFO | |
1201 | scm_sysintern ("S_IFIFO", SCM_MAKINUM (S_IFIFO)); | |
1202 | #endif | |
1203 | ||
1204 | ||
1205 | scm_tc16_fd = scm_newsmob (&fd_smob); | |
1206 | scm_tc16_dir = scm_newsmob (&dir_smob); | |
1207 | ||
1208 | #include "filesys.x" | |
1209 | } |