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