fports.c 20 KB

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