fports.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. /* Copyright 1995-2004,2006-2015,2017-2020,2022
  2. Free Software Foundation, Inc.
  3. This file is part of Guile.
  4. Guile is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Guile is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with Guile. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #define _LARGEFILE64_SOURCE /* ask for stat64 etc */
  16. #define _GNU_SOURCE /* ask for LONG_LONG_MAX/LONG_LONG_MIN */
  17. #ifdef HAVE_CONFIG_H
  18. # include <config.h>
  19. #endif
  20. #include <stdio.h>
  21. #include <fcntl.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #ifdef HAVE_IO_H
  25. #include <io.h>
  26. #endif
  27. #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
  28. #include <sys/stat.h>
  29. #endif
  30. #include <poll.h>
  31. #include <errno.h>
  32. #include <sys/types.h>
  33. #include <sys/stat.h>
  34. #include <sys/select.h>
  35. #include <full-write.h>
  36. #include "async.h"
  37. #include "boolean.h"
  38. #include "dynwind.h"
  39. #include "extensions.h"
  40. #include "fdes-finalizers.h"
  41. #include "filesys.h"
  42. #include "fluids.h"
  43. #include "gc.h"
  44. #include "gsubr.h"
  45. #include "hashtab.h"
  46. #include "keywords.h"
  47. #include "modules.h"
  48. #include "numbers.h"
  49. #include "pairs.h"
  50. #include "ports-internal.h"
  51. #include "posix.h"
  52. #include "read.h"
  53. #include "strings.h"
  54. #include "symbols.h"
  55. #include "syscalls.h"
  56. #include "variable.h"
  57. #include "version.h"
  58. #include "fports.h"
  59. #if SIZEOF_OFF_T == SIZEOF_INT
  60. #define OFF_T_MAX INT_MAX
  61. #define OFF_T_MIN INT_MIN
  62. #elif SIZEOF_OFF_T == SIZEOF_LONG
  63. #define OFF_T_MAX LONG_MAX
  64. #define OFF_T_MIN LONG_MIN
  65. #elif SIZEOF_OFF_T == SIZEOF_LONG_LONG
  66. #define OFF_T_MAX LONG_LONG_MAX
  67. #define OFF_T_MIN LONG_LONG_MIN
  68. #else
  69. #error Oops, unknown OFF_T size
  70. #endif
  71. scm_t_port_type *scm_file_port_type;
  72. /* Move ports with the specified file descriptor to new descriptors,
  73. * resetting the revealed count to 0.
  74. */
  75. static void
  76. scm_i_evict_port (void *closure, SCM port)
  77. {
  78. int fd = * (int*) closure;
  79. if (SCM_OPFPORTP (port))
  80. {
  81. scm_t_fport *fp = SCM_FSTREAM (port);
  82. if ((fp != NULL) && (fp->fdes == fd))
  83. {
  84. fp->fdes = dup (fd);
  85. if (fp->fdes == -1)
  86. scm_syserror ("scm_evict_ports");
  87. scm_set_port_revealed_x (port, scm_from_int (0));
  88. }
  89. }
  90. }
  91. void
  92. scm_evict_ports (int fd)
  93. {
  94. scm_c_port_for_each (scm_i_evict_port, (void *) &fd);
  95. }
  96. SCM_DEFINE (scm_file_port_p, "file-port?", 1, 0, 0,
  97. (SCM obj),
  98. "Determine whether @var{obj} is a port that is related to a file.")
  99. #define FUNC_NAME s_scm_file_port_p
  100. {
  101. return scm_from_bool (SCM_FPORTP (obj));
  102. }
  103. #undef FUNC_NAME
  104. static SCM sys_file_port_name_canonicalization;
  105. static SCM sym_relative;
  106. static SCM sym_absolute;
  107. static SCM
  108. fport_canonicalize_filename (SCM filename)
  109. {
  110. SCM mode = scm_fluid_ref (sys_file_port_name_canonicalization);
  111. if (!scm_is_string (filename))
  112. {
  113. return filename;
  114. }
  115. else if (scm_is_eq (mode, sym_relative))
  116. {
  117. SCM path, rel;
  118. path = scm_variable_ref (scm_c_module_lookup (scm_the_root_module (),
  119. "%load-path"));
  120. rel = scm_i_relativize_path (filename, path);
  121. return scm_is_true (rel) ? rel : filename;
  122. }
  123. else if (scm_is_eq (mode, sym_absolute))
  124. {
  125. char *str, *canon;
  126. str = scm_to_locale_string (filename);
  127. canon = canonicalize_file_name (str);
  128. free (str);
  129. return canon ? scm_take_locale_string (canon) : filename;
  130. }
  131. else
  132. {
  133. return filename;
  134. }
  135. }
  136. int
  137. scm_i_mode_to_open_flags (SCM mode, int *is_binary, const char *FUNC_NAME)
  138. {
  139. int flags = 0;
  140. const char *md, *ptr;
  141. if (SCM_UNLIKELY (!scm_is_string (mode)))
  142. scm_out_of_range (FUNC_NAME, mode);
  143. if (SCM_UNLIKELY (!scm_i_try_narrow_string (mode)))
  144. scm_out_of_range (FUNC_NAME, mode);
  145. md = scm_i_string_chars (mode);
  146. *is_binary = 0;
  147. switch (*md)
  148. {
  149. case 'r':
  150. flags |= O_RDONLY;
  151. break;
  152. case 'w':
  153. flags |= O_WRONLY | O_CREAT | O_TRUNC;
  154. break;
  155. case 'a':
  156. flags |= O_WRONLY | O_CREAT | O_APPEND;
  157. break;
  158. default:
  159. scm_out_of_range (FUNC_NAME, mode);
  160. }
  161. ptr = md + 1;
  162. while (*ptr != '\0')
  163. {
  164. switch (*ptr)
  165. {
  166. case '+':
  167. flags = (flags & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
  168. break;
  169. case 'b':
  170. *is_binary = 1;
  171. #if defined (O_BINARY)
  172. flags |= O_BINARY;
  173. #endif
  174. break;
  175. case 'e':
  176. flags |= O_CLOEXEC;
  177. break;
  178. case '0': /* unbuffered: handled later. */
  179. case 'l': /* line buffered: handled during output. */
  180. break;
  181. default:
  182. scm_out_of_range (FUNC_NAME, mode);
  183. }
  184. ptr++;
  185. }
  186. return flags;
  187. }
  188. /* scm_open_file_with_encoding
  189. Return a new port open on a given file.
  190. The mode string must match the pattern: [rwa+]** which
  191. is interpreted in the usual unix way.
  192. Unless binary mode is requested, the character encoding of the new
  193. port is determined as follows: First, if GUESS_ENCODING is true,
  194. 'file-encoding' is used to guess the encoding of the file. If
  195. GUESS_ENCODING is false or if 'file-encoding' fails, ENCODING is used
  196. unless it is also false. As a last resort, the default port encoding
  197. is used. It is an error to pass a non-false GUESS_ENCODING or
  198. ENCODING if binary mode is requested.
  199. Return the new port. */
  200. SCM
  201. scm_open_file_with_encoding (SCM filename, SCM mode,
  202. SCM guess_encoding, SCM encoding)
  203. #define FUNC_NAME "open-file"
  204. {
  205. SCM port;
  206. int fdes, flags, binary = 0;
  207. unsigned int retries;
  208. char *file;
  209. if (SCM_UNLIKELY (!(scm_is_false (encoding) || scm_is_string (encoding))))
  210. scm_wrong_type_arg_msg (FUNC_NAME, 0, encoding,
  211. "encoding to be string or false");
  212. scm_dynwind_begin (0);
  213. file = scm_to_locale_string (filename);
  214. scm_dynwind_free (file);
  215. flags = scm_i_mode_to_open_flags (mode, &binary, FUNC_NAME);
  216. for (retries = 0, fdes = -1;
  217. fdes < 0 && retries < 2;
  218. retries++)
  219. {
  220. SCM_SYSCALL (fdes = open_or_open64 (file, flags, 0666));
  221. if (fdes == -1)
  222. {
  223. int en = errno;
  224. if (en == EMFILE && retries == 0)
  225. /* Run the GC in case it collects open file ports that are no
  226. longer referenced. */
  227. scm_i_gc (FUNC_NAME);
  228. else
  229. SCM_SYSERROR_MSG ("~A: ~S",
  230. scm_cons (scm_strerror (scm_from_int (en)),
  231. scm_cons (filename, SCM_EOL)), en);
  232. }
  233. }
  234. /* Create a port from this file descriptor. The port's encoding is initially
  235. %default-port-encoding. */
  236. port = scm_i_fdes_to_port (fdes, scm_i_mode_bits (mode),
  237. fport_canonicalize_filename (filename),
  238. 0);
  239. if (binary)
  240. {
  241. if (scm_is_true (encoding))
  242. scm_misc_error (FUNC_NAME,
  243. "Encoding specified on a binary port",
  244. scm_list_1 (encoding));
  245. if (scm_is_true (guess_encoding))
  246. scm_misc_error (FUNC_NAME,
  247. "Request to guess encoding on a binary port",
  248. SCM_EOL);
  249. /* Use the binary-friendly ISO-8859-1 encoding. */
  250. scm_i_set_port_encoding_x (port, NULL);
  251. }
  252. else
  253. {
  254. char *enc = NULL;
  255. if (scm_is_true (guess_encoding))
  256. {
  257. if (SCM_INPUT_PORT_P (port))
  258. enc = scm_i_scan_for_encoding (port);
  259. else
  260. scm_misc_error (FUNC_NAME,
  261. "Request to guess encoding on an output-only port",
  262. SCM_EOL);
  263. }
  264. if (!enc && scm_is_true (encoding))
  265. {
  266. char *buf = scm_to_latin1_string (encoding);
  267. enc = scm_gc_strdup (buf, "encoding");
  268. free (buf);
  269. }
  270. if (enc)
  271. scm_i_set_port_encoding_x (port, enc);
  272. }
  273. scm_dynwind_end ();
  274. return port;
  275. }
  276. #undef FUNC_NAME
  277. SCM
  278. scm_open_file (SCM filename, SCM mode)
  279. {
  280. return scm_open_file_with_encoding (filename, mode, SCM_BOOL_F, SCM_BOOL_F);
  281. }
  282. /* We can't define these using SCM_KEYWORD, because keywords have not
  283. yet been initialized when scm_init_fports is called. */
  284. static SCM k_guess_encoding = SCM_UNDEFINED;
  285. static SCM k_encoding = SCM_UNDEFINED;
  286. SCM_INTERNAL SCM scm_i_open_file (SCM, SCM, SCM);
  287. SCM_DEFINE (scm_i_open_file, "open-file", 2, 0, 1,
  288. (SCM filename, SCM mode, SCM keyword_args),
  289. "Open the file whose name is @var{filename}, and return a port\n"
  290. "representing that file. The attributes of the port are\n"
  291. "determined by the @var{mode} string. The way in which this is\n"
  292. "interpreted is similar to C stdio. The first character must be\n"
  293. "one of the following:\n"
  294. "@table @samp\n"
  295. "@item r\n"
  296. "Open an existing file for input.\n"
  297. "@item w\n"
  298. "Open a file for output, creating it if it doesn't already exist\n"
  299. "or removing its contents if it does.\n"
  300. "@item a\n"
  301. "Open a file for output, creating it if it doesn't already\n"
  302. "exist. All writes to the port will go to the end of the file.\n"
  303. "The \"append mode\" can be turned off while the port is in use\n"
  304. "@pxref{Ports and File Descriptors, fcntl}\n"
  305. "@end table\n"
  306. "The following additional characters can be appended:\n"
  307. "@table @samp\n"
  308. "@item b\n"
  309. "Open the underlying file in binary mode, if supported by the system.\n"
  310. "Also, open the file using the binary-compatible character encoding\n"
  311. "\"ISO-8859-1\", ignoring the default port encoding.\n"
  312. "@item +\n"
  313. "Open the port for both input and output. E.g., @code{r+}: open\n"
  314. "an existing file for both input and output.\n"
  315. "@item e\n"
  316. "Mark the underlying file descriptor as close-on-exec, as per the\n"
  317. "@code{O_CLOEXEC} flag.\n"
  318. "@item 0\n"
  319. "Create an \"unbuffered\" port. In this case input and output\n"
  320. "operations are passed directly to the underlying port\n"
  321. "implementation without additional buffering. This is likely to\n"
  322. "slow down I/O operations. The buffering mode can be changed\n"
  323. "while a port is in use @pxref{Ports and File Descriptors,\n"
  324. "setvbuf}\n"
  325. "@item l\n"
  326. "Add line-buffering to the port. The port output buffer will be\n"
  327. "automatically flushed whenever a newline character is written.\n"
  328. "@end table\n"
  329. "In theory we could create read/write ports which were buffered\n"
  330. "in one direction only. However this isn't included in the\n"
  331. "current interfaces. If a file cannot be opened with the access\n"
  332. "requested, @code{open-file} throws an exception.")
  333. #define FUNC_NAME s_scm_i_open_file
  334. {
  335. SCM encoding = SCM_BOOL_F;
  336. SCM guess_encoding = SCM_BOOL_F;
  337. scm_c_bind_keyword_arguments (FUNC_NAME, keyword_args, 0,
  338. k_guess_encoding, &guess_encoding,
  339. k_encoding, &encoding,
  340. SCM_UNDEFINED);
  341. return scm_open_file_with_encoding (filename, mode,
  342. guess_encoding, encoding);
  343. }
  344. #undef FUNC_NAME
  345. /* Building Guile ports from a file descriptor. */
  346. int
  347. scm_i_fdes_is_valid (int fdes, long mode_bits)
  348. {
  349. #ifdef F_GETFL
  350. int flags = fcntl (fdes, F_GETFL, 0);
  351. if (flags == -1)
  352. return 0;
  353. flags &= O_ACCMODE;
  354. if (flags == O_RDWR)
  355. return 1;
  356. if (flags != O_WRONLY && (mode_bits & SCM_WRTNG))
  357. return 0;
  358. if (flags != O_RDONLY && (mode_bits & SCM_RDNG))
  359. return 0;
  360. return 1;
  361. #else
  362. /* If we don't have F_GETFL, as on mingw, at least we can test that
  363. it is a valid file descriptor. */
  364. struct stat st;
  365. return fstat (fdes, &st) == 0;
  366. #endif
  367. }
  368. /* Build a Scheme port from an open file descriptor `fdes'.
  369. MODE indicates whether FILE is open for reading or writing; it uses
  370. the same notation as open-file's second argument.
  371. NAME is a string to be used as the port's filename.
  372. */
  373. SCM
  374. scm_i_fdes_to_port (int fdes, long mode_bits, SCM name, unsigned options)
  375. #define FUNC_NAME "scm_fdes_to_port"
  376. {
  377. SCM port;
  378. scm_t_fport *fp;
  379. if (options & SCM_FPORT_OPTION_VERIFY)
  380. {
  381. errno = 0;
  382. if (!scm_i_fdes_is_valid (fdes, mode_bits))
  383. {
  384. if (errno)
  385. SCM_SYSERROR;
  386. SCM_MISC_ERROR ("requested file mode not available on fdes",
  387. SCM_EOL);
  388. }
  389. }
  390. fp = (scm_t_fport *) scm_gc_malloc_pointerless (sizeof (scm_t_fport),
  391. "file port");
  392. fp->fdes = fdes;
  393. fp->options = options;
  394. fp->revealed = 0;
  395. port = scm_c_make_port (scm_file_port_type, mode_bits, (scm_t_bits)fp);
  396. SCM_SET_FILENAME (port, name);
  397. return port;
  398. }
  399. #undef FUNC_NAME
  400. SCM
  401. scm_fdes_to_port (int fdes, char *mode, SCM name)
  402. {
  403. return scm_i_fdes_to_port (fdes, scm_mode_bits (mode), name,
  404. SCM_FPORT_OPTION_VERIFY);
  405. }
  406. /* Return a lower bound on the number of bytes available for input. */
  407. static int
  408. fport_input_waiting (SCM port)
  409. {
  410. int fdes = SCM_FSTREAM (port)->fdes;
  411. struct pollfd pollfd = { fdes, POLLIN, 0 };
  412. if (poll (&pollfd, 1, 0) < 0)
  413. scm_syserror ("fport_input_waiting");
  414. return pollfd.revents & POLLIN ? 1 : 0;
  415. }
  416. /* Revealed counts --- an oddity inherited from SCSH. */
  417. #define SCM_REVEALED(x) (SCM_FSTREAM(x)->revealed)
  418. /* Find a port in the table and return its revealed count.
  419. Also used by the garbage collector.
  420. */
  421. int
  422. scm_revealed_count (SCM port)
  423. {
  424. return SCM_REVEALED (port);
  425. }
  426. SCM_DEFINE (scm_port_revealed, "port-revealed", 1, 0, 0,
  427. (SCM port),
  428. "Return the revealed count for @var{port}.")
  429. #define FUNC_NAME s_scm_port_revealed
  430. {
  431. port = SCM_COERCE_OUTPORT (port);
  432. SCM_VALIDATE_OPFPORT (1, port);
  433. return scm_from_int (scm_revealed_count (port));
  434. }
  435. #undef FUNC_NAME
  436. /* Set the revealed count for a port. */
  437. SCM_DEFINE (scm_set_port_revealed_x, "set-port-revealed!", 2, 0, 0,
  438. (SCM port, SCM rcount),
  439. "Sets the revealed count for a port to a given value.\n"
  440. "The return value is unspecified.")
  441. #define FUNC_NAME s_scm_set_port_revealed_x
  442. {
  443. int r;
  444. port = SCM_COERCE_OUTPORT (port);
  445. SCM_VALIDATE_OPFPORT (1, port);
  446. r = scm_to_int (rcount);
  447. SCM_REVEALED (port) = r;
  448. return SCM_UNSPECIFIED;
  449. }
  450. #undef FUNC_NAME
  451. /* Set the revealed count for a port. */
  452. SCM_DEFINE (scm_adjust_port_revealed_x, "adjust-port-revealed!", 2, 0, 0,
  453. (SCM port, SCM addend),
  454. "Add @var{addend} to the revealed count of @var{port}.\n"
  455. "The return value is unspecified.")
  456. #define FUNC_NAME s_scm_adjust_port_revealed_x
  457. {
  458. int a;
  459. port = SCM_COERCE_OUTPORT (port);
  460. SCM_VALIDATE_OPFPORT (1, port);
  461. a = scm_to_int (addend);
  462. SCM_REVEALED (port) += a;
  463. return SCM_UNSPECIFIED;
  464. }
  465. #undef FUNC_NAME
  466. static int
  467. fport_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
  468. {
  469. scm_puts ("#<", port);
  470. scm_print_port_mode (exp, port);
  471. if (SCM_OPFPORTP (exp))
  472. {
  473. int fdes;
  474. SCM name = SCM_FILENAME (exp);
  475. if (scm_is_string (name) || scm_is_symbol (name))
  476. scm_display (name, port);
  477. else
  478. scm_puts (SCM_PORT_TYPE (exp)->name, port);
  479. scm_putc (' ', port);
  480. fdes = (SCM_FSTREAM (exp))->fdes;
  481. #if (defined HAVE_TTYNAME) && (defined HAVE_POSIX)
  482. if (isatty (fdes))
  483. scm_display (scm_ttyname (exp), port);
  484. else
  485. #endif /* HAVE_TTYNAME */
  486. scm_intprint (fdes, 10, port);
  487. }
  488. else
  489. {
  490. scm_puts (SCM_PORT_TYPE (exp)->name, port);
  491. scm_putc (' ', port);
  492. scm_uintprint ((scm_t_bits) SCM_PORT (exp), 16, port);
  493. }
  494. scm_putc ('>', port);
  495. return 1;
  496. }
  497. /* fill a port's read-buffer with a single read. returns the first
  498. char or EOF if end of file. */
  499. static size_t
  500. fport_read (SCM port, SCM dst, size_t start, size_t count)
  501. {
  502. scm_t_fport *fp = SCM_FSTREAM (port);
  503. signed char *ptr = SCM_BYTEVECTOR_CONTENTS (dst) + start;
  504. ssize_t ret;
  505. retry:
  506. ret = read (fp->fdes, ptr, count);
  507. if (ret < 0)
  508. {
  509. if (errno == EINTR)
  510. {
  511. scm_async_tick ();
  512. goto retry;
  513. }
  514. if (errno == EWOULDBLOCK || errno == EAGAIN)
  515. return -1;
  516. scm_syserror ("fport_read");
  517. }
  518. return ret;
  519. }
  520. static size_t
  521. fport_write (SCM port, SCM src, size_t start, size_t count)
  522. {
  523. int fd = SCM_FPORT_FDES (port);
  524. signed char *ptr = SCM_BYTEVECTOR_CONTENTS (src) + start;
  525. ssize_t ret;
  526. retry:
  527. ret = write (fd, ptr, count);
  528. if (ret < 0)
  529. {
  530. if (errno == EINTR)
  531. {
  532. scm_async_tick ();
  533. goto retry;
  534. }
  535. if (errno == EWOULDBLOCK || errno == EAGAIN)
  536. return -1;
  537. scm_syserror ("fport_write");
  538. }
  539. return ret;
  540. }
  541. static scm_t_off
  542. fport_seek (SCM port, scm_t_off offset, int whence)
  543. {
  544. scm_t_fport *fp = SCM_FSTREAM (port);
  545. scm_t_off result;
  546. result = lseek (fp->fdes, offset, whence);
  547. if (result == -1)
  548. scm_syserror ("fport_seek");
  549. return result;
  550. }
  551. static void
  552. fport_truncate (SCM port, scm_t_off length)
  553. {
  554. scm_t_fport *fp = SCM_FSTREAM (port);
  555. if (ftruncate (fp->fdes, length) == -1)
  556. scm_syserror ("ftruncate");
  557. }
  558. static void
  559. fport_close (SCM port)
  560. {
  561. scm_t_fport *fp = SCM_FSTREAM (port);
  562. if (SCM_REVEALED (port) > 0)
  563. /* The port has a non-zero revealed count, so don't close the
  564. underlying file descriptor. */
  565. return;
  566. scm_run_fdes_finalizers (fp->fdes);
  567. if (close (fp->fdes) != 0)
  568. /* It's not useful to retry after EINTR, as the file descriptor is
  569. in an undefined state. See http://lwn.net/Articles/365294/.
  570. Instead just throw an error if close fails, trusting that the fd
  571. was cleaned up. */
  572. scm_syserror ("fport_close");
  573. }
  574. static int
  575. fport_random_access_p (SCM port)
  576. {
  577. scm_t_fport *fp = SCM_FSTREAM (port);
  578. if (fp->options & SCM_FPORT_OPTION_NOT_SEEKABLE)
  579. return 0;
  580. if (lseek (fp->fdes, 0, SEEK_CUR) == -1)
  581. return 0;
  582. return 1;
  583. }
  584. static int
  585. fport_wait_fd (SCM port)
  586. {
  587. return SCM_FSTREAM (port)->fdes;
  588. }
  589. /* Query the OS to get the natural buffering for FPORT, if available. */
  590. static void
  591. fport_get_natural_buffer_sizes (SCM port, size_t *read_size, size_t *write_size)
  592. {
  593. #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
  594. scm_t_fport *fp = SCM_FSTREAM (port);
  595. struct stat st;
  596. if (fstat (fp->fdes, &st) == 0)
  597. *read_size = *write_size = st.st_blksize;
  598. #endif
  599. }
  600. static scm_t_port_type *
  601. scm_make_fptob ()
  602. {
  603. scm_t_port_type *ptob = scm_make_port_type ("file", fport_read, fport_write);
  604. scm_set_port_print (ptob, fport_print);
  605. scm_set_port_needs_close_on_gc (ptob, 1);
  606. scm_set_port_close (ptob, fport_close);
  607. scm_set_port_seek (ptob, fport_seek);
  608. scm_set_port_truncate (ptob, fport_truncate);
  609. scm_set_port_read_wait_fd (ptob, fport_wait_fd);
  610. scm_set_port_write_wait_fd (ptob, fport_wait_fd);
  611. scm_set_port_input_waiting (ptob, fport_input_waiting);
  612. scm_set_port_random_access_p (ptob, fport_random_access_p);
  613. scm_set_port_get_natural_buffer_sizes (ptob, fport_get_natural_buffer_sizes);
  614. return ptob;
  615. }
  616. /* We can't initialize the keywords from 'scm_init_fports', because
  617. keywords haven't yet been initialized at that point. */
  618. void
  619. scm_init_fports_keywords ()
  620. {
  621. k_guess_encoding = scm_from_latin1_keyword ("guess-encoding");
  622. k_encoding = scm_from_latin1_keyword ("encoding");
  623. }
  624. static void
  625. scm_init_ice_9_fports (void)
  626. {
  627. #include "fports.x"
  628. }
  629. void
  630. scm_init_fports ()
  631. {
  632. scm_file_port_type = scm_make_fptob ();
  633. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  634. "scm_init_ice_9_fports",
  635. (scm_t_extension_init_func) scm_init_ice_9_fports,
  636. NULL);
  637. /* The following bindings are used early in boot-9.scm. */
  638. /* Used by `include' and also by `file-exists?' if `stat' is
  639. unavailable. */
  640. scm_c_define_gsubr (s_scm_i_open_file, 2, 0, 1, (scm_t_subr) scm_i_open_file);
  641. /* Used by `open-file.', also via C. */
  642. sym_relative = scm_from_latin1_symbol ("relative");
  643. sym_absolute = scm_from_latin1_symbol ("absolute");
  644. sys_file_port_name_canonicalization = scm_make_fluid ();
  645. scm_c_define ("%file-port-name-canonicalization",
  646. sys_file_port_name_canonicalization);
  647. }