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 | |
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 | |
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" |
0f2d19dd | 48 | |
20e6290e | 49 | #include "filesys.h" |
0f2d19dd JB |
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 | ||
3594582b | 66 | #ifdef LIBC_H_WITH_UNISTD_H |
1f9e2226 JB |
67 | #include <libc.h> |
68 | #endif | |
69 | ||
0f2d19dd JB |
70 | #ifdef HAVE_SYS_SELECT_H |
71 | #include <sys/select.h> | |
72 | #endif | |
73 | ||
1f9e2226 JB |
74 | #ifdef HAVE_STRING_H |
75 | #include <string.h> | |
76 | #endif | |
77 | ||
8cc71382 | 78 | #include <sys/types.h> |
0f2d19dd JB |
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 | ||
d7b8a21a JB |
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 | |
0f2d19dd JB |
123 | \f |
124 | ||
0f2d19dd JB |
125 | |
126 | \f | |
127 | ||
128 | /* {Permissions} | |
129 | */ | |
130 | ||
3d8d56df | 131 | SCM_PROC (s_chown, "chown", 3, 0, 0, scm_chown); |
1cc91f1b | 132 | |
0f2d19dd | 133 | SCM |
3d8d56df | 134 | scm_chown (path, owner, group) |
0f2d19dd JB |
135 | SCM path; |
136 | SCM owner; | |
137 | SCM group; | |
0f2d19dd JB |
138 | { |
139 | int val; | |
02b754d3 | 140 | |
3d8d56df | 141 | SCM_ASSERT (SCM_NIMP (path) && SCM_ROSTRINGP (path), path, SCM_ARG1, s_chown); |
3d8d56df GH |
142 | SCM_ASSERT (SCM_INUMP (owner), owner, SCM_ARG2, s_chown); |
143 | SCM_ASSERT (SCM_INUMP (group), group, SCM_ARG3, s_chown); | |
89958ad0 JB |
144 | |
145 | SCM_COERCE_SUBSTR (path); | |
02b754d3 GH |
146 | SCM_SYSCALL (val = chown (SCM_ROCHARS (path), |
147 | SCM_INUM (owner), SCM_INUM (group))); | |
148 | if (val != 0) | |
3d8d56df | 149 | scm_syserror (s_chown); |
02b754d3 | 150 | return SCM_UNSPECIFIED; |
0f2d19dd JB |
151 | } |
152 | ||
153 | ||
3d8d56df | 154 | SCM_PROC (s_chmod, "chmod", 2, 0, 0, scm_chmod); |
1cc91f1b | 155 | |
0f2d19dd | 156 | SCM |
3d8d56df | 157 | scm_chmod (port_or_path, mode) |
0f2d19dd JB |
158 | SCM port_or_path; |
159 | SCM mode; | |
0f2d19dd JB |
160 | { |
161 | int rv; | |
3d8d56df GH |
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); | |
89958ad0 JB |
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 | } | |
0f2d19dd JB |
169 | else |
170 | { | |
3d8d56df | 171 | SCM_ASSERT (SCM_OPFPORTP (port_or_path), port_or_path, SCM_ARG1, s_chmod); |
0f2d19dd JB |
172 | rv = fileno ((FILE *)SCM_STREAM (port_or_path)); |
173 | if (rv != -1) | |
174 | SCM_SYSCALL (rv = fchmod (rv, SCM_INUM (mode))); | |
175 | } | |
02b754d3 | 176 | if (rv != 0) |
3d8d56df | 177 | scm_syserror (s_chmod); |
02b754d3 | 178 | return SCM_UNSPECIFIED; |
0f2d19dd JB |
179 | } |
180 | ||
181 | SCM_PROC (s_umask, "umask", 0, 1, 0, scm_umask); | |
1cc91f1b | 182 | |
0f2d19dd JB |
183 | SCM |
184 | scm_umask (mode) | |
185 | SCM mode; | |
0f2d19dd JB |
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 | |
0f2d19dd | 202 | |
3d8d56df | 203 | SCM_PROC (s_open, "open", 2, 1, 0, scm_open); |
1cc91f1b | 204 | |
0f2d19dd | 205 | SCM |
3d8d56df | 206 | scm_open (path, flags, mode) |
0f2d19dd JB |
207 | SCM path; |
208 | SCM flags; | |
209 | SCM mode; | |
0f2d19dd JB |
210 | { |
211 | int fd; | |
3d8d56df GH |
212 | SCM newpt; |
213 | FILE *f; | |
214 | char *port_mode; | |
215 | int iflags; | |
0f2d19dd | 216 | |
3d8d56df GH |
217 | SCM_ASSERT (SCM_NIMP (path) && SCM_ROSTRINGP (path), path, SCM_ARG1, s_open); |
218 | iflags = scm_num2long (flags, (char *) SCM_ARG2, s_open); | |
0f2d19dd JB |
219 | |
220 | if (SCM_SUBSTRP (path)) | |
221 | path = scm_makfromstr (SCM_ROCHARS (path), SCM_ROLENGTH (path), 0); | |
222 | ||
223 | SCM_DEFER_INTS; | |
3d8d56df GH |
224 | if (SCM_UNBNDP (mode)) |
225 | SCM_SYSCALL (fd = open (SCM_ROCHARS (path), iflags)); | |
0f2d19dd JB |
226 | else |
227 | { | |
3d8d56df GH |
228 | SCM_ASSERT (SCM_INUMP (mode), mode, SCM_ARG3, s_open); |
229 | SCM_SYSCALL (fd = open (SCM_ROCHARS (path), iflags, SCM_INUM (mode))); | |
0f2d19dd | 230 | } |
3d8d56df GH |
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) | |
0f2d19dd | 244 | { |
3d8d56df GH |
245 | SCM_SYSCALL (close (fd)); |
246 | scm_syserror (s_open); | |
0f2d19dd | 247 | } |
3d8d56df GH |
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 | } | |
0f2d19dd | 259 | SCM_ALLOW_INTS; |
1cc91f1b | 260 | |
3d8d56df | 261 | return newpt; |
0f2d19dd JB |
262 | } |
263 | ||
0f2d19dd JB |
264 | \f |
265 | /* {Files} | |
266 | */ | |
1cc91f1b | 267 | |
ae5253c5 GH |
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 | ||
1cc91f1b JB |
277 | static SCM scm_stat2scm SCM_P ((struct stat *stat_temp)); |
278 | ||
0f2d19dd JB |
279 | static SCM |
280 | scm_stat2scm (stat_temp) | |
281 | struct stat *stat_temp; | |
0f2d19dd | 282 | { |
ae5253c5 | 283 | SCM ans = scm_make_vector (SCM_MAKINUM (15), SCM_UNSPECIFIED, SCM_BOOL_F); |
0f2d19dd | 284 | SCM *ve = SCM_VELTS (ans); |
ae5253c5 | 285 | |
0f2d19dd JB |
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 | |
ae5253c5 GH |
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 | } | |
0f2d19dd JB |
364 | |
365 | return ans; | |
366 | } | |
367 | ||
3d8d56df | 368 | SCM_PROC (s_stat, "stat", 1, 0, 0, scm_stat); |
1cc91f1b | 369 | |
0f2d19dd | 370 | SCM |
c0ebd8c5 MD |
371 | scm_stat (file) |
372 | SCM file; | |
0f2d19dd | 373 | { |
657c49b3 | 374 | int rv = 1; |
0f2d19dd JB |
375 | struct stat stat_temp; |
376 | ||
c0ebd8c5 MD |
377 | if (SCM_INUMP (file)) |
378 | SCM_SYSCALL (rv = fstat (SCM_INUM (file), &stat_temp)); | |
0f2d19dd JB |
379 | else |
380 | { | |
c0ebd8c5 MD |
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 | |
0f2d19dd | 386 | { |
c0ebd8c5 MD |
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)); | |
0f2d19dd | 393 | } |
0f2d19dd | 394 | } |
02b754d3 | 395 | if (rv != 0) |
3d8d56df GH |
396 | { |
397 | int en = errno; | |
398 | ||
399 | scm_syserror_msg (s_stat, "%s: %S", | |
400 | scm_listify (scm_makfrom0str (strerror (errno)), | |
c0ebd8c5 | 401 | file, |
3d8d56df GH |
402 | SCM_UNDEFINED), |
403 | en); | |
404 | } | |
02b754d3 | 405 | return scm_stat2scm (&stat_temp); |
0f2d19dd JB |
406 | } |
407 | ||
408 | ||
409 | \f | |
410 | /* {Modifying Directories} | |
411 | */ | |
412 | ||
3d8d56df | 413 | SCM_PROC (s_link, "link", 2, 0, 0, scm_link); |
1cc91f1b | 414 | |
0f2d19dd | 415 | SCM |
3d8d56df | 416 | scm_link (oldpath, newpath) |
0f2d19dd JB |
417 | SCM oldpath; |
418 | SCM newpath; | |
0f2d19dd JB |
419 | { |
420 | int val; | |
02b754d3 | 421 | |
3d8d56df | 422 | SCM_ASSERT (SCM_NIMP (oldpath) && SCM_ROSTRINGP (oldpath), oldpath, SCM_ARG1, s_link); |
0f2d19dd JB |
423 | if (SCM_SUBSTRP (oldpath)) |
424 | oldpath = scm_makfromstr (SCM_ROCHARS (oldpath), SCM_ROLENGTH (oldpath), 0); | |
3d8d56df | 425 | SCM_ASSERT (SCM_NIMP (newpath) && SCM_ROSTRINGP (newpath), newpath, SCM_ARG2, s_link); |
0f2d19dd JB |
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))); | |
02b754d3 | 429 | if (val != 0) |
3d8d56df | 430 | scm_syserror (s_link); |
02b754d3 | 431 | return SCM_UNSPECIFIED; |
0f2d19dd JB |
432 | } |
433 | ||
434 | ||
435 | ||
3d8d56df | 436 | SCM_PROC (s_rename, "rename-file", 2, 0, 0, scm_rename); |
1cc91f1b | 437 | |
0f2d19dd | 438 | SCM |
3d8d56df | 439 | scm_rename (oldname, newname) |
0f2d19dd JB |
440 | SCM oldname; |
441 | SCM newname; | |
0f2d19dd JB |
442 | { |
443 | int rv; | |
89958ad0 JB |
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); | |
0f2d19dd | 450 | #ifdef HAVE_RENAME |
89958ad0 | 451 | SCM_SYSCALL (rv = rename (SCM_ROCHARS (oldname), SCM_ROCHARS (newname))); |
02b754d3 | 452 | if (rv != 0) |
3d8d56df | 453 | scm_syserror (s_rename); |
02b754d3 | 454 | return SCM_UNSPECIFIED; |
0f2d19dd JB |
455 | #else |
456 | SCM_DEFER_INTS; | |
89958ad0 | 457 | SCM_SYSCALL (rv = link (SCM_ROCHARS (oldname), SCM_ROCHARS (newname))); |
02b754d3 | 458 | if (rv == 0) |
0f2d19dd | 459 | { |
89958ad0 | 460 | SCM_SYSCALL (rv = unlink (SCM_ROCHARS (oldname)));; |
02b754d3 | 461 | if (rv != 0) |
0f2d19dd | 462 | /* unlink failed. remove new name */ |
89958ad0 | 463 | SCM_SYSCALL (unlink (SCM_ROCHARS (newname))); |
0f2d19dd JB |
464 | } |
465 | SCM_ALLOW_INTS; | |
02b754d3 | 466 | if (rv != 0) |
3d8d56df | 467 | scm_syserror (s_rename); |
02b754d3 | 468 | return SCM_UNSPECIFIED; |
0f2d19dd JB |
469 | #endif |
470 | } | |
471 | ||
472 | ||
3d8d56df | 473 | SCM_PROC(s_delete_file, "delete-file", 1, 0, 0, scm_delete_file); |
1cc91f1b | 474 | |
2f3ed1ba | 475 | SCM |
3d8d56df | 476 | scm_delete_file (str) |
2f3ed1ba | 477 | SCM str; |
2f3ed1ba JB |
478 | { |
479 | int ans; | |
89958ad0 JB |
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))); | |
2f3ed1ba | 483 | if (ans != 0) |
3d8d56df | 484 | scm_syserror (s_delete_file); |
2f3ed1ba JB |
485 | return SCM_UNSPECIFIED; |
486 | } | |
487 | ||
0f2d19dd | 488 | |
3d8d56df | 489 | SCM_PROC (s_mkdir, "mkdir", 1, 1, 0, scm_mkdir); |
1cc91f1b | 490 | |
0f2d19dd | 491 | SCM |
3d8d56df | 492 | scm_mkdir (path, mode) |
0f2d19dd JB |
493 | SCM path; |
494 | SCM mode; | |
0f2d19dd JB |
495 | { |
496 | #ifdef HAVE_MKDIR | |
497 | int rv; | |
498 | mode_t mask; | |
89958ad0 JB |
499 | SCM_ASSERT (SCM_NIMP (path) && SCM_ROSTRINGP (path), path, SCM_ARG1, |
500 | s_mkdir); | |
501 | SCM_COERCE_SUBSTR (path); | |
0f2d19dd JB |
502 | if (SCM_UNBNDP (mode)) |
503 | { | |
504 | mask = umask (0); | |
505 | umask (mask); | |
89958ad0 | 506 | SCM_SYSCALL (rv = mkdir (SCM_ROCHARS (path), 0777 ^ mask)); |
0f2d19dd JB |
507 | } |
508 | else | |
509 | { | |
3d8d56df | 510 | SCM_ASSERT (SCM_INUMP (mode), mode, SCM_ARG2, s_mkdir); |
89958ad0 | 511 | SCM_SYSCALL (rv = mkdir (SCM_ROCHARS (path), SCM_INUM (mode))); |
0f2d19dd | 512 | } |
02b754d3 | 513 | if (rv != 0) |
3d8d56df | 514 | scm_syserror (s_mkdir); |
02b754d3 | 515 | return SCM_UNSPECIFIED; |
0f2d19dd | 516 | #else |
3d8d56df | 517 | scm_sysmissing (s_mkdir); |
02b754d3 GH |
518 | /* not reached. */ |
519 | return SCM_BOOL_F; | |
0f2d19dd JB |
520 | #endif |
521 | } | |
522 | ||
523 | ||
3d8d56df | 524 | SCM_PROC (s_rmdir, "rmdir", 1, 0, 0, scm_rmdir); |
1cc91f1b | 525 | |
0f2d19dd | 526 | SCM |
3d8d56df | 527 | scm_rmdir (path) |
0f2d19dd | 528 | SCM path; |
0f2d19dd JB |
529 | { |
530 | #ifdef HAVE_RMDIR | |
531 | int val; | |
02b754d3 | 532 | |
89958ad0 JB |
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))); | |
02b754d3 | 537 | if (val != 0) |
3d8d56df | 538 | scm_syserror (s_rmdir); |
02b754d3 | 539 | return SCM_UNSPECIFIED; |
0f2d19dd | 540 | #else |
3d8d56df | 541 | scm_sysmissing (s_rmdir); |
02b754d3 GH |
542 | /* not reached. */ |
543 | return SCM_BOOL_F; | |
0f2d19dd JB |
544 | #endif |
545 | } | |
546 | ||
547 | \f | |
548 | /* {Examining Directories} | |
549 | */ | |
550 | ||
551 | long scm_tc16_dir; | |
552 | ||
3d8d56df | 553 | SCM_PROC (s_opendir, "opendir", 1, 0, 0, scm_opendir); |
1cc91f1b | 554 | |
0f2d19dd | 555 | SCM |
3d8d56df | 556 | scm_opendir (dirname) |
0f2d19dd | 557 | SCM dirname; |
0f2d19dd JB |
558 | { |
559 | DIR *ds; | |
560 | SCM dir; | |
89958ad0 JB |
561 | SCM_ASSERT (SCM_NIMP (dirname) && SCM_ROSTRINGP (dirname), dirname, SCM_ARG1, |
562 | s_opendir); | |
563 | SCM_COERCE_SUBSTR (dirname); | |
0f2d19dd JB |
564 | SCM_NEWCELL (dir); |
565 | SCM_DEFER_INTS; | |
89958ad0 | 566 | SCM_SYSCALL (ds = opendir (SCM_ROCHARS (dirname))); |
02b754d3 | 567 | if (ds == NULL) |
3d8d56df | 568 | scm_syserror (s_opendir); |
a6c64c3c | 569 | SCM_SETCAR (dir, scm_tc16_dir | SCM_OPN); |
0f2d19dd JB |
570 | SCM_SETCDR (dir, ds); |
571 | SCM_ALLOW_INTS; | |
572 | return dir; | |
573 | } | |
574 | ||
575 | ||
3d8d56df | 576 | SCM_PROC (s_readdir, "readdir", 1, 0, 0, scm_readdir); |
1cc91f1b | 577 | |
0f2d19dd | 578 | SCM |
3d8d56df | 579 | scm_readdir (port) |
0f2d19dd | 580 | SCM port; |
0f2d19dd JB |
581 | { |
582 | struct dirent *rdent; | |
583 | SCM_DEFER_INTS; | |
3d8d56df | 584 | SCM_ASSERT (SCM_NIMP (port) && SCM_OPDIRP (port), port, SCM_ARG1, s_readdir); |
0f2d19dd JB |
585 | errno = 0; |
586 | SCM_SYSCALL (rdent = readdir ((DIR *) SCM_CDR (port))); | |
587 | SCM_ALLOW_INTS; | |
02b754d3 | 588 | if (errno != 0) |
3d8d56df | 589 | scm_syserror (s_readdir); |
02b754d3 GH |
590 | return (rdent ? scm_makfromstr (rdent->d_name, NAMLEN (rdent), 0) |
591 | : SCM_EOF_VAL); | |
0f2d19dd JB |
592 | } |
593 | ||
594 | ||
595 | ||
596 | SCM_PROC (s_rewinddir, "rewinddir", 1, 0, 0, scm_rewinddir); | |
1cc91f1b | 597 | |
0f2d19dd JB |
598 | SCM |
599 | scm_rewinddir (port) | |
600 | SCM port; | |
0f2d19dd JB |
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 | ||
3d8d56df | 609 | SCM_PROC (s_closedir, "closedir", 1, 0, 0, scm_closedir); |
1cc91f1b | 610 | |
0f2d19dd | 611 | SCM |
3d8d56df | 612 | scm_closedir (port) |
0f2d19dd | 613 | SCM port; |
0f2d19dd JB |
614 | { |
615 | int sts; | |
02b754d3 | 616 | |
3d8d56df | 617 | SCM_ASSERT (SCM_NIMP (port) && SCM_DIRP (port), port, SCM_ARG1, s_closedir); |
0f2d19dd JB |
618 | SCM_DEFER_INTS; |
619 | if (SCM_CLOSEDP (port)) | |
620 | { | |
621 | SCM_ALLOW_INTS; | |
02b754d3 | 622 | return SCM_UNSPECIFIED; |
0f2d19dd JB |
623 | } |
624 | SCM_SYSCALL (sts = closedir ((DIR *) SCM_CDR (port))); | |
02b754d3 | 625 | if (sts != 0) |
3d8d56df | 626 | scm_syserror (s_closedir); |
a6c64c3c | 627 | SCM_SETCAR (port, scm_tc16_dir); |
0f2d19dd | 628 | SCM_ALLOW_INTS; |
02b754d3 | 629 | return SCM_UNSPECIFIED; |
0f2d19dd JB |
630 | } |
631 | ||
632 | ||
633 | ||
1cc91f1b JB |
634 | |
635 | static int scm_dir_print SCM_P ((SCM sexp, SCM port, scm_print_state *pstate)); | |
636 | ||
0f2d19dd | 637 | static int |
9882ea19 | 638 | scm_dir_print (sexp, port, pstate) |
0f2d19dd JB |
639 | SCM sexp; |
640 | SCM port; | |
9882ea19 | 641 | scm_print_state *pstate; |
0f2d19dd JB |
642 | { |
643 | scm_prinport (sexp, port, "directory"); | |
644 | return 1; | |
645 | } | |
646 | ||
1cc91f1b JB |
647 | |
648 | static scm_sizet scm_dir_free SCM_P ((SCM p)); | |
649 | ||
0f2d19dd JB |
650 | static scm_sizet |
651 | scm_dir_free (p) | |
652 | SCM p; | |
0f2d19dd JB |
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 | ||
3d8d56df | 666 | SCM_PROC (s_chdir, "chdir", 1, 0, 0, scm_chdir); |
1cc91f1b | 667 | |
0f2d19dd | 668 | SCM |
3d8d56df | 669 | scm_chdir (str) |
0f2d19dd | 670 | SCM str; |
0f2d19dd JB |
671 | { |
672 | int ans; | |
02b754d3 | 673 | |
89958ad0 JB |
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))); | |
02b754d3 | 677 | if (ans != 0) |
3d8d56df | 678 | scm_syserror (s_chdir); |
02b754d3 | 679 | return SCM_UNSPECIFIED; |
0f2d19dd JB |
680 | } |
681 | ||
682 | ||
683 | ||
3d8d56df | 684 | SCM_PROC (s_getcwd, "getcwd", 0, 0, 0, scm_getcwd); |
1cc91f1b | 685 | |
0f2d19dd | 686 | SCM |
3d8d56df | 687 | scm_getcwd () |
0f2d19dd JB |
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; | |
3d8d56df | 697 | wd = scm_must_malloc (size, s_getcwd); |
0f2d19dd JB |
698 | while ((rv = getcwd (wd, size)) == 0 && errno == ERANGE) |
699 | { | |
700 | scm_must_free (wd); | |
701 | size *= 2; | |
3d8d56df | 702 | wd = scm_must_malloc (size, s_getcwd); |
0f2d19dd | 703 | } |
02b754d3 | 704 | if (rv == 0) |
3d8d56df | 705 | scm_syserror (s_getcwd); |
02b754d3 | 706 | result = scm_makfromstr (wd, strlen (wd), 0); |
0f2d19dd JB |
707 | scm_must_free (wd); |
708 | SCM_ALLOW_INTS; | |
709 | return result; | |
710 | #else | |
3d8d56df | 711 | scm_sysmissing (s_getcwd); |
02b754d3 GH |
712 | /* not reached. */ |
713 | return SCM_BOOL_F; | |
0f2d19dd JB |
714 | #endif |
715 | } | |
716 | ||
717 | \f | |
718 | ||
1cc91f1b JB |
719 | |
720 | static void fill_select_type SCM_P ((SELECT_TYPE * set, SCM list)); | |
721 | ||
0f2d19dd JB |
722 | static void |
723 | fill_select_type (set, list) | |
724 | SELECT_TYPE * set; | |
725 | SCM list; | |
0f2d19dd JB |
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); | |
0f2d19dd JB |
735 | list = SCM_CDR (list); |
736 | } | |
737 | } | |
738 | ||
1cc91f1b JB |
739 | |
740 | static SCM retrieve_select_type SCM_P ((SELECT_TYPE * set, SCM list)); | |
741 | ||
0f2d19dd JB |
742 | static SCM |
743 | retrieve_select_type (set, list) | |
744 | SELECT_TYPE * set; | |
745 | SCM list; | |
0f2d19dd JB |
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 | } | |
0f2d19dd JB |
763 | list = SCM_CDR (list); |
764 | } | |
765 | return answer; | |
766 | } | |
767 | ||
768 | ||
66b47b6c MD |
769 | /* {Checking for events} |
770 | */ | |
771 | ||
3d8d56df | 772 | SCM_PROC (s_select, "select", 3, 2, 0, scm_select); |
1cc91f1b | 773 | |
0f2d19dd | 774 | SCM |
3d8d56df | 775 | scm_select (reads, writes, excepts, secs, msecs) |
0f2d19dd JB |
776 | SCM reads; |
777 | SCM writes; | |
778 | SCM excepts; | |
779 | SCM secs; | |
780 | SCM msecs; | |
0f2d19dd JB |
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 | ||
3d8d56df GH |
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); | |
0f2d19dd JB |
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 | { | |
3d8d56df | 806 | SCM_ASSERT (SCM_INUMP (secs), secs, SCM_ARG4, s_select); |
0f2d19dd JB |
807 | if (SCM_UNBNDP (msecs)) |
808 | msecs = SCM_INUM0; | |
809 | else | |
3d8d56df | 810 | SCM_ASSERT (SCM_INUMP (msecs), msecs, SCM_ARG5, s_select); |
0f2d19dd JB |
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); | |
0f2d19dd | 820 | if (sreturn < 0) |
3d8d56df | 821 | scm_syserror (s_select); |
52f4f4d6 | 822 | SCM_ALLOW_INTS; |
02b754d3 GH |
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); | |
0f2d19dd | 827 | #else |
3d8d56df | 828 | scm_sysmissing (s_select); |
02b754d3 GH |
829 | /* not reached. */ |
830 | return SCM_BOOL_F; | |
0f2d19dd JB |
831 | #endif |
832 | } | |
833 | ||
66b47b6c MD |
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? */ | |
370312ae | 906 | # ifdef HAVE_SELECT |
66b47b6c MD |
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 | } | |
370312ae GH |
931 | # else |
932 | # ifdef FIONREAD | |
933 | { | |
934 | long remir; | |
935 | ioctl(fileno(f), FIONREAD, &remir); | |
936 | return remir; | |
937 | } | |
66b47b6c | 938 | # else |
4edc089c GH |
939 | scm_misc_error ("char-ready?", "Not fully implemented on this platform", |
940 | SCM_EOL); | |
66b47b6c MD |
941 | # endif |
942 | # endif | |
943 | } | |
944 | #endif | |
945 | ||
0f2d19dd | 946 | \f |
4c1feaa5 JB |
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 | |
0f2d19dd JB |
970 | /* {Symbolic Links} |
971 | */ | |
972 | ||
3d8d56df | 973 | SCM_PROC (s_symlink, "symlink", 2, 0, 0, scm_symlink); |
1cc91f1b | 974 | |
0f2d19dd | 975 | SCM |
3d8d56df | 976 | scm_symlink(oldpath, newpath) |
0f2d19dd JB |
977 | SCM oldpath; |
978 | SCM newpath; | |
0f2d19dd JB |
979 | { |
980 | #ifdef HAVE_SYMLINK | |
981 | int val; | |
02b754d3 | 982 | |
89958ad0 JB |
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))); | |
02b754d3 | 990 | if (val != 0) |
3d8d56df | 991 | scm_syserror (s_symlink); |
02b754d3 | 992 | return SCM_UNSPECIFIED; |
0f2d19dd | 993 | #else |
3d8d56df | 994 | scm_sysmissing (s_symlink); |
02b754d3 GH |
995 | /* not reached. */ |
996 | return SCM_BOOL_F; | |
0f2d19dd JB |
997 | #endif |
998 | } | |
999 | ||
1000 | ||
3d8d56df | 1001 | SCM_PROC (s_readlink, "readlink", 1, 0, 0, scm_readlink); |
1cc91f1b | 1002 | |
0f2d19dd | 1003 | SCM |
3d8d56df | 1004 | scm_readlink(path) |
0f2d19dd | 1005 | SCM path; |
0f2d19dd JB |
1006 | { |
1007 | #ifdef HAVE_READLINK | |
1008 | scm_sizet rv; | |
1009 | scm_sizet size = 100; | |
1010 | char *buf; | |
1011 | SCM result; | |
89958ad0 JB |
1012 | SCM_ASSERT (SCM_NIMP (path) && SCM_ROSTRINGP (path), path, (char *) SCM_ARG1, |
1013 | s_readlink); | |
1014 | SCM_COERCE_SUBSTR (path); | |
0f2d19dd | 1015 | SCM_DEFER_INTS; |
3d8d56df | 1016 | buf = scm_must_malloc (size, s_readlink); |
89958ad0 | 1017 | while ((rv = readlink (SCM_ROCHARS (path), buf, (scm_sizet) size)) == size) |
0f2d19dd JB |
1018 | { |
1019 | scm_must_free (buf); | |
1020 | size *= 2; | |
3d8d56df | 1021 | buf = scm_must_malloc (size, s_readlink); |
0f2d19dd | 1022 | } |
02b754d3 | 1023 | if (rv == -1) |
3d8d56df | 1024 | scm_syserror (s_readlink); |
02b754d3 | 1025 | result = scm_makfromstr (buf, rv, 0); |
0f2d19dd JB |
1026 | scm_must_free (buf); |
1027 | SCM_ALLOW_INTS; | |
1028 | return result; | |
1029 | #else | |
3d8d56df | 1030 | scm_sysmissing (s_readlink); |
02b754d3 GH |
1031 | /* not reached. */ |
1032 | return SCM_BOOL_F; | |
0f2d19dd JB |
1033 | #endif |
1034 | } | |
1035 | ||
1036 | ||
3d8d56df | 1037 | SCM_PROC (s_lstat, "lstat", 1, 0, 0, scm_lstat); |
1cc91f1b | 1038 | |
0f2d19dd | 1039 | SCM |
3d8d56df | 1040 | scm_lstat(str) |
0f2d19dd | 1041 | SCM str; |
0f2d19dd | 1042 | { |
02b754d3 GH |
1043 | #ifdef HAVE_LSTAT |
1044 | int rv; | |
0f2d19dd | 1045 | struct stat stat_temp; |
02b754d3 | 1046 | |
89958ad0 JB |
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)); | |
02b754d3 | 1051 | if (rv != 0) |
3d8d56df GH |
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 | } | |
02b754d3 | 1061 | return scm_stat2scm(&stat_temp); |
0f2d19dd | 1062 | #else |
3d8d56df | 1063 | scm_sysmissing (s_lstat); |
02b754d3 GH |
1064 | /* not reached. */ |
1065 | return SCM_BOOL_F; | |
0f2d19dd JB |
1066 | #endif |
1067 | } | |
1068 | ||
1069 | ||
3d8d56df | 1070 | SCM_PROC (s_copy_file, "copy-file", 2, 0, 0, scm_copy_file); |
1cc91f1b | 1071 | |
0f2d19dd | 1072 | SCM |
3d8d56df | 1073 | scm_copy_file (oldfile, newfile) |
0f2d19dd JB |
1074 | SCM oldfile; |
1075 | SCM newfile; | |
0f2d19dd JB |
1076 | { |
1077 | int oldfd, newfd; | |
1078 | int n; | |
1079 | char buf[BUFSIZ]; /* this space could be shared. */ | |
1080 | struct stat oldstat; | |
1081 | ||
3d8d56df | 1082 | SCM_ASSERT (SCM_NIMP (oldfile) && SCM_ROSTRINGP (oldfile), oldfile, SCM_ARG1, s_copy_file); |
0f2d19dd JB |
1083 | if (SCM_SUBSTRP (oldfile)) |
1084 | oldfile = scm_makfromstr (SCM_ROCHARS (oldfile), SCM_ROLENGTH (oldfile), 0); | |
3d8d56df | 1085 | SCM_ASSERT (SCM_NIMP (newfile) && SCM_ROSTRINGP (newfile), newfile, SCM_ARG2, s_copy_file); |
0f2d19dd JB |
1086 | if (SCM_SUBSTRP (newfile)) |
1087 | newfile = scm_makfromstr (SCM_ROCHARS (newfile), SCM_ROLENGTH (newfile), 0); | |
1088 | if (stat (SCM_ROCHARS (oldfile), &oldstat) == -1) | |
3d8d56df | 1089 | scm_syserror (s_copy_file); |
0f2d19dd JB |
1090 | SCM_DEFER_INTS; |
1091 | oldfd = open (SCM_ROCHARS (oldfile), O_RDONLY); | |
1092 | if (oldfd == -1) | |
3d8d56df | 1093 | scm_syserror (s_copy_file); |
02b754d3 GH |
1094 | |
1095 | /* use POSIX flags instead of 07777?. */ | |
0f2d19dd JB |
1096 | newfd = open (SCM_ROCHARS (newfile), O_WRONLY | O_CREAT | O_TRUNC, |
1097 | oldstat.st_mode & 07777); | |
1098 | if (newfd == -1) | |
3d8d56df | 1099 | scm_syserror (s_copy_file); |
02b754d3 | 1100 | |
0f2d19dd JB |
1101 | while ((n = read (oldfd, buf, sizeof buf)) > 0) |
1102 | if (write (newfd, buf, n) != n) | |
1103 | { | |
1104 | close (oldfd); | |
1105 | close (newfd); | |
3d8d56df | 1106 | scm_syserror (s_copy_file); |
0f2d19dd JB |
1107 | } |
1108 | close (oldfd); | |
1109 | if (close (newfd) == -1) | |
3d8d56df | 1110 | scm_syserror (s_copy_file); |
0f2d19dd | 1111 | SCM_ALLOW_INTS; |
02b754d3 | 1112 | return SCM_UNSPECIFIED; |
0f2d19dd JB |
1113 | } |
1114 | ||
1115 | \f | |
1cc91f1b | 1116 | |
0f2d19dd JB |
1117 | void |
1118 | scm_init_filesys () | |
0f2d19dd | 1119 | { |
52f4f4d6 | 1120 | scm_add_feature ("i/o-extensions"); |
0f2d19dd | 1121 | |
0f2d19dd JB |
1122 | scm_tc16_dir = scm_newsmob (&dir_smob); |
1123 | ||
3d8d56df GH |
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 | ||
4c1feaa5 JB |
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 | |
3d8d56df | 1182 | |
0f2d19dd JB |
1183 | #include "filesys.x" |
1184 | } |