Commit | Line | Data |
---|---|---|
2b829bbb | 1 | /* Copyright (C) 1996,1997,1998,1999,2000,2001, 2002, 2004, 2006 Free Software Foundation, Inc. |
0f2d19dd | 2 | * |
73be1d9e MV |
3 | * This library is free software; you can redistribute it and/or |
4 | * modify it under the terms of the GNU Lesser General Public | |
5 | * License as published by the Free Software Foundation; either | |
6 | * version 2.1 of the License, or (at your option) any later version. | |
0f2d19dd | 7 | * |
73be1d9e MV |
8 | * This library 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 GNU | |
11 | * Lesser General Public License for more details. | |
0f2d19dd | 12 | * |
73be1d9e MV |
13 | * You should have received a copy of the GNU Lesser General Public |
14 | * License along with this library; if not, write to the Free Software | |
92205699 | 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
73be1d9e | 16 | */ |
1bbd0b84 | 17 | |
1bbd0b84 | 18 | |
0f2d19dd | 19 | \f |
464ee095 KR |
20 | |
21 | /* See stime.c for comments on why _POSIX_C_SOURCE is not always defined. */ | |
00fbf1e0 | 22 | #define _GNU_SOURCE /* ask glibc for everything */ |
2b829bbb | 23 | #define _LARGEFILE64_SOURCE /* ask for stat64 etc */ |
464ee095 KR |
24 | #ifdef __hpux |
25 | #define _POSIX_C_SOURCE 199506L /* for readdir_r */ | |
edea856c | 26 | #endif |
6d484a0b | 27 | |
83079454 RB |
28 | #if HAVE_CONFIG_H |
29 | # include <config.h> | |
30 | #endif | |
31 | ||
2b829bbb KR |
32 | /* AIX requires this to be the first thing in the file. The #pragma |
33 | directive is indented so pre-ANSI compilers will ignore it, rather | |
34 | than choke on it. */ | |
35 | #ifndef __GNUC__ | |
36 | # if HAVE_ALLOCA_H | |
37 | # include <alloca.h> | |
38 | # else | |
39 | # ifdef _AIX | |
40 | # pragma alloca | |
41 | # else | |
42 | # ifndef alloca /* predefined by HP cc +Olibcalls */ | |
43 | char *alloca (); | |
44 | # endif | |
45 | # endif | |
46 | # endif | |
47 | #endif | |
48 | ||
3d8d56df | 49 | #include <stdio.h> |
e6e2e95a MD |
50 | #include <errno.h> |
51 | ||
a0599745 MD |
52 | #include "libguile/_scm.h" |
53 | #include "libguile/smob.h" | |
54 | #include "libguile/feature.h" | |
55 | #include "libguile/fports.h" | |
2b829bbb | 56 | #include "libguile/private-gc.h" /* for SCM_MAX */ |
a0599745 MD |
57 | #include "libguile/iselect.h" |
58 | #include "libguile/strings.h" | |
59 | #include "libguile/vectors.h" | |
c96d76b8 | 60 | #include "libguile/lang.h" |
1299a0f1 | 61 | #include "libguile/dynwind.h" |
0f2d19dd | 62 | |
a0599745 MD |
63 | #include "libguile/validate.h" |
64 | #include "libguile/filesys.h" | |
def804a3 | 65 | |
0f2d19dd | 66 | \f |
def804a3 JB |
67 | #ifdef HAVE_IO_H |
68 | #include <io.h> | |
69 | #endif | |
70 | ||
e0c73a1c MV |
71 | #ifdef HAVE_DIRECT_H |
72 | #include <direct.h> | |
73 | #endif | |
74 | ||
0f2d19dd JB |
75 | #ifdef TIME_WITH_SYS_TIME |
76 | # include <sys/time.h> | |
77 | # include <time.h> | |
78 | #else | |
79 | # if HAVE_SYS_TIME_H | |
80 | # include <sys/time.h> | |
81 | # else | |
82 | # include <time.h> | |
83 | # endif | |
84 | #endif | |
85 | ||
86 | #ifdef HAVE_UNISTD_H | |
87 | #include <unistd.h> | |
88 | #endif | |
89 | ||
3594582b | 90 | #ifdef LIBC_H_WITH_UNISTD_H |
1f9e2226 JB |
91 | #include <libc.h> |
92 | #endif | |
93 | ||
0f2d19dd JB |
94 | #ifdef HAVE_SYS_SELECT_H |
95 | #include <sys/select.h> | |
96 | #endif | |
97 | ||
1f9e2226 JB |
98 | #ifdef HAVE_STRING_H |
99 | #include <string.h> | |
100 | #endif | |
101 | ||
8cc71382 | 102 | #include <sys/types.h> |
0f2d19dd JB |
103 | #include <sys/stat.h> |
104 | #include <fcntl.h> | |
105 | ||
82893676 | 106 | #ifdef HAVE_PWD_H |
0f2d19dd | 107 | #include <pwd.h> |
82893676 | 108 | #endif |
0f2d19dd JB |
109 | |
110 | ||
e0c73a1c MV |
111 | #if defined (__MINGW32__) || defined (_MSC_VER) || defined (__BORLANDC__) |
112 | # include "win32-dirent.h" | |
113 | # define NAMLEN(dirent) strlen((dirent)->d_name) | |
2b829bbb | 114 | /* The following bits are per AC_HEADER_DIRENT doco in the autoconf manual */ |
e0c73a1c | 115 | #elif HAVE_DIRENT_H |
0f2d19dd JB |
116 | # include <dirent.h> |
117 | # define NAMLEN(dirent) strlen((dirent)->d_name) | |
118 | #else | |
119 | # define dirent direct | |
120 | # define NAMLEN(dirent) (dirent)->d_namlen | |
121 | # if HAVE_SYS_NDIR_H | |
122 | # include <sys/ndir.h> | |
123 | # endif | |
124 | # if HAVE_SYS_DIR_H | |
125 | # include <sys/dir.h> | |
126 | # endif | |
127 | # if HAVE_NDIR_H | |
128 | # include <ndir.h> | |
129 | # endif | |
130 | #endif | |
131 | ||
d7b8a21a JB |
132 | /* Ultrix has S_IFSOCK, but no S_ISSOCK. Ipe! */ |
133 | #if defined (S_IFSOCK) && ! defined (S_ISSOCK) | |
134 | #define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK) | |
135 | #endif | |
82893676 MG |
136 | |
137 | /* The MinGW gcc does not define the S_ISSOCK macro. Any other native Windows | |
138 | compiler like BorlandC or MSVC has none of these macros defined. */ | |
139 | #ifdef __MINGW32__ | |
8f99e3f3 SJ |
140 | |
141 | # ifdef _S_IFIFO | |
142 | # undef _S_IFIFO | |
143 | # endif | |
144 | # ifdef _S_IFCHR | |
145 | # undef _S_IFCHR | |
146 | # endif | |
147 | # ifdef _S_IFBLK | |
148 | # undef _S_IFBLK | |
149 | # endif | |
150 | # ifdef _S_IFDIR | |
151 | # undef _S_IFDIR | |
152 | # endif | |
153 | # ifdef _S_IFREG | |
154 | # undef _S_IFREG | |
155 | # endif | |
156 | # ifdef _S_IFSOCK | |
157 | # undef _S_IFSOCK | |
158 | # endif | |
159 | ||
160 | # define _S_IFIFO 0x1000 /* FIFO */ | |
161 | # define _S_IFCHR 0x2000 /* Character */ | |
162 | # define _S_IFBLK 0x3000 /* Block */ | |
163 | # define _S_IFDIR 0x4000 /* Directory */ | |
164 | # define _S_IFREG 0x8000 /* Regular */ | |
165 | # define _S_IFSOCK 0xC000 /* Socket */ | |
166 | ||
167 | # ifdef S_ISBLK | |
168 | # undef S_ISBLK | |
169 | # endif | |
170 | # ifdef S_ISFIFO | |
171 | # undef S_ISFIFO | |
172 | # endif | |
173 | # ifdef S_ISCHR | |
174 | # undef S_ISCHR | |
175 | # endif | |
176 | # ifdef S_ISDIR | |
177 | # undef S_ISDIR | |
178 | # endif | |
179 | # ifdef S_ISREG | |
180 | # undef S_ISREG | |
181 | # endif | |
182 | # ifdef S_ISSOCK | |
183 | # undef S_ISSOCK | |
184 | # endif | |
185 | ||
186 | # define S_ISBLK(mode) (((mode) & _S_IFMT) == _S_IFBLK) | |
82893676 | 187 | # define S_ISFIFO(mode) (((mode) & _S_IFMT) == _S_IFIFO) |
8f99e3f3 SJ |
188 | # define S_ISCHR(mode) (((mode) & _S_IFMT) == _S_IFCHR) |
189 | # define S_ISDIR(mode) (((mode) & _S_IFMT) == _S_IFDIR) | |
190 | # define S_ISREG(mode) (((mode) & _S_IFMT) == _S_IFREG) | |
191 | # define S_ISSOCK(mode) (((mode) & _S_IFMT) == _S_IFSOCK) | |
192 | ||
193 | #endif /* __MINGW32__ */ | |
82893676 MG |
194 | |
195 | /* Some more definitions for the native Windows port. */ | |
196 | #ifdef __MINGW32__ | |
197 | # define mkdir(path, mode) mkdir (path) | |
198 | # define fsync(fd) _commit (fd) | |
199 | # define fchmod(fd, mode) (-1) | |
200 | #endif /* __MINGW32__ */ | |
2b829bbb KR |
201 | |
202 | /* This definition is for Solaris 10, it's probably not right elsewhere, but | |
203 | that's ok, it shouldn't be used elsewhere. */ | |
204 | #if ! HAVE_DIRFD | |
205 | #define dirfd(dirstream) (dirstream->dd_fd) | |
206 | #endif | |
207 | ||
0f2d19dd JB |
208 | \f |
209 | ||
1299a0f1 MV |
210 | /* Two helper macros for an often used pattern */ |
211 | ||
212 | #define STRING_SYSCALL(str,cstr,code) \ | |
213 | do { \ | |
214 | int eno; \ | |
215 | char *cstr = scm_to_locale_string (str); \ | |
216 | SCM_SYSCALL (code); \ | |
217 | eno = errno; free (cstr); errno = eno; \ | |
218 | } while (0) | |
219 | ||
220 | #define STRING2_SYSCALL(str1,cstr1,str2,cstr2,code) \ | |
221 | do { \ | |
222 | int eno; \ | |
223 | char *cstr1, *cstr2; \ | |
661ae7ab | 224 | scm_dynwind_begin (0); \ |
1299a0f1 | 225 | cstr1 = scm_to_locale_string (str1); \ |
661ae7ab | 226 | scm_dynwind_free (cstr1); \ |
1299a0f1 | 227 | cstr2 = scm_to_locale_string (str2); \ |
661ae7ab | 228 | scm_dynwind_free (cstr2); \ |
1299a0f1 | 229 | SCM_SYSCALL (code); \ |
661ae7ab | 230 | eno = errno; scm_dynwind_end (); errno = eno; \ |
1299a0f1 | 231 | } while (0) |
0f2d19dd JB |
232 | |
233 | \f | |
234 | ||
235 | /* {Permissions} | |
236 | */ | |
237 | ||
82893676 | 238 | #ifdef HAVE_CHOWN |
a1ec6916 | 239 | SCM_DEFINE (scm_chown, "chown", 3, 0, 0, |
1bbd0b84 | 240 | (SCM object, SCM owner, SCM group), |
d831b039 GH |
241 | "Change the ownership and group of the file referred to by @var{object} to\n" |
242 | "the integer values @var{owner} and @var{group}. @var{object} can be\n" | |
243 | "a string containing a file name or, if the platform\n" | |
244 | "supports fchown, a port or integer file descriptor\n" | |
245 | "which is open on the file. The return value\n" | |
d3818c29 | 246 | "is unspecified.\n\n" |
d831b039 | 247 | "If @var{object} is a symbolic link, either the\n" |
d3818c29 MD |
248 | "ownership of the link or the ownership of the referenced file will be\n" |
249 | "changed depending on the operating system (lchown is\n" | |
250 | "unsupported at present). If @var{owner} or @var{group} is specified\n" | |
251 | "as @code{-1}, then that ID is not changed.") | |
1bbd0b84 | 252 | #define FUNC_NAME s_scm_chown |
0f2d19dd | 253 | { |
6afcd3b2 | 254 | int rv; |
02b754d3 | 255 | |
78446828 MV |
256 | object = SCM_COERCE_OUTPORT (object); |
257 | ||
d831b039 | 258 | #ifdef HAVE_FCHOWN |
a55c2b68 | 259 | if (scm_is_integer (object) || (SCM_OPFPORTP (object))) |
6afcd3b2 | 260 | { |
a55c2b68 MV |
261 | int fdes = (SCM_OPFPORTP (object)? |
262 | SCM_FPORT_FDES (object) : scm_to_int (object)); | |
d831b039 | 263 | |
a55c2b68 | 264 | SCM_SYSCALL (rv = fchown (fdes, scm_to_int (owner), scm_to_int (group))); |
6afcd3b2 GH |
265 | } |
266 | else | |
d831b039 | 267 | #endif |
6afcd3b2 | 268 | { |
1299a0f1 MV |
269 | STRING_SYSCALL (object, c_object, |
270 | rv = chown (c_object, | |
271 | scm_to_int (owner), scm_to_int (group))); | |
6afcd3b2 GH |
272 | } |
273 | if (rv == -1) | |
1bbd0b84 | 274 | SCM_SYSERROR; |
02b754d3 | 275 | return SCM_UNSPECIFIED; |
0f2d19dd | 276 | } |
1bbd0b84 | 277 | #undef FUNC_NAME |
82893676 | 278 | #endif /* HAVE_CHOWN */ |
0f2d19dd JB |
279 | |
280 | ||
a1ec6916 | 281 | SCM_DEFINE (scm_chmod, "chmod", 2, 0, 0, |
1bbd0b84 | 282 | (SCM object, SCM mode), |
d3818c29 MD |
283 | "Changes the permissions of the file referred to by @var{obj}.\n" |
284 | "@var{obj} can be a string containing a file name or a port or integer file\n" | |
285 | "descriptor which is open on a file (in which case @code{fchmod} is used\n" | |
286 | "as the underlying system call).\n" | |
287 | "@var{mode} specifies\n" | |
288 | "the new permissions as a decimal number, e.g., @code{(chmod \"foo\" #o755)}.\n" | |
289 | "The return value is unspecified.") | |
1bbd0b84 | 290 | #define FUNC_NAME s_scm_chmod |
0f2d19dd JB |
291 | { |
292 | int rv; | |
6afcd3b2 GH |
293 | int fdes; |
294 | ||
78446828 MV |
295 | object = SCM_COERCE_OUTPORT (object); |
296 | ||
a55c2b68 | 297 | if (scm_is_integer (object) || SCM_OPFPORTP (object)) |
89958ad0 | 298 | { |
a55c2b68 MV |
299 | if (scm_is_integer (object)) |
300 | fdes = scm_to_int (object); | |
6afcd3b2 | 301 | else |
77a76b64 | 302 | fdes = SCM_FPORT_FDES (object); |
a55c2b68 | 303 | SCM_SYSCALL (rv = fchmod (fdes, scm_to_int (mode))); |
89958ad0 | 304 | } |
0f2d19dd JB |
305 | else |
306 | { | |
1299a0f1 MV |
307 | STRING_SYSCALL (object, c_object, |
308 | rv = chmod (c_object, scm_to_int (mode))); | |
0f2d19dd | 309 | } |
6afcd3b2 | 310 | if (rv == -1) |
1bbd0b84 | 311 | SCM_SYSERROR; |
02b754d3 | 312 | return SCM_UNSPECIFIED; |
0f2d19dd | 313 | } |
1bbd0b84 | 314 | #undef FUNC_NAME |
0f2d19dd | 315 | |
a1ec6916 | 316 | SCM_DEFINE (scm_umask, "umask", 0, 1, 0, |
1bbd0b84 | 317 | (SCM mode), |
bb2c02f2 | 318 | "If @var{mode} is omitted, returns a decimal number representing the current\n" |
d3818c29 MD |
319 | "file creation mask. Otherwise the file creation mask is set to\n" |
320 | "@var{mode} and the previous value is returned.\n\n" | |
321 | "E.g., @code{(umask #o022)} sets the mask to octal 22, decimal 18.") | |
1bbd0b84 | 322 | #define FUNC_NAME s_scm_umask |
0f2d19dd JB |
323 | { |
324 | mode_t mask; | |
325 | if (SCM_UNBNDP (mode)) | |
326 | { | |
327 | mask = umask (0); | |
328 | umask (mask); | |
329 | } | |
330 | else | |
331 | { | |
a55c2b68 | 332 | mask = umask (scm_to_uint (mode)); |
0f2d19dd | 333 | } |
a55c2b68 | 334 | return scm_from_uint (mask); |
0f2d19dd | 335 | } |
1bbd0b84 | 336 | #undef FUNC_NAME |
0f2d19dd JB |
337 | |
338 | \f | |
0f2d19dd | 339 | |
a1ec6916 | 340 | SCM_DEFINE (scm_open_fdes, "open-fdes", 2, 1, 0, |
1bbd0b84 | 341 | (SCM path, SCM flags, SCM mode), |
1e6808ea MG |
342 | "Similar to @code{open} but return a file descriptor instead of\n" |
343 | "a port.") | |
1bbd0b84 | 344 | #define FUNC_NAME s_scm_open_fdes |
0f2d19dd JB |
345 | { |
346 | int fd; | |
3d8d56df | 347 | int iflags; |
6afcd3b2 | 348 | int imode; |
0f2d19dd | 349 | |
1be6b49c ML |
350 | iflags = SCM_NUM2INT (2, flags); |
351 | imode = SCM_NUM2INT_DEF (3, mode, 0666); | |
1299a0f1 | 352 | STRING_SYSCALL (path, c_path, fd = open (c_path, iflags, imode)); |
3d8d56df | 353 | if (fd == -1) |
1bbd0b84 | 354 | SCM_SYSERROR; |
e11e83f3 | 355 | return scm_from_int (fd); |
6afcd3b2 | 356 | } |
1bbd0b84 | 357 | #undef FUNC_NAME |
6afcd3b2 | 358 | |
a1ec6916 | 359 | SCM_DEFINE (scm_open, "open", 2, 1, 0, |
1bbd0b84 | 360 | (SCM path, SCM flags, SCM mode), |
d3818c29 MD |
361 | "Open the file named by @var{path} for reading and/or writing.\n" |
362 | "@var{flags} is an integer specifying how the file should be opened.\n" | |
363 | "@var{mode} is an integer specifying the permission bits of the file, if\n" | |
364 | "it needs to be created, before the umask is applied. The default is 666\n" | |
365 | "(Unix itself has no default).\n\n" | |
366 | "@var{flags} can be constructed by combining variables using @code{logior}.\n" | |
367 | "Basic flags are:\n\n" | |
368 | "@defvar O_RDONLY\n" | |
369 | "Open the file read-only.\n" | |
370 | "@end defvar\n" | |
371 | "@defvar O_WRONLY\n" | |
9401323e | 372 | "Open the file write-only.\n" |
d3818c29 MD |
373 | "@end defvar\n" |
374 | "@defvar O_RDWR\n" | |
375 | "Open the file read/write.\n" | |
376 | "@end defvar\n" | |
377 | "@defvar O_APPEND\n" | |
378 | "Append to the file instead of truncating.\n" | |
379 | "@end defvar\n" | |
380 | "@defvar O_CREAT\n" | |
381 | "Create the file if it does not already exist.\n" | |
382 | "@end defvar\n\n" | |
383 | "See the Unix documentation of the @code{open} system call\n" | |
384 | "for additional flags.") | |
1bbd0b84 | 385 | #define FUNC_NAME s_scm_open |
6afcd3b2 GH |
386 | { |
387 | SCM newpt; | |
388 | char *port_mode; | |
389 | int fd; | |
6afcd3b2 GH |
390 | int iflags; |
391 | ||
e11e83f3 | 392 | fd = scm_to_int (scm_open_fdes (path, flags, mode)); |
1be6b49c | 393 | iflags = SCM_NUM2INT (2, flags); |
3d8d56df | 394 | if (iflags & O_RDWR) |
77a76b64 JB |
395 | { |
396 | if (iflags & O_APPEND) | |
397 | port_mode = "a+"; | |
398 | else if (iflags & O_CREAT) | |
399 | port_mode = "w+"; | |
400 | else | |
401 | port_mode = "r+"; | |
402 | } | |
3d8d56df | 403 | else { |
77a76b64 JB |
404 | if (iflags & O_APPEND) |
405 | port_mode = "a"; | |
406 | else if (iflags & O_WRONLY) | |
3d8d56df GH |
407 | port_mode = "w"; |
408 | else | |
409 | port_mode = "r"; | |
410 | } | |
77a76b64 | 411 | newpt = scm_fdes_to_port (fd, port_mode, path); |
3d8d56df | 412 | return newpt; |
0f2d19dd | 413 | } |
1bbd0b84 | 414 | #undef FUNC_NAME |
0f2d19dd | 415 | |
a1ec6916 | 416 | SCM_DEFINE (scm_close, "close", 1, 0, 0, |
1bbd0b84 | 417 | (SCM fd_or_port), |
8f85c0c6 | 418 | "Similar to close-port (@pxref{Closing, close-port}),\n" |
d3818c29 MD |
419 | "but also works on file descriptors. A side\n" |
420 | "effect of closing a file descriptor is that any ports using that file\n" | |
421 | "descriptor are moved to a different file descriptor and have\n" | |
422 | "their revealed counts set to zero.") | |
1bbd0b84 | 423 | #define FUNC_NAME s_scm_close |
eadd48de GH |
424 | { |
425 | int rv; | |
426 | int fd; | |
427 | ||
78446828 MV |
428 | fd_or_port = SCM_COERCE_OUTPORT (fd_or_port); |
429 | ||
0c95b57d | 430 | if (SCM_PORTP (fd_or_port)) |
eadd48de | 431 | return scm_close_port (fd_or_port); |
a55c2b68 | 432 | fd = scm_to_int (fd_or_port); |
eadd48de | 433 | scm_evict_ports (fd); /* see scsh manual. */ |
a9488d12 | 434 | SCM_SYSCALL (rv = close (fd)); |
eadd48de GH |
435 | /* following scsh, closing an already closed file descriptor is |
436 | not an error. */ | |
437 | if (rv < 0 && errno != EBADF) | |
1bbd0b84 | 438 | SCM_SYSERROR; |
7888309b | 439 | return scm_from_bool (rv >= 0); |
eadd48de | 440 | } |
1bbd0b84 | 441 | #undef FUNC_NAME |
eadd48de | 442 | |
c2ca4493 GH |
443 | SCM_DEFINE (scm_close_fdes, "close-fdes", 1, 0, 0, |
444 | (SCM fd), | |
445 | "A simple wrapper for the @code{close} system call.\n" | |
446 | "Close file descriptor @var{fd}, which must be an integer.\n" | |
447 | "Unlike close (@pxref{Ports and File Descriptors, close}),\n" | |
448 | "the file descriptor will be closed even if a port is using it.\n" | |
449 | "The return value is unspecified.") | |
450 | #define FUNC_NAME s_scm_close_fdes | |
451 | { | |
452 | int c_fd; | |
453 | int rv; | |
454 | ||
a55c2b68 | 455 | c_fd = scm_to_int (fd); |
c2ca4493 GH |
456 | SCM_SYSCALL (rv = close (c_fd)); |
457 | if (rv < 0) | |
458 | SCM_SYSERROR; | |
459 | return SCM_UNSPECIFIED; | |
460 | } | |
461 | #undef FUNC_NAME | |
462 | ||
0f2d19dd JB |
463 | \f |
464 | /* {Files} | |
465 | */ | |
1cc91f1b | 466 | |
ae5253c5 GH |
467 | SCM_SYMBOL (scm_sym_regular, "regular"); |
468 | SCM_SYMBOL (scm_sym_directory, "directory"); | |
f326ecf3 | 469 | #ifdef HAVE_S_ISLNK |
ae5253c5 | 470 | SCM_SYMBOL (scm_sym_symlink, "symlink"); |
f326ecf3 | 471 | #endif |
ae5253c5 GH |
472 | SCM_SYMBOL (scm_sym_block_special, "block-special"); |
473 | SCM_SYMBOL (scm_sym_char_special, "char-special"); | |
474 | SCM_SYMBOL (scm_sym_fifo, "fifo"); | |
475 | SCM_SYMBOL (scm_sym_sock, "socket"); | |
476 | SCM_SYMBOL (scm_sym_unknown, "unknown"); | |
477 | ||
0f2d19dd | 478 | static SCM |
2b829bbb | 479 | scm_stat2scm (struct stat_or_stat64 *stat_temp) |
0f2d19dd | 480 | { |
00ffa0e7 | 481 | SCM ans = scm_c_make_vector (15, SCM_UNSPECIFIED); |
ae5253c5 | 482 | |
4057a3e0 | 483 | SCM_SIMPLE_VECTOR_SET(ans, 0, scm_from_ulong (stat_temp->st_dev)); |
2b829bbb | 484 | SCM_SIMPLE_VECTOR_SET(ans, 1, scm_from_ino_t_or_ino64_t (stat_temp->st_ino)); |
4057a3e0 MV |
485 | SCM_SIMPLE_VECTOR_SET(ans, 2, scm_from_ulong (stat_temp->st_mode)); |
486 | SCM_SIMPLE_VECTOR_SET(ans, 3, scm_from_ulong (stat_temp->st_nlink)); | |
487 | SCM_SIMPLE_VECTOR_SET(ans, 4, scm_from_ulong (stat_temp->st_uid)); | |
488 | SCM_SIMPLE_VECTOR_SET(ans, 5, scm_from_ulong (stat_temp->st_gid)); | |
1fd85bc5 | 489 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
4057a3e0 | 490 | SCM_SIMPLE_VECTOR_SET(ans, 6, scm_from_ulong (stat_temp->st_rdev)); |
0f2d19dd | 491 | #else |
4057a3e0 | 492 | SCM_SIMPLE_VECTOR_SET(ans, 6, SCM_BOOL_F); |
0f2d19dd | 493 | #endif |
2b829bbb | 494 | SCM_SIMPLE_VECTOR_SET(ans, 7, scm_from_off_t_or_off64_t (stat_temp->st_size)); |
4057a3e0 MV |
495 | SCM_SIMPLE_VECTOR_SET(ans, 8, scm_from_ulong (stat_temp->st_atime)); |
496 | SCM_SIMPLE_VECTOR_SET(ans, 9, scm_from_ulong (stat_temp->st_mtime)); | |
497 | SCM_SIMPLE_VECTOR_SET(ans, 10, scm_from_ulong (stat_temp->st_ctime)); | |
1fd85bc5 | 498 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
4057a3e0 | 499 | SCM_SIMPLE_VECTOR_SET(ans, 11, scm_from_ulong (stat_temp->st_blksize)); |
0f2d19dd | 500 | #else |
4057a3e0 | 501 | SCM_SIMPLE_VECTOR_SET(ans, 11, scm_from_ulong (4096L)); |
0f2d19dd | 502 | #endif |
1fd85bc5 | 503 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
2b829bbb | 504 | SCM_SIMPLE_VECTOR_SET(ans, 12, scm_from_blkcnt_t_or_blkcnt64_t (stat_temp->st_blocks)); |
0f2d19dd | 505 | #else |
4057a3e0 | 506 | SCM_SIMPLE_VECTOR_SET(ans, 12, SCM_BOOL_F); |
0f2d19dd | 507 | #endif |
ae5253c5 GH |
508 | { |
509 | int mode = stat_temp->st_mode; | |
510 | ||
511 | if (S_ISREG (mode)) | |
4057a3e0 | 512 | SCM_SIMPLE_VECTOR_SET(ans, 13, scm_sym_regular); |
ae5253c5 | 513 | else if (S_ISDIR (mode)) |
4057a3e0 | 514 | SCM_SIMPLE_VECTOR_SET(ans, 13, scm_sym_directory); |
f326ecf3 | 515 | #ifdef HAVE_S_ISLNK |
ae5253c5 | 516 | else if (S_ISLNK (mode)) |
4057a3e0 | 517 | SCM_SIMPLE_VECTOR_SET(ans, 13, scm_sym_symlink); |
f326ecf3 | 518 | #endif |
ae5253c5 | 519 | else if (S_ISBLK (mode)) |
4057a3e0 | 520 | SCM_SIMPLE_VECTOR_SET(ans, 13, scm_sym_block_special); |
ae5253c5 | 521 | else if (S_ISCHR (mode)) |
4057a3e0 | 522 | SCM_SIMPLE_VECTOR_SET(ans, 13, scm_sym_char_special); |
ae5253c5 | 523 | else if (S_ISFIFO (mode)) |
4057a3e0 | 524 | SCM_SIMPLE_VECTOR_SET(ans, 13, scm_sym_fifo); |
e655d034 | 525 | #ifdef S_ISSOCK |
ae5253c5 | 526 | else if (S_ISSOCK (mode)) |
4057a3e0 | 527 | SCM_SIMPLE_VECTOR_SET(ans, 13, scm_sym_sock); |
e655d034 | 528 | #endif |
ae5253c5 | 529 | else |
4057a3e0 | 530 | SCM_SIMPLE_VECTOR_SET(ans, 13, scm_sym_unknown); |
ae5253c5 | 531 | |
4057a3e0 | 532 | SCM_SIMPLE_VECTOR_SET(ans, 14, scm_from_int ((~S_IFMT) & mode)); |
ae5253c5 GH |
533 | |
534 | /* the layout of the bits in ve[14] is intended to be portable. | |
535 | If there are systems that don't follow the usual convention, | |
536 | the following could be used: | |
537 | ||
538 | tmp = 0; | |
539 | if (S_ISUID & mode) tmp += 1; | |
540 | tmp <<= 1; | |
541 | if (S_IRGRP & mode) tmp += 1; | |
542 | tmp <<= 1; | |
543 | if (S_ISVTX & mode) tmp += 1; | |
544 | tmp <<= 1; | |
545 | if (S_IRUSR & mode) tmp += 1; | |
546 | tmp <<= 1; | |
547 | if (S_IWUSR & mode) tmp += 1; | |
548 | tmp <<= 1; | |
549 | if (S_IXUSR & mode) tmp += 1; | |
550 | tmp <<= 1; | |
551 | if (S_IWGRP & mode) tmp += 1; | |
552 | tmp <<= 1; | |
553 | if (S_IXGRP & mode) tmp += 1; | |
554 | tmp <<= 1; | |
555 | if (S_IROTH & mode) tmp += 1; | |
556 | tmp <<= 1; | |
557 | if (S_IWOTH & mode) tmp += 1; | |
558 | tmp <<= 1; | |
559 | if (S_IXOTH & mode) tmp += 1; | |
560 | ||
4057a3e0 | 561 | SCM_SIMPLE_VECTOR_SET(ans, 14, scm_from_int (tmp)); |
ae5253c5 GH |
562 | |
563 | */ | |
564 | } | |
0f2d19dd JB |
565 | |
566 | return ans; | |
567 | } | |
568 | ||
e0c73a1c MV |
569 | #ifdef __MINGW32__ |
570 | /* | |
571 | * Try getting the appropiate stat buffer for a given file descriptor | |
572 | * under Windows. It differentiates between file, pipe and socket | |
573 | * descriptors. | |
574 | */ | |
575 | static int fstat_Win32 (int fdes, struct stat *buf) | |
576 | { | |
577 | int error, optlen = sizeof (int); | |
578 | ||
579 | memset (buf, 0, sizeof (struct stat)); | |
580 | ||
581 | /* Is this a socket ? */ | |
582 | if (getsockopt (fdes, SOL_SOCKET, SO_ERROR, (void *) &error, &optlen) >= 0) | |
583 | { | |
584 | buf->st_mode = _S_IFSOCK | _S_IREAD | _S_IWRITE | _S_IEXEC; | |
585 | buf->st_nlink = 1; | |
586 | buf->st_atime = buf->st_ctime = buf->st_mtime = time (NULL); | |
587 | return 0; | |
588 | } | |
589 | /* Maybe a regular file or pipe ? */ | |
590 | return fstat (fdes, buf); | |
591 | } | |
592 | #endif /* __MINGW32__ */ | |
593 | ||
a1ec6916 | 594 | SCM_DEFINE (scm_stat, "stat", 1, 0, 0, |
1bbd0b84 | 595 | (SCM object), |
1e6808ea MG |
596 | "Return an object containing various information about the file\n" |
597 | "determined by @var{obj}. @var{obj} can be a string containing\n" | |
598 | "a file name or a port or integer file descriptor which is open\n" | |
599 | "on a file (in which case @code{fstat} is used as the underlying\n" | |
600 | "system call).\n" | |
601 | "\n" | |
602 | "The object returned by @code{stat} can be passed as a single\n" | |
603 | "parameter to the following procedures, all of which return\n" | |
604 | "integers:\n" | |
605 | "\n" | |
d3818c29 MD |
606 | "@table @code\n" |
607 | "@item stat:dev\n" | |
608 | "The device containing the file.\n" | |
609 | "@item stat:ino\n" | |
1e6808ea MG |
610 | "The file serial number, which distinguishes this file from all\n" |
611 | "other files on the same device.\n" | |
d3818c29 | 612 | "@item stat:mode\n" |
1e6808ea MG |
613 | "The mode of the file. This includes file type information and\n" |
614 | "the file permission bits. See @code{stat:type} and\n" | |
615 | "@code{stat:perms} below.\n" | |
d3818c29 MD |
616 | "@item stat:nlink\n" |
617 | "The number of hard links to the file.\n" | |
618 | "@item stat:uid\n" | |
619 | "The user ID of the file's owner.\n" | |
620 | "@item stat:gid\n" | |
621 | "The group ID of the file.\n" | |
622 | "@item stat:rdev\n" | |
623 | "Device ID; this entry is defined only for character or block\n" | |
624 | "special files.\n" | |
625 | "@item stat:size\n" | |
626 | "The size of a regular file in bytes.\n" | |
627 | "@item stat:atime\n" | |
628 | "The last access time for the file.\n" | |
629 | "@item stat:mtime\n" | |
630 | "The last modification time for the file.\n" | |
631 | "@item stat:ctime\n" | |
632 | "The last modification time for the attributes of the file.\n" | |
633 | "@item stat:blksize\n" | |
1e6808ea MG |
634 | "The optimal block size for reading or writing the file, in\n" |
635 | "bytes.\n" | |
d3818c29 | 636 | "@item stat:blocks\n" |
1e6808ea MG |
637 | "The amount of disk space that the file occupies measured in\n" |
638 | "units of 512 byte blocks.\n" | |
639 | "@end table\n" | |
640 | "\n" | |
d3818c29 | 641 | "In addition, the following procedures return the information\n" |
1e6808ea MG |
642 | "from stat:mode in a more convenient form:\n" |
643 | "\n" | |
d3818c29 MD |
644 | "@table @code\n" |
645 | "@item stat:type\n" | |
646 | "A symbol representing the type of file. Possible values are\n" | |
1e6808ea MG |
647 | "regular, directory, symlink, block-special, char-special, fifo,\n" |
648 | "socket and unknown\n" | |
d3818c29 MD |
649 | "@item stat:perms\n" |
650 | "An integer representing the access permission bits.\n" | |
651 | "@end table") | |
1bbd0b84 | 652 | #define FUNC_NAME s_scm_stat |
0f2d19dd | 653 | { |
6afcd3b2 GH |
654 | int rv; |
655 | int fdes; | |
2b829bbb | 656 | struct stat_or_stat64 stat_temp; |
0f2d19dd | 657 | |
e11e83f3 | 658 | if (scm_is_integer (object)) |
36284627 | 659 | { |
e0c73a1c | 660 | #ifdef __MINGW32__ |
e11e83f3 | 661 | SCM_SYSCALL (rv = fstat_Win32 (scm_to_int (object), &stat_temp)); |
e0c73a1c | 662 | #else |
2b829bbb | 663 | SCM_SYSCALL (rv = fstat_or_fstat64 (scm_to_int (object), &stat_temp)); |
e0c73a1c | 664 | #endif |
36284627 | 665 | } |
7f9994d9 | 666 | else if (scm_is_string (object)) |
36284627 | 667 | { |
7f9994d9 | 668 | char *file = scm_to_locale_string (object); |
e0c73a1c | 669 | #ifdef __MINGW32__ |
7f9994d9 | 670 | char *p; |
e0c73a1c MV |
671 | p = file + strlen (file) - 1; |
672 | while (p > file && (*p == '/' || *p == '\\')) | |
673 | *p-- = '\0'; | |
7f9994d9 | 674 | #endif |
2b829bbb | 675 | SCM_SYSCALL (rv = stat_or_stat64 (file, &stat_temp)); |
e0c73a1c | 676 | free (file); |
36284627 | 677 | } |
1ea47048 | 678 | else |
0f2d19dd | 679 | { |
36284627 DH |
680 | object = SCM_COERCE_OUTPORT (object); |
681 | SCM_VALIDATE_OPFPORT (1, object); | |
682 | fdes = SCM_FPORT_FDES (object); | |
e0c73a1c MV |
683 | #ifdef __MINGW32__ |
684 | SCM_SYSCALL (rv = fstat_Win32 (fdes, &stat_temp)); | |
685 | #else | |
2b829bbb | 686 | SCM_SYSCALL (rv = fstat_or_fstat64 (fdes, &stat_temp)); |
e0c73a1c | 687 | #endif |
6afcd3b2 | 688 | } |
36284627 | 689 | |
6afcd3b2 | 690 | if (rv == -1) |
3d8d56df GH |
691 | { |
692 | int en = errno; | |
693 | ||
5d2d2ffc | 694 | SCM_SYSERROR_MSG ("~A: ~S", |
3572cd6b | 695 | scm_list_2 (scm_strerror (scm_from_int (en)), |
1afff620 | 696 | object), |
e0c08f17 | 697 | en); |
3d8d56df | 698 | } |
02b754d3 | 699 | return scm_stat2scm (&stat_temp); |
0f2d19dd | 700 | } |
1bbd0b84 | 701 | #undef FUNC_NAME |
0f2d19dd | 702 | |
0f2d19dd JB |
703 | \f |
704 | /* {Modifying Directories} | |
705 | */ | |
706 | ||
82893676 | 707 | #ifdef HAVE_LINK |
a1ec6916 | 708 | SCM_DEFINE (scm_link, "link", 2, 0, 0, |
1bbd0b84 | 709 | (SCM oldpath, SCM newpath), |
6d36532c GH |
710 | "Creates a new name @var{newpath} in the file system for the\n" |
711 | "file named by @var{oldpath}. If @var{oldpath} is a symbolic\n" | |
712 | "link, the link may or may not be followed depending on the\n" | |
713 | "system.") | |
1bbd0b84 | 714 | #define FUNC_NAME s_scm_link |
0f2d19dd JB |
715 | { |
716 | int val; | |
02b754d3 | 717 | |
1299a0f1 MV |
718 | STRING2_SYSCALL (oldpath, c_oldpath, |
719 | newpath, c_newpath, | |
720 | val = link (c_oldpath, c_newpath)); | |
02b754d3 | 721 | if (val != 0) |
1bbd0b84 | 722 | SCM_SYSERROR; |
02b754d3 | 723 | return SCM_UNSPECIFIED; |
0f2d19dd | 724 | } |
1bbd0b84 | 725 | #undef FUNC_NAME |
82893676 | 726 | #endif /* HAVE_LINK */ |
0f2d19dd | 727 | |
1299a0f1 MV |
728 | #ifdef HAVE_RENAME |
729 | #define my_rename rename | |
730 | #else | |
731 | static int | |
732 | my_rename (const char *oldname, const char *newname) | |
733 | { | |
734 | int rv; | |
0f2d19dd | 735 | |
1299a0f1 MV |
736 | SCM_SYSCALL (rv = link (oldname, newname)); |
737 | if (rv == 0) | |
738 | { | |
739 | SCM_SYSCALL (rv = unlink (oldname)); | |
740 | if (rv != 0) | |
741 | /* unlink failed. remove new name */ | |
742 | SCM_SYSCALL (unlink (newname)); | |
743 | } | |
744 | return rv; | |
745 | } | |
746 | #endif | |
0f2d19dd | 747 | |
a1ec6916 | 748 | SCM_DEFINE (scm_rename, "rename-file", 2, 0, 0, |
1bbd0b84 | 749 | (SCM oldname, SCM newname), |
6d36532c | 750 | "Renames the file specified by @var{oldname} to @var{newname}.\n" |
d3818c29 | 751 | "The return value is unspecified.") |
1bbd0b84 | 752 | #define FUNC_NAME s_scm_rename |
0f2d19dd JB |
753 | { |
754 | int rv; | |
1299a0f1 MV |
755 | |
756 | STRING2_SYSCALL (oldname, c_oldname, | |
757 | newname, c_newname, | |
758 | rv = my_rename (c_oldname, c_newname)); | |
02b754d3 | 759 | if (rv != 0) |
1bbd0b84 | 760 | SCM_SYSERROR; |
02b754d3 | 761 | return SCM_UNSPECIFIED; |
0f2d19dd | 762 | } |
1bbd0b84 | 763 | #undef FUNC_NAME |
0f2d19dd JB |
764 | |
765 | ||
3b3b36dd | 766 | SCM_DEFINE (scm_delete_file, "delete-file", 1, 0, 0, |
1bbd0b84 | 767 | (SCM str), |
d3818c29 | 768 | "Deletes (or \"unlinks\") the file specified by @var{path}.") |
1bbd0b84 | 769 | #define FUNC_NAME s_scm_delete_file |
2f3ed1ba JB |
770 | { |
771 | int ans; | |
1299a0f1 | 772 | STRING_SYSCALL (str, c_str, ans = unlink (c_str)); |
2f3ed1ba | 773 | if (ans != 0) |
1bbd0b84 | 774 | SCM_SYSERROR; |
2f3ed1ba JB |
775 | return SCM_UNSPECIFIED; |
776 | } | |
1bbd0b84 | 777 | #undef FUNC_NAME |
2f3ed1ba | 778 | |
f25f761d | 779 | #ifdef HAVE_MKDIR |
a1ec6916 | 780 | SCM_DEFINE (scm_mkdir, "mkdir", 1, 1, 0, |
1bbd0b84 | 781 | (SCM path, SCM mode), |
d3818c29 MD |
782 | "Create a new directory named by @var{path}. If @var{mode} is omitted\n" |
783 | "then the permissions of the directory file are set using the current\n" | |
784 | "umask. Otherwise they are set to the decimal value specified with\n" | |
785 | "@var{mode}. The return value is unspecified.") | |
1bbd0b84 | 786 | #define FUNC_NAME s_scm_mkdir |
0f2d19dd | 787 | { |
0f2d19dd JB |
788 | int rv; |
789 | mode_t mask; | |
1299a0f1 | 790 | |
0f2d19dd JB |
791 | if (SCM_UNBNDP (mode)) |
792 | { | |
793 | mask = umask (0); | |
794 | umask (mask); | |
1299a0f1 | 795 | STRING_SYSCALL (path, c_path, rv = mkdir (c_path, 0777 ^ mask)); |
0f2d19dd JB |
796 | } |
797 | else | |
798 | { | |
1299a0f1 | 799 | STRING_SYSCALL (path, c_path, rv = mkdir (c_path, scm_to_uint (mode))); |
0f2d19dd | 800 | } |
02b754d3 | 801 | if (rv != 0) |
1bbd0b84 | 802 | SCM_SYSERROR; |
02b754d3 | 803 | return SCM_UNSPECIFIED; |
0f2d19dd | 804 | } |
1bbd0b84 | 805 | #undef FUNC_NAME |
f25f761d | 806 | #endif /* HAVE_MKDIR */ |
0f2d19dd | 807 | |
f25f761d | 808 | #ifdef HAVE_RMDIR |
a1ec6916 | 809 | SCM_DEFINE (scm_rmdir, "rmdir", 1, 0, 0, |
1bbd0b84 | 810 | (SCM path), |
d3818c29 MD |
811 | "Remove the existing directory named by @var{path}. The directory must\n" |
812 | "be empty for this to succeed. The return value is unspecified.") | |
1bbd0b84 | 813 | #define FUNC_NAME s_scm_rmdir |
0f2d19dd | 814 | { |
0f2d19dd | 815 | int val; |
02b754d3 | 816 | |
1299a0f1 | 817 | STRING_SYSCALL (path, c_path, val = rmdir (c_path)); |
02b754d3 | 818 | if (val != 0) |
1bbd0b84 | 819 | SCM_SYSERROR; |
02b754d3 | 820 | return SCM_UNSPECIFIED; |
0f2d19dd | 821 | } |
1bbd0b84 | 822 | #undef FUNC_NAME |
f25f761d | 823 | #endif |
0f2d19dd JB |
824 | |
825 | \f | |
30ea841d | 826 | |
0f2d19dd JB |
827 | /* {Examining Directories} |
828 | */ | |
829 | ||
92c2555f | 830 | scm_t_bits scm_tc16_dir; |
0f2d19dd | 831 | |
30ea841d | 832 | |
a1ec6916 | 833 | SCM_DEFINE (scm_directory_stream_p, "directory-stream?", 1, 0, 0, |
1bbd0b84 | 834 | (SCM obj), |
1e6808ea MG |
835 | "Return a boolean indicating whether @var{object} is a directory\n" |
836 | "stream as returned by @code{opendir}.") | |
1bbd0b84 | 837 | #define FUNC_NAME s_scm_directory_stream_p |
77242ff9 | 838 | { |
7888309b | 839 | return scm_from_bool (SCM_DIRP (obj)); |
77242ff9 | 840 | } |
1bbd0b84 | 841 | #undef FUNC_NAME |
77242ff9 | 842 | |
30ea841d | 843 | |
a1ec6916 | 844 | SCM_DEFINE (scm_opendir, "opendir", 1, 0, 0, |
1bbd0b84 | 845 | (SCM dirname), |
d3818c29 MD |
846 | "Open the directory specified by @var{path} and return a directory\n" |
847 | "stream.") | |
1bbd0b84 | 848 | #define FUNC_NAME s_scm_opendir |
0f2d19dd JB |
849 | { |
850 | DIR *ds; | |
1299a0f1 | 851 | STRING_SYSCALL (dirname, c_dirname, ds = opendir (c_dirname)); |
02b754d3 | 852 | if (ds == NULL) |
1bbd0b84 | 853 | SCM_SYSERROR; |
30ea841d | 854 | SCM_RETURN_NEWSMOB (scm_tc16_dir | SCM_DIR_FLAG_OPEN, ds); |
0f2d19dd | 855 | } |
1bbd0b84 | 856 | #undef FUNC_NAME |
0f2d19dd JB |
857 | |
858 | ||
9be808d8 KR |
859 | /* FIXME: The glibc manual has a portability note that readdir_r may not |
860 | null-terminate its return string. The circumstances outlined for this | |
2b829bbb KR |
861 | are not clear, nor is it clear what should be done about it. Lets use |
862 | NAMLEN and worry about what else should be done if/when someone can | |
863 | figure it out. */ | |
9be808d8 | 864 | |
a1ec6916 | 865 | SCM_DEFINE (scm_readdir, "readdir", 1, 0, 0, |
1bbd0b84 | 866 | (SCM port), |
d3818c29 MD |
867 | "Return (as a string) the next directory entry from the directory stream\n" |
868 | "@var{stream}. If there is no remaining entry to be read then the\n" | |
869 | "end of file object is returned.") | |
1bbd0b84 | 870 | #define FUNC_NAME s_scm_readdir |
0f2d19dd | 871 | { |
2b829bbb | 872 | struct dirent_or_dirent64 *rdent; |
30ea841d DH |
873 | |
874 | SCM_VALIDATE_DIR (1, port); | |
875 | if (!SCM_DIR_OPEN_P (port)) | |
1afff620 | 876 | SCM_MISC_ERROR ("Directory ~S is not open.", scm_list_1 (port)); |
30ea841d | 877 | |
9be808d8 | 878 | #if HAVE_READDIR_R |
2b829bbb KR |
879 | /* As noted in the glibc manual, on various systems (such as Solaris) the |
880 | d_name[] field is only 1 char and you're expected to size the dirent | |
881 | buffer for readdir_r based on NAME_MAX. The SCM_MAX expressions below | |
882 | effectively give either sizeof(d_name) or NAME_MAX+1, whichever is | |
883 | bigger. | |
884 | ||
885 | On solaris 10 there's no NAME_MAX constant, it's necessary to use | |
886 | pathconf(). We prefer NAME_MAX though, since it should be a constant | |
887 | and will therefore save a system call. We also prefer it since dirfd() | |
888 | is not available everywhere. | |
889 | ||
890 | An alternative to dirfd() would be to open() the directory and then use | |
891 | fdopendir(), if the latter is available. That'd let us hold the fd | |
892 | somewhere in the smob, or just the dirent size calculated once. */ | |
893 | { | |
894 | struct dirent_or_dirent64 de; /* just for sizeof */ | |
895 | DIR *ds = (DIR *) SCM_CELL_WORD_1 (port); | |
896 | size_t namlen; | |
897 | #ifdef NAME_MAX | |
898 | char buf [SCM_MAX (sizeof (de), | |
899 | sizeof (de) - sizeof (de.d_name) + NAME_MAX + 1)]; | |
9be808d8 | 900 | #else |
2b829bbb KR |
901 | char *buf; |
902 | long name_max = fpathconf (dirfd (ds), _PC_NAME_MAX); | |
903 | if (name_max == -1) | |
904 | SCM_SYSERROR; | |
905 | buf = alloca (SCM_MAX (sizeof (de), | |
906 | sizeof (de) - sizeof (de.d_name) + name_max + 1)); | |
9be808d8 | 907 | #endif |
2b829bbb KR |
908 | |
909 | errno = 0; | |
910 | SCM_SYSCALL (readdir_r_or_readdir64_r (ds, (struct dirent_or_dirent64 *) buf, &rdent)); | |
9be808d8 KR |
911 | if (errno != 0) |
912 | SCM_SYSERROR; | |
2b829bbb KR |
913 | if (! rdent) |
914 | return SCM_EOF_VAL; | |
915 | ||
916 | namlen = NAMLEN (rdent); | |
30ea841d | 917 | |
cc95e00a | 918 | return (rdent ? scm_from_locale_stringn (rdent->d_name, NAMLEN (rdent)) |
9be808d8 KR |
919 | : SCM_EOF_VAL); |
920 | } | |
2b829bbb KR |
921 | #else |
922 | { | |
923 | SCM ret; | |
924 | scm_dynwind_begin (0); | |
925 | scm_i_dynwind_pthread_mutex_lock (&scm_i_misc_mutex); | |
926 | ||
927 | errno = 0; | |
928 | SCM_SYSCALL (rdent = readdir_or_readdir64 ((DIR *) SCM_CELL_WORD_1 (port))); | |
929 | if (errno != 0) | |
930 | SCM_SYSERROR; | |
931 | ||
932 | ret = (rdent ? scm_from_locale_stringn (rdent->d_name, NAMLEN (rdent)) | |
933 | : SCM_EOF_VAL); | |
934 | ||
935 | scm_dynwind_end (); | |
936 | return ret; | |
937 | } | |
938 | #endif | |
0f2d19dd | 939 | } |
1bbd0b84 | 940 | #undef FUNC_NAME |
0f2d19dd JB |
941 | |
942 | ||
a1ec6916 | 943 | SCM_DEFINE (scm_rewinddir, "rewinddir", 1, 0, 0, |
1bbd0b84 | 944 | (SCM port), |
d3818c29 MD |
945 | "Reset the directory port @var{stream} so that the next call to\n" |
946 | "@code{readdir} will return the first directory entry.") | |
1bbd0b84 | 947 | #define FUNC_NAME s_scm_rewinddir |
0f2d19dd | 948 | { |
30ea841d DH |
949 | SCM_VALIDATE_DIR (1, port); |
950 | if (!SCM_DIR_OPEN_P (port)) | |
1afff620 | 951 | SCM_MISC_ERROR ("Directory ~S is not open.", scm_list_1 (port)); |
30ea841d | 952 | |
4260a7fc | 953 | rewinddir ((DIR *) SCM_CELL_WORD_1 (port)); |
30ea841d | 954 | |
0f2d19dd JB |
955 | return SCM_UNSPECIFIED; |
956 | } | |
1bbd0b84 | 957 | #undef FUNC_NAME |
0f2d19dd JB |
958 | |
959 | ||
a1ec6916 | 960 | SCM_DEFINE (scm_closedir, "closedir", 1, 0, 0, |
1bbd0b84 | 961 | (SCM port), |
d3818c29 MD |
962 | "Close the directory stream @var{stream}.\n" |
963 | "The return value is unspecified.") | |
1bbd0b84 | 964 | #define FUNC_NAME s_scm_closedir |
0f2d19dd | 965 | { |
30ea841d | 966 | SCM_VALIDATE_DIR (1, port); |
02b754d3 | 967 | |
30ea841d | 968 | if (SCM_DIR_OPEN_P (port)) |
0f2d19dd | 969 | { |
30ea841d DH |
970 | int sts; |
971 | ||
972 | SCM_SYSCALL (sts = closedir ((DIR *) SCM_CELL_WORD_1 (port))); | |
973 | if (sts != 0) | |
974 | SCM_SYSERROR; | |
975 | ||
976 | SCM_SET_CELL_WORD_0 (port, scm_tc16_dir); | |
0f2d19dd | 977 | } |
30ea841d | 978 | |
02b754d3 | 979 | return SCM_UNSPECIFIED; |
0f2d19dd | 980 | } |
1bbd0b84 | 981 | #undef FUNC_NAME |
0f2d19dd JB |
982 | |
983 | ||
0f2d19dd | 984 | static int |
e81d98ec | 985 | scm_dir_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED) |
0f2d19dd | 986 | { |
f8b16091 | 987 | scm_puts ("#<", port); |
30ea841d | 988 | if (!SCM_DIR_OPEN_P (exp)) |
f8b16091 | 989 | scm_puts ("closed: ", port); |
0d03da62 | 990 | scm_puts ("directory stream ", port); |
0345e278 | 991 | scm_uintprint (SCM_CELL_WORD_1 (exp), 16, port); |
f8b16091 | 992 | scm_putc ('>', port); |
0f2d19dd JB |
993 | return 1; |
994 | } | |
995 | ||
1cc91f1b | 996 | |
1be6b49c | 997 | static size_t |
1bbd0b84 | 998 | scm_dir_free (SCM p) |
0f2d19dd | 999 | { |
30ea841d | 1000 | if (SCM_DIR_OPEN_P (p)) |
4260a7fc | 1001 | closedir ((DIR *) SCM_CELL_WORD_1 (p)); |
0f2d19dd JB |
1002 | return 0; |
1003 | } | |
1004 | ||
0f2d19dd JB |
1005 | \f |
1006 | /* {Navigating Directories} | |
1007 | */ | |
1008 | ||
1009 | ||
a1ec6916 | 1010 | SCM_DEFINE (scm_chdir, "chdir", 1, 0, 0, |
1bbd0b84 | 1011 | (SCM str), |
d3818c29 MD |
1012 | "Change the current working directory to @var{path}.\n" |
1013 | "The return value is unspecified.") | |
1bbd0b84 | 1014 | #define FUNC_NAME s_scm_chdir |
0f2d19dd JB |
1015 | { |
1016 | int ans; | |
02b754d3 | 1017 | |
1299a0f1 | 1018 | STRING_SYSCALL (str, c_str, ans = chdir (c_str)); |
02b754d3 | 1019 | if (ans != 0) |
1bbd0b84 | 1020 | SCM_SYSERROR; |
02b754d3 | 1021 | return SCM_UNSPECIFIED; |
0f2d19dd | 1022 | } |
1bbd0b84 | 1023 | #undef FUNC_NAME |
0f2d19dd | 1024 | |
f25f761d | 1025 | #ifdef HAVE_GETCWD |
a1ec6916 | 1026 | SCM_DEFINE (scm_getcwd, "getcwd", 0, 0, 0, |
1bbd0b84 | 1027 | (), |
1e6808ea | 1028 | "Return the name of the current working directory.") |
1bbd0b84 | 1029 | #define FUNC_NAME s_scm_getcwd |
0f2d19dd | 1030 | { |
0f2d19dd | 1031 | char *rv; |
1be6b49c | 1032 | size_t size = 100; |
0f2d19dd JB |
1033 | char *wd; |
1034 | SCM result; | |
1035 | ||
4c9419ac | 1036 | wd = scm_malloc (size); |
0f2d19dd JB |
1037 | while ((rv = getcwd (wd, size)) == 0 && errno == ERANGE) |
1038 | { | |
4c9419ac | 1039 | free (wd); |
0f2d19dd | 1040 | size *= 2; |
4c9419ac | 1041 | wd = scm_malloc (size); |
0f2d19dd | 1042 | } |
02b754d3 | 1043 | if (rv == 0) |
11e1db06 KR |
1044 | { |
1045 | int save_errno = errno; | |
1046 | free (wd); | |
1047 | errno = save_errno; | |
1048 | SCM_SYSERROR; | |
1049 | } | |
cc95e00a | 1050 | result = scm_from_locale_stringn (wd, strlen (wd)); |
4c9419ac | 1051 | free (wd); |
0f2d19dd | 1052 | return result; |
0f2d19dd | 1053 | } |
1bbd0b84 | 1054 | #undef FUNC_NAME |
f25f761d | 1055 | #endif /* HAVE_GETCWD */ |
0f2d19dd JB |
1056 | |
1057 | \f | |
1058 | ||
28d77376 GH |
1059 | #ifdef HAVE_SELECT |
1060 | ||
1061 | /* check that element is a port or file descriptor. if it's a port | |
1062 | and its buffer is ready for use, add it to the ports_ready list. | |
1063 | otherwise add its file descriptor to *set. the type of list can be | |
1064 | determined from pos: SCM_ARG1 for reads, SCM_ARG2 for writes, | |
1065 | SCM_ARG3 for excepts. */ | |
cafc12ff | 1066 | static int |
28d77376 | 1067 | set_element (SELECT_TYPE *set, SCM *ports_ready, SCM element, int pos) |
a48a89bc | 1068 | { |
cafc12ff | 1069 | int fd; |
d831b039 | 1070 | |
e11e83f3 | 1071 | if (scm_is_integer (element)) |
28d77376 | 1072 | { |
e11e83f3 | 1073 | fd = scm_to_int (element); |
28d77376 GH |
1074 | } |
1075 | else | |
1076 | { | |
1077 | int use_buf = 0; | |
1078 | ||
1079 | element = SCM_COERCE_OUTPORT (element); | |
1080 | SCM_ASSERT (SCM_OPFPORTP (element), element, pos, "select"); | |
1081 | if (pos == SCM_ARG1) | |
1082 | { | |
1083 | /* check whether port has buffered input. */ | |
92c2555f | 1084 | scm_t_port *pt = SCM_PTAB_ENTRY (element); |
28d77376 GH |
1085 | |
1086 | if (pt->read_pos < pt->read_end) | |
1087 | use_buf = 1; | |
1088 | } | |
1089 | else if (pos == SCM_ARG2) | |
1090 | { | |
1091 | /* check whether port's output buffer has room. */ | |
92c2555f | 1092 | scm_t_port *pt = SCM_PTAB_ENTRY (element); |
28d77376 GH |
1093 | |
1094 | /* > 1 since writing the last byte in the buffer causes flush. */ | |
1095 | if (pt->write_end - pt->write_pos > 1) | |
1096 | use_buf = 1; | |
1097 | } | |
1098 | fd = use_buf ? -1 : SCM_FPORT_FDES (element); | |
1099 | } | |
1100 | if (fd == -1) | |
1101 | *ports_ready = scm_cons (element, *ports_ready); | |
1102 | else | |
1103 | FD_SET (fd, set); | |
cafc12ff | 1104 | return fd; |
a48a89bc | 1105 | } |
1cc91f1b | 1106 | |
28d77376 GH |
1107 | /* check list_or_vec, a list or vector of ports or file descriptors, |
1108 | adding each member to either the ports_ready list (if it's a port | |
1109 | with a usable buffer) or to *set. the kind of list_or_vec can be | |
1110 | determined from pos: SCM_ARG1 for reads, SCM_ARG2 for writes, | |
1111 | SCM_ARG3 for excepts. */ | |
cafc12ff | 1112 | static int |
28d77376 | 1113 | fill_select_type (SELECT_TYPE *set, SCM *ports_ready, SCM list_or_vec, int pos) |
0f2d19dd | 1114 | { |
28d77376 GH |
1115 | int max_fd = 0; |
1116 | ||
4057a3e0 | 1117 | if (scm_is_simple_vector (list_or_vec)) |
0f2d19dd | 1118 | { |
4057a3e0 | 1119 | int i = SCM_SIMPLE_VECTOR_LENGTH (list_or_vec); |
a48a89bc | 1120 | |
28d77376 | 1121 | while (--i >= 0) |
a48a89bc | 1122 | { |
4057a3e0 MV |
1123 | int fd = set_element (set, ports_ready, |
1124 | SCM_SIMPLE_VECTOR_REF (list_or_vec, i), pos); | |
28d77376 | 1125 | |
cafc12ff MD |
1126 | if (fd > max_fd) |
1127 | max_fd = fd; | |
a48a89bc GH |
1128 | } |
1129 | } | |
1130 | else | |
1131 | { | |
c96d76b8 | 1132 | while (!SCM_NULL_OR_NIL_P (list_or_vec)) |
a48a89bc | 1133 | { |
28d77376 GH |
1134 | int fd = set_element (set, ports_ready, SCM_CAR (list_or_vec), pos); |
1135 | ||
cafc12ff MD |
1136 | if (fd > max_fd) |
1137 | max_fd = fd; | |
28d77376 | 1138 | list_or_vec = SCM_CDR (list_or_vec); |
a48a89bc | 1139 | } |
0f2d19dd | 1140 | } |
cafc12ff MD |
1141 | |
1142 | return max_fd; | |
0f2d19dd JB |
1143 | } |
1144 | ||
28d77376 GH |
1145 | /* if element (a file descriptor or port) appears in *set, cons it to |
1146 | list. return list. */ | |
a48a89bc GH |
1147 | static SCM |
1148 | get_element (SELECT_TYPE *set, SCM element, SCM list) | |
1149 | { | |
28d77376 GH |
1150 | int fd; |
1151 | ||
e11e83f3 | 1152 | if (scm_is_integer (element)) |
a48a89bc | 1153 | { |
e11e83f3 | 1154 | fd = scm_to_int (element); |
a48a89bc | 1155 | } |
28d77376 | 1156 | else |
a48a89bc | 1157 | { |
28d77376 | 1158 | fd = SCM_FPORT_FDES (SCM_COERCE_OUTPORT (element)); |
a48a89bc | 1159 | } |
28d77376 GH |
1160 | if (FD_ISSET (fd, set)) |
1161 | list = scm_cons (element, list); | |
a48a89bc GH |
1162 | return list; |
1163 | } | |
1cc91f1b | 1164 | |
28d77376 GH |
1165 | /* construct component of scm_select return value. |
1166 | set: pointer to set of file descriptors found by select to be ready | |
1167 | ports_ready: ports ready due to buffering | |
1168 | list_or_vec: original list/vector handed to scm_select. | |
1169 | the return value is a list/vector of ready ports/file descriptors. | |
1170 | works by finding the objects in list which correspond to members of | |
1171 | *set and appending them to ports_ready. result is converted to a | |
1172 | vector if list_or_vec is a vector. */ | |
0f2d19dd | 1173 | static SCM |
28d77376 | 1174 | retrieve_select_type (SELECT_TYPE *set, SCM ports_ready, SCM list_or_vec) |
0f2d19dd | 1175 | { |
28d77376 | 1176 | SCM answer_list = ports_ready; |
a48a89bc | 1177 | |
4057a3e0 | 1178 | if (scm_is_simple_vector (list_or_vec)) |
0f2d19dd | 1179 | { |
4057a3e0 | 1180 | int i = SCM_SIMPLE_VECTOR_LENGTH (list_or_vec); |
a48a89bc | 1181 | |
28d77376 | 1182 | while (--i >= 0) |
0f2d19dd | 1183 | { |
4057a3e0 MV |
1184 | answer_list = get_element (set, |
1185 | SCM_SIMPLE_VECTOR_REF (list_or_vec, i), | |
1186 | answer_list); | |
0f2d19dd | 1187 | } |
a48a89bc GH |
1188 | return scm_vector (answer_list); |
1189 | } | |
1190 | else | |
1191 | { | |
28d77376 | 1192 | /* list_or_vec must be a list. */ |
c96d76b8 | 1193 | while (!SCM_NULL_OR_NIL_P (list_or_vec)) |
0f2d19dd | 1194 | { |
28d77376 GH |
1195 | answer_list = get_element (set, SCM_CAR (list_or_vec), answer_list); |
1196 | list_or_vec = SCM_CDR (list_or_vec); | |
0f2d19dd | 1197 | } |
a48a89bc | 1198 | return answer_list; |
0f2d19dd | 1199 | } |
0f2d19dd JB |
1200 | } |
1201 | ||
1bbd0b84 | 1202 | /* Static helper functions above refer to s_scm_select directly as s_select */ |
a1ec6916 | 1203 | SCM_DEFINE (scm_select, "select", 3, 2, 0, |
1bbd0b84 | 1204 | (SCM reads, SCM writes, SCM excepts, SCM secs, SCM usecs), |
28d77376 | 1205 | "This procedure has a variety of uses: waiting for the ability\n" |
bb2c02f2 | 1206 | "to provide input, accept output, or the existence of\n" |
28d77376 GH |
1207 | "exceptional conditions on a collection of ports or file\n" |
1208 | "descriptors, or waiting for a timeout to occur.\n" | |
1209 | "It also returns if interrupted by a signal.\n\n" | |
1210 | "@var{reads}, @var{writes} and @var{excepts} can be lists or\n" | |
1211 | "vectors, with each member a port or a file descriptor.\n" | |
1212 | "The value returned is a list of three corresponding\n" | |
1213 | "lists or vectors containing only the members which meet the\n" | |
1214 | "specified requirement. The ability of port buffers to\n" | |
1215 | "provide input or accept output is taken into account.\n" | |
1216 | "Ordering of the input lists or vectors is not preserved.\n\n" | |
1217 | "The optional arguments @var{secs} and @var{usecs} specify the\n" | |
1218 | "timeout. Either @var{secs} can be specified alone, as\n" | |
1219 | "either an integer or a real number, or both @var{secs} and\n" | |
1220 | "@var{usecs} can be specified as integers, in which case\n" | |
1221 | "@var{usecs} is an additional timeout expressed in\n" | |
1222 | "microseconds. If @var{secs} is omitted or is @code{#f} then\n" | |
1223 | "select will wait for as long as it takes for one of the other\n" | |
1224 | "conditions to be satisfied.\n\n" | |
1225 | "The scsh version of @code{select} differs as follows:\n" | |
1226 | "Only vectors are accepted for the first three arguments.\n" | |
1227 | "The @var{usecs} argument is not supported.\n" | |
1228 | "Multiple values are returned instead of a list.\n" | |
1229 | "Duplicates in the input vectors appear only once in output.\n" | |
9401323e | 1230 | "An additional @code{select!} interface is provided.") |
1bbd0b84 | 1231 | #define FUNC_NAME s_scm_select |
0f2d19dd | 1232 | { |
0f2d19dd | 1233 | struct timeval timeout; |
28d77376 | 1234 | struct timeval * time_ptr; |
0f2d19dd JB |
1235 | SELECT_TYPE read_set; |
1236 | SELECT_TYPE write_set; | |
1237 | SELECT_TYPE except_set; | |
28d77376 GH |
1238 | int read_count; |
1239 | int write_count; | |
1240 | int except_count; | |
1241 | /* these lists accumulate ports which are ready due to buffering. | |
1242 | their file descriptors don't need to be added to the select sets. */ | |
1243 | SCM read_ports_ready = SCM_EOL; | |
1244 | SCM write_ports_ready = SCM_EOL; | |
1245 | int max_fd; | |
1246 | ||
4057a3e0 | 1247 | if (scm_is_simple_vector (reads)) |
28d77376 | 1248 | { |
4057a3e0 | 1249 | read_count = SCM_SIMPLE_VECTOR_LENGTH (reads); |
28d77376 GH |
1250 | } |
1251 | else | |
1252 | { | |
1253 | read_count = scm_ilength (reads); | |
1254 | SCM_ASSERT (read_count >= 0, reads, SCM_ARG1, FUNC_NAME); | |
1255 | } | |
4057a3e0 | 1256 | if (scm_is_simple_vector (writes)) |
28d77376 | 1257 | { |
4057a3e0 | 1258 | write_count = SCM_SIMPLE_VECTOR_LENGTH (writes); |
28d77376 GH |
1259 | } |
1260 | else | |
1261 | { | |
1262 | write_count = scm_ilength (writes); | |
1263 | SCM_ASSERT (write_count >= 0, writes, SCM_ARG2, FUNC_NAME); | |
1264 | } | |
4057a3e0 | 1265 | if (scm_is_simple_vector (excepts)) |
28d77376 | 1266 | { |
4057a3e0 | 1267 | except_count = SCM_SIMPLE_VECTOR_LENGTH (excepts); |
28d77376 GH |
1268 | } |
1269 | else | |
1270 | { | |
1271 | except_count = scm_ilength (excepts); | |
1272 | SCM_ASSERT (except_count >= 0, excepts, SCM_ARG3, FUNC_NAME); | |
1273 | } | |
0f2d19dd JB |
1274 | |
1275 | FD_ZERO (&read_set); | |
1276 | FD_ZERO (&write_set); | |
1277 | FD_ZERO (&except_set); | |
1278 | ||
28d77376 GH |
1279 | max_fd = fill_select_type (&read_set, &read_ports_ready, reads, SCM_ARG1); |
1280 | ||
1281 | { | |
1282 | int write_max = fill_select_type (&write_set, &write_ports_ready, | |
1283 | writes, SCM_ARG2); | |
1284 | int except_max = fill_select_type (&except_set, NULL, | |
1285 | excepts, SCM_ARG3); | |
1286 | ||
1287 | if (write_max > max_fd) | |
1288 | max_fd = write_max; | |
1289 | if (except_max > max_fd) | |
1290 | max_fd = except_max; | |
1291 | } | |
0f2d19dd | 1292 | |
ae1b098b GH |
1293 | /* if there's a port with a ready buffer, don't block, just |
1294 | check for ready file descriptors. */ | |
d2e53ed6 | 1295 | if (!scm_is_null (read_ports_ready) || !scm_is_null (write_ports_ready)) |
ae1b098b GH |
1296 | { |
1297 | timeout.tv_sec = 0; | |
1298 | timeout.tv_usec = 0; | |
1299 | time_ptr = &timeout; | |
1300 | } | |
7888309b | 1301 | else if (SCM_UNBNDP (secs) || scm_is_false (secs)) |
28d77376 | 1302 | time_ptr = 0; |
0f2d19dd JB |
1303 | else |
1304 | { | |
a55c2b68 | 1305 | if (scm_is_unsigned_integer (secs, 0, ULONG_MAX)) |
a48a89bc | 1306 | { |
a55c2b68 | 1307 | timeout.tv_sec = scm_to_ulong (secs); |
a48a89bc GH |
1308 | if (SCM_UNBNDP (usecs)) |
1309 | timeout.tv_usec = 0; | |
1310 | else | |
a55c2b68 | 1311 | timeout.tv_usec = scm_to_long (usecs); |
a48a89bc | 1312 | } |
0f2d19dd | 1313 | else |
a48a89bc | 1314 | { |
d9a67fc4 | 1315 | double fl = scm_to_double (secs); |
a48a89bc GH |
1316 | |
1317 | if (!SCM_UNBNDP (usecs)) | |
c1bfcf60 | 1318 | SCM_WRONG_TYPE_ARG (4, secs); |
a48a89bc | 1319 | if (fl > LONG_MAX) |
c1bfcf60 | 1320 | SCM_OUT_OF_RANGE (4, secs); |
a48a89bc GH |
1321 | timeout.tv_sec = (long) fl; |
1322 | timeout.tv_usec = (long) ((fl - timeout.tv_sec) * 1000000); | |
1323 | } | |
28d77376 | 1324 | time_ptr = &timeout; |
0f2d19dd JB |
1325 | } |
1326 | ||
28d77376 | 1327 | { |
9de87eea MV |
1328 | int rv = scm_std_select (max_fd + 1, |
1329 | &read_set, &write_set, &except_set, | |
1330 | time_ptr); | |
28d77376 GH |
1331 | if (rv < 0) |
1332 | SCM_SYSERROR; | |
1333 | } | |
1afff620 KN |
1334 | return scm_list_3 (retrieve_select_type (&read_set, read_ports_ready, reads), |
1335 | retrieve_select_type (&write_set, write_ports_ready, writes), | |
1336 | retrieve_select_type (&except_set, SCM_EOL, excepts)); | |
0f2d19dd | 1337 | } |
1bbd0b84 | 1338 | #undef FUNC_NAME |
f25f761d | 1339 | #endif /* HAVE_SELECT */ |
0f2d19dd JB |
1340 | |
1341 | \f | |
4c1feaa5 | 1342 | |
82893676 | 1343 | #ifdef HAVE_FCNTL |
af45e3b0 | 1344 | SCM_DEFINE (scm_fcntl, "fcntl", 2, 1, 0, |
1bbd0b84 | 1345 | (SCM object, SCM cmd, SCM value), |
d3818c29 MD |
1346 | "Apply @var{command} to the specified file descriptor or the underlying\n" |
1347 | "file descriptor of the specified port. @var{value} is an optional\n" | |
1348 | "integer argument.\n\n" | |
1349 | "Values for @var{command} are:\n\n" | |
1350 | "@table @code\n" | |
1351 | "@item F_DUPFD\n" | |
1352 | "Duplicate a file descriptor\n" | |
1353 | "@item F_GETFD\n" | |
1354 | "Get flags associated with the file descriptor.\n" | |
1355 | "@item F_SETFD\n" | |
1356 | "Set flags associated with the file descriptor to @var{value}.\n" | |
1357 | "@item F_GETFL\n" | |
1358 | "Get flags associated with the open file.\n" | |
1359 | "@item F_SETFL\n" | |
1360 | "Set flags associated with the open file to @var{value}\n" | |
1361 | "@item F_GETOWN\n" | |
1362 | "Get the process ID of a socket's owner, for @code{SIGIO} signals.\n" | |
1363 | "@item F_SETOWN\n" | |
1364 | "Set the process that owns a socket to @var{value}, for @code{SIGIO} signals.\n" | |
1365 | "@item FD_CLOEXEC\n" | |
55892d87 NJ |
1366 | "The value used to indicate the \"close on exec\" flag with @code{F_GETFL} or\n" |
1367 | "@code{F_SETFL}.\n" | |
a3c8b9fc | 1368 | "@end table") |
1bbd0b84 | 1369 | #define FUNC_NAME s_scm_fcntl |
4c1feaa5 JB |
1370 | { |
1371 | int rv; | |
6afcd3b2 GH |
1372 | int fdes; |
1373 | int ivalue; | |
4c1feaa5 | 1374 | |
78446828 MV |
1375 | object = SCM_COERCE_OUTPORT (object); |
1376 | ||
0c95b57d | 1377 | if (SCM_OPFPORTP (object)) |
77a76b64 | 1378 | fdes = SCM_FPORT_FDES (object); |
6afcd3b2 | 1379 | else |
a55c2b68 | 1380 | fdes = scm_to_int (object); |
af45e3b0 | 1381 | |
a55c2b68 | 1382 | if (SCM_UNBNDP (value)) |
6afcd3b2 | 1383 | ivalue = 0; |
a55c2b68 MV |
1384 | else |
1385 | ivalue = scm_to_int (value); | |
af45e3b0 | 1386 | |
a55c2b68 | 1387 | SCM_SYSCALL (rv = fcntl (fdes, scm_to_int (cmd), ivalue)); |
77a76b64 | 1388 | if (rv == -1) |
1bbd0b84 | 1389 | SCM_SYSERROR; |
a55c2b68 | 1390 | return scm_from_int (rv); |
4c1feaa5 | 1391 | } |
1bbd0b84 | 1392 | #undef FUNC_NAME |
82893676 | 1393 | #endif /* HAVE_FCNTL */ |
6afcd3b2 | 1394 | |
a1ec6916 | 1395 | SCM_DEFINE (scm_fsync, "fsync", 1, 0, 0, |
1bbd0b84 | 1396 | (SCM object), |
d3818c29 MD |
1397 | "Copies any unwritten data for the specified output file descriptor to disk.\n" |
1398 | "If @var{port/fd} is a port, its buffer is flushed before the underlying\n" | |
1399 | "file descriptor is fsync'd.\n" | |
1400 | "The return value is unspecified.") | |
1bbd0b84 | 1401 | #define FUNC_NAME s_scm_fsync |
6afcd3b2 GH |
1402 | { |
1403 | int fdes; | |
1404 | ||
78446828 MV |
1405 | object = SCM_COERCE_OUTPORT (object); |
1406 | ||
0c95b57d | 1407 | if (SCM_OPFPORTP (object)) |
6afcd3b2 | 1408 | { |
affc96b5 | 1409 | scm_flush (object); |
77a76b64 | 1410 | fdes = SCM_FPORT_FDES (object); |
6afcd3b2 GH |
1411 | } |
1412 | else | |
a55c2b68 MV |
1413 | fdes = scm_to_int (object); |
1414 | ||
6afcd3b2 | 1415 | if (fsync (fdes) == -1) |
1bbd0b84 | 1416 | SCM_SYSERROR; |
6afcd3b2 GH |
1417 | return SCM_UNSPECIFIED; |
1418 | } | |
1bbd0b84 | 1419 | #undef FUNC_NAME |
0f2d19dd | 1420 | |
f25f761d | 1421 | #ifdef HAVE_SYMLINK |
a1ec6916 | 1422 | SCM_DEFINE (scm_symlink, "symlink", 2, 0, 0, |
1bbd0b84 | 1423 | (SCM oldpath, SCM newpath), |
d3818c29 MD |
1424 | "Create a symbolic link named @var{path-to} with the value (i.e., pointing to)\n" |
1425 | "@var{path-from}. The return value is unspecified.") | |
1bbd0b84 | 1426 | #define FUNC_NAME s_scm_symlink |
0f2d19dd | 1427 | { |
0f2d19dd | 1428 | int val; |
02b754d3 | 1429 | |
1299a0f1 MV |
1430 | STRING2_SYSCALL (oldpath, c_oldpath, |
1431 | newpath, c_newpath, | |
1432 | val = symlink (c_oldpath, c_newpath)); | |
02b754d3 | 1433 | if (val != 0) |
1bbd0b84 | 1434 | SCM_SYSERROR; |
02b754d3 | 1435 | return SCM_UNSPECIFIED; |
0f2d19dd | 1436 | } |
1bbd0b84 | 1437 | #undef FUNC_NAME |
f25f761d | 1438 | #endif /* HAVE_SYMLINK */ |
0f2d19dd | 1439 | |
f25f761d | 1440 | #ifdef HAVE_READLINK |
a1ec6916 | 1441 | SCM_DEFINE (scm_readlink, "readlink", 1, 0, 0, |
1bbd0b84 | 1442 | (SCM path), |
1e6808ea MG |
1443 | "Return the value of the symbolic link named by @var{path} (a\n" |
1444 | "string), i.e., the file that the link points to.") | |
1bbd0b84 | 1445 | #define FUNC_NAME s_scm_readlink |
0f2d19dd | 1446 | { |
6a738a25 JB |
1447 | int rv; |
1448 | int size = 100; | |
0f2d19dd JB |
1449 | char *buf; |
1450 | SCM result; | |
1299a0f1 MV |
1451 | char *c_path; |
1452 | ||
661ae7ab | 1453 | scm_dynwind_begin (0); |
1299a0f1 MV |
1454 | |
1455 | c_path = scm_to_locale_string (path); | |
661ae7ab | 1456 | scm_dynwind_free (c_path); |
1299a0f1 | 1457 | |
4c9419ac | 1458 | buf = scm_malloc (size); |
1299a0f1 MV |
1459 | |
1460 | while ((rv = readlink (c_path, buf, size)) == size) | |
0f2d19dd | 1461 | { |
4c9419ac | 1462 | free (buf); |
0f2d19dd | 1463 | size *= 2; |
4c9419ac | 1464 | buf = scm_malloc (size); |
0f2d19dd | 1465 | } |
02b754d3 | 1466 | if (rv == -1) |
11e1db06 KR |
1467 | { |
1468 | int save_errno = errno; | |
1469 | free (buf); | |
1470 | errno = save_errno; | |
1471 | SCM_SYSERROR; | |
1472 | } | |
1299a0f1 MV |
1473 | result = scm_take_locale_stringn (buf, rv); |
1474 | ||
661ae7ab | 1475 | scm_dynwind_end (); |
0f2d19dd | 1476 | return result; |
0f2d19dd | 1477 | } |
1bbd0b84 | 1478 | #undef FUNC_NAME |
f25f761d | 1479 | #endif /* HAVE_READLINK */ |
0f2d19dd | 1480 | |
f25f761d | 1481 | #ifdef HAVE_LSTAT |
a1ec6916 | 1482 | SCM_DEFINE (scm_lstat, "lstat", 1, 0, 0, |
1bbd0b84 | 1483 | (SCM str), |
d3818c29 | 1484 | "Similar to @code{stat}, but does not follow symbolic links, i.e.,\n" |
9401323e | 1485 | "it will return information about a symbolic link itself, not the\n" |
d3818c29 | 1486 | "file it points to. @var{path} must be a string.") |
1bbd0b84 | 1487 | #define FUNC_NAME s_scm_lstat |
0f2d19dd | 1488 | { |
02b754d3 | 1489 | int rv; |
2b829bbb | 1490 | struct stat_or_stat64 stat_temp; |
02b754d3 | 1491 | |
2b829bbb | 1492 | STRING_SYSCALL (str, c_str, rv = lstat_or_lstat64 (c_str, &stat_temp)); |
02b754d3 | 1493 | if (rv != 0) |
3d8d56df GH |
1494 | { |
1495 | int en = errno; | |
1496 | ||
5d2d2ffc | 1497 | SCM_SYSERROR_MSG ("~A: ~S", |
3572cd6b | 1498 | scm_list_2 (scm_strerror (scm_from_int (en)), str), |
e0c08f17 | 1499 | en); |
3d8d56df | 1500 | } |
1299a0f1 | 1501 | return scm_stat2scm (&stat_temp); |
0f2d19dd | 1502 | } |
1bbd0b84 | 1503 | #undef FUNC_NAME |
f25f761d | 1504 | #endif /* HAVE_LSTAT */ |
0f2d19dd | 1505 | |
a1ec6916 | 1506 | SCM_DEFINE (scm_copy_file, "copy-file", 2, 0, 0, |
1bbd0b84 | 1507 | (SCM oldfile, SCM newfile), |
d3818c29 MD |
1508 | "Copy the file specified by @var{path-from} to @var{path-to}.\n" |
1509 | "The return value is unspecified.") | |
1bbd0b84 | 1510 | #define FUNC_NAME s_scm_copy_file |
0f2d19dd | 1511 | { |
1299a0f1 | 1512 | char *c_oldfile, *c_newfile; |
0f2d19dd | 1513 | int oldfd, newfd; |
37eb673b | 1514 | int n, rv; |
77a76b64 | 1515 | char buf[BUFSIZ]; |
2b829bbb | 1516 | struct stat_or_stat64 oldstat; |
0f2d19dd | 1517 | |
661ae7ab | 1518 | scm_dynwind_begin (0); |
1299a0f1 MV |
1519 | |
1520 | c_oldfile = scm_to_locale_string (oldfile); | |
661ae7ab | 1521 | scm_dynwind_free (c_oldfile); |
1299a0f1 | 1522 | c_newfile = scm_to_locale_string (newfile); |
661ae7ab | 1523 | scm_dynwind_free (c_newfile); |
37eb673b | 1524 | |
2b829bbb | 1525 | oldfd = open_or_open64 (c_oldfile, O_RDONLY); |
0f2d19dd | 1526 | if (oldfd == -1) |
1bbd0b84 | 1527 | SCM_SYSERROR; |
02b754d3 | 1528 | |
37eb673b KR |
1529 | #ifdef __MINGW32__ |
1530 | SCM_SYSCALL (rv = fstat_Win32 (oldfd, &oldstat)); | |
1531 | #else | |
2b829bbb | 1532 | SCM_SYSCALL (rv = fstat_or_fstat64 (oldfd, &oldstat)); |
37eb673b KR |
1533 | #endif |
1534 | if (rv == -1) | |
1535 | goto err_close_oldfd; | |
1536 | ||
02b754d3 | 1537 | /* use POSIX flags instead of 07777?. */ |
2b829bbb KR |
1538 | newfd = open_or_open64 (c_newfile, O_WRONLY | O_CREAT | O_TRUNC, |
1539 | oldstat.st_mode & 07777); | |
0f2d19dd | 1540 | if (newfd == -1) |
01046395 | 1541 | { |
37eb673b | 1542 | err_close_oldfd: |
01046395 KR |
1543 | close (oldfd); |
1544 | SCM_SYSERROR; | |
1545 | } | |
02b754d3 | 1546 | |
0f2d19dd JB |
1547 | while ((n = read (oldfd, buf, sizeof buf)) > 0) |
1548 | if (write (newfd, buf, n) != n) | |
1549 | { | |
1550 | close (oldfd); | |
1551 | close (newfd); | |
1bbd0b84 | 1552 | SCM_SYSERROR; |
0f2d19dd JB |
1553 | } |
1554 | close (oldfd); | |
1555 | if (close (newfd) == -1) | |
1bbd0b84 | 1556 | SCM_SYSERROR; |
1299a0f1 | 1557 | |
661ae7ab | 1558 | scm_dynwind_end (); |
02b754d3 | 1559 | return SCM_UNSPECIFIED; |
0f2d19dd | 1560 | } |
1bbd0b84 | 1561 | #undef FUNC_NAME |
0f2d19dd JB |
1562 | |
1563 | \f | |
6a738a25 JB |
1564 | /* Filename manipulation */ |
1565 | ||
1566 | SCM scm_dot_string; | |
1567 | ||
a1ec6916 | 1568 | SCM_DEFINE (scm_dirname, "dirname", 1, 0, 0, |
1bbd0b84 | 1569 | (SCM filename), |
fa6a543f MG |
1570 | "Return the directory name component of the file name\n" |
1571 | "@var{filename}. If @var{filename} does not contain a directory\n" | |
1572 | "component, @code{.} is returned.") | |
1bbd0b84 | 1573 | #define FUNC_NAME s_scm_dirname |
6a738a25 | 1574 | { |
cc95e00a | 1575 | const char *s; |
9fd38a3d DH |
1576 | long int i; |
1577 | unsigned long int len; | |
1578 | ||
34d19ef6 | 1579 | SCM_VALIDATE_STRING (1, filename); |
9fd38a3d | 1580 | |
cc95e00a MV |
1581 | s = scm_i_string_chars (filename); |
1582 | len = scm_i_string_length (filename); | |
9fd38a3d | 1583 | |
6a738a25 | 1584 | i = len - 1; |
82893676 MG |
1585 | #ifdef __MINGW32__ |
1586 | while (i >= 0 && (s[i] == '/' || s[i] == '\\')) --i; | |
e0c73a1c | 1587 | while (i >= 0 && (s[i] != '/' && s[i] != '\\')) --i; |
82893676 MG |
1588 | while (i >= 0 && (s[i] == '/' || s[i] == '\\')) --i; |
1589 | #else | |
6a738a25 JB |
1590 | while (i >= 0 && s[i] == '/') --i; |
1591 | while (i >= 0 && s[i] != '/') --i; | |
1592 | while (i >= 0 && s[i] == '/') --i; | |
82893676 | 1593 | #endif /* ndef __MINGW32__ */ |
6a738a25 JB |
1594 | if (i < 0) |
1595 | { | |
82893676 MG |
1596 | #ifdef __MINGW32__ |
1597 | if (len > 0 && (s[0] == '/' || s[0] == '\\')) | |
1598 | #else | |
6a738a25 | 1599 | if (len > 0 && s[0] == '/') |
82893676 | 1600 | #endif /* ndef __MINGW32__ */ |
cc95e00a | 1601 | return scm_c_substring (filename, 0, 1); |
6a738a25 JB |
1602 | else |
1603 | return scm_dot_string; | |
1604 | } | |
1605 | else | |
cc95e00a | 1606 | return scm_c_substring (filename, 0, i + 1); |
6a738a25 | 1607 | } |
1bbd0b84 | 1608 | #undef FUNC_NAME |
6a738a25 | 1609 | |
a1ec6916 | 1610 | SCM_DEFINE (scm_basename, "basename", 1, 1, 0, |
1bbd0b84 | 1611 | (SCM filename, SCM suffix), |
fa6a543f MG |
1612 | "Return the base name of the file name @var{filename}. The\n" |
1613 | "base name is the file name without any directory components.\n" | |
bb2c02f2 | 1614 | "If @var{suffix} is provided, and is equal to the end of\n" |
fa6a543f | 1615 | "@var{basename}, it is removed also.") |
1bbd0b84 | 1616 | #define FUNC_NAME s_scm_basename |
6a738a25 | 1617 | { |
cc95e00a | 1618 | const char *f, *s = 0; |
6a738a25 | 1619 | int i, j, len, end; |
9fd38a3d | 1620 | |
34d19ef6 | 1621 | SCM_VALIDATE_STRING (1, filename); |
cc95e00a MV |
1622 | f = scm_i_string_chars (filename); |
1623 | len = scm_i_string_length (filename); | |
9fd38a3d | 1624 | |
6a738a25 JB |
1625 | if (SCM_UNBNDP (suffix)) |
1626 | j = -1; | |
1627 | else | |
1628 | { | |
9fd38a3d | 1629 | SCM_VALIDATE_STRING (2, suffix); |
cc95e00a MV |
1630 | s = scm_i_string_chars (suffix); |
1631 | j = scm_i_string_length (suffix) - 1; | |
6a738a25 | 1632 | } |
6a738a25 | 1633 | i = len - 1; |
82893676 MG |
1634 | #ifdef __MINGW32__ |
1635 | while (i >= 0 && (f[i] == '/' || f[i] == '\\')) --i; | |
1636 | #else | |
6a738a25 | 1637 | while (i >= 0 && f[i] == '/') --i; |
82893676 | 1638 | #endif /* ndef __MINGW32__ */ |
6a738a25 JB |
1639 | end = i; |
1640 | while (i >= 0 && j >= 0 && f[i] == s[j]) --i, --j; | |
1641 | if (j == -1) | |
1642 | end = i; | |
82893676 | 1643 | #ifdef __MINGW32__ |
2e945bcc | 1644 | while (i >= 0 && f[i] != '/' && f[i] != '\\') --i; |
82893676 | 1645 | #else |
6a738a25 | 1646 | while (i >= 0 && f[i] != '/') --i; |
82893676 | 1647 | #endif /* ndef __MINGW32__ */ |
6a738a25 JB |
1648 | if (i == end) |
1649 | { | |
82893676 | 1650 | #ifdef __MINGW32__ |
2e945bcc | 1651 | if (len > 0 && (f[0] == '/' || f[0] == '\\')) |
82893676 | 1652 | #else |
6a738a25 | 1653 | if (len > 0 && f[0] == '/') |
82893676 | 1654 | #endif /* ndef __MINGW32__ */ |
cc95e00a | 1655 | return scm_c_substring (filename, 0, 1); |
6a738a25 JB |
1656 | else |
1657 | return scm_dot_string; | |
1658 | } | |
1659 | else | |
cc95e00a | 1660 | return scm_c_substring (filename, i+1, end+1); |
6a738a25 | 1661 | } |
1bbd0b84 | 1662 | #undef FUNC_NAME |
6a738a25 JB |
1663 | |
1664 | ||
1665 | ||
1666 | \f | |
1cc91f1b | 1667 | |
0f2d19dd JB |
1668 | void |
1669 | scm_init_filesys () | |
0f2d19dd | 1670 | { |
e841c3e0 KN |
1671 | scm_tc16_dir = scm_make_smob_type ("directory", 0); |
1672 | scm_set_smob_free (scm_tc16_dir, scm_dir_free); | |
1673 | scm_set_smob_print (scm_tc16_dir, scm_dir_print); | |
0f2d19dd | 1674 | |
cc95e00a | 1675 | scm_dot_string = scm_permanent_object (scm_from_locale_string (".")); |
a163dda9 | 1676 | |
3d8d56df | 1677 | #ifdef O_RDONLY |
b9bd8526 | 1678 | scm_c_define ("O_RDONLY", scm_from_long (O_RDONLY)); |
3d8d56df GH |
1679 | #endif |
1680 | #ifdef O_WRONLY | |
b9bd8526 | 1681 | scm_c_define ("O_WRONLY", scm_from_long (O_WRONLY)); |
3d8d56df GH |
1682 | #endif |
1683 | #ifdef O_RDWR | |
b9bd8526 | 1684 | scm_c_define ("O_RDWR", scm_from_long (O_RDWR)); |
3d8d56df GH |
1685 | #endif |
1686 | #ifdef O_CREAT | |
b9bd8526 | 1687 | scm_c_define ("O_CREAT", scm_from_long (O_CREAT)); |
3d8d56df GH |
1688 | #endif |
1689 | #ifdef O_EXCL | |
b9bd8526 | 1690 | scm_c_define ("O_EXCL", scm_from_long (O_EXCL)); |
3d8d56df GH |
1691 | #endif |
1692 | #ifdef O_NOCTTY | |
b9bd8526 | 1693 | scm_c_define ("O_NOCTTY", scm_from_long (O_NOCTTY)); |
3d8d56df GH |
1694 | #endif |
1695 | #ifdef O_TRUNC | |
b9bd8526 | 1696 | scm_c_define ("O_TRUNC", scm_from_long (O_TRUNC)); |
3d8d56df GH |
1697 | #endif |
1698 | #ifdef O_APPEND | |
b9bd8526 | 1699 | scm_c_define ("O_APPEND", scm_from_long (O_APPEND)); |
3d8d56df | 1700 | #endif |
6afcd3b2 | 1701 | #ifdef O_NONBLOCK |
b9bd8526 | 1702 | scm_c_define ("O_NONBLOCK", scm_from_long (O_NONBLOCK)); |
3d8d56df GH |
1703 | #endif |
1704 | #ifdef O_NDELAY | |
b9bd8526 | 1705 | scm_c_define ("O_NDELAY", scm_from_long (O_NDELAY)); |
3d8d56df GH |
1706 | #endif |
1707 | #ifdef O_SYNC | |
b9bd8526 | 1708 | scm_c_define ("O_SYNC", scm_from_long (O_SYNC)); |
3d8d56df GH |
1709 | #endif |
1710 | ||
4c1feaa5 | 1711 | #ifdef F_DUPFD |
b9bd8526 | 1712 | scm_c_define ("F_DUPFD", scm_from_long (F_DUPFD)); |
4c1feaa5 JB |
1713 | #endif |
1714 | #ifdef F_GETFD | |
b9bd8526 | 1715 | scm_c_define ("F_GETFD", scm_from_long (F_GETFD)); |
4c1feaa5 JB |
1716 | #endif |
1717 | #ifdef F_SETFD | |
b9bd8526 | 1718 | scm_c_define ("F_SETFD", scm_from_long (F_SETFD)); |
4c1feaa5 JB |
1719 | #endif |
1720 | #ifdef F_GETFL | |
b9bd8526 | 1721 | scm_c_define ("F_GETFL", scm_from_long (F_GETFL)); |
4c1feaa5 JB |
1722 | #endif |
1723 | #ifdef F_SETFL | |
b9bd8526 | 1724 | scm_c_define ("F_SETFL", scm_from_long (F_SETFL)); |
4c1feaa5 JB |
1725 | #endif |
1726 | #ifdef F_GETOWN | |
b9bd8526 | 1727 | scm_c_define ("F_GETOWN", scm_from_long (F_GETOWN)); |
4c1feaa5 JB |
1728 | #endif |
1729 | #ifdef F_SETOWN | |
b9bd8526 | 1730 | scm_c_define ("F_SETOWN", scm_from_long (F_SETOWN)); |
4c1feaa5 JB |
1731 | #endif |
1732 | #ifdef FD_CLOEXEC | |
b9bd8526 | 1733 | scm_c_define ("FD_CLOEXEC", scm_from_long (FD_CLOEXEC)); |
bd9e24b3 | 1734 | #endif |
3d8d56df | 1735 | |
a0599745 | 1736 | #include "libguile/filesys.x" |
0f2d19dd | 1737 | } |
89e00824 ML |
1738 | |
1739 | /* | |
1740 | Local Variables: | |
1741 | c-file-style: "gnu" | |
1742 | End: | |
1743 | */ |