fports.c 21 KB

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