wfileops.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  1. /* Copyright (C) 1993-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Written by Ulrich Drepper <drepper@cygnus.com>.
  4. Based on the single byte version by Per Bothner <bothner@cygnus.com>.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but 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. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>.
  16. As a special exception, if you link the code in this file with
  17. files compiled with a GNU compiler to produce an executable,
  18. that does not cause the resulting executable to be covered by
  19. the GNU Lesser General Public License. This exception does not
  20. however invalidate any other reasons why the executable file
  21. might be covered by the GNU Lesser General Public License.
  22. This exception applies to code released by its copyright holders
  23. in files containing the exception. */
  24. #include <assert.h>
  25. #include <libioP.h>
  26. #include <wchar.h>
  27. #include <gconv.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. /* Convert TO_DO wide character from DATA to FP.
  31. Then mark FP as having empty buffers. */
  32. int
  33. _IO_wdo_write (FILE *fp, const wchar_t *data, size_t to_do)
  34. {
  35. struct _IO_codecvt *cc = fp->_codecvt;
  36. if (to_do > 0)
  37. {
  38. if (fp->_IO_write_end == fp->_IO_write_ptr
  39. && fp->_IO_write_end != fp->_IO_write_base)
  40. {
  41. if (_IO_new_do_write (fp, fp->_IO_write_base,
  42. fp->_IO_write_ptr - fp->_IO_write_base) == EOF)
  43. return WEOF;
  44. }
  45. do
  46. {
  47. enum __codecvt_result result;
  48. const wchar_t *new_data;
  49. char mb_buf[MB_LEN_MAX];
  50. char *write_base, *write_ptr, *buf_end;
  51. if (fp->_IO_write_ptr - fp->_IO_write_base < sizeof (mb_buf))
  52. {
  53. /* Make sure we have room for at least one multibyte
  54. character. */
  55. write_ptr = write_base = mb_buf;
  56. buf_end = mb_buf + sizeof (mb_buf);
  57. }
  58. else
  59. {
  60. write_ptr = fp->_IO_write_ptr;
  61. write_base = fp->_IO_write_base;
  62. buf_end = fp->_IO_buf_end;
  63. }
  64. /* Now convert from the internal format into the external buffer. */
  65. result = (*cc->__codecvt_do_out) (cc, &fp->_wide_data->_IO_state,
  66. data, data + to_do, &new_data,
  67. write_ptr,
  68. buf_end,
  69. &write_ptr);
  70. /* Write out what we produced so far. */
  71. if (_IO_new_do_write (fp, write_base, write_ptr - write_base) == EOF)
  72. /* Something went wrong. */
  73. return WEOF;
  74. to_do -= new_data - data;
  75. /* Next see whether we had problems during the conversion. If yes,
  76. we cannot go on. */
  77. if (result != __codecvt_ok
  78. && (result != __codecvt_partial || new_data - data == 0))
  79. break;
  80. data = new_data;
  81. }
  82. while (to_do > 0);
  83. }
  84. _IO_wsetg (fp, fp->_wide_data->_IO_buf_base, fp->_wide_data->_IO_buf_base,
  85. fp->_wide_data->_IO_buf_base);
  86. fp->_wide_data->_IO_write_base = fp->_wide_data->_IO_write_ptr
  87. = fp->_wide_data->_IO_buf_base;
  88. fp->_wide_data->_IO_write_end = ((fp->_flags & (_IO_LINE_BUF | _IO_UNBUFFERED))
  89. ? fp->_wide_data->_IO_buf_base
  90. : fp->_wide_data->_IO_buf_end);
  91. return to_do == 0 ? 0 : WEOF;
  92. }
  93. libc_hidden_def (_IO_wdo_write)
  94. wint_t
  95. _IO_wfile_underflow (FILE *fp)
  96. {
  97. struct _IO_codecvt *cd;
  98. enum __codecvt_result status;
  99. ssize_t count;
  100. /* C99 requires EOF to be "sticky". */
  101. if (fp->_flags & _IO_EOF_SEEN)
  102. return WEOF;
  103. if (__glibc_unlikely (fp->_flags & _IO_NO_READS))
  104. {
  105. fp->_flags |= _IO_ERR_SEEN;
  106. __set_errno (EBADF);
  107. return WEOF;
  108. }
  109. if (fp->_wide_data->_IO_read_ptr < fp->_wide_data->_IO_read_end)
  110. return *fp->_wide_data->_IO_read_ptr;
  111. cd = fp->_codecvt;
  112. /* Maybe there is something left in the external buffer. */
  113. if (fp->_IO_read_ptr < fp->_IO_read_end)
  114. {
  115. /* There is more in the external. Convert it. */
  116. const char *read_stop = (const char *) fp->_IO_read_ptr;
  117. fp->_wide_data->_IO_last_state = fp->_wide_data->_IO_state;
  118. fp->_wide_data->_IO_read_base = fp->_wide_data->_IO_read_ptr =
  119. fp->_wide_data->_IO_buf_base;
  120. status = (*cd->__codecvt_do_in) (cd, &fp->_wide_data->_IO_state,
  121. fp->_IO_read_ptr, fp->_IO_read_end,
  122. &read_stop,
  123. fp->_wide_data->_IO_read_ptr,
  124. fp->_wide_data->_IO_buf_end,
  125. &fp->_wide_data->_IO_read_end);
  126. fp->_IO_read_base = fp->_IO_read_ptr;
  127. fp->_IO_read_ptr = (char *) read_stop;
  128. /* If we managed to generate some text return the next character. */
  129. if (fp->_wide_data->_IO_read_ptr < fp->_wide_data->_IO_read_end)
  130. return *fp->_wide_data->_IO_read_ptr;
  131. if (status == __codecvt_error)
  132. {
  133. __set_errno (EILSEQ);
  134. fp->_flags |= _IO_ERR_SEEN;
  135. return WEOF;
  136. }
  137. /* Move the remaining content of the read buffer to the beginning. */
  138. memmove (fp->_IO_buf_base, fp->_IO_read_ptr,
  139. fp->_IO_read_end - fp->_IO_read_ptr);
  140. fp->_IO_read_end = (fp->_IO_buf_base
  141. + (fp->_IO_read_end - fp->_IO_read_ptr));
  142. fp->_IO_read_base = fp->_IO_read_ptr = fp->_IO_buf_base;
  143. }
  144. else
  145. fp->_IO_read_base = fp->_IO_read_ptr = fp->_IO_read_end =
  146. fp->_IO_buf_base;
  147. if (fp->_IO_buf_base == NULL)
  148. {
  149. /* Maybe we already have a push back pointer. */
  150. if (fp->_IO_save_base != NULL)
  151. {
  152. free (fp->_IO_save_base);
  153. fp->_flags &= ~_IO_IN_BACKUP;
  154. }
  155. _IO_doallocbuf (fp);
  156. fp->_IO_read_base = fp->_IO_read_ptr = fp->_IO_read_end =
  157. fp->_IO_buf_base;
  158. }
  159. fp->_IO_write_base = fp->_IO_write_ptr = fp->_IO_write_end =
  160. fp->_IO_buf_base;
  161. if (fp->_wide_data->_IO_buf_base == NULL)
  162. {
  163. /* Maybe we already have a push back pointer. */
  164. if (fp->_wide_data->_IO_save_base != NULL)
  165. {
  166. free (fp->_wide_data->_IO_save_base);
  167. fp->_flags &= ~_IO_IN_BACKUP;
  168. }
  169. _IO_wdoallocbuf (fp);
  170. }
  171. /* FIXME This can/should be moved to genops ?? */
  172. if (fp->_flags & (_IO_LINE_BUF | _IO_UNBUFFERED))
  173. {
  174. /* We used to flush all line-buffered stream. This really isn't
  175. required by any standard. My recollection is that
  176. traditional Unix systems did this for stdout. stderr better
  177. not be line buffered. So we do just that here
  178. explicitly. --drepper */
  179. _IO_acquire_lock (_IO_stdout);
  180. if ((_IO_stdout->_flags & (_IO_LINKED | _IO_NO_WRITES | _IO_LINE_BUF))
  181. == (_IO_LINKED | _IO_LINE_BUF))
  182. _IO_OVERFLOW (_IO_stdout, EOF);
  183. _IO_release_lock (_IO_stdout);
  184. }
  185. _IO_switch_to_get_mode (fp);
  186. fp->_wide_data->_IO_read_base = fp->_wide_data->_IO_read_ptr =
  187. fp->_wide_data->_IO_buf_base;
  188. fp->_wide_data->_IO_read_end = fp->_wide_data->_IO_buf_base;
  189. fp->_wide_data->_IO_write_base = fp->_wide_data->_IO_write_ptr =
  190. fp->_wide_data->_IO_write_end = fp->_wide_data->_IO_buf_base;
  191. const char *read_ptr_copy;
  192. char accbuf[MB_LEN_MAX];
  193. size_t naccbuf = 0;
  194. again:
  195. count = _IO_SYSREAD (fp, fp->_IO_read_end,
  196. fp->_IO_buf_end - fp->_IO_read_end);
  197. if (count <= 0)
  198. {
  199. if (count == 0 && naccbuf == 0)
  200. {
  201. fp->_flags |= _IO_EOF_SEEN;
  202. fp->_offset = _IO_pos_BAD;
  203. }
  204. else
  205. fp->_flags |= _IO_ERR_SEEN, count = 0;
  206. }
  207. fp->_IO_read_end += count;
  208. if (count == 0)
  209. {
  210. if (naccbuf != 0)
  211. /* There are some bytes in the external buffer but they don't
  212. convert to anything. */
  213. __set_errno (EILSEQ);
  214. return WEOF;
  215. }
  216. if (fp->_offset != _IO_pos_BAD)
  217. _IO_pos_adjust (fp->_offset, count);
  218. /* Now convert the read input. */
  219. fp->_wide_data->_IO_last_state = fp->_wide_data->_IO_state;
  220. fp->_IO_read_base = fp->_IO_read_ptr;
  221. const char *from = fp->_IO_read_ptr;
  222. const char *to = fp->_IO_read_end;
  223. size_t to_copy = count;
  224. if (__glibc_unlikely (naccbuf != 0))
  225. {
  226. to_copy = MIN (sizeof (accbuf) - naccbuf, count);
  227. to = __mempcpy (&accbuf[naccbuf], from, to_copy);
  228. naccbuf += to_copy;
  229. from = accbuf;
  230. }
  231. status = (*cd->__codecvt_do_in) (cd, &fp->_wide_data->_IO_state,
  232. from, to, &read_ptr_copy,
  233. fp->_wide_data->_IO_read_end,
  234. fp->_wide_data->_IO_buf_end,
  235. &fp->_wide_data->_IO_read_end);
  236. if (__glibc_unlikely (naccbuf != 0))
  237. fp->_IO_read_ptr += MAX (0, read_ptr_copy - &accbuf[naccbuf - to_copy]);
  238. else
  239. fp->_IO_read_ptr = (char *) read_ptr_copy;
  240. if (fp->_wide_data->_IO_read_end == fp->_wide_data->_IO_buf_base)
  241. {
  242. if (status == __codecvt_error)
  243. {
  244. out_eilseq:
  245. __set_errno (EILSEQ);
  246. fp->_flags |= _IO_ERR_SEEN;
  247. return WEOF;
  248. }
  249. /* The read bytes make no complete character. Try reading again. */
  250. assert (status == __codecvt_partial);
  251. if (naccbuf == 0)
  252. {
  253. if (fp->_IO_read_base < fp->_IO_read_ptr)
  254. {
  255. /* Partially used the buffer for some input data that
  256. produces no output. */
  257. size_t avail = fp->_IO_read_end - fp->_IO_read_ptr;
  258. memmove (fp->_IO_read_base, fp->_IO_read_ptr, avail);
  259. fp->_IO_read_ptr = fp->_IO_read_base;
  260. fp->_IO_read_end -= avail;
  261. goto again;
  262. }
  263. naccbuf = fp->_IO_read_end - fp->_IO_read_ptr;
  264. if (naccbuf >= sizeof (accbuf))
  265. goto out_eilseq;
  266. memcpy (accbuf, fp->_IO_read_ptr, naccbuf);
  267. }
  268. else
  269. {
  270. size_t used = read_ptr_copy - accbuf;
  271. if (used > 0)
  272. {
  273. memmove (accbuf, read_ptr_copy, naccbuf - used);
  274. naccbuf -= used;
  275. }
  276. if (naccbuf == sizeof (accbuf))
  277. goto out_eilseq;
  278. }
  279. fp->_IO_read_ptr = fp->_IO_read_end = fp->_IO_read_base;
  280. goto again;
  281. }
  282. return *fp->_wide_data->_IO_read_ptr;
  283. }
  284. libc_hidden_def (_IO_wfile_underflow)
  285. static wint_t
  286. _IO_wfile_underflow_mmap (FILE *fp)
  287. {
  288. struct _IO_codecvt *cd;
  289. const char *read_stop;
  290. if (__glibc_unlikely (fp->_flags & _IO_NO_READS))
  291. {
  292. fp->_flags |= _IO_ERR_SEEN;
  293. __set_errno (EBADF);
  294. return WEOF;
  295. }
  296. if (fp->_wide_data->_IO_read_ptr < fp->_wide_data->_IO_read_end)
  297. return *fp->_wide_data->_IO_read_ptr;
  298. cd = fp->_codecvt;
  299. /* Maybe there is something left in the external buffer. */
  300. if (fp->_IO_read_ptr >= fp->_IO_read_end
  301. /* No. But maybe the read buffer is not fully set up. */
  302. && _IO_file_underflow_mmap (fp) == EOF)
  303. /* Nothing available. _IO_file_underflow_mmap has set the EOF or error
  304. flags as appropriate. */
  305. return WEOF;
  306. /* There is more in the external. Convert it. */
  307. read_stop = (const char *) fp->_IO_read_ptr;
  308. if (fp->_wide_data->_IO_buf_base == NULL)
  309. {
  310. /* Maybe we already have a push back pointer. */
  311. if (fp->_wide_data->_IO_save_base != NULL)
  312. {
  313. free (fp->_wide_data->_IO_save_base);
  314. fp->_flags &= ~_IO_IN_BACKUP;
  315. }
  316. _IO_wdoallocbuf (fp);
  317. }
  318. fp->_wide_data->_IO_last_state = fp->_wide_data->_IO_state;
  319. fp->_wide_data->_IO_read_base = fp->_wide_data->_IO_read_ptr =
  320. fp->_wide_data->_IO_buf_base;
  321. (*cd->__codecvt_do_in) (cd, &fp->_wide_data->_IO_state,
  322. fp->_IO_read_ptr, fp->_IO_read_end,
  323. &read_stop,
  324. fp->_wide_data->_IO_read_ptr,
  325. fp->_wide_data->_IO_buf_end,
  326. &fp->_wide_data->_IO_read_end);
  327. fp->_IO_read_ptr = (char *) read_stop;
  328. /* If we managed to generate some text return the next character. */
  329. if (fp->_wide_data->_IO_read_ptr < fp->_wide_data->_IO_read_end)
  330. return *fp->_wide_data->_IO_read_ptr;
  331. /* There is some garbage at the end of the file. */
  332. __set_errno (EILSEQ);
  333. fp->_flags |= _IO_ERR_SEEN;
  334. return WEOF;
  335. }
  336. static wint_t
  337. _IO_wfile_underflow_maybe_mmap (FILE *fp)
  338. {
  339. /* This is the first read attempt. Doing the underflow will choose mmap
  340. or vanilla operations and then punt to the chosen underflow routine.
  341. Then we can punt to ours. */
  342. if (_IO_file_underflow_maybe_mmap (fp) == EOF)
  343. return WEOF;
  344. return _IO_WUNDERFLOW (fp);
  345. }
  346. wint_t
  347. _IO_wfile_overflow (FILE *f, wint_t wch)
  348. {
  349. if (f->_flags & _IO_NO_WRITES) /* SET ERROR */
  350. {
  351. f->_flags |= _IO_ERR_SEEN;
  352. __set_errno (EBADF);
  353. return WEOF;
  354. }
  355. /* If currently reading or no buffer allocated. */
  356. if ((f->_flags & _IO_CURRENTLY_PUTTING) == 0)
  357. {
  358. /* Allocate a buffer if needed. */
  359. if (f->_wide_data->_IO_write_base == 0)
  360. {
  361. _IO_wdoallocbuf (f);
  362. _IO_free_wbackup_area (f);
  363. _IO_wsetg (f, f->_wide_data->_IO_buf_base,
  364. f->_wide_data->_IO_buf_base, f->_wide_data->_IO_buf_base);
  365. if (f->_IO_write_base == NULL)
  366. {
  367. _IO_doallocbuf (f);
  368. _IO_setg (f, f->_IO_buf_base, f->_IO_buf_base, f->_IO_buf_base);
  369. }
  370. }
  371. else
  372. {
  373. /* Otherwise must be currently reading. If _IO_read_ptr
  374. (and hence also _IO_read_end) is at the buffer end,
  375. logically slide the buffer forwards one block (by setting
  376. the read pointers to all point at the beginning of the
  377. block). This makes room for subsequent output.
  378. Otherwise, set the read pointers to _IO_read_end (leaving
  379. that alone, so it can continue to correspond to the
  380. external position). */
  381. if (f->_wide_data->_IO_read_ptr == f->_wide_data->_IO_buf_end)
  382. {
  383. f->_IO_read_end = f->_IO_read_ptr = f->_IO_buf_base;
  384. f->_wide_data->_IO_read_end = f->_wide_data->_IO_read_ptr =
  385. f->_wide_data->_IO_buf_base;
  386. }
  387. }
  388. f->_wide_data->_IO_write_ptr = f->_wide_data->_IO_read_ptr;
  389. f->_wide_data->_IO_write_base = f->_wide_data->_IO_write_ptr;
  390. f->_wide_data->_IO_write_end = f->_wide_data->_IO_buf_end;
  391. f->_wide_data->_IO_read_base = f->_wide_data->_IO_read_ptr =
  392. f->_wide_data->_IO_read_end;
  393. f->_IO_write_ptr = f->_IO_read_ptr;
  394. f->_IO_write_base = f->_IO_write_ptr;
  395. f->_IO_write_end = f->_IO_buf_end;
  396. f->_IO_read_base = f->_IO_read_ptr = f->_IO_read_end;
  397. f->_flags |= _IO_CURRENTLY_PUTTING;
  398. if (f->_flags & (_IO_LINE_BUF | _IO_UNBUFFERED))
  399. f->_wide_data->_IO_write_end = f->_wide_data->_IO_write_ptr;
  400. }
  401. if (wch == WEOF)
  402. return _IO_do_flush (f);
  403. if (f->_wide_data->_IO_write_ptr == f->_wide_data->_IO_buf_end)
  404. /* Buffer is really full */
  405. if (_IO_do_flush (f) == EOF)
  406. return WEOF;
  407. *f->_wide_data->_IO_write_ptr++ = wch;
  408. if ((f->_flags & _IO_UNBUFFERED)
  409. || ((f->_flags & _IO_LINE_BUF) && wch == L'\n'))
  410. if (_IO_do_flush (f) == EOF)
  411. return WEOF;
  412. return wch;
  413. }
  414. libc_hidden_def (_IO_wfile_overflow)
  415. wint_t
  416. _IO_wfile_sync (FILE *fp)
  417. {
  418. ssize_t delta;
  419. wint_t retval = 0;
  420. /* char* ptr = cur_ptr(); */
  421. if (fp->_wide_data->_IO_write_ptr > fp->_wide_data->_IO_write_base)
  422. if (_IO_do_flush (fp))
  423. return WEOF;
  424. delta = fp->_wide_data->_IO_read_ptr - fp->_wide_data->_IO_read_end;
  425. if (delta != 0)
  426. {
  427. /* We have to find out how many bytes we have to go back in the
  428. external buffer. */
  429. struct _IO_codecvt *cv = fp->_codecvt;
  430. off64_t new_pos;
  431. int clen = (*cv->__codecvt_do_encoding) (cv);
  432. if (clen > 0)
  433. /* It is easy, a fixed number of input bytes are used for each
  434. wide character. */
  435. delta *= clen;
  436. else
  437. {
  438. /* We have to find out the hard way how much to back off.
  439. To do this we determine how much input we needed to
  440. generate the wide characters up to the current reading
  441. position. */
  442. int nread;
  443. fp->_wide_data->_IO_state = fp->_wide_data->_IO_last_state;
  444. nread = (*cv->__codecvt_do_length) (cv, &fp->_wide_data->_IO_state,
  445. fp->_IO_read_base,
  446. fp->_IO_read_end, delta);
  447. fp->_IO_read_ptr = fp->_IO_read_base + nread;
  448. delta = -(fp->_IO_read_end - fp->_IO_read_base - nread);
  449. }
  450. new_pos = _IO_SYSSEEK (fp, delta, 1);
  451. if (new_pos != (off64_t) EOF)
  452. {
  453. fp->_wide_data->_IO_read_end = fp->_wide_data->_IO_read_ptr;
  454. fp->_IO_read_end = fp->_IO_read_ptr;
  455. }
  456. else if (errno == ESPIPE)
  457. ; /* Ignore error from unseekable devices. */
  458. else
  459. retval = WEOF;
  460. }
  461. if (retval != WEOF)
  462. fp->_offset = _IO_pos_BAD;
  463. /* FIXME: Cleanup - can this be shared? */
  464. /* setg(base(), ptr, ptr); */
  465. return retval;
  466. }
  467. libc_hidden_def (_IO_wfile_sync)
  468. /* Adjust the internal buffer pointers to reflect the state in the external
  469. buffer. The content between fp->_IO_read_base and fp->_IO_read_ptr is
  470. assumed to be converted and available in the range
  471. fp->_wide_data->_IO_read_base and fp->_wide_data->_IO_read_end.
  472. Returns 0 on success and -1 on error with the _IO_ERR_SEEN flag set. */
  473. static int
  474. adjust_wide_data (FILE *fp, bool do_convert)
  475. {
  476. struct _IO_codecvt *cv = fp->_codecvt;
  477. int clen = (*cv->__codecvt_do_encoding) (cv);
  478. /* Take the easy way out for constant length encodings if we don't need to
  479. convert. */
  480. if (!do_convert && clen > 0)
  481. {
  482. fp->_wide_data->_IO_read_end += ((fp->_IO_read_ptr - fp->_IO_read_base)
  483. / clen);
  484. goto done;
  485. }
  486. enum __codecvt_result status;
  487. const char *read_stop = (const char *) fp->_IO_read_base;
  488. do
  489. {
  490. fp->_wide_data->_IO_last_state = fp->_wide_data->_IO_state;
  491. status = (*cv->__codecvt_do_in) (cv, &fp->_wide_data->_IO_state,
  492. fp->_IO_read_base, fp->_IO_read_ptr,
  493. &read_stop,
  494. fp->_wide_data->_IO_read_base,
  495. fp->_wide_data->_IO_buf_end,
  496. &fp->_wide_data->_IO_read_end);
  497. /* Should we return EILSEQ? */
  498. if (__glibc_unlikely (status == __codecvt_error))
  499. {
  500. fp->_flags |= _IO_ERR_SEEN;
  501. return -1;
  502. }
  503. }
  504. while (__builtin_expect (status == __codecvt_partial, 0));
  505. done:
  506. /* Now seek to _IO_read_end to behave as if we have read it all in. */
  507. fp->_wide_data->_IO_read_ptr = fp->_wide_data->_IO_read_end;
  508. return 0;
  509. }
  510. /* ftell{,o} implementation for wide mode. Don't modify any state of the file
  511. pointer while we try to get the current state of the stream except in one
  512. case, which is when we have unflushed writes in append mode. */
  513. static off64_t
  514. do_ftell_wide (FILE *fp)
  515. {
  516. off64_t result, offset = 0;
  517. /* No point looking for offsets in the buffer if it hasn't even been
  518. allocated. */
  519. if (fp->_wide_data->_IO_buf_base != NULL)
  520. {
  521. const wchar_t *wide_read_base;
  522. const wchar_t *wide_read_ptr;
  523. const wchar_t *wide_read_end;
  524. bool unflushed_writes = (fp->_wide_data->_IO_write_ptr
  525. > fp->_wide_data->_IO_write_base);
  526. bool append_mode = (fp->_flags & _IO_IS_APPENDING) == _IO_IS_APPENDING;
  527. /* When we have unflushed writes in append mode, seek to the end of the
  528. file and record that offset. This is the only time we change the file
  529. stream state and it is safe since the file handle is active. */
  530. if (unflushed_writes && append_mode)
  531. {
  532. result = _IO_SYSSEEK (fp, 0, _IO_seek_end);
  533. if (result == _IO_pos_BAD)
  534. return EOF;
  535. else
  536. fp->_offset = result;
  537. }
  538. /* XXX For wide stream with backup store it is not very
  539. reasonable to determine the offset. The pushed-back
  540. character might require a state change and we need not be
  541. able to compute the initial state by reverse transformation
  542. since there is no guarantee of symmetry. So we don't even
  543. try and return an error. */
  544. if (_IO_in_backup (fp))
  545. {
  546. if (fp->_wide_data->_IO_read_ptr < fp->_wide_data->_IO_read_end)
  547. {
  548. __set_errno (EINVAL);
  549. return -1;
  550. }
  551. /* Nothing in the backup store, so note the backed up pointers
  552. without changing the state. */
  553. wide_read_base = fp->_wide_data->_IO_save_base;
  554. wide_read_ptr = wide_read_base;
  555. wide_read_end = fp->_wide_data->_IO_save_end;
  556. }
  557. else
  558. {
  559. wide_read_base = fp->_wide_data->_IO_read_base;
  560. wide_read_ptr = fp->_wide_data->_IO_read_ptr;
  561. wide_read_end = fp->_wide_data->_IO_read_end;
  562. }
  563. struct _IO_codecvt *cv = fp->_codecvt;
  564. int clen = (*cv->__codecvt_do_encoding) (cv);
  565. if (!unflushed_writes)
  566. {
  567. if (clen > 0)
  568. {
  569. offset -= (wide_read_end - wide_read_ptr) * clen;
  570. offset -= fp->_IO_read_end - fp->_IO_read_ptr;
  571. }
  572. else
  573. {
  574. int nread;
  575. size_t delta = wide_read_ptr - wide_read_base;
  576. __mbstate_t state = fp->_wide_data->_IO_last_state;
  577. nread = (*cv->__codecvt_do_length) (cv, &state,
  578. fp->_IO_read_base,
  579. fp->_IO_read_end, delta);
  580. offset -= fp->_IO_read_end - fp->_IO_read_base - nread;
  581. }
  582. }
  583. else
  584. {
  585. if (clen > 0)
  586. offset += (fp->_wide_data->_IO_write_ptr
  587. - fp->_wide_data->_IO_write_base) * clen;
  588. else
  589. {
  590. size_t delta = (fp->_wide_data->_IO_write_ptr
  591. - fp->_wide_data->_IO_write_base);
  592. /* Allocate enough space for the conversion. */
  593. size_t outsize = delta * sizeof (wchar_t);
  594. char *out = malloc (outsize);
  595. char *outstop = out;
  596. const wchar_t *in = fp->_wide_data->_IO_write_base;
  597. enum __codecvt_result status;
  598. __mbstate_t state = fp->_wide_data->_IO_last_state;
  599. status = (*cv->__codecvt_do_out) (cv, &state,
  600. in, in + delta, &in,
  601. out, out + outsize, &outstop);
  602. /* We don't check for __codecvt_partial because it can be
  603. returned on one of two conditions: either the output
  604. buffer is full or the input sequence is incomplete. We
  605. take care to allocate enough buffer and our input
  606. sequences must be complete since they are accepted as
  607. wchar_t; if not, then that is an error. */
  608. if (__glibc_unlikely (status != __codecvt_ok))
  609. {
  610. free (out);
  611. return WEOF;
  612. }
  613. offset += outstop - out;
  614. free (out);
  615. }
  616. /* We don't trust _IO_read_end to represent the current file offset
  617. when writing in append mode because the value would have to be
  618. shifted to the end of the file during a flush. Use the write base
  619. instead, along with the new offset we got above when we did a seek
  620. to the end of the file. */
  621. if (append_mode)
  622. offset += fp->_IO_write_ptr - fp->_IO_write_base;
  623. /* For all other modes, _IO_read_end represents the file offset. */
  624. else
  625. offset += fp->_IO_write_ptr - fp->_IO_read_end;
  626. }
  627. }
  628. if (fp->_offset != _IO_pos_BAD)
  629. result = fp->_offset;
  630. else
  631. result = _IO_SYSSEEK (fp, 0, _IO_seek_cur);
  632. if (result == EOF)
  633. return result;
  634. result += offset;
  635. if (result < 0)
  636. {
  637. __set_errno (EINVAL);
  638. return EOF;
  639. }
  640. return result;
  641. }
  642. off64_t
  643. _IO_wfile_seekoff (FILE *fp, off64_t offset, int dir, int mode)
  644. {
  645. off64_t result;
  646. off64_t delta, new_offset;
  647. long int count;
  648. /* Short-circuit into a separate function. We don't want to mix any
  649. functionality and we don't want to touch anything inside the FILE
  650. object. */
  651. if (mode == 0)
  652. return do_ftell_wide (fp);
  653. /* POSIX.1 8.2.3.7 says that after a call the fflush() the file
  654. offset of the underlying file must be exact. */
  655. int must_be_exact = ((fp->_wide_data->_IO_read_base
  656. == fp->_wide_data->_IO_read_end)
  657. && (fp->_wide_data->_IO_write_base
  658. == fp->_wide_data->_IO_write_ptr));
  659. bool was_writing = ((fp->_wide_data->_IO_write_ptr
  660. > fp->_wide_data->_IO_write_base)
  661. || _IO_in_put_mode (fp));
  662. /* Flush unwritten characters.
  663. (This may do an unneeded write if we seek within the buffer.
  664. But to be able to switch to reading, we would need to set
  665. egptr to pptr. That can't be done in the current design,
  666. which assumes file_ptr() is eGptr. Anyway, since we probably
  667. end up flushing when we close(), it doesn't make much difference.)
  668. FIXME: simulate mem-mapped files. */
  669. if (was_writing && _IO_switch_to_wget_mode (fp))
  670. return WEOF;
  671. if (fp->_wide_data->_IO_buf_base == NULL)
  672. {
  673. /* It could be that we already have a pushback buffer. */
  674. if (fp->_wide_data->_IO_read_base != NULL)
  675. {
  676. free (fp->_wide_data->_IO_read_base);
  677. fp->_flags &= ~_IO_IN_BACKUP;
  678. }
  679. _IO_doallocbuf (fp);
  680. _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base);
  681. _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base, fp->_IO_buf_base);
  682. _IO_wsetp (fp, fp->_wide_data->_IO_buf_base,
  683. fp->_wide_data->_IO_buf_base);
  684. _IO_wsetg (fp, fp->_wide_data->_IO_buf_base,
  685. fp->_wide_data->_IO_buf_base, fp->_wide_data->_IO_buf_base);
  686. }
  687. switch (dir)
  688. {
  689. struct _IO_codecvt *cv;
  690. int clen;
  691. case _IO_seek_cur:
  692. /* Adjust for read-ahead (bytes is buffer). To do this we must
  693. find out which position in the external buffer corresponds to
  694. the current position in the internal buffer. */
  695. cv = fp->_codecvt;
  696. clen = (*cv->__codecvt_do_encoding) (cv);
  697. if (mode != 0 || !was_writing)
  698. {
  699. if (clen > 0)
  700. {
  701. offset -= (fp->_wide_data->_IO_read_end
  702. - fp->_wide_data->_IO_read_ptr) * clen;
  703. /* Adjust by readahead in external buffer. */
  704. offset -= fp->_IO_read_end - fp->_IO_read_ptr;
  705. }
  706. else
  707. {
  708. int nread;
  709. delta = (fp->_wide_data->_IO_read_ptr
  710. - fp->_wide_data->_IO_read_base);
  711. fp->_wide_data->_IO_state = fp->_wide_data->_IO_last_state;
  712. nread = (*cv->__codecvt_do_length) (cv,
  713. &fp->_wide_data->_IO_state,
  714. fp->_IO_read_base,
  715. fp->_IO_read_end, delta);
  716. fp->_IO_read_ptr = fp->_IO_read_base + nread;
  717. fp->_wide_data->_IO_read_end = fp->_wide_data->_IO_read_ptr;
  718. offset -= fp->_IO_read_end - fp->_IO_read_base - nread;
  719. }
  720. }
  721. if (fp->_offset == _IO_pos_BAD)
  722. goto dumb;
  723. /* Make offset absolute, assuming current pointer is file_ptr(). */
  724. offset += fp->_offset;
  725. dir = _IO_seek_set;
  726. break;
  727. case _IO_seek_set:
  728. break;
  729. case _IO_seek_end:
  730. {
  731. struct stat64 st;
  732. if (_IO_SYSSTAT (fp, &st) == 0 && S_ISREG (st.st_mode))
  733. {
  734. offset += st.st_size;
  735. dir = _IO_seek_set;
  736. }
  737. else
  738. goto dumb;
  739. }
  740. }
  741. _IO_free_wbackup_area (fp);
  742. /* At this point, dir==_IO_seek_set. */
  743. /* If destination is within current buffer, optimize: */
  744. if (fp->_offset != _IO_pos_BAD && fp->_IO_read_base != NULL
  745. && !_IO_in_backup (fp))
  746. {
  747. off64_t start_offset = (fp->_offset
  748. - (fp->_IO_read_end - fp->_IO_buf_base));
  749. if (offset >= start_offset && offset < fp->_offset)
  750. {
  751. _IO_setg (fp, fp->_IO_buf_base,
  752. fp->_IO_buf_base + (offset - start_offset),
  753. fp->_IO_read_end);
  754. _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base);
  755. _IO_wsetg (fp, fp->_wide_data->_IO_buf_base,
  756. fp->_wide_data->_IO_buf_base,
  757. fp->_wide_data->_IO_buf_base);
  758. _IO_wsetp (fp, fp->_wide_data->_IO_buf_base,
  759. fp->_wide_data->_IO_buf_base);
  760. if (adjust_wide_data (fp, false))
  761. goto dumb;
  762. _IO_mask_flags (fp, 0, _IO_EOF_SEEN);
  763. goto resync;
  764. }
  765. }
  766. if (fp->_flags & _IO_NO_READS)
  767. goto dumb;
  768. /* Try to seek to a block boundary, to improve kernel page management. */
  769. new_offset = offset & ~(fp->_IO_buf_end - fp->_IO_buf_base - 1);
  770. delta = offset - new_offset;
  771. if (delta > fp->_IO_buf_end - fp->_IO_buf_base)
  772. {
  773. new_offset = offset;
  774. delta = 0;
  775. }
  776. result = _IO_SYSSEEK (fp, new_offset, 0);
  777. if (result < 0)
  778. return EOF;
  779. if (delta == 0)
  780. count = 0;
  781. else
  782. {
  783. count = _IO_SYSREAD (fp, fp->_IO_buf_base,
  784. (must_be_exact
  785. ? delta : fp->_IO_buf_end - fp->_IO_buf_base));
  786. if (count < delta)
  787. {
  788. /* We weren't allowed to read, but try to seek the remainder. */
  789. offset = count == EOF ? delta : delta-count;
  790. dir = _IO_seek_cur;
  791. goto dumb;
  792. }
  793. }
  794. _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base + delta,
  795. fp->_IO_buf_base + count);
  796. _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base);
  797. _IO_wsetg (fp, fp->_wide_data->_IO_buf_base,
  798. fp->_wide_data->_IO_buf_base, fp->_wide_data->_IO_buf_base);
  799. _IO_wsetp (fp, fp->_wide_data->_IO_buf_base, fp->_wide_data->_IO_buf_base);
  800. if (adjust_wide_data (fp, true))
  801. goto dumb;
  802. fp->_offset = result + count;
  803. _IO_mask_flags (fp, 0, _IO_EOF_SEEN);
  804. return offset;
  805. dumb:
  806. _IO_unsave_markers (fp);
  807. result = _IO_SYSSEEK (fp, offset, dir);
  808. if (result != EOF)
  809. {
  810. _IO_mask_flags (fp, 0, _IO_EOF_SEEN);
  811. fp->_offset = result;
  812. _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base, fp->_IO_buf_base);
  813. _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base);
  814. _IO_wsetg (fp, fp->_wide_data->_IO_buf_base,
  815. fp->_wide_data->_IO_buf_base, fp->_wide_data->_IO_buf_base);
  816. _IO_wsetp (fp, fp->_wide_data->_IO_buf_base,
  817. fp->_wide_data->_IO_buf_base);
  818. }
  819. return result;
  820. resync:
  821. /* We need to do it since it is possible that the file offset in
  822. the kernel may be changed behind our back. It may happen when
  823. we fopen a file and then do a fork. One process may access the
  824. file and the kernel file offset will be changed. */
  825. if (fp->_offset >= 0)
  826. _IO_SYSSEEK (fp, fp->_offset, 0);
  827. return offset;
  828. }
  829. libc_hidden_def (_IO_wfile_seekoff)
  830. size_t
  831. _IO_wfile_xsputn (FILE *f, const void *data, size_t n)
  832. {
  833. const wchar_t *s = (const wchar_t *) data;
  834. size_t to_do = n;
  835. int must_flush = 0;
  836. size_t count;
  837. if (n <= 0)
  838. return 0;
  839. /* This is an optimized implementation.
  840. If the amount to be written straddles a block boundary
  841. (or the filebuf is unbuffered), use sys_write directly. */
  842. /* First figure out how much space is available in the buffer. */
  843. count = f->_wide_data->_IO_write_end - f->_wide_data->_IO_write_ptr;
  844. if ((f->_flags & _IO_LINE_BUF) && (f->_flags & _IO_CURRENTLY_PUTTING))
  845. {
  846. count = f->_wide_data->_IO_buf_end - f->_wide_data->_IO_write_ptr;
  847. if (count >= n)
  848. {
  849. const wchar_t *p;
  850. for (p = s + n; p > s; )
  851. {
  852. if (*--p == L'\n')
  853. {
  854. count = p - s + 1;
  855. must_flush = 1;
  856. break;
  857. }
  858. }
  859. }
  860. }
  861. /* Then fill the buffer. */
  862. if (count > 0)
  863. {
  864. if (count > to_do)
  865. count = to_do;
  866. if (count > 20)
  867. {
  868. f->_wide_data->_IO_write_ptr =
  869. __wmempcpy (f->_wide_data->_IO_write_ptr, s, count);
  870. s += count;
  871. }
  872. else
  873. {
  874. wchar_t *p = f->_wide_data->_IO_write_ptr;
  875. int i = (int) count;
  876. while (--i >= 0)
  877. *p++ = *s++;
  878. f->_wide_data->_IO_write_ptr = p;
  879. }
  880. to_do -= count;
  881. }
  882. if (to_do > 0)
  883. to_do -= _IO_wdefault_xsputn (f, s, to_do);
  884. if (must_flush
  885. && f->_wide_data->_IO_write_ptr != f->_wide_data->_IO_write_base)
  886. _IO_wdo_write (f, f->_wide_data->_IO_write_base,
  887. f->_wide_data->_IO_write_ptr
  888. - f->_wide_data->_IO_write_base);
  889. return n - to_do;
  890. }
  891. libc_hidden_def (_IO_wfile_xsputn)
  892. const struct _IO_jump_t _IO_wfile_jumps libio_vtable =
  893. {
  894. JUMP_INIT_DUMMY,
  895. JUMP_INIT(finish, _IO_new_file_finish),
  896. JUMP_INIT(overflow, (_IO_overflow_t) _IO_wfile_overflow),
  897. JUMP_INIT(underflow, (_IO_underflow_t) _IO_wfile_underflow),
  898. JUMP_INIT(uflow, (_IO_underflow_t) _IO_wdefault_uflow),
  899. JUMP_INIT(pbackfail, (_IO_pbackfail_t) _IO_wdefault_pbackfail),
  900. JUMP_INIT(xsputn, _IO_wfile_xsputn),
  901. JUMP_INIT(xsgetn, _IO_file_xsgetn),
  902. JUMP_INIT(seekoff, _IO_wfile_seekoff),
  903. JUMP_INIT(seekpos, _IO_default_seekpos),
  904. JUMP_INIT(setbuf, _IO_new_file_setbuf),
  905. JUMP_INIT(sync, (_IO_sync_t) _IO_wfile_sync),
  906. JUMP_INIT(doallocate, _IO_wfile_doallocate),
  907. JUMP_INIT(read, _IO_file_read),
  908. JUMP_INIT(write, _IO_new_file_write),
  909. JUMP_INIT(seek, _IO_file_seek),
  910. JUMP_INIT(close, _IO_file_close),
  911. JUMP_INIT(stat, _IO_file_stat),
  912. JUMP_INIT(showmanyc, _IO_default_showmanyc),
  913. JUMP_INIT(imbue, _IO_default_imbue)
  914. };
  915. libc_hidden_data_def (_IO_wfile_jumps)
  916. const struct _IO_jump_t _IO_wfile_jumps_mmap libio_vtable =
  917. {
  918. JUMP_INIT_DUMMY,
  919. JUMP_INIT(finish, _IO_new_file_finish),
  920. JUMP_INIT(overflow, (_IO_overflow_t) _IO_wfile_overflow),
  921. JUMP_INIT(underflow, (_IO_underflow_t) _IO_wfile_underflow_mmap),
  922. JUMP_INIT(uflow, (_IO_underflow_t) _IO_wdefault_uflow),
  923. JUMP_INIT(pbackfail, (_IO_pbackfail_t) _IO_wdefault_pbackfail),
  924. JUMP_INIT(xsputn, _IO_wfile_xsputn),
  925. JUMP_INIT(xsgetn, _IO_file_xsgetn),
  926. JUMP_INIT(seekoff, _IO_wfile_seekoff),
  927. JUMP_INIT(seekpos, _IO_default_seekpos),
  928. JUMP_INIT(setbuf, _IO_file_setbuf_mmap),
  929. JUMP_INIT(sync, (_IO_sync_t) _IO_wfile_sync),
  930. JUMP_INIT(doallocate, _IO_wfile_doallocate),
  931. JUMP_INIT(read, _IO_file_read),
  932. JUMP_INIT(write, _IO_new_file_write),
  933. JUMP_INIT(seek, _IO_file_seek),
  934. JUMP_INIT(close, _IO_file_close_mmap),
  935. JUMP_INIT(stat, _IO_file_stat),
  936. JUMP_INIT(showmanyc, _IO_default_showmanyc),
  937. JUMP_INIT(imbue, _IO_default_imbue)
  938. };
  939. const struct _IO_jump_t _IO_wfile_jumps_maybe_mmap libio_vtable =
  940. {
  941. JUMP_INIT_DUMMY,
  942. JUMP_INIT(finish, _IO_new_file_finish),
  943. JUMP_INIT(overflow, (_IO_overflow_t) _IO_wfile_overflow),
  944. JUMP_INIT(underflow, (_IO_underflow_t) _IO_wfile_underflow_maybe_mmap),
  945. JUMP_INIT(uflow, (_IO_underflow_t) _IO_wdefault_uflow),
  946. JUMP_INIT(pbackfail, (_IO_pbackfail_t) _IO_wdefault_pbackfail),
  947. JUMP_INIT(xsputn, _IO_wfile_xsputn),
  948. JUMP_INIT(xsgetn, _IO_file_xsgetn),
  949. JUMP_INIT(seekoff, _IO_wfile_seekoff),
  950. JUMP_INIT(seekpos, _IO_default_seekpos),
  951. JUMP_INIT(setbuf, _IO_file_setbuf_mmap),
  952. JUMP_INIT(sync, (_IO_sync_t) _IO_wfile_sync),
  953. JUMP_INIT(doallocate, _IO_wfile_doallocate),
  954. JUMP_INIT(read, _IO_file_read),
  955. JUMP_INIT(write, _IO_new_file_write),
  956. JUMP_INIT(seek, _IO_file_seek),
  957. JUMP_INIT(close, _IO_file_close),
  958. JUMP_INIT(stat, _IO_file_stat),
  959. JUMP_INIT(showmanyc, _IO_default_showmanyc),
  960. JUMP_INIT(imbue, _IO_default_imbue)
  961. };