fports.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  1. /* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
  2. * 2004, 2006, 2007, 2008, 2009, 2010, 2011, 2012 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. #define _POSIX_C_SOURCE 200809L /* F_DUPFD_CLOEXEC */
  22. #ifdef HAVE_CONFIG_H
  23. # include <config.h>
  24. #endif
  25. #include <stdio.h>
  26. #include <fcntl.h>
  27. #include "libguile/_scm.h"
  28. #include "libguile/strings.h"
  29. #include "libguile/validate.h"
  30. #include "libguile/gc.h"
  31. #include "libguile/posix.h"
  32. #include "libguile/dynwind.h"
  33. #include "libguile/hashtab.h"
  34. #include "libguile/fports.h"
  35. #ifdef HAVE_STRING_H
  36. #include <string.h>
  37. #endif
  38. #ifdef HAVE_UNISTD_H
  39. #include <unistd.h>
  40. #endif
  41. #ifdef HAVE_IO_H
  42. #include <io.h>
  43. #endif
  44. #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
  45. #include <sys/stat.h>
  46. #endif
  47. #ifdef HAVE_POLL_H
  48. #include <poll.h>
  49. #endif
  50. #include <errno.h>
  51. #include <sys/types.h>
  52. #include <full-write.h>
  53. #include "libguile/iselect.h"
  54. /* Some defines for Windows (native port, not Cygwin). */
  55. #ifdef __MINGW32__
  56. # include <sys/stat.h>
  57. # include <winsock2.h>
  58. #endif /* __MINGW32__ */
  59. #include <full-write.h>
  60. /* Mingw (version 3.4.5, circa 2006) has ftruncate as an alias for chsize
  61. already, but have this code here in case that wasn't so in past versions,
  62. or perhaps to help other minimal DOS environments.
  63. gnulib ftruncate.c has code using fcntl F_CHSIZE and F_FREESP, which
  64. might be possibilities if we've got other systems without ftruncate. */
  65. #if defined HAVE_CHSIZE && ! defined HAVE_FTRUNCATE
  66. # define ftruncate(fd, size) chsize (fd, size)
  67. # undef HAVE_FTRUNCATE
  68. # define HAVE_FTRUNCATE 1
  69. #endif
  70. #if SIZEOF_OFF_T == SIZEOF_INT
  71. #define OFF_T_MAX INT_MAX
  72. #define OFF_T_MIN INT_MIN
  73. #elif SIZEOF_OFF_T == SIZEOF_LONG
  74. #define OFF_T_MAX LONG_MAX
  75. #define OFF_T_MIN LONG_MIN
  76. #elif SIZEOF_OFF_T == SIZEOF_LONG_LONG
  77. #define OFF_T_MAX LONG_LONG_MAX
  78. #define OFF_T_MIN LONG_LONG_MIN
  79. #else
  80. #error Oops, unknown OFF_T size
  81. #endif
  82. scm_t_bits scm_tc16_fport;
  83. /* default buffer size, used if the O/S won't supply a value. */
  84. static const size_t default_buffer_size = 1024;
  85. /* create FPORT buffer with specified sizes (or -1 to use default size or
  86. 0 for no buffer. */
  87. static void
  88. scm_fport_buffer_add (SCM port, long read_size, int write_size)
  89. #define FUNC_NAME "scm_fport_buffer_add"
  90. {
  91. scm_t_port *pt = SCM_PTAB_ENTRY (port);
  92. if (read_size == -1 || write_size == -1)
  93. {
  94. size_t default_size;
  95. #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
  96. struct stat st;
  97. scm_t_fport *fp = SCM_FSTREAM (port);
  98. default_size = (fstat (fp->fdes, &st) == -1) ? default_buffer_size
  99. : st.st_blksize;
  100. #else
  101. default_size = default_buffer_size;
  102. #endif
  103. if (read_size == -1)
  104. read_size = default_size;
  105. if (write_size == -1)
  106. write_size = default_size;
  107. }
  108. if (SCM_INPUT_PORT_P (port) && read_size > 0)
  109. {
  110. pt->read_buf = scm_gc_malloc_pointerless (read_size, "port buffer");
  111. pt->read_pos = pt->read_end = pt->read_buf;
  112. pt->read_buf_size = read_size;
  113. }
  114. else
  115. {
  116. pt->read_pos = pt->read_buf = pt->read_end = &pt->shortbuf;
  117. pt->read_buf_size = 1;
  118. }
  119. if (SCM_OUTPUT_PORT_P (port) && write_size > 0)
  120. {
  121. pt->write_buf = scm_gc_malloc_pointerless (write_size, "port buffer");
  122. pt->write_pos = pt->write_buf;
  123. pt->write_buf_size = write_size;
  124. }
  125. else
  126. {
  127. pt->write_buf = pt->write_pos = &pt->shortbuf;
  128. pt->write_buf_size = 1;
  129. }
  130. pt->write_end = pt->write_buf + pt->write_buf_size;
  131. if (read_size > 0 || write_size > 0)
  132. SCM_SET_CELL_WORD_0 (port, SCM_CELL_WORD_0 (port) & ~SCM_BUF0);
  133. else
  134. SCM_SET_CELL_WORD_0 (port, SCM_CELL_WORD_0 (port) | SCM_BUF0);
  135. }
  136. #undef FUNC_NAME
  137. SCM_DEFINE (scm_setvbuf, "setvbuf", 2, 1, 0,
  138. (SCM port, SCM mode, SCM size),
  139. "Set the buffering mode for @var{port}. @var{mode} can be:\n"
  140. "@table @code\n"
  141. "@item _IONBF\n"
  142. "non-buffered\n"
  143. "@item _IOLBF\n"
  144. "line buffered\n"
  145. "@item _IOFBF\n"
  146. "block buffered, using a newly allocated buffer of @var{size} bytes.\n"
  147. "If @var{size} is omitted, a default size will be used.\n"
  148. "@end table")
  149. #define FUNC_NAME s_scm_setvbuf
  150. {
  151. int cmode;
  152. long csize;
  153. SCM drained;
  154. scm_t_port *pt;
  155. port = SCM_COERCE_OUTPORT (port);
  156. SCM_VALIDATE_OPFPORT (1,port);
  157. cmode = scm_to_int (mode);
  158. if (cmode != _IONBF && cmode != _IOFBF && cmode != _IOLBF)
  159. scm_out_of_range (FUNC_NAME, mode);
  160. if (cmode == _IOLBF)
  161. {
  162. SCM_SET_CELL_WORD_0 (port, SCM_CELL_WORD_0 (port) | SCM_BUFLINE);
  163. cmode = _IOFBF;
  164. }
  165. else
  166. {
  167. SCM_SET_CELL_WORD_0 (port, SCM_CELL_WORD_0 (port) & ~(scm_t_bits)SCM_BUFLINE);
  168. }
  169. if (SCM_UNBNDP (size))
  170. {
  171. if (cmode == _IOFBF)
  172. csize = -1;
  173. else
  174. csize = 0;
  175. }
  176. else
  177. {
  178. csize = scm_to_int (size);
  179. if (csize < 0 || (cmode == _IONBF && csize > 0))
  180. scm_out_of_range (FUNC_NAME, size);
  181. }
  182. pt = SCM_PTAB_ENTRY (port);
  183. if (SCM_INPUT_PORT_P (port))
  184. drained = scm_drain_input (port);
  185. else
  186. drained = scm_nullstr;
  187. if (SCM_OUTPUT_PORT_P (port))
  188. scm_flush_unlocked (port);
  189. if (pt->read_buf == pt->putback_buf)
  190. {
  191. pt->read_buf = pt->saved_read_buf;
  192. pt->read_pos = pt->saved_read_pos;
  193. pt->read_end = pt->saved_read_end;
  194. pt->read_buf_size = pt->saved_read_buf_size;
  195. }
  196. if (pt->read_buf != &pt->shortbuf)
  197. scm_gc_free (pt->read_buf, pt->read_buf_size, "port buffer");
  198. if (pt->write_buf != &pt->shortbuf)
  199. scm_gc_free (pt->write_buf, pt->write_buf_size, "port buffer");
  200. scm_fport_buffer_add (port, csize, csize);
  201. if (scm_is_true (drained) && scm_c_string_length (drained))
  202. scm_unread_string (drained, port);
  203. return SCM_UNSPECIFIED;
  204. }
  205. #undef FUNC_NAME
  206. /* Move ports with the specified file descriptor to new descriptors,
  207. * resetting the revealed count to 0.
  208. */
  209. static void
  210. scm_i_evict_port (void *closure, SCM port)
  211. {
  212. int fd = * (int*) closure;
  213. if (SCM_FPORTP (port))
  214. {
  215. scm_t_port *p;
  216. scm_t_fport *fp;
  217. /* XXX: In some cases, we can encounter a port with no associated ptab
  218. entry. */
  219. p = SCM_PTAB_ENTRY (port);
  220. fp = (p != NULL) ? (scm_t_fport *) p->stream : NULL;
  221. if ((fp != NULL) && (fp->fdes == fd))
  222. {
  223. if (fp->cloexec_p)
  224. /* The result will be 3 or more. Presumably we don't want
  225. to accidentally end on 0, 1, or 2. */
  226. fp->fdes = fcntl (fd, F_DUPFD_CLOEXEC, 3);
  227. else
  228. fp->fdes = dup (fd);
  229. if (fp->fdes == -1)
  230. scm_syserror ("scm_evict_ports");
  231. scm_set_port_revealed_x (port, scm_from_int (0));
  232. }
  233. }
  234. }
  235. void
  236. scm_evict_ports (int fd)
  237. {
  238. scm_c_port_for_each (scm_i_evict_port, (void *) &fd);
  239. }
  240. SCM_DEFINE (scm_file_port_p, "file-port?", 1, 0, 0,
  241. (SCM obj),
  242. "Determine whether @var{obj} is a port that is related to a file.")
  243. #define FUNC_NAME s_scm_file_port_p
  244. {
  245. return scm_from_bool (SCM_FPORTP (obj));
  246. }
  247. #undef FUNC_NAME
  248. SCM_DEFINE (scm_file_port_close_on_exec_p, "file-port-close-on-exec?", 1, 0, 0,
  249. (SCM fport),
  250. "Determine whether @var{fobj} was created with the @code{FD_CLOEXEC} flag set.")
  251. #define FUNC_NAME s_scm_file_port_close_on_exec_p
  252. {
  253. SCM_VALIDATE_OPFPORT (1, fport);
  254. return scm_from_bool (SCM_FSTREAM (fport)->cloexec_p);
  255. }
  256. #undef FUNC_NAME
  257. static SCM sys_file_port_name_canonicalization;
  258. SCM_SYMBOL (sym_relative, "relative");
  259. SCM_SYMBOL (sym_absolute, "absolute");
  260. static SCM
  261. fport_canonicalize_filename (SCM filename)
  262. {
  263. SCM mode = scm_fluid_ref (sys_file_port_name_canonicalization);
  264. if (!scm_is_string (filename))
  265. {
  266. return filename;
  267. }
  268. else if (scm_is_eq (mode, sym_relative))
  269. {
  270. SCM path, rel;
  271. path = scm_variable_ref (scm_c_module_lookup (scm_the_root_module (),
  272. "%load-path"));
  273. rel = scm_i_relativize_path (filename, path);
  274. return scm_is_true (rel) ? rel : filename;
  275. }
  276. else if (scm_is_eq (mode, sym_absolute))
  277. {
  278. char *str, *canon;
  279. str = scm_to_locale_string (filename);
  280. canon = canonicalize_file_name (str);
  281. free (str);
  282. return canon ? scm_take_locale_string (canon) : filename;
  283. }
  284. else
  285. {
  286. return filename;
  287. }
  288. }
  289. /* scm_open_file
  290. * Return a new port open on a given file.
  291. *
  292. * The mode string must match the pattern: [rwa+]** which
  293. * is interpreted in the usual unix way.
  294. *
  295. * Return the new port.
  296. */
  297. SCM_DEFINE (scm_open_file, "open-file", 2, 0, 0,
  298. (SCM filename, SCM mode),
  299. "Open the file whose name is @var{filename}, and return a port\n"
  300. "representing that file. The attributes of the port are\n"
  301. "determined by the @var{mode} string. The way in which this is\n"
  302. "interpreted is similar to C stdio. The first character must be\n"
  303. "one of the following:\n"
  304. "@table @samp\n"
  305. "@item r\n"
  306. "Open an existing file for input.\n"
  307. "@item w\n"
  308. "Open a file for output, creating it if it doesn't already exist\n"
  309. "or removing its contents if it does.\n"
  310. "@item a\n"
  311. "Open a file for output, creating it if it doesn't already\n"
  312. "exist. All writes to the port will go to the end of the file.\n"
  313. "The \"append mode\" can be turned off while the port is in use\n"
  314. "@pxref{Ports and File Descriptors, fcntl}\n"
  315. "@end table\n"
  316. "The following additional characters can be appended:\n"
  317. "@table @samp\n"
  318. "@item b\n"
  319. "Open the underlying file in binary mode, if supported by the system.\n"
  320. "Also, open the file using the binary-compatible character encoding\n"
  321. "\"ISO-8859-1\", ignoring the port's encoding and the coding declaration\n"
  322. "at the top of the input file, if any.\n"
  323. "@item +\n"
  324. "Open the port for both input and output. E.g., @code{r+}: open\n"
  325. "an existing file for both input and output.\n"
  326. "@item 0\n"
  327. "Create an \"unbuffered\" port. In this case input and output\n"
  328. "operations are passed directly to the underlying port\n"
  329. "implementation without additional buffering. This is likely to\n"
  330. "slow down I/O operations. The buffering mode can be changed\n"
  331. "while a port is in use @pxref{Ports and File Descriptors,\n"
  332. "setvbuf}\n"
  333. "@item l\n"
  334. "Add line-buffering to the port. The port output buffer will be\n"
  335. "automatically flushed whenever a newline character is written.\n"
  336. "@end table\n"
  337. "When the file is opened, this procedure will scan for a coding\n"
  338. "declaration@pxref{Character Encoding of Source Files}. If present\n"
  339. "will use that encoding for interpreting the file. Otherwise, the\n"
  340. "port's encoding will be used.\n"
  341. "\n"
  342. "In theory we could create read/write ports which were buffered\n"
  343. "in one direction only. However this isn't included in the\n"
  344. "current interfaces. If a file cannot be opened with the access\n"
  345. "requested, @code{open-file} throws an exception.")
  346. #define FUNC_NAME s_scm_open_file
  347. {
  348. SCM port;
  349. int fdes, flags = 0, use_encoding = 1;
  350. unsigned int retries;
  351. char *file, *md, *ptr;
  352. scm_dynwind_begin (0);
  353. file = scm_to_locale_string (filename);
  354. scm_dynwind_free (file);
  355. md = scm_to_locale_string (mode);
  356. scm_dynwind_free (md);
  357. switch (*md)
  358. {
  359. case 'r':
  360. flags |= O_RDONLY;
  361. break;
  362. case 'w':
  363. flags |= O_WRONLY | O_CREAT | O_TRUNC;
  364. break;
  365. case 'a':
  366. flags |= O_WRONLY | O_CREAT | O_APPEND;
  367. break;
  368. default:
  369. scm_out_of_range (FUNC_NAME, mode);
  370. }
  371. ptr = md + 1;
  372. while (*ptr != '\0')
  373. {
  374. switch (*ptr)
  375. {
  376. case '+':
  377. flags = (flags & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
  378. break;
  379. case 'b':
  380. use_encoding = 0;
  381. #if defined (O_BINARY)
  382. flags |= O_BINARY;
  383. #endif
  384. break;
  385. case '0': /* unbuffered: handled later. */
  386. case 'l': /* line buffered: handled during output. */
  387. break;
  388. default:
  389. scm_out_of_range (FUNC_NAME, mode);
  390. }
  391. ptr++;
  392. }
  393. for (retries = 0, fdes = -1;
  394. fdes < 0 && retries < 2;
  395. retries++)
  396. {
  397. SCM_SYSCALL (fdes = open_or_open64 (file, flags, 0666));
  398. if (fdes == -1)
  399. {
  400. int en = errno;
  401. if (en == EMFILE && retries == 0)
  402. /* Run the GC in case it collects open file ports that are no
  403. longer referenced. */
  404. scm_i_gc (FUNC_NAME);
  405. else
  406. SCM_SYSERROR_MSG ("~A: ~S",
  407. scm_cons (scm_strerror (scm_from_int (en)),
  408. scm_cons (filename, SCM_EOL)), en);
  409. }
  410. }
  411. /* Create a port from this file descriptor. The port's encoding is initially
  412. %default-port-encoding. */
  413. port = scm_i_fdes_to_port (fdes, scm_i_mode_bits (mode),
  414. fport_canonicalize_filename (filename));
  415. if (use_encoding)
  416. {
  417. /* If this file has a coding declaration, use that as the port
  418. encoding. */
  419. if (SCM_INPUT_PORT_P (port))
  420. {
  421. char *enc = scm_i_scan_for_encoding (port);
  422. if (enc != NULL)
  423. scm_i_set_port_encoding_x (port, enc);
  424. }
  425. }
  426. else
  427. /* If this is a binary file, use the binary-friendly ISO-8859-1
  428. encoding. */
  429. scm_i_set_port_encoding_x (port, NULL);
  430. scm_dynwind_end ();
  431. return port;
  432. }
  433. #undef FUNC_NAME
  434. #ifdef __MINGW32__
  435. /*
  436. * Try getting the appropiate file flags for a given file descriptor
  437. * under Windows. This incorporates some fancy operations because Windows
  438. * differentiates between file, pipe and socket descriptors.
  439. */
  440. #ifndef O_ACCMODE
  441. # define O_ACCMODE 0x0003
  442. #endif
  443. static int getflags (int fdes)
  444. {
  445. int flags = 0;
  446. struct stat buf;
  447. int error, optlen = sizeof (int);
  448. /* Is this a socket ? */
  449. if (getsockopt (fdes, SOL_SOCKET, SO_ERROR, (void *) &error, &optlen) >= 0)
  450. flags = O_RDWR;
  451. /* Maybe a regular file ? */
  452. else if (fstat (fdes, &buf) < 0)
  453. flags = -1;
  454. else
  455. {
  456. /* Or an anonymous pipe handle ? */
  457. if (buf.st_mode & _S_IFIFO)
  458. flags = PeekNamedPipe ((HANDLE) _get_osfhandle (fdes), NULL, 0,
  459. NULL, NULL, NULL) ? O_RDONLY : O_WRONLY;
  460. /* stdin ? */
  461. else if (fdes == fileno (stdin) && isatty (fdes))
  462. flags = O_RDONLY;
  463. /* stdout / stderr ? */
  464. else if ((fdes == fileno (stdout) || fdes == fileno (stderr)) &&
  465. isatty (fdes))
  466. flags = O_WRONLY;
  467. else
  468. flags = buf.st_mode;
  469. }
  470. return flags;
  471. }
  472. #endif /* __MINGW32__ */
  473. /* Building Guile ports from a file descriptor. */
  474. /* Build a Scheme port from an open file descriptor `fdes'.
  475. MODE indicates whether FILE is open for reading or writing; it uses
  476. the same notation as open-file's second argument.
  477. NAME is a string to be used as the port's filename.
  478. */
  479. SCM
  480. scm_i_fdes_to_port (int fdes, long mode_bits, SCM name)
  481. #define FUNC_NAME "scm_fdes_to_port"
  482. {
  483. SCM port;
  484. scm_t_fport *fp;
  485. int flags;
  486. int fd_flags;
  487. /* test that fdes is valid. */
  488. #ifdef __MINGW32__
  489. flags = getflags (fdes);
  490. #else
  491. flags = fcntl (fdes, F_GETFL, 0);
  492. #endif
  493. if (flags == -1)
  494. SCM_SYSERROR;
  495. flags &= O_ACCMODE;
  496. if (flags != O_RDWR
  497. && ((flags != O_WRONLY && (mode_bits & SCM_WRTNG))
  498. || (flags != O_RDONLY && (mode_bits & SCM_RDNG))))
  499. {
  500. SCM_MISC_ERROR ("requested file mode not available on fdes", SCM_EOL);
  501. }
  502. #ifdef F_GETFD
  503. fd_flags = fcntl (fdes, F_GETFD, 0);
  504. #else
  505. fd_flags = 0;
  506. #endif
  507. if (fd_flags == -1)
  508. SCM_SYSERROR;
  509. fp = (scm_t_fport *) scm_gc_malloc_pointerless (sizeof (scm_t_fport),
  510. "file port");
  511. fp->fdes = fdes;
  512. fp->cloexec_p = fd_flags & FD_CLOEXEC ? 1 : 0;
  513. fp->reserved = 0;
  514. port = scm_c_make_port (scm_tc16_fport, mode_bits, (scm_t_bits)fp);
  515. SCM_PTAB_ENTRY (port)->rw_random = SCM_FDES_RANDOM_P (fdes);
  516. if (mode_bits & SCM_BUF0)
  517. scm_fport_buffer_add (port, 0, 0);
  518. else
  519. scm_fport_buffer_add (port, -1, -1);
  520. SCM_SET_FILENAME (port, name);
  521. return port;
  522. }
  523. #undef FUNC_NAME
  524. SCM
  525. scm_fdes_to_port (int fdes, char *mode, SCM name)
  526. {
  527. return scm_i_fdes_to_port (fdes, scm_mode_bits (mode), name);
  528. }
  529. /* Return a lower bound on the number of bytes available for input. */
  530. static int
  531. fport_input_waiting (SCM port)
  532. {
  533. int fdes = SCM_FSTREAM (port)->fdes;
  534. /* `FD_SETSIZE', which is 1024 on GNU systems, effectively limits the
  535. highest numerical value of file descriptors that can be monitored.
  536. Thus, use poll(2) whenever that is possible. */
  537. #ifdef HAVE_POLL
  538. struct pollfd pollfd = { fdes, POLLIN, 0 };
  539. if (poll (&pollfd, 1, 0) < 0)
  540. scm_syserror ("fport_input_waiting");
  541. return pollfd.revents & POLLIN ? 1 : 0;
  542. #elif defined(HAVE_SELECT)
  543. struct timeval timeout;
  544. SELECT_TYPE read_set;
  545. SELECT_TYPE write_set;
  546. SELECT_TYPE except_set;
  547. FD_ZERO (&read_set);
  548. FD_ZERO (&write_set);
  549. FD_ZERO (&except_set);
  550. FD_SET (fdes, &read_set);
  551. timeout.tv_sec = 0;
  552. timeout.tv_usec = 0;
  553. if (select (SELECT_SET_SIZE,
  554. &read_set, &write_set, &except_set, &timeout)
  555. < 0)
  556. scm_syserror ("fport_input_waiting");
  557. return FD_ISSET (fdes, &read_set) ? 1 : 0;
  558. #elif HAVE_IOCTL && defined (FIONREAD)
  559. /* Note: cannot test just defined(FIONREAD) here, since mingw has FIONREAD
  560. (for use with winsock ioctlsocket()) but not ioctl(). */
  561. int fdes = SCM_FSTREAM (port)->fdes;
  562. int remir;
  563. ioctl(fdes, FIONREAD, &remir);
  564. return remir;
  565. #else
  566. scm_misc_error ("fport_input_waiting",
  567. "Not fully implemented on this platform",
  568. SCM_EOL);
  569. #endif
  570. }
  571. static int
  572. fport_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
  573. {
  574. scm_puts_unlocked ("#<", port);
  575. scm_print_port_mode (exp, port);
  576. if (SCM_OPFPORTP (exp))
  577. {
  578. int fdes;
  579. SCM name = SCM_FILENAME (exp);
  580. if (scm_is_string (name) || scm_is_symbol (name))
  581. scm_display (name, port);
  582. else
  583. scm_puts_unlocked (SCM_PTOBNAME (SCM_PTOBNUM (exp)), port);
  584. scm_putc_unlocked (' ', port);
  585. fdes = (SCM_FSTREAM (exp))->fdes;
  586. #if (defined HAVE_TTYNAME) && (defined HAVE_POSIX)
  587. if (isatty (fdes))
  588. scm_display (scm_ttyname (exp), port);
  589. else
  590. #endif /* HAVE_TTYNAME */
  591. scm_intprint (fdes, 10, port);
  592. }
  593. else
  594. {
  595. scm_puts_unlocked (SCM_PTOBNAME (SCM_PTOBNUM (exp)), port);
  596. scm_putc_unlocked (' ', port);
  597. scm_uintprint ((scm_t_bits) SCM_PTAB_ENTRY (exp), 16, port);
  598. }
  599. scm_putc_unlocked ('>', port);
  600. return 1;
  601. }
  602. static void fport_flush (SCM port);
  603. /* fill a port's read-buffer with a single read. returns the first
  604. char or EOF if end of file. */
  605. static scm_t_wchar
  606. fport_fill_input (SCM port)
  607. {
  608. long count;
  609. scm_t_port *pt = SCM_PTAB_ENTRY (port);
  610. scm_t_fport *fp = SCM_FSTREAM (port);
  611. SCM_SYSCALL (count = read (fp->fdes, pt->read_buf, pt->read_buf_size));
  612. if (count == -1)
  613. scm_syserror ("fport_fill_input");
  614. if (count == 0)
  615. return (scm_t_wchar) EOF;
  616. else
  617. {
  618. pt->read_pos = pt->read_buf;
  619. pt->read_end = pt->read_buf + count;
  620. return *pt->read_buf;
  621. }
  622. }
  623. static scm_t_off
  624. fport_seek (SCM port, scm_t_off offset, int whence)
  625. {
  626. scm_t_port *pt = SCM_PTAB_ENTRY (port);
  627. scm_t_fport *fp = SCM_FSTREAM (port);
  628. off_t_or_off64_t rv;
  629. off_t_or_off64_t result;
  630. if (pt->rw_active == SCM_PORT_WRITE)
  631. {
  632. if (offset != 0 || whence != SEEK_CUR)
  633. {
  634. fport_flush (port);
  635. result = rv = lseek_or_lseek64 (fp->fdes, offset, whence);
  636. }
  637. else
  638. {
  639. /* read current position without disturbing the buffer. */
  640. rv = lseek_or_lseek64 (fp->fdes, offset, whence);
  641. result = rv + (pt->write_pos - pt->write_buf);
  642. }
  643. }
  644. else if (pt->rw_active == SCM_PORT_READ)
  645. {
  646. if (offset != 0 || whence != SEEK_CUR)
  647. {
  648. /* could expand to avoid a second seek. */
  649. scm_end_input_unlocked (port);
  650. result = rv = lseek_or_lseek64 (fp->fdes, offset, whence);
  651. }
  652. else
  653. {
  654. /* read current position without disturbing the buffer
  655. (particularly the unread-char buffer). */
  656. rv = lseek_or_lseek64 (fp->fdes, offset, whence);
  657. result = rv - (pt->read_end - pt->read_pos);
  658. if (pt->read_buf == pt->putback_buf)
  659. result -= pt->saved_read_end - pt->saved_read_pos;
  660. }
  661. }
  662. else /* SCM_PORT_NEITHER */
  663. {
  664. result = rv = lseek_or_lseek64 (fp->fdes, offset, whence);
  665. }
  666. if (rv == -1)
  667. scm_syserror ("fport_seek");
  668. return result;
  669. }
  670. static void
  671. fport_truncate (SCM port, scm_t_off length)
  672. {
  673. scm_t_fport *fp = SCM_FSTREAM (port);
  674. if (ftruncate (fp->fdes, length) == -1)
  675. scm_syserror ("ftruncate");
  676. }
  677. static void
  678. fport_write (SCM port, const void *data, size_t size)
  679. #define FUNC_NAME "fport_write"
  680. {
  681. /* this procedure tries to minimize the number of writes/flushes. */
  682. scm_t_port *pt = SCM_PTAB_ENTRY (port);
  683. if (pt->write_buf == &pt->shortbuf
  684. || (pt->write_pos == pt->write_buf && size >= pt->write_buf_size))
  685. {
  686. /* Unbuffered port, or port with empty buffer and data won't fit in
  687. buffer. */
  688. if (full_write (SCM_FPORT_FDES (port), data, size) < size)
  689. SCM_SYSERROR;
  690. return;
  691. }
  692. {
  693. scm_t_off space = pt->write_end - pt->write_pos;
  694. if (size <= space)
  695. {
  696. /* data fits in buffer. */
  697. memcpy (pt->write_pos, data, size);
  698. pt->write_pos += size;
  699. if (pt->write_pos == pt->write_end)
  700. {
  701. fport_flush (port);
  702. /* we can skip the line-buffering check if nothing's buffered. */
  703. return;
  704. }
  705. }
  706. else
  707. {
  708. memcpy (pt->write_pos, data, space);
  709. pt->write_pos = pt->write_end;
  710. fport_flush (port);
  711. {
  712. const void *ptr = ((const char *) data) + space;
  713. size_t remaining = size - space;
  714. if (size >= pt->write_buf_size)
  715. {
  716. if (full_write (SCM_FPORT_FDES (port), ptr, remaining)
  717. < remaining)
  718. SCM_SYSERROR;
  719. return;
  720. }
  721. else
  722. {
  723. memcpy (pt->write_pos, ptr, remaining);
  724. pt->write_pos += remaining;
  725. }
  726. }
  727. }
  728. /* handle line buffering. */
  729. if ((SCM_CELL_WORD_0 (port) & SCM_BUFLINE) && memchr (data, '\n', size))
  730. fport_flush (port);
  731. }
  732. }
  733. #undef FUNC_NAME
  734. static void
  735. fport_flush (SCM port)
  736. {
  737. size_t written;
  738. scm_t_port *pt = SCM_PTAB_ENTRY (port);
  739. scm_t_fport *fp = SCM_FSTREAM (port);
  740. size_t count = pt->write_pos - pt->write_buf;
  741. written = full_write (fp->fdes, pt->write_buf, count);
  742. if (written < count)
  743. scm_syserror ("scm_flush");
  744. pt->write_pos = pt->write_buf;
  745. pt->rw_active = SCM_PORT_NEITHER;
  746. }
  747. /* clear the read buffer and adjust the file position for unread bytes. */
  748. static void
  749. fport_end_input (SCM port, int offset)
  750. {
  751. scm_t_fport *fp = SCM_FSTREAM (port);
  752. scm_t_port *pt = SCM_PTAB_ENTRY (port);
  753. offset += pt->read_end - pt->read_pos;
  754. if (offset > 0)
  755. {
  756. pt->read_pos = pt->read_end;
  757. /* will throw error if unread-char used at beginning of file
  758. then attempting to write. seems correct. */
  759. if (lseek (fp->fdes, -offset, SEEK_CUR) == -1)
  760. scm_syserror ("fport_end_input");
  761. }
  762. pt->rw_active = SCM_PORT_NEITHER;
  763. }
  764. static void
  765. close_the_fd (void *data)
  766. {
  767. scm_t_fport *fp = data;
  768. close (fp->fdes);
  769. /* There's already one exception. That's probably enough! */
  770. errno = 0;
  771. }
  772. static int
  773. fport_close (SCM port)
  774. {
  775. scm_t_fport *fp = SCM_FSTREAM (port);
  776. int rv;
  777. scm_dynwind_begin (0);
  778. scm_dynwind_unwind_handler (close_the_fd, fp, 0);
  779. fport_flush (port);
  780. scm_dynwind_end ();
  781. scm_port_non_buffer (SCM_PTAB_ENTRY (port));
  782. rv = close (fp->fdes);
  783. if (rv)
  784. /* It's not useful to retry after EINTR, as the file descriptor is
  785. in an undefined state. See http://lwn.net/Articles/365294/.
  786. Instead just throw an error if close fails, trusting that the fd
  787. was cleaned up. */
  788. scm_syserror ("fport_close");
  789. return 0;
  790. }
  791. static size_t
  792. fport_free (SCM port)
  793. {
  794. fport_close (port);
  795. return 0;
  796. }
  797. static scm_t_bits
  798. scm_make_fptob ()
  799. {
  800. scm_t_bits tc = scm_make_port_type ("file", fport_fill_input, fport_write);
  801. scm_set_port_free (tc, fport_free);
  802. scm_set_port_print (tc, fport_print);
  803. scm_set_port_flush (tc, fport_flush);
  804. scm_set_port_end_input (tc, fport_end_input);
  805. scm_set_port_close (tc, fport_close);
  806. scm_set_port_seek (tc, fport_seek);
  807. scm_set_port_truncate (tc, fport_truncate);
  808. scm_set_port_input_waiting (tc, fport_input_waiting);
  809. return tc;
  810. }
  811. void
  812. scm_init_fports ()
  813. {
  814. scm_tc16_fport = scm_make_fptob ();
  815. scm_c_define ("_IOFBF", scm_from_int (_IOFBF));
  816. scm_c_define ("_IOLBF", scm_from_int (_IOLBF));
  817. scm_c_define ("_IONBF", scm_from_int (_IONBF));
  818. sys_file_port_name_canonicalization = scm_make_fluid ();
  819. scm_c_define ("%file-port-name-canonicalization",
  820. sys_file_port_name_canonicalization);
  821. #include "libguile/fports.x"
  822. }
  823. /*
  824. Local Variables:
  825. c-file-style: "gnu"
  826. End:
  827. */