genops.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  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. /* Generic or default I/O operations. */
  24. #include "libioP.h"
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <stdbool.h>
  28. #include <sched.h>
  29. #ifdef _IO_MTSAFE_IO
  30. static _IO_lock_t list_all_lock = _IO_lock_initializer;
  31. #endif
  32. static FILE *run_fp;
  33. #ifdef _IO_MTSAFE_IO
  34. static void
  35. flush_cleanup (void *not_used)
  36. {
  37. if (run_fp != NULL)
  38. _IO_funlockfile (run_fp);
  39. _IO_lock_unlock (list_all_lock);
  40. }
  41. #endif
  42. /* Fields in struct _IO_FILE after the _lock field are internal to
  43. glibc and opaque to applications. We can change them as long as
  44. the size of struct _IO_FILE is unchanged, which is checked as the
  45. part of glibc ABI with sizes of _IO_2_1_stdin_, _IO_2_1_stdout_
  46. and _IO_2_1_stderr_.
  47. NB: When _IO_vtable_offset (fp) == 0, copy relocation will cover the
  48. whole struct _IO_FILE. Otherwise, only fields up to the _lock field
  49. will be copied. */
  50. _Static_assert (offsetof (struct _IO_FILE, _prevchain)
  51. > offsetof (struct _IO_FILE, _lock),
  52. "offset of _prevchain > offset of _lock");
  53. void
  54. _IO_un_link (struct _IO_FILE_plus *fp)
  55. {
  56. if (fp->file._flags & _IO_LINKED)
  57. {
  58. FILE **f;
  59. #ifdef _IO_MTSAFE_IO
  60. _IO_cleanup_region_start_noarg (flush_cleanup);
  61. _IO_lock_lock (list_all_lock);
  62. run_fp = (FILE *) fp;
  63. _IO_flockfile ((FILE *) fp);
  64. #endif
  65. if (_IO_list_all == NULL)
  66. ;
  67. else if (_IO_vtable_offset ((FILE *) fp) == 0)
  68. {
  69. FILE **pr = fp->file._prevchain;
  70. FILE *nx = fp->file._chain;
  71. *pr = nx;
  72. if (nx != NULL)
  73. nx->_prevchain = pr;
  74. }
  75. else if (fp == _IO_list_all)
  76. _IO_list_all = (struct _IO_FILE_plus *) _IO_list_all->file._chain;
  77. else
  78. for (f = &_IO_list_all->file._chain; *f; f = &(*f)->_chain)
  79. if (*f == (FILE *) fp)
  80. {
  81. *f = fp->file._chain;
  82. break;
  83. }
  84. fp->file._flags &= ~_IO_LINKED;
  85. #ifdef _IO_MTSAFE_IO
  86. _IO_funlockfile ((FILE *) fp);
  87. run_fp = NULL;
  88. _IO_lock_unlock (list_all_lock);
  89. _IO_cleanup_region_end (0);
  90. #endif
  91. }
  92. }
  93. libc_hidden_def (_IO_un_link)
  94. void
  95. _IO_link_in (struct _IO_FILE_plus *fp)
  96. {
  97. if ((fp->file._flags & _IO_LINKED) == 0)
  98. {
  99. fp->file._flags |= _IO_LINKED;
  100. #ifdef _IO_MTSAFE_IO
  101. _IO_cleanup_region_start_noarg (flush_cleanup);
  102. _IO_lock_lock (list_all_lock);
  103. run_fp = (FILE *) fp;
  104. _IO_flockfile ((FILE *) fp);
  105. #endif
  106. fp->file._chain = (FILE *) _IO_list_all;
  107. if (_IO_vtable_offset ((FILE *) fp) == 0)
  108. {
  109. fp->file._prevchain = (FILE **) &_IO_list_all;
  110. if (_IO_list_all != NULL)
  111. _IO_list_all->file._prevchain = &fp->file._chain;
  112. }
  113. _IO_list_all = fp;
  114. #ifdef _IO_MTSAFE_IO
  115. _IO_funlockfile ((FILE *) fp);
  116. run_fp = NULL;
  117. _IO_lock_unlock (list_all_lock);
  118. _IO_cleanup_region_end (0);
  119. #endif
  120. }
  121. }
  122. libc_hidden_def (_IO_link_in)
  123. /* Return minimum _pos markers
  124. Assumes the current get area is the main get area. */
  125. static ssize_t
  126. _IO_least_marker (FILE *fp, char *end_p)
  127. {
  128. ssize_t least_so_far = end_p - fp->_IO_read_base;
  129. struct _IO_marker *mark;
  130. for (mark = fp->_markers; mark != NULL; mark = mark->_next)
  131. if (mark->_pos < least_so_far)
  132. least_so_far = mark->_pos;
  133. return least_so_far;
  134. }
  135. /* Switch current get area from backup buffer to (start of) main get area. */
  136. void
  137. _IO_switch_to_main_get_area (FILE *fp)
  138. {
  139. char *tmp;
  140. fp->_flags &= ~_IO_IN_BACKUP;
  141. /* Swap _IO_read_end and _IO_save_end. */
  142. tmp = fp->_IO_read_end;
  143. fp->_IO_read_end = fp->_IO_save_end;
  144. fp->_IO_save_end= tmp;
  145. /* Swap _IO_read_base and _IO_save_base. */
  146. tmp = fp->_IO_read_base;
  147. fp->_IO_read_base = fp->_IO_save_base;
  148. fp->_IO_save_base = tmp;
  149. /* Set _IO_read_ptr. */
  150. fp->_IO_read_ptr = fp->_IO_read_base;
  151. }
  152. /* Switch current get area from main get area to (end of) backup area. */
  153. void
  154. _IO_switch_to_backup_area (FILE *fp)
  155. {
  156. char *tmp;
  157. fp->_flags |= _IO_IN_BACKUP;
  158. /* Swap _IO_read_end and _IO_save_end. */
  159. tmp = fp->_IO_read_end;
  160. fp->_IO_read_end = fp->_IO_save_end;
  161. fp->_IO_save_end = tmp;
  162. /* Swap _IO_read_base and _IO_save_base. */
  163. tmp = fp->_IO_read_base;
  164. fp->_IO_read_base = fp->_IO_save_base;
  165. fp->_IO_save_base = tmp;
  166. /* Set _IO_read_ptr. */
  167. fp->_IO_read_ptr = fp->_IO_read_end;
  168. }
  169. int
  170. _IO_switch_to_get_mode (FILE *fp)
  171. {
  172. if (fp->_IO_write_ptr > fp->_IO_write_base)
  173. if (_IO_OVERFLOW (fp, EOF) == EOF)
  174. return EOF;
  175. if (_IO_in_backup (fp))
  176. fp->_IO_read_base = fp->_IO_backup_base;
  177. else
  178. {
  179. fp->_IO_read_base = fp->_IO_buf_base;
  180. if (fp->_IO_write_ptr > fp->_IO_read_end)
  181. fp->_IO_read_end = fp->_IO_write_ptr;
  182. }
  183. fp->_IO_read_ptr = fp->_IO_write_ptr;
  184. fp->_IO_write_base = fp->_IO_write_ptr = fp->_IO_write_end = fp->_IO_read_ptr;
  185. fp->_flags &= ~_IO_CURRENTLY_PUTTING;
  186. return 0;
  187. }
  188. libc_hidden_def (_IO_switch_to_get_mode)
  189. void
  190. _IO_free_backup_area (FILE *fp)
  191. {
  192. if (_IO_in_backup (fp))
  193. _IO_switch_to_main_get_area (fp); /* Just in case. */
  194. _IO_free_backup_buf (fp, fp->_IO_save_base);
  195. fp->_IO_save_base = NULL;
  196. fp->_IO_save_end = NULL;
  197. fp->_IO_backup_base = NULL;
  198. }
  199. libc_hidden_def (_IO_free_backup_area)
  200. int
  201. __overflow (FILE *f, int ch)
  202. {
  203. /* This is a single-byte stream. */
  204. if (f->_mode == 0)
  205. _IO_fwide (f, -1);
  206. return _IO_OVERFLOW (f, ch);
  207. }
  208. libc_hidden_def (__overflow)
  209. static int
  210. save_for_backup (FILE *fp, char *end_p)
  211. {
  212. /* Append [_IO_read_base..end_p] to backup area. */
  213. ssize_t least_mark = _IO_least_marker (fp, end_p);
  214. /* needed_size is how much space we need in the backup area. */
  215. size_t needed_size = (end_p - fp->_IO_read_base) - least_mark;
  216. /* FIXME: Dubious arithmetic if pointers are NULL */
  217. size_t current_Bsize = fp->_IO_save_end - fp->_IO_save_base;
  218. size_t avail; /* Extra space available for future expansion. */
  219. ssize_t delta;
  220. struct _IO_marker *mark;
  221. if (needed_size > current_Bsize)
  222. {
  223. char *new_buffer;
  224. avail = 100;
  225. new_buffer = (char *) malloc (avail + needed_size);
  226. if (new_buffer == NULL)
  227. return EOF; /* FIXME */
  228. if (least_mark < 0)
  229. {
  230. __mempcpy (__mempcpy (new_buffer + avail,
  231. fp->_IO_save_end + least_mark,
  232. -least_mark),
  233. fp->_IO_read_base,
  234. end_p - fp->_IO_read_base);
  235. }
  236. else
  237. memcpy (new_buffer + avail,
  238. fp->_IO_read_base + least_mark,
  239. needed_size);
  240. _IO_free_backup_buf (fp, fp->_IO_save_base);
  241. fp->_IO_save_base = new_buffer;
  242. fp->_IO_save_end = new_buffer + avail + needed_size;
  243. }
  244. else
  245. {
  246. avail = current_Bsize - needed_size;
  247. if (least_mark < 0)
  248. {
  249. memmove (fp->_IO_save_base + avail,
  250. fp->_IO_save_end + least_mark,
  251. -least_mark);
  252. memcpy (fp->_IO_save_base + avail - least_mark,
  253. fp->_IO_read_base,
  254. end_p - fp->_IO_read_base);
  255. }
  256. else if (needed_size > 0)
  257. memcpy (fp->_IO_save_base + avail,
  258. fp->_IO_read_base + least_mark,
  259. needed_size);
  260. }
  261. fp->_IO_backup_base = fp->_IO_save_base + avail;
  262. /* Adjust all the streammarkers. */
  263. delta = end_p - fp->_IO_read_base;
  264. for (mark = fp->_markers; mark != NULL; mark = mark->_next)
  265. mark->_pos -= delta;
  266. return 0;
  267. }
  268. int
  269. __underflow (FILE *fp)
  270. {
  271. if (_IO_vtable_offset (fp) == 0 && _IO_fwide (fp, -1) != -1)
  272. return EOF;
  273. if (fp->_mode == 0)
  274. _IO_fwide (fp, -1);
  275. if (_IO_in_put_mode (fp))
  276. if (_IO_switch_to_get_mode (fp) == EOF)
  277. return EOF;
  278. if (fp->_IO_read_ptr < fp->_IO_read_end)
  279. return *(unsigned char *) fp->_IO_read_ptr;
  280. if (_IO_in_backup (fp))
  281. {
  282. _IO_switch_to_main_get_area (fp);
  283. if (fp->_IO_read_ptr < fp->_IO_read_end)
  284. return *(unsigned char *) fp->_IO_read_ptr;
  285. }
  286. if (_IO_have_markers (fp))
  287. {
  288. if (save_for_backup (fp, fp->_IO_read_end))
  289. return EOF;
  290. }
  291. else if (_IO_have_backup (fp))
  292. _IO_free_backup_area (fp);
  293. return _IO_UNDERFLOW (fp);
  294. }
  295. libc_hidden_def (__underflow)
  296. int
  297. __uflow (FILE *fp)
  298. {
  299. if (_IO_vtable_offset (fp) == 0 && _IO_fwide (fp, -1) != -1)
  300. return EOF;
  301. if (fp->_mode == 0)
  302. _IO_fwide (fp, -1);
  303. if (_IO_in_put_mode (fp))
  304. if (_IO_switch_to_get_mode (fp) == EOF)
  305. return EOF;
  306. if (fp->_IO_read_ptr < fp->_IO_read_end)
  307. return *(unsigned char *) fp->_IO_read_ptr++;
  308. if (_IO_in_backup (fp))
  309. {
  310. _IO_switch_to_main_get_area (fp);
  311. if (fp->_IO_read_ptr < fp->_IO_read_end)
  312. return *(unsigned char *) fp->_IO_read_ptr++;
  313. }
  314. if (_IO_have_markers (fp))
  315. {
  316. if (save_for_backup (fp, fp->_IO_read_end))
  317. return EOF;
  318. }
  319. else if (_IO_have_backup (fp))
  320. _IO_free_backup_area (fp);
  321. return _IO_UFLOW (fp);
  322. }
  323. libc_hidden_def (__uflow)
  324. void
  325. _IO_setb (FILE *f, char *b, char *eb, int a)
  326. {
  327. if (f->_IO_buf_base && !(f->_flags & _IO_USER_BUF))
  328. free (f->_IO_buf_base);
  329. f->_IO_buf_base = b;
  330. f->_IO_buf_end = eb;
  331. if (a)
  332. f->_flags &= ~_IO_USER_BUF;
  333. else
  334. f->_flags |= _IO_USER_BUF;
  335. }
  336. libc_hidden_def (_IO_setb)
  337. void
  338. _IO_doallocbuf (FILE *fp)
  339. {
  340. if (fp->_IO_buf_base)
  341. return;
  342. if (!(fp->_flags & _IO_UNBUFFERED) || fp->_mode > 0)
  343. if (_IO_DOALLOCATE (fp) != EOF)
  344. return;
  345. _IO_setb (fp, fp->_shortbuf, fp->_shortbuf+1, 0);
  346. }
  347. libc_hidden_def (_IO_doallocbuf)
  348. int
  349. _IO_default_underflow (FILE *fp)
  350. {
  351. return EOF;
  352. }
  353. int
  354. _IO_default_uflow (FILE *fp)
  355. {
  356. int ch = _IO_UNDERFLOW (fp);
  357. if (ch == EOF)
  358. return EOF;
  359. return *(unsigned char *) fp->_IO_read_ptr++;
  360. }
  361. libc_hidden_def (_IO_default_uflow)
  362. size_t
  363. _IO_default_xsputn (FILE *f, const void *data, size_t n)
  364. {
  365. const char *s = (char *) data;
  366. size_t more = n;
  367. if (more <= 0)
  368. return 0;
  369. for (;;)
  370. {
  371. /* Space available. */
  372. if (f->_IO_write_ptr < f->_IO_write_end)
  373. {
  374. size_t count = f->_IO_write_end - f->_IO_write_ptr;
  375. if (count > more)
  376. count = more;
  377. if (count > 20)
  378. {
  379. f->_IO_write_ptr = __mempcpy (f->_IO_write_ptr, s, count);
  380. s += count;
  381. }
  382. else if (count)
  383. {
  384. char *p = f->_IO_write_ptr;
  385. ssize_t i;
  386. for (i = count; --i >= 0; )
  387. *p++ = *s++;
  388. f->_IO_write_ptr = p;
  389. }
  390. more -= count;
  391. }
  392. if (more == 0 || _IO_OVERFLOW (f, (unsigned char) *s++) == EOF)
  393. break;
  394. more--;
  395. }
  396. return n - more;
  397. }
  398. libc_hidden_def (_IO_default_xsputn)
  399. size_t
  400. _IO_sgetn (FILE *fp, void *data, size_t n)
  401. {
  402. /* FIXME handle putback buffer here! */
  403. return _IO_XSGETN (fp, data, n);
  404. }
  405. libc_hidden_def (_IO_sgetn)
  406. size_t
  407. _IO_default_xsgetn (FILE *fp, void *data, size_t n)
  408. {
  409. size_t more = n;
  410. char *s = (char*) data;
  411. for (;;)
  412. {
  413. /* Data available. */
  414. if (fp->_IO_read_ptr < fp->_IO_read_end)
  415. {
  416. size_t count = fp->_IO_read_end - fp->_IO_read_ptr;
  417. if (count > more)
  418. count = more;
  419. if (count > 20)
  420. {
  421. s = __mempcpy (s, fp->_IO_read_ptr, count);
  422. fp->_IO_read_ptr += count;
  423. }
  424. else if (count)
  425. {
  426. char *p = fp->_IO_read_ptr;
  427. int i = (int) count;
  428. while (--i >= 0)
  429. *s++ = *p++;
  430. fp->_IO_read_ptr = p;
  431. }
  432. more -= count;
  433. }
  434. if (more == 0 || __underflow (fp) == EOF)
  435. break;
  436. }
  437. return n - more;
  438. }
  439. libc_hidden_def (_IO_default_xsgetn)
  440. FILE *
  441. _IO_default_setbuf (FILE *fp, char *p, ssize_t len)
  442. {
  443. if (_IO_SYNC (fp) == EOF)
  444. return NULL;
  445. if (p == NULL || len == 0)
  446. {
  447. fp->_flags |= _IO_UNBUFFERED;
  448. _IO_setb (fp, fp->_shortbuf, fp->_shortbuf+1, 0);
  449. }
  450. else
  451. {
  452. fp->_flags &= ~_IO_UNBUFFERED;
  453. _IO_setb (fp, p, p+len, 0);
  454. }
  455. fp->_IO_write_base = fp->_IO_write_ptr = fp->_IO_write_end = NULL;
  456. fp->_IO_read_base = fp->_IO_read_ptr = fp->_IO_read_end = NULL;
  457. return fp;
  458. }
  459. off64_t
  460. _IO_default_seekpos (FILE *fp, off64_t pos, int mode)
  461. {
  462. return _IO_SEEKOFF (fp, pos, 0, mode);
  463. }
  464. int
  465. _IO_default_doallocate (FILE *fp)
  466. {
  467. char *buf;
  468. buf = malloc(BUFSIZ);
  469. if (__glibc_unlikely (buf == NULL))
  470. return EOF;
  471. _IO_setb (fp, buf, buf+BUFSIZ, 1);
  472. return 1;
  473. }
  474. libc_hidden_def (_IO_default_doallocate)
  475. void
  476. _IO_init_internal (FILE *fp, int flags)
  477. {
  478. _IO_no_init (fp, flags, -1, NULL, NULL);
  479. }
  480. void
  481. _IO_init (FILE *fp, int flags)
  482. {
  483. IO_set_accept_foreign_vtables (&_IO_vtable_check);
  484. _IO_init_internal (fp, flags);
  485. }
  486. static int stdio_needs_locking;
  487. /* In a single-threaded process most stdio locks can be omitted. After
  488. _IO_enable_locks is called, locks are not optimized away any more.
  489. It must be first called while the process is still single-threaded.
  490. This lock optimization can be disabled on a per-file basis by setting
  491. _IO_FLAGS2_NEED_LOCK, because a file can have user-defined callbacks
  492. or can be locked with flockfile and then a thread may be created
  493. between a lock and unlock, so omitting the lock is not valid.
  494. Here we have to make sure that the flag is set on all existing files
  495. and files created later. */
  496. void
  497. _IO_enable_locks (void)
  498. {
  499. _IO_ITER i;
  500. if (stdio_needs_locking)
  501. return;
  502. stdio_needs_locking = 1;
  503. for (i = _IO_iter_begin (); i != _IO_iter_end (); i = _IO_iter_next (i))
  504. _IO_iter_file (i)->_flags2 |= _IO_FLAGS2_NEED_LOCK;
  505. }
  506. libc_hidden_def (_IO_enable_locks)
  507. void
  508. _IO_old_init (FILE *fp, int flags)
  509. {
  510. fp->_flags = _IO_MAGIC|flags;
  511. fp->_flags2 = 0;
  512. if (stdio_needs_locking)
  513. fp->_flags2 |= _IO_FLAGS2_NEED_LOCK;
  514. fp->_IO_buf_base = NULL;
  515. fp->_IO_buf_end = NULL;
  516. fp->_IO_read_base = NULL;
  517. fp->_IO_read_ptr = NULL;
  518. fp->_IO_read_end = NULL;
  519. fp->_IO_write_base = NULL;
  520. fp->_IO_write_ptr = NULL;
  521. fp->_IO_write_end = NULL;
  522. fp->_chain = NULL; /* Not necessary. */
  523. fp->_IO_save_base = NULL;
  524. fp->_IO_backup_base = NULL;
  525. fp->_IO_save_end = NULL;
  526. fp->_markers = NULL;
  527. fp->_cur_column = 0;
  528. #if _IO_JUMPS_OFFSET
  529. fp->_vtable_offset = 0;
  530. #endif
  531. #ifdef _IO_MTSAFE_IO
  532. if (fp->_lock != NULL)
  533. _IO_lock_init (*fp->_lock);
  534. #endif
  535. }
  536. void
  537. _IO_no_init (FILE *fp, int flags, int orientation,
  538. struct _IO_wide_data *wd, const struct _IO_jump_t *jmp)
  539. {
  540. _IO_old_init (fp, flags);
  541. fp->_mode = orientation;
  542. if (orientation >= 0)
  543. {
  544. fp->_wide_data = wd;
  545. fp->_wide_data->_IO_buf_base = NULL;
  546. fp->_wide_data->_IO_buf_end = NULL;
  547. fp->_wide_data->_IO_read_base = NULL;
  548. fp->_wide_data->_IO_read_ptr = NULL;
  549. fp->_wide_data->_IO_read_end = NULL;
  550. fp->_wide_data->_IO_write_base = NULL;
  551. fp->_wide_data->_IO_write_ptr = NULL;
  552. fp->_wide_data->_IO_write_end = NULL;
  553. fp->_wide_data->_IO_save_base = NULL;
  554. fp->_wide_data->_IO_backup_base = NULL;
  555. fp->_wide_data->_IO_save_end = NULL;
  556. fp->_wide_data->_wide_vtable = jmp;
  557. }
  558. else
  559. /* Cause predictable crash when a wide function is called on a byte
  560. stream. */
  561. fp->_wide_data = (struct _IO_wide_data *) -1L;
  562. fp->_freeres_list = NULL;
  563. fp->_total_written = 0;
  564. }
  565. int
  566. _IO_default_sync (FILE *fp)
  567. {
  568. return 0;
  569. }
  570. /* The way the C++ classes are mapped into the C functions in the
  571. current implementation, this function can get called twice! */
  572. void
  573. _IO_default_finish (FILE *fp, int dummy)
  574. {
  575. struct _IO_marker *mark;
  576. if (fp->_IO_buf_base && !(fp->_flags & _IO_USER_BUF))
  577. {
  578. free (fp->_IO_buf_base);
  579. fp->_IO_buf_base = fp->_IO_buf_end = NULL;
  580. }
  581. for (mark = fp->_markers; mark != NULL; mark = mark->_next)
  582. mark->_sbuf = NULL;
  583. if (fp->_IO_save_base)
  584. {
  585. _IO_free_backup_buf (fp, fp->_IO_save_base);
  586. fp->_IO_save_base = NULL;
  587. }
  588. _IO_un_link ((struct _IO_FILE_plus *) fp);
  589. #ifdef _IO_MTSAFE_IO
  590. if (fp->_lock != NULL)
  591. _IO_lock_fini (*fp->_lock);
  592. #endif
  593. }
  594. libc_hidden_def (_IO_default_finish)
  595. off64_t
  596. _IO_default_seekoff (FILE *fp, off64_t offset, int dir, int mode)
  597. {
  598. return _IO_pos_BAD;
  599. }
  600. int
  601. _IO_sputbackc (FILE *fp, int c)
  602. {
  603. int result;
  604. if (fp->_IO_read_ptr > fp->_IO_read_base && !_IO_in_backup (fp)
  605. && (unsigned char)fp->_IO_read_ptr[-1] == (unsigned char)c)
  606. {
  607. fp->_IO_read_ptr--;
  608. result = (unsigned char) c;
  609. }
  610. else
  611. result = _IO_PBACKFAIL (fp, c);
  612. if (result != EOF)
  613. fp->_flags &= ~_IO_EOF_SEEN;
  614. return result;
  615. }
  616. libc_hidden_def (_IO_sputbackc)
  617. int
  618. _IO_sungetc (FILE *fp)
  619. {
  620. int result;
  621. if (fp->_IO_read_ptr > fp->_IO_read_base)
  622. {
  623. fp->_IO_read_ptr--;
  624. result = (unsigned char) *fp->_IO_read_ptr;
  625. }
  626. else
  627. result = _IO_PBACKFAIL (fp, EOF);
  628. if (result != EOF)
  629. fp->_flags &= ~_IO_EOF_SEEN;
  630. return result;
  631. }
  632. unsigned
  633. _IO_adjust_column (unsigned start, const char *line, int count)
  634. {
  635. const char *ptr = line + count;
  636. while (ptr > line)
  637. if (*--ptr == '\n')
  638. return line + count - ptr - 1;
  639. return start + count;
  640. }
  641. libc_hidden_def (_IO_adjust_column)
  642. int
  643. _IO_flush_all (void)
  644. {
  645. int result = 0;
  646. FILE *fp;
  647. #ifdef _IO_MTSAFE_IO
  648. _IO_cleanup_region_start_noarg (flush_cleanup);
  649. _IO_lock_lock (list_all_lock);
  650. #endif
  651. for (fp = (FILE *) _IO_list_all; fp != NULL; fp = fp->_chain)
  652. {
  653. run_fp = fp;
  654. _IO_flockfile (fp);
  655. if (((fp->_mode <= 0 && fp->_IO_write_ptr > fp->_IO_write_base)
  656. || (_IO_vtable_offset (fp) == 0
  657. && fp->_mode > 0 && (fp->_wide_data->_IO_write_ptr
  658. > fp->_wide_data->_IO_write_base))
  659. )
  660. && _IO_OVERFLOW (fp, EOF) == EOF)
  661. result = EOF;
  662. if (_IO_fileno (fp) >= 0
  663. && ((fp->_mode <= 0 && fp->_IO_read_ptr < fp->_IO_read_end)
  664. || (_IO_vtable_offset (fp) == 0
  665. && fp->_mode > 0 && (fp->_wide_data->_IO_read_ptr
  666. < fp->_wide_data->_IO_read_end)))
  667. && _IO_SYNC (fp) != 0)
  668. result = EOF;
  669. _IO_funlockfile (fp);
  670. run_fp = NULL;
  671. }
  672. #ifdef _IO_MTSAFE_IO
  673. _IO_lock_unlock (list_all_lock);
  674. _IO_cleanup_region_end (0);
  675. #endif
  676. return result;
  677. }
  678. libc_hidden_def (_IO_flush_all)
  679. void
  680. _IO_flush_all_linebuffered (void)
  681. {
  682. FILE *fp;
  683. #ifdef _IO_MTSAFE_IO
  684. _IO_cleanup_region_start_noarg (flush_cleanup);
  685. _IO_lock_lock (list_all_lock);
  686. #endif
  687. for (fp = (FILE *) _IO_list_all; fp != NULL; fp = fp->_chain)
  688. {
  689. run_fp = fp;
  690. _IO_flockfile (fp);
  691. if ((fp->_flags & _IO_NO_WRITES) == 0 && fp->_flags & _IO_LINE_BUF)
  692. _IO_OVERFLOW (fp, EOF);
  693. _IO_funlockfile (fp);
  694. run_fp = NULL;
  695. }
  696. #ifdef _IO_MTSAFE_IO
  697. _IO_lock_unlock (list_all_lock);
  698. _IO_cleanup_region_end (0);
  699. #endif
  700. }
  701. libc_hidden_def (_IO_flush_all_linebuffered)
  702. weak_alias (_IO_flush_all_linebuffered, _flushlbf)
  703. /* The following is a bit tricky. In general, we want to unbuffer the
  704. streams so that all output which follows is seen. If we are not
  705. looking for memory leaks it does not make much sense to free the
  706. actual buffer because this will happen anyway once the program
  707. terminated. If we do want to look for memory leaks we have to free
  708. the buffers. Whether something is freed is determined by the
  709. function called by __libc_freeres (those are not called as part of
  710. the atexit routine, different from _IO_cleanup). The problem is we do
  711. not know whether the freeres code is called first or _IO_cleanup.
  712. if the former is the case, we set the DEALLOC_BUFFER variable to
  713. true and _IO_unbuffer_all will take care of the rest. If
  714. _IO_unbuffer_all is called first we add the streams to a list
  715. which the freeres function later can walk through. */
  716. static void _IO_unbuffer_all (void);
  717. static bool dealloc_buffers;
  718. static FILE *freeres_list;
  719. static void
  720. _IO_unbuffer_all (void)
  721. {
  722. FILE *fp;
  723. #ifdef _IO_MTSAFE_IO
  724. _IO_cleanup_region_start_noarg (flush_cleanup);
  725. _IO_lock_lock (list_all_lock);
  726. #endif
  727. for (fp = (FILE *) _IO_list_all; fp; fp = fp->_chain)
  728. {
  729. int legacy = 0;
  730. run_fp = fp;
  731. _IO_flockfile (fp);
  732. #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1)
  733. if (__glibc_unlikely (_IO_vtable_offset (fp) != 0))
  734. legacy = 1;
  735. #endif
  736. /* Free up the backup area if it was ever allocated. */
  737. if (_IO_have_backup (fp))
  738. _IO_free_backup_area (fp);
  739. if (!legacy && fp->_mode > 0 && _IO_have_wbackup (fp))
  740. _IO_free_wbackup_area (fp);
  741. if (! (fp->_flags & _IO_UNBUFFERED)
  742. /* Iff stream is un-orientated, it wasn't used. */
  743. && (legacy || fp->_mode != 0))
  744. {
  745. if (! legacy && ! dealloc_buffers && !(fp->_flags & _IO_USER_BUF))
  746. {
  747. fp->_flags |= _IO_USER_BUF;
  748. fp->_freeres_list = freeres_list;
  749. freeres_list = fp;
  750. fp->_freeres_buf = fp->_IO_buf_base;
  751. }
  752. _IO_SETBUF (fp, NULL, 0);
  753. if (! legacy && fp->_mode > 0)
  754. _IO_wsetb (fp, NULL, NULL, 0);
  755. }
  756. /* Make sure that never again the wide char functions can be
  757. used. */
  758. if (! legacy)
  759. fp->_mode = -1;
  760. _IO_funlockfile (fp);
  761. run_fp = NULL;
  762. }
  763. #ifdef _IO_MTSAFE_IO
  764. _IO_lock_unlock (list_all_lock);
  765. _IO_cleanup_region_end (0);
  766. #endif
  767. }
  768. void
  769. __libio_freemem (void)
  770. {
  771. dealloc_buffers = true;
  772. while (freeres_list != NULL)
  773. {
  774. free (freeres_list->_freeres_buf);
  775. freeres_list = freeres_list->_freeres_list;
  776. }
  777. }
  778. int
  779. _IO_cleanup (void)
  780. {
  781. int result = _IO_flush_all ();
  782. /* We currently don't have a reliable mechanism for making sure that
  783. C++ static destructors are executed in the correct order.
  784. So it is possible that other static destructors might want to
  785. write to cout - and they're supposed to be able to do so.
  786. The following will make the standard streambufs be unbuffered,
  787. which forces any output from late destructors to be written out. */
  788. _IO_unbuffer_all ();
  789. return result;
  790. }
  791. void
  792. _IO_init_marker (struct _IO_marker *marker, FILE *fp)
  793. {
  794. marker->_sbuf = fp;
  795. if (_IO_in_put_mode (fp))
  796. _IO_switch_to_get_mode (fp);
  797. if (_IO_in_backup (fp))
  798. marker->_pos = fp->_IO_read_ptr - fp->_IO_read_end;
  799. else
  800. marker->_pos = fp->_IO_read_ptr - fp->_IO_read_base;
  801. /* Should perhaps sort the chain? */
  802. marker->_next = fp->_markers;
  803. fp->_markers = marker;
  804. }
  805. void
  806. _IO_remove_marker (struct _IO_marker *marker)
  807. {
  808. /* Unlink from sb's chain. */
  809. struct _IO_marker **ptr = &marker->_sbuf->_markers;
  810. for (; ; ptr = &(*ptr)->_next)
  811. {
  812. if (*ptr == NULL)
  813. break;
  814. else if (*ptr == marker)
  815. {
  816. *ptr = marker->_next;
  817. return;
  818. }
  819. }
  820. /* FIXME: if _sbuf has a backup area that is no longer needed,
  821. should we delete it now, or wait until the next underflow? */
  822. }
  823. #define BAD_DELTA EOF
  824. int
  825. _IO_marker_difference (struct _IO_marker *mark1, struct _IO_marker *mark2)
  826. {
  827. return mark1->_pos - mark2->_pos;
  828. }
  829. /* Return difference between MARK and current position of MARK's stream. */
  830. int
  831. _IO_marker_delta (struct _IO_marker *mark)
  832. {
  833. int cur_pos;
  834. if (mark->_sbuf == NULL)
  835. return BAD_DELTA;
  836. if (_IO_in_backup (mark->_sbuf))
  837. cur_pos = mark->_sbuf->_IO_read_ptr - mark->_sbuf->_IO_read_end;
  838. else
  839. cur_pos = mark->_sbuf->_IO_read_ptr - mark->_sbuf->_IO_read_base;
  840. return mark->_pos - cur_pos;
  841. }
  842. int
  843. _IO_seekmark (FILE *fp, struct _IO_marker *mark, int delta)
  844. {
  845. if (mark->_sbuf != fp)
  846. return EOF;
  847. if (mark->_pos >= 0)
  848. {
  849. if (_IO_in_backup (fp))
  850. _IO_switch_to_main_get_area (fp);
  851. fp->_IO_read_ptr = fp->_IO_read_base + mark->_pos;
  852. }
  853. else
  854. {
  855. if (!_IO_in_backup (fp))
  856. _IO_switch_to_backup_area (fp);
  857. fp->_IO_read_ptr = fp->_IO_read_end + mark->_pos;
  858. }
  859. return 0;
  860. }
  861. void
  862. _IO_unsave_markers (FILE *fp)
  863. {
  864. struct _IO_marker *mark = fp->_markers;
  865. if (mark)
  866. {
  867. fp->_markers = NULL;
  868. }
  869. if (_IO_have_backup (fp))
  870. _IO_free_backup_area (fp);
  871. }
  872. libc_hidden_def (_IO_unsave_markers)
  873. int
  874. _IO_default_pbackfail (FILE *fp, int c)
  875. {
  876. if (fp->_IO_read_ptr > fp->_IO_read_base && !_IO_in_backup (fp)
  877. && (unsigned char) fp->_IO_read_ptr[-1] == c)
  878. --fp->_IO_read_ptr;
  879. else
  880. {
  881. /* Need to handle a filebuf in write mode (switch to read mode). FIXME!*/
  882. if (!_IO_in_backup (fp))
  883. {
  884. /* We need to keep the invariant that the main get area
  885. logically follows the backup area. */
  886. if (fp->_IO_read_ptr > fp->_IO_read_base && _IO_have_backup (fp))
  887. {
  888. if (save_for_backup (fp, fp->_IO_read_ptr))
  889. return EOF;
  890. }
  891. else if (!_IO_have_backup (fp))
  892. {
  893. /* No backup buffer: allocate one. */
  894. int backup_size = 128;
  895. char *bbuf = (char *) malloc (backup_size);
  896. if (bbuf == NULL)
  897. {
  898. /* Guarantee a 1-char pushback. */
  899. bbuf = fp->_short_backupbuf;
  900. backup_size = 1;
  901. }
  902. fp->_IO_save_base = bbuf;
  903. fp->_IO_save_end = fp->_IO_save_base + backup_size;
  904. fp->_IO_backup_base = fp->_IO_save_end;
  905. }
  906. fp->_IO_read_base = fp->_IO_read_ptr;
  907. _IO_switch_to_backup_area (fp);
  908. }
  909. else if (fp->_IO_read_ptr <= fp->_IO_read_base)
  910. {
  911. /* Increase size of existing backup buffer. */
  912. size_t new_size;
  913. size_t old_size = fp->_IO_read_end - fp->_IO_read_base;
  914. char *new_buf;
  915. new_size = 2 * old_size;
  916. new_buf = (char *) malloc (new_size);
  917. if (new_buf == NULL)
  918. return EOF;
  919. memcpy (new_buf + (new_size - old_size), fp->_IO_read_base,
  920. old_size);
  921. _IO_free_backup_buf (fp, fp->_IO_read_base);
  922. _IO_setg (fp, new_buf, new_buf + (new_size - old_size),
  923. new_buf + new_size);
  924. fp->_IO_backup_base = fp->_IO_read_ptr;
  925. }
  926. *--fp->_IO_read_ptr = c;
  927. }
  928. return (unsigned char) c;
  929. }
  930. libc_hidden_def (_IO_default_pbackfail)
  931. off64_t
  932. _IO_default_seek (FILE *fp, off64_t offset, int dir)
  933. {
  934. return _IO_pos_BAD;
  935. }
  936. int
  937. _IO_default_stat (FILE *fp, void *st)
  938. {
  939. return EOF;
  940. }
  941. ssize_t
  942. _IO_default_read (FILE *fp, void *data, ssize_t n)
  943. {
  944. return -1;
  945. }
  946. ssize_t
  947. _IO_default_write (FILE *fp, const void *data, ssize_t n)
  948. {
  949. return 0;
  950. }
  951. int
  952. _IO_default_showmanyc (FILE *fp)
  953. {
  954. return -1;
  955. }
  956. void
  957. _IO_default_imbue (FILE *fp, void *locale)
  958. {
  959. }
  960. _IO_ITER
  961. _IO_iter_begin (void)
  962. {
  963. return (_IO_ITER) _IO_list_all;
  964. }
  965. libc_hidden_def (_IO_iter_begin)
  966. _IO_ITER
  967. _IO_iter_end (void)
  968. {
  969. return NULL;
  970. }
  971. libc_hidden_def (_IO_iter_end)
  972. _IO_ITER
  973. _IO_iter_next (_IO_ITER iter)
  974. {
  975. return iter->_chain;
  976. }
  977. libc_hidden_def (_IO_iter_next)
  978. FILE *
  979. _IO_iter_file (_IO_ITER iter)
  980. {
  981. return iter;
  982. }
  983. libc_hidden_def (_IO_iter_file)
  984. void
  985. _IO_list_lock (void)
  986. {
  987. #ifdef _IO_MTSAFE_IO
  988. _IO_lock_lock (list_all_lock);
  989. #endif
  990. }
  991. libc_hidden_def (_IO_list_lock)
  992. void
  993. _IO_list_unlock (void)
  994. {
  995. #ifdef _IO_MTSAFE_IO
  996. _IO_lock_unlock (list_all_lock);
  997. #endif
  998. }
  999. libc_hidden_def (_IO_list_unlock)
  1000. void
  1001. _IO_list_resetlock (void)
  1002. {
  1003. #ifdef _IO_MTSAFE_IO
  1004. _IO_lock_init (list_all_lock);
  1005. #endif
  1006. }
  1007. libc_hidden_def (_IO_list_resetlock)