wfileops.c 30 KB

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