fports.c 20 KB

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