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