fports.c 24 KB

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