genops.c 26 KB

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