fileops.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511
  1. /* Copyright (C) 1993-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Written by Per Bothner <bothner@cygnus.com>.
  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. <http://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 "libioP.h"
  24. #include <assert.h>
  25. #include <fcntl.h>
  26. #include <sys/mman.h>
  27. #include <sys/param.h>
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include <string.h>
  31. #include <errno.h>
  32. #include <unistd.h>
  33. #include <stdlib.h>
  34. #include "../wcsmbs/wcsmbsload.h"
  35. #include "../iconv/gconv_charset.h"
  36. #include "../iconv/gconv_int.h"
  37. #include <shlib-compat.h>
  38. #include <not-cancel.h>
  39. #include <kernel-features.h>
  40. extern struct __gconv_trans_data __libio_translit attribute_hidden;
  41. /* An fstream can be in at most one of put mode, get mode, or putback mode.
  42. Putback mode is a variant of get mode.
  43. In a filebuf, there is only one current position, instead of two
  44. separate get and put pointers. In get mode, the current position
  45. is that of gptr(); in put mode that of pptr().
  46. The position in the buffer that corresponds to the position
  47. in external file system is normally _IO_read_end, except in putback
  48. mode, when it is _IO_save_end and also when the file is in append mode,
  49. since switching from read to write mode automatically sends the position in
  50. the external file system to the end of file.
  51. If the field _fb._offset is >= 0, it gives the offset in
  52. the file as a whole corresponding to eGptr(). (?)
  53. PUT MODE:
  54. If a filebuf is in put mode, then all of _IO_read_ptr, _IO_read_end,
  55. and _IO_read_base are equal to each other. These are usually equal
  56. to _IO_buf_base, though not necessarily if we have switched from
  57. get mode to put mode. (The reason is to maintain the invariant
  58. that _IO_read_end corresponds to the external file position.)
  59. _IO_write_base is non-NULL and usually equal to _IO_buf_base.
  60. We also have _IO_write_end == _IO_buf_end, but only in fully buffered mode.
  61. The un-flushed character are those between _IO_write_base and _IO_write_ptr.
  62. GET MODE:
  63. If a filebuf is in get or putback mode, eback() != egptr().
  64. In get mode, the unread characters are between gptr() and egptr().
  65. The OS file position corresponds to that of egptr().
  66. PUTBACK MODE:
  67. Putback mode is used to remember "excess" characters that have
  68. been sputbackc'd in a separate putback buffer.
  69. In putback mode, the get buffer points to the special putback buffer.
  70. The unread characters are the characters between gptr() and egptr()
  71. in the putback buffer, as well as the area between save_gptr()
  72. and save_egptr(), which point into the original reserve buffer.
  73. (The pointers save_gptr() and save_egptr() are the values
  74. of gptr() and egptr() at the time putback mode was entered.)
  75. The OS position corresponds to that of save_egptr().
  76. LINE BUFFERED OUTPUT:
  77. During line buffered output, _IO_write_base==base() && epptr()==base().
  78. However, ptr() may be anywhere between base() and ebuf().
  79. This forces a call to filebuf::overflow(int C) on every put.
  80. If there is more space in the buffer, and C is not a '\n',
  81. then C is inserted, and pptr() incremented.
  82. UNBUFFERED STREAMS:
  83. If a filebuf is unbuffered(), the _shortbuf[1] is used as the buffer.
  84. */
  85. #define CLOSED_FILEBUF_FLAGS \
  86. (_IO_IS_FILEBUF+_IO_NO_READS+_IO_NO_WRITES+_IO_TIED_PUT_GET)
  87. void
  88. _IO_new_file_init_internal (struct _IO_FILE_plus *fp)
  89. {
  90. /* POSIX.1 allows another file handle to be used to change the position
  91. of our file descriptor. Hence we actually don't know the actual
  92. position before we do the first fseek (and until a following fflush). */
  93. fp->file._offset = _IO_pos_BAD;
  94. fp->file._flags |= CLOSED_FILEBUF_FLAGS;
  95. _IO_link_in (fp);
  96. fp->file._fileno = -1;
  97. }
  98. /* External version of _IO_new_file_init_internal which switches off
  99. vtable validation. */
  100. void
  101. _IO_new_file_init (struct _IO_FILE_plus *fp)
  102. {
  103. IO_set_accept_foreign_vtables (&_IO_vtable_check);
  104. _IO_new_file_init_internal (fp);
  105. }
  106. int
  107. _IO_new_file_close_it (FILE *fp)
  108. {
  109. int write_status;
  110. if (!_IO_file_is_open (fp))
  111. return EOF;
  112. if ((fp->_flags & _IO_NO_WRITES) == 0
  113. && (fp->_flags & _IO_CURRENTLY_PUTTING) != 0)
  114. write_status = _IO_do_flush (fp);
  115. else
  116. write_status = 0;
  117. _IO_unsave_markers (fp);
  118. int close_status = ((fp->_flags2 & _IO_FLAGS2_NOCLOSE) == 0
  119. ? _IO_SYSCLOSE (fp) : 0);
  120. /* Free buffer. */
  121. if (fp->_mode > 0)
  122. {
  123. if (_IO_have_wbackup (fp))
  124. _IO_free_wbackup_area (fp);
  125. _IO_wsetb (fp, NULL, NULL, 0);
  126. _IO_wsetg (fp, NULL, NULL, NULL);
  127. _IO_wsetp (fp, NULL, NULL);
  128. }
  129. _IO_setb (fp, NULL, NULL, 0);
  130. _IO_setg (fp, NULL, NULL, NULL);
  131. _IO_setp (fp, NULL, NULL);
  132. _IO_un_link ((struct _IO_FILE_plus *) fp);
  133. fp->_flags = _IO_MAGIC|CLOSED_FILEBUF_FLAGS;
  134. fp->_fileno = -1;
  135. fp->_offset = _IO_pos_BAD;
  136. return close_status ? close_status : write_status;
  137. }
  138. libc_hidden_ver (_IO_new_file_close_it, _IO_file_close_it)
  139. void
  140. _IO_new_file_finish (FILE *fp, int dummy)
  141. {
  142. if (_IO_file_is_open (fp))
  143. {
  144. _IO_do_flush (fp);
  145. if (!(fp->_flags & _IO_DELETE_DONT_CLOSE))
  146. _IO_SYSCLOSE (fp);
  147. }
  148. _IO_default_finish (fp, 0);
  149. }
  150. libc_hidden_ver (_IO_new_file_finish, _IO_file_finish)
  151. FILE *
  152. _IO_file_open (FILE *fp, const char *filename, int posix_mode, int prot,
  153. int read_write, int is32not64)
  154. {
  155. int fdesc;
  156. if (__glibc_unlikely (fp->_flags2 & _IO_FLAGS2_NOTCANCEL))
  157. fdesc = __open_nocancel (filename,
  158. posix_mode | (is32not64 ? 0 : O_LARGEFILE), prot);
  159. else
  160. fdesc = __open (filename, posix_mode | (is32not64 ? 0 : O_LARGEFILE), prot);
  161. if (fdesc < 0)
  162. return NULL;
  163. fp->_fileno = fdesc;
  164. _IO_mask_flags (fp, read_write,_IO_NO_READS+_IO_NO_WRITES+_IO_IS_APPENDING);
  165. /* For append mode, send the file offset to the end of the file. Don't
  166. update the offset cache though, since the file handle is not active. */
  167. if ((read_write & (_IO_IS_APPENDING | _IO_NO_READS))
  168. == (_IO_IS_APPENDING | _IO_NO_READS))
  169. {
  170. off64_t new_pos = _IO_SYSSEEK (fp, 0, _IO_seek_end);
  171. if (new_pos == _IO_pos_BAD && errno != ESPIPE)
  172. {
  173. __close_nocancel (fdesc);
  174. return NULL;
  175. }
  176. }
  177. _IO_link_in ((struct _IO_FILE_plus *) fp);
  178. return fp;
  179. }
  180. libc_hidden_def (_IO_file_open)
  181. FILE *
  182. _IO_new_file_fopen (FILE *fp, const char *filename, const char *mode,
  183. int is32not64)
  184. {
  185. int oflags = 0, omode;
  186. int read_write;
  187. int oprot = 0666;
  188. int i;
  189. FILE *result;
  190. const char *cs;
  191. const char *last_recognized;
  192. if (_IO_file_is_open (fp))
  193. return 0;
  194. switch (*mode)
  195. {
  196. case 'r':
  197. omode = O_RDONLY;
  198. read_write = _IO_NO_WRITES;
  199. break;
  200. case 'w':
  201. omode = O_WRONLY;
  202. oflags = O_CREAT|O_TRUNC;
  203. read_write = _IO_NO_READS;
  204. break;
  205. case 'a':
  206. omode = O_WRONLY;
  207. oflags = O_CREAT|O_APPEND;
  208. read_write = _IO_NO_READS|_IO_IS_APPENDING;
  209. break;
  210. default:
  211. __set_errno (EINVAL);
  212. return NULL;
  213. }
  214. last_recognized = mode;
  215. for (i = 1; i < 7; ++i)
  216. {
  217. switch (*++mode)
  218. {
  219. case '\0':
  220. break;
  221. case '+':
  222. omode = O_RDWR;
  223. read_write &= _IO_IS_APPENDING;
  224. last_recognized = mode;
  225. continue;
  226. case 'x':
  227. oflags |= O_EXCL;
  228. last_recognized = mode;
  229. continue;
  230. case 'b':
  231. last_recognized = mode;
  232. continue;
  233. case 'm':
  234. fp->_flags2 |= _IO_FLAGS2_MMAP;
  235. continue;
  236. case 'c':
  237. fp->_flags2 |= _IO_FLAGS2_NOTCANCEL;
  238. continue;
  239. case 'e':
  240. oflags |= O_CLOEXEC;
  241. fp->_flags2 |= _IO_FLAGS2_CLOEXEC;
  242. continue;
  243. default:
  244. /* Ignore. */
  245. continue;
  246. }
  247. break;
  248. }
  249. result = _IO_file_open (fp, filename, omode|oflags, oprot, read_write,
  250. is32not64);
  251. if (result != NULL)
  252. {
  253. /* Test whether the mode string specifies the conversion. */
  254. cs = strstr (last_recognized + 1, ",ccs=");
  255. if (cs != NULL)
  256. {
  257. /* Yep. Load the appropriate conversions and set the orientation
  258. to wide. */
  259. struct gconv_fcts fcts;
  260. struct _IO_codecvt *cc;
  261. char *endp = __strchrnul (cs + 5, ',');
  262. char *ccs = malloc (endp - (cs + 5) + 3);
  263. if (ccs == NULL)
  264. {
  265. int malloc_err = errno; /* Whatever malloc failed with. */
  266. (void) _IO_file_close_it (fp);
  267. __set_errno (malloc_err);
  268. return NULL;
  269. }
  270. *((char *) __mempcpy (ccs, cs + 5, endp - (cs + 5))) = '\0';
  271. strip (ccs, ccs);
  272. if (__wcsmbs_named_conv (&fcts, ccs[2] == '\0'
  273. ? upstr (ccs, cs + 5) : ccs) != 0)
  274. {
  275. /* Something went wrong, we cannot load the conversion modules.
  276. This means we cannot proceed since the user explicitly asked
  277. for these. */
  278. (void) _IO_file_close_it (fp);
  279. free (ccs);
  280. __set_errno (EINVAL);
  281. return NULL;
  282. }
  283. free (ccs);
  284. assert (fcts.towc_nsteps == 1);
  285. assert (fcts.tomb_nsteps == 1);
  286. fp->_wide_data->_IO_read_ptr = fp->_wide_data->_IO_read_end;
  287. fp->_wide_data->_IO_write_ptr = fp->_wide_data->_IO_write_base;
  288. /* Clear the state. We start all over again. */
  289. memset (&fp->_wide_data->_IO_state, '\0', sizeof (__mbstate_t));
  290. memset (&fp->_wide_data->_IO_last_state, '\0', sizeof (__mbstate_t));
  291. cc = fp->_codecvt = &fp->_wide_data->_codecvt;
  292. /* The functions are always the same. */
  293. *cc = __libio_codecvt;
  294. cc->__cd_in.__cd.__nsteps = fcts.towc_nsteps;
  295. cc->__cd_in.__cd.__steps = fcts.towc;
  296. cc->__cd_in.__cd.__data[0].__invocation_counter = 0;
  297. cc->__cd_in.__cd.__data[0].__internal_use = 1;
  298. cc->__cd_in.__cd.__data[0].__flags = __GCONV_IS_LAST;
  299. cc->__cd_in.__cd.__data[0].__statep = &result->_wide_data->_IO_state;
  300. cc->__cd_out.__cd.__nsteps = fcts.tomb_nsteps;
  301. cc->__cd_out.__cd.__steps = fcts.tomb;
  302. cc->__cd_out.__cd.__data[0].__invocation_counter = 0;
  303. cc->__cd_out.__cd.__data[0].__internal_use = 1;
  304. cc->__cd_out.__cd.__data[0].__flags
  305. = __GCONV_IS_LAST | __GCONV_TRANSLIT;
  306. cc->__cd_out.__cd.__data[0].__statep =
  307. &result->_wide_data->_IO_state;
  308. /* From now on use the wide character callback functions. */
  309. _IO_JUMPS_FILE_plus (fp) = fp->_wide_data->_wide_vtable;
  310. /* Set the mode now. */
  311. result->_mode = 1;
  312. }
  313. }
  314. return result;
  315. }
  316. libc_hidden_ver (_IO_new_file_fopen, _IO_file_fopen)
  317. FILE *
  318. _IO_new_file_attach (FILE *fp, int fd)
  319. {
  320. if (_IO_file_is_open (fp))
  321. return NULL;
  322. fp->_fileno = fd;
  323. fp->_flags &= ~(_IO_NO_READS+_IO_NO_WRITES);
  324. fp->_flags |= _IO_DELETE_DONT_CLOSE;
  325. /* Get the current position of the file. */
  326. /* We have to do that since that may be junk. */
  327. fp->_offset = _IO_pos_BAD;
  328. int save_errno = errno;
  329. if (_IO_SEEKOFF (fp, (off64_t)0, _IO_seek_cur, _IOS_INPUT|_IOS_OUTPUT)
  330. == _IO_pos_BAD && errno != ESPIPE)
  331. return NULL;
  332. __set_errno (save_errno);
  333. return fp;
  334. }
  335. libc_hidden_ver (_IO_new_file_attach, _IO_file_attach)
  336. FILE *
  337. _IO_new_file_setbuf (FILE *fp, char *p, ssize_t len)
  338. {
  339. if (_IO_default_setbuf (fp, p, len) == NULL)
  340. return NULL;
  341. fp->_IO_write_base = fp->_IO_write_ptr = fp->_IO_write_end
  342. = fp->_IO_buf_base;
  343. _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base, fp->_IO_buf_base);
  344. return fp;
  345. }
  346. libc_hidden_ver (_IO_new_file_setbuf, _IO_file_setbuf)
  347. FILE *
  348. _IO_file_setbuf_mmap (FILE *fp, char *p, ssize_t len)
  349. {
  350. FILE *result;
  351. /* Change the function table. */
  352. _IO_JUMPS_FILE_plus (fp) = &_IO_file_jumps;
  353. fp->_wide_data->_wide_vtable = &_IO_wfile_jumps;
  354. /* And perform the normal operation. */
  355. result = _IO_new_file_setbuf (fp, p, len);
  356. /* If the call failed, restore to using mmap. */
  357. if (result == NULL)
  358. {
  359. _IO_JUMPS_FILE_plus (fp) = &_IO_file_jumps_mmap;
  360. fp->_wide_data->_wide_vtable = &_IO_wfile_jumps_mmap;
  361. }
  362. return result;
  363. }
  364. static size_t new_do_write (FILE *, const char *, size_t);
  365. /* Write TO_DO bytes from DATA to FP.
  366. Then mark FP as having empty buffers. */
  367. int
  368. _IO_new_do_write (FILE *fp, const char *data, size_t to_do)
  369. {
  370. return (to_do == 0
  371. || (size_t) new_do_write (fp, data, to_do) == to_do) ? 0 : EOF;
  372. }
  373. libc_hidden_ver (_IO_new_do_write, _IO_do_write)
  374. static size_t
  375. new_do_write (FILE *fp, const char *data, size_t to_do)
  376. {
  377. size_t count;
  378. if (fp->_flags & _IO_IS_APPENDING)
  379. /* On a system without a proper O_APPEND implementation,
  380. you would need to sys_seek(0, SEEK_END) here, but is
  381. not needed nor desirable for Unix- or Posix-like systems.
  382. Instead, just indicate that offset (before and after) is
  383. unpredictable. */
  384. fp->_offset = _IO_pos_BAD;
  385. else if (fp->_IO_read_end != fp->_IO_write_base)
  386. {
  387. off64_t new_pos
  388. = _IO_SYSSEEK (fp, fp->_IO_write_base - fp->_IO_read_end, 1);
  389. if (new_pos == _IO_pos_BAD)
  390. return 0;
  391. fp->_offset = new_pos;
  392. }
  393. count = _IO_SYSWRITE (fp, data, to_do);
  394. if (fp->_cur_column && count)
  395. fp->_cur_column = _IO_adjust_column (fp->_cur_column - 1, data, count) + 1;
  396. _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base, fp->_IO_buf_base);
  397. fp->_IO_write_base = fp->_IO_write_ptr = fp->_IO_buf_base;
  398. fp->_IO_write_end = (fp->_mode <= 0
  399. && (fp->_flags & (_IO_LINE_BUF | _IO_UNBUFFERED))
  400. ? fp->_IO_buf_base : fp->_IO_buf_end);
  401. return count;
  402. }
  403. int
  404. _IO_new_file_underflow (FILE *fp)
  405. {
  406. ssize_t count;
  407. /* C99 requires EOF to be "sticky". */
  408. if (fp->_flags & _IO_EOF_SEEN)
  409. return EOF;
  410. if (fp->_flags & _IO_NO_READS)
  411. {
  412. fp->_flags |= _IO_ERR_SEEN;
  413. __set_errno (EBADF);
  414. return EOF;
  415. }
  416. if (fp->_IO_read_ptr < fp->_IO_read_end)
  417. return *(unsigned char *) fp->_IO_read_ptr;
  418. if (fp->_IO_buf_base == NULL)
  419. {
  420. /* Maybe we already have a push back pointer. */
  421. if (fp->_IO_save_base != NULL)
  422. {
  423. free (fp->_IO_save_base);
  424. fp->_flags &= ~_IO_IN_BACKUP;
  425. }
  426. _IO_doallocbuf (fp);
  427. }
  428. /* FIXME This can/should be moved to genops ?? */
  429. if (fp->_flags & (_IO_LINE_BUF|_IO_UNBUFFERED))
  430. {
  431. /* We used to flush all line-buffered stream. This really isn't
  432. required by any standard. My recollection is that
  433. traditional Unix systems did this for stdout. stderr better
  434. not be line buffered. So we do just that here
  435. explicitly. --drepper */
  436. _IO_acquire_lock (_IO_stdout);
  437. if ((_IO_stdout->_flags & (_IO_LINKED | _IO_NO_WRITES | _IO_LINE_BUF))
  438. == (_IO_LINKED | _IO_LINE_BUF))
  439. _IO_OVERFLOW (_IO_stdout, EOF);
  440. _IO_release_lock (_IO_stdout);
  441. }
  442. _IO_switch_to_get_mode (fp);
  443. /* This is very tricky. We have to adjust those
  444. pointers before we call _IO_SYSREAD () since
  445. we may longjump () out while waiting for
  446. input. Those pointers may be screwed up. H.J. */
  447. fp->_IO_read_base = fp->_IO_read_ptr = fp->_IO_buf_base;
  448. fp->_IO_read_end = fp->_IO_buf_base;
  449. fp->_IO_write_base = fp->_IO_write_ptr = fp->_IO_write_end
  450. = fp->_IO_buf_base;
  451. count = _IO_SYSREAD (fp, fp->_IO_buf_base,
  452. fp->_IO_buf_end - fp->_IO_buf_base);
  453. if (count <= 0)
  454. {
  455. if (count == 0)
  456. fp->_flags |= _IO_EOF_SEEN;
  457. else
  458. fp->_flags |= _IO_ERR_SEEN, count = 0;
  459. }
  460. fp->_IO_read_end += count;
  461. if (count == 0)
  462. {
  463. /* If a stream is read to EOF, the calling application may switch active
  464. handles. As a result, our offset cache would no longer be valid, so
  465. unset it. */
  466. fp->_offset = _IO_pos_BAD;
  467. return EOF;
  468. }
  469. if (fp->_offset != _IO_pos_BAD)
  470. _IO_pos_adjust (fp->_offset, count);
  471. return *(unsigned char *) fp->_IO_read_ptr;
  472. }
  473. libc_hidden_ver (_IO_new_file_underflow, _IO_file_underflow)
  474. /* Guts of underflow callback if we mmap the file. This stats the file and
  475. updates the stream state to match. In the normal case we return zero.
  476. If the file is no longer eligible for mmap, its jump tables are reset to
  477. the vanilla ones and we return nonzero. */
  478. static int
  479. mmap_remap_check (FILE *fp)
  480. {
  481. struct stat64 st;
  482. if (_IO_SYSSTAT (fp, &st) == 0
  483. && S_ISREG (st.st_mode) && st.st_size != 0
  484. /* Limit the file size to 1MB for 32-bit machines. */
  485. && (sizeof (ptrdiff_t) > 4 || st.st_size < 1*1024*1024))
  486. {
  487. const size_t pagesize = __getpagesize ();
  488. # define ROUNDED(x) (((x) + pagesize - 1) & ~(pagesize - 1))
  489. if (ROUNDED (st.st_size) < ROUNDED (fp->_IO_buf_end
  490. - fp->_IO_buf_base))
  491. {
  492. /* We can trim off some pages past the end of the file. */
  493. (void) __munmap (fp->_IO_buf_base + ROUNDED (st.st_size),
  494. ROUNDED (fp->_IO_buf_end - fp->_IO_buf_base)
  495. - ROUNDED (st.st_size));
  496. fp->_IO_buf_end = fp->_IO_buf_base + st.st_size;
  497. }
  498. else if (ROUNDED (st.st_size) > ROUNDED (fp->_IO_buf_end
  499. - fp->_IO_buf_base))
  500. {
  501. /* The file added some pages. We need to remap it. */
  502. void *p;
  503. #if _G_HAVE_MREMAP
  504. p = __mremap (fp->_IO_buf_base, ROUNDED (fp->_IO_buf_end
  505. - fp->_IO_buf_base),
  506. ROUNDED (st.st_size), MREMAP_MAYMOVE);
  507. if (p == MAP_FAILED)
  508. {
  509. (void) __munmap (fp->_IO_buf_base,
  510. fp->_IO_buf_end - fp->_IO_buf_base);
  511. goto punt;
  512. }
  513. #else
  514. (void) __munmap (fp->_IO_buf_base,
  515. fp->_IO_buf_end - fp->_IO_buf_base);
  516. p = __mmap64 (NULL, st.st_size, PROT_READ, MAP_SHARED,
  517. fp->_fileno, 0);
  518. if (p == MAP_FAILED)
  519. goto punt;
  520. #endif
  521. fp->_IO_buf_base = p;
  522. fp->_IO_buf_end = fp->_IO_buf_base + st.st_size;
  523. }
  524. else
  525. {
  526. /* The number of pages didn't change. */
  527. fp->_IO_buf_end = fp->_IO_buf_base + st.st_size;
  528. }
  529. # undef ROUNDED
  530. fp->_offset -= fp->_IO_read_end - fp->_IO_read_ptr;
  531. _IO_setg (fp, fp->_IO_buf_base,
  532. fp->_offset < fp->_IO_buf_end - fp->_IO_buf_base
  533. ? fp->_IO_buf_base + fp->_offset : fp->_IO_buf_end,
  534. fp->_IO_buf_end);
  535. /* If we are already positioned at or past the end of the file, don't
  536. change the current offset. If not, seek past what we have mapped,
  537. mimicking the position left by a normal underflow reading into its
  538. buffer until EOF. */
  539. if (fp->_offset < fp->_IO_buf_end - fp->_IO_buf_base)
  540. {
  541. if (__lseek64 (fp->_fileno, fp->_IO_buf_end - fp->_IO_buf_base,
  542. SEEK_SET)
  543. != fp->_IO_buf_end - fp->_IO_buf_base)
  544. fp->_flags |= _IO_ERR_SEEN;
  545. else
  546. fp->_offset = fp->_IO_buf_end - fp->_IO_buf_base;
  547. }
  548. return 0;
  549. }
  550. else
  551. {
  552. /* Life is no longer good for mmap. Punt it. */
  553. (void) __munmap (fp->_IO_buf_base,
  554. fp->_IO_buf_end - fp->_IO_buf_base);
  555. punt:
  556. fp->_IO_buf_base = fp->_IO_buf_end = NULL;
  557. _IO_setg (fp, NULL, NULL, NULL);
  558. if (fp->_mode <= 0)
  559. _IO_JUMPS_FILE_plus (fp) = &_IO_file_jumps;
  560. else
  561. _IO_JUMPS_FILE_plus (fp) = &_IO_wfile_jumps;
  562. fp->_wide_data->_wide_vtable = &_IO_wfile_jumps;
  563. return 1;
  564. }
  565. }
  566. /* Special callback replacing the underflow callbacks if we mmap the file. */
  567. int
  568. _IO_file_underflow_mmap (FILE *fp)
  569. {
  570. if (fp->_IO_read_ptr < fp->_IO_read_end)
  571. return *(unsigned char *) fp->_IO_read_ptr;
  572. if (__glibc_unlikely (mmap_remap_check (fp)))
  573. /* We punted to the regular file functions. */
  574. return _IO_UNDERFLOW (fp);
  575. if (fp->_IO_read_ptr < fp->_IO_read_end)
  576. return *(unsigned char *) fp->_IO_read_ptr;
  577. fp->_flags |= _IO_EOF_SEEN;
  578. return EOF;
  579. }
  580. static void
  581. decide_maybe_mmap (FILE *fp)
  582. {
  583. /* We use the file in read-only mode. This could mean we can
  584. mmap the file and use it without any copying. But not all
  585. file descriptors are for mmap-able objects and on 32-bit
  586. machines we don't want to map files which are too large since
  587. this would require too much virtual memory. */
  588. struct stat64 st;
  589. if (_IO_SYSSTAT (fp, &st) == 0
  590. && S_ISREG (st.st_mode) && st.st_size != 0
  591. /* Limit the file size to 1MB for 32-bit machines. */
  592. && (sizeof (ptrdiff_t) > 4 || st.st_size < 1*1024*1024)
  593. /* Sanity check. */
  594. && (fp->_offset == _IO_pos_BAD || fp->_offset <= st.st_size))
  595. {
  596. /* Try to map the file. */
  597. void *p;
  598. p = __mmap64 (NULL, st.st_size, PROT_READ, MAP_SHARED, fp->_fileno, 0);
  599. if (p != MAP_FAILED)
  600. {
  601. /* OK, we managed to map the file. Set the buffer up and use a
  602. special jump table with simplified underflow functions which
  603. never tries to read anything from the file. */
  604. if (__lseek64 (fp->_fileno, st.st_size, SEEK_SET) != st.st_size)
  605. {
  606. (void) __munmap (p, st.st_size);
  607. fp->_offset = _IO_pos_BAD;
  608. }
  609. else
  610. {
  611. _IO_setb (fp, p, (char *) p + st.st_size, 0);
  612. if (fp->_offset == _IO_pos_BAD)
  613. fp->_offset = 0;
  614. _IO_setg (fp, p, p + fp->_offset, p + st.st_size);
  615. fp->_offset = st.st_size;
  616. if (fp->_mode <= 0)
  617. _IO_JUMPS_FILE_plus (fp) = &_IO_file_jumps_mmap;
  618. else
  619. _IO_JUMPS_FILE_plus (fp) = &_IO_wfile_jumps_mmap;
  620. fp->_wide_data->_wide_vtable = &_IO_wfile_jumps_mmap;
  621. return;
  622. }
  623. }
  624. }
  625. /* We couldn't use mmap, so revert to the vanilla file operations. */
  626. if (fp->_mode <= 0)
  627. _IO_JUMPS_FILE_plus (fp) = &_IO_file_jumps;
  628. else
  629. _IO_JUMPS_FILE_plus (fp) = &_IO_wfile_jumps;
  630. fp->_wide_data->_wide_vtable = &_IO_wfile_jumps;
  631. }
  632. int
  633. _IO_file_underflow_maybe_mmap (FILE *fp)
  634. {
  635. /* This is the first read attempt. Choose mmap or vanilla operations
  636. and then punt to the chosen underflow routine. */
  637. decide_maybe_mmap (fp);
  638. return _IO_UNDERFLOW (fp);
  639. }
  640. int
  641. _IO_new_file_overflow (FILE *f, int ch)
  642. {
  643. if (f->_flags & _IO_NO_WRITES) /* SET ERROR */
  644. {
  645. f->_flags |= _IO_ERR_SEEN;
  646. __set_errno (EBADF);
  647. return EOF;
  648. }
  649. /* If currently reading or no buffer allocated. */
  650. if ((f->_flags & _IO_CURRENTLY_PUTTING) == 0 || f->_IO_write_base == NULL)
  651. {
  652. /* Allocate a buffer if needed. */
  653. if (f->_IO_write_base == NULL)
  654. {
  655. _IO_doallocbuf (f);
  656. _IO_setg (f, f->_IO_buf_base, f->_IO_buf_base, f->_IO_buf_base);
  657. }
  658. /* Otherwise must be currently reading.
  659. If _IO_read_ptr (and hence also _IO_read_end) is at the buffer end,
  660. logically slide the buffer forwards one block (by setting the
  661. read pointers to all point at the beginning of the block). This
  662. makes room for subsequent output.
  663. Otherwise, set the read pointers to _IO_read_end (leaving that
  664. alone, so it can continue to correspond to the external position). */
  665. if (__glibc_unlikely (_IO_in_backup (f)))
  666. {
  667. size_t nbackup = f->_IO_read_end - f->_IO_read_ptr;
  668. _IO_free_backup_area (f);
  669. f->_IO_read_base -= MIN (nbackup,
  670. f->_IO_read_base - f->_IO_buf_base);
  671. f->_IO_read_ptr = f->_IO_read_base;
  672. }
  673. if (f->_IO_read_ptr == f->_IO_buf_end)
  674. f->_IO_read_end = f->_IO_read_ptr = f->_IO_buf_base;
  675. f->_IO_write_ptr = f->_IO_read_ptr;
  676. f->_IO_write_base = f->_IO_write_ptr;
  677. f->_IO_write_end = f->_IO_buf_end;
  678. f->_IO_read_base = f->_IO_read_ptr = f->_IO_read_end;
  679. f->_flags |= _IO_CURRENTLY_PUTTING;
  680. if (f->_mode <= 0 && f->_flags & (_IO_LINE_BUF | _IO_UNBUFFERED))
  681. f->_IO_write_end = f->_IO_write_ptr;
  682. }
  683. if (ch == EOF)
  684. return _IO_do_write (f, f->_IO_write_base,
  685. f->_IO_write_ptr - f->_IO_write_base);
  686. if (f->_IO_write_ptr == f->_IO_buf_end ) /* Buffer is really full */
  687. if (_IO_do_flush (f) == EOF)
  688. return EOF;
  689. *f->_IO_write_ptr++ = ch;
  690. if ((f->_flags & _IO_UNBUFFERED)
  691. || ((f->_flags & _IO_LINE_BUF) && ch == '\n'))
  692. if (_IO_do_write (f, f->_IO_write_base,
  693. f->_IO_write_ptr - f->_IO_write_base) == EOF)
  694. return EOF;
  695. return (unsigned char) ch;
  696. }
  697. libc_hidden_ver (_IO_new_file_overflow, _IO_file_overflow)
  698. int
  699. _IO_new_file_sync (FILE *fp)
  700. {
  701. ssize_t delta;
  702. int retval = 0;
  703. /* char* ptr = cur_ptr(); */
  704. if (fp->_IO_write_ptr > fp->_IO_write_base)
  705. if (_IO_do_flush(fp)) return EOF;
  706. delta = fp->_IO_read_ptr - fp->_IO_read_end;
  707. if (delta != 0)
  708. {
  709. off64_t new_pos = _IO_SYSSEEK (fp, delta, 1);
  710. if (new_pos != (off64_t) EOF)
  711. fp->_IO_read_end = fp->_IO_read_ptr;
  712. else if (errno == ESPIPE)
  713. ; /* Ignore error from unseekable devices. */
  714. else
  715. retval = EOF;
  716. }
  717. if (retval != EOF)
  718. fp->_offset = _IO_pos_BAD;
  719. /* FIXME: Cleanup - can this be shared? */
  720. /* setg(base(), ptr, ptr); */
  721. return retval;
  722. }
  723. libc_hidden_ver (_IO_new_file_sync, _IO_file_sync)
  724. static int
  725. _IO_file_sync_mmap (FILE *fp)
  726. {
  727. if (fp->_IO_read_ptr != fp->_IO_read_end)
  728. {
  729. if (__lseek64 (fp->_fileno, fp->_IO_read_ptr - fp->_IO_buf_base,
  730. SEEK_SET)
  731. != fp->_IO_read_ptr - fp->_IO_buf_base)
  732. {
  733. fp->_flags |= _IO_ERR_SEEN;
  734. return EOF;
  735. }
  736. }
  737. fp->_offset = fp->_IO_read_ptr - fp->_IO_buf_base;
  738. fp->_IO_read_end = fp->_IO_read_ptr = fp->_IO_read_base;
  739. return 0;
  740. }
  741. /* ftell{,o} implementation. The only time we modify the state of the stream
  742. is when we have unflushed writes. In that case we seek to the end and
  743. record that offset in the stream object. */
  744. static off64_t
  745. do_ftell (FILE *fp)
  746. {
  747. off64_t result, offset = 0;
  748. /* No point looking at unflushed data if we haven't allocated buffers
  749. yet. */
  750. if (fp->_IO_buf_base != NULL)
  751. {
  752. bool unflushed_writes = fp->_IO_write_ptr > fp->_IO_write_base;
  753. bool append_mode = (fp->_flags & _IO_IS_APPENDING) == _IO_IS_APPENDING;
  754. /* When we have unflushed writes in append mode, seek to the end of the
  755. file and record that offset. This is the only time we change the file
  756. stream state and it is safe since the file handle is active. */
  757. if (unflushed_writes && append_mode)
  758. {
  759. result = _IO_SYSSEEK (fp, 0, _IO_seek_end);
  760. if (result == _IO_pos_BAD)
  761. return EOF;
  762. else
  763. fp->_offset = result;
  764. }
  765. /* Adjust for unflushed data. */
  766. if (!unflushed_writes)
  767. offset -= fp->_IO_read_end - fp->_IO_read_ptr;
  768. /* We don't trust _IO_read_end to represent the current file offset when
  769. writing in append mode because the value would have to be shifted to
  770. the end of the file during a flush. Use the write base instead, along
  771. with the new offset we got above when we did a seek to the end of the
  772. file. */
  773. else if (append_mode)
  774. offset += fp->_IO_write_ptr - fp->_IO_write_base;
  775. /* For all other modes, _IO_read_end represents the file offset. */
  776. else
  777. offset += fp->_IO_write_ptr - fp->_IO_read_end;
  778. }
  779. if (fp->_offset != _IO_pos_BAD)
  780. result = fp->_offset;
  781. else
  782. result = _IO_SYSSEEK (fp, 0, _IO_seek_cur);
  783. if (result == EOF)
  784. return result;
  785. result += offset;
  786. if (result < 0)
  787. {
  788. __set_errno (EINVAL);
  789. return EOF;
  790. }
  791. return result;
  792. }
  793. off64_t
  794. _IO_new_file_seekoff (FILE *fp, off64_t offset, int dir, int mode)
  795. {
  796. off64_t result;
  797. off64_t delta, new_offset;
  798. long count;
  799. /* Short-circuit into a separate function. We don't want to mix any
  800. functionality and we don't want to touch anything inside the FILE
  801. object. */
  802. if (mode == 0)
  803. return do_ftell (fp);
  804. /* POSIX.1 8.2.3.7 says that after a call the fflush() the file
  805. offset of the underlying file must be exact. */
  806. int must_be_exact = (fp->_IO_read_base == fp->_IO_read_end
  807. && fp->_IO_write_base == fp->_IO_write_ptr);
  808. bool was_writing = (fp->_IO_write_ptr > fp->_IO_write_base
  809. || _IO_in_put_mode (fp));
  810. /* Flush unwritten characters.
  811. (This may do an unneeded write if we seek within the buffer.
  812. But to be able to switch to reading, we would need to set
  813. egptr to pptr. That can't be done in the current design,
  814. which assumes file_ptr() is eGptr. Anyway, since we probably
  815. end up flushing when we close(), it doesn't make much difference.)
  816. FIXME: simulate mem-mapped files. */
  817. if (was_writing && _IO_switch_to_get_mode (fp))
  818. return EOF;
  819. if (fp->_IO_buf_base == NULL)
  820. {
  821. /* It could be that we already have a pushback buffer. */
  822. if (fp->_IO_read_base != NULL)
  823. {
  824. free (fp->_IO_read_base);
  825. fp->_flags &= ~_IO_IN_BACKUP;
  826. }
  827. _IO_doallocbuf (fp);
  828. _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base);
  829. _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base, fp->_IO_buf_base);
  830. }
  831. switch (dir)
  832. {
  833. case _IO_seek_cur:
  834. /* Adjust for read-ahead (bytes is buffer). */
  835. offset -= fp->_IO_read_end - fp->_IO_read_ptr;
  836. if (fp->_offset == _IO_pos_BAD)
  837. goto dumb;
  838. /* Make offset absolute, assuming current pointer is file_ptr(). */
  839. offset += fp->_offset;
  840. if (offset < 0)
  841. {
  842. __set_errno (EINVAL);
  843. return EOF;
  844. }
  845. dir = _IO_seek_set;
  846. break;
  847. case _IO_seek_set:
  848. break;
  849. case _IO_seek_end:
  850. {
  851. struct stat64 st;
  852. if (_IO_SYSSTAT (fp, &st) == 0 && S_ISREG (st.st_mode))
  853. {
  854. offset += st.st_size;
  855. dir = _IO_seek_set;
  856. }
  857. else
  858. goto dumb;
  859. }
  860. }
  861. _IO_free_backup_area (fp);
  862. /* At this point, dir==_IO_seek_set. */
  863. /* If destination is within current buffer, optimize: */
  864. if (fp->_offset != _IO_pos_BAD && fp->_IO_read_base != NULL
  865. && !_IO_in_backup (fp))
  866. {
  867. off64_t start_offset = (fp->_offset
  868. - (fp->_IO_read_end - fp->_IO_buf_base));
  869. if (offset >= start_offset && offset < fp->_offset)
  870. {
  871. _IO_setg (fp, fp->_IO_buf_base,
  872. fp->_IO_buf_base + (offset - start_offset),
  873. fp->_IO_read_end);
  874. _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base);
  875. _IO_mask_flags (fp, 0, _IO_EOF_SEEN);
  876. goto resync;
  877. }
  878. }
  879. if (fp->_flags & _IO_NO_READS)
  880. goto dumb;
  881. /* Try to seek to a block boundary, to improve kernel page management. */
  882. new_offset = offset & ~(fp->_IO_buf_end - fp->_IO_buf_base - 1);
  883. delta = offset - new_offset;
  884. if (delta > fp->_IO_buf_end - fp->_IO_buf_base)
  885. {
  886. new_offset = offset;
  887. delta = 0;
  888. }
  889. result = _IO_SYSSEEK (fp, new_offset, 0);
  890. if (result < 0)
  891. return EOF;
  892. if (delta == 0)
  893. count = 0;
  894. else
  895. {
  896. count = _IO_SYSREAD (fp, fp->_IO_buf_base,
  897. (must_be_exact
  898. ? delta : fp->_IO_buf_end - fp->_IO_buf_base));
  899. if (count < delta)
  900. {
  901. /* We weren't allowed to read, but try to seek the remainder. */
  902. offset = count == EOF ? delta : delta-count;
  903. dir = _IO_seek_cur;
  904. goto dumb;
  905. }
  906. }
  907. _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base + delta,
  908. fp->_IO_buf_base + count);
  909. _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base);
  910. fp->_offset = result + count;
  911. _IO_mask_flags (fp, 0, _IO_EOF_SEEN);
  912. return offset;
  913. dumb:
  914. _IO_unsave_markers (fp);
  915. result = _IO_SYSSEEK (fp, offset, dir);
  916. if (result != EOF)
  917. {
  918. _IO_mask_flags (fp, 0, _IO_EOF_SEEN);
  919. fp->_offset = result;
  920. _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base, fp->_IO_buf_base);
  921. _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base);
  922. }
  923. return result;
  924. resync:
  925. /* We need to do it since it is possible that the file offset in
  926. the kernel may be changed behind our back. It may happen when
  927. we fopen a file and then do a fork. One process may access the
  928. file and the kernel file offset will be changed. */
  929. if (fp->_offset >= 0)
  930. _IO_SYSSEEK (fp, fp->_offset, 0);
  931. return offset;
  932. }
  933. libc_hidden_ver (_IO_new_file_seekoff, _IO_file_seekoff)
  934. off64_t
  935. _IO_file_seekoff_mmap (FILE *fp, off64_t offset, int dir, int mode)
  936. {
  937. off64_t result;
  938. /* If we are only interested in the current position, calculate it and
  939. return right now. This calculation does the right thing when we are
  940. using a pushback buffer, but in the usual case has the same value as
  941. (fp->_IO_read_ptr - fp->_IO_buf_base). */
  942. if (mode == 0)
  943. return fp->_offset - (fp->_IO_read_end - fp->_IO_read_ptr);
  944. switch (dir)
  945. {
  946. case _IO_seek_cur:
  947. /* Adjust for read-ahead (bytes is buffer). */
  948. offset += fp->_IO_read_ptr - fp->_IO_read_base;
  949. break;
  950. case _IO_seek_set:
  951. break;
  952. case _IO_seek_end:
  953. offset += fp->_IO_buf_end - fp->_IO_buf_base;
  954. break;
  955. }
  956. /* At this point, dir==_IO_seek_set. */
  957. if (offset < 0)
  958. {
  959. /* No negative offsets are valid. */
  960. __set_errno (EINVAL);
  961. return EOF;
  962. }
  963. result = _IO_SYSSEEK (fp, offset, 0);
  964. if (result < 0)
  965. return EOF;
  966. if (offset > fp->_IO_buf_end - fp->_IO_buf_base)
  967. /* One can fseek arbitrarily past the end of the file
  968. and it is meaningless until one attempts to read.
  969. Leave the buffer pointers in EOF state until underflow. */
  970. _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_end, fp->_IO_buf_end);
  971. else
  972. /* Adjust the read pointers to match the file position,
  973. but so the next read attempt will call underflow. */
  974. _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base + offset,
  975. fp->_IO_buf_base + offset);
  976. fp->_offset = result;
  977. _IO_mask_flags (fp, 0, _IO_EOF_SEEN);
  978. return offset;
  979. }
  980. static off64_t
  981. _IO_file_seekoff_maybe_mmap (FILE *fp, off64_t offset, int dir,
  982. int mode)
  983. {
  984. /* We only get here when we haven't tried to read anything yet.
  985. So there is nothing more useful for us to do here than just
  986. the underlying lseek call. */
  987. off64_t result = _IO_SYSSEEK (fp, offset, dir);
  988. if (result < 0)
  989. return EOF;
  990. fp->_offset = result;
  991. return result;
  992. }
  993. ssize_t
  994. _IO_file_read (FILE *fp, void *buf, ssize_t size)
  995. {
  996. return (__builtin_expect (fp->_flags2 & _IO_FLAGS2_NOTCANCEL, 0)
  997. ? __read_nocancel (fp->_fileno, buf, size)
  998. : __read (fp->_fileno, buf, size));
  999. }
  1000. libc_hidden_def (_IO_file_read)
  1001. off64_t
  1002. _IO_file_seek (FILE *fp, off64_t offset, int dir)
  1003. {
  1004. return __lseek64 (fp->_fileno, offset, dir);
  1005. }
  1006. libc_hidden_def (_IO_file_seek)
  1007. int
  1008. _IO_file_stat (FILE *fp, void *st)
  1009. {
  1010. return __fxstat64 (_STAT_VER, fp->_fileno, (struct stat64 *) st);
  1011. }
  1012. libc_hidden_def (_IO_file_stat)
  1013. int
  1014. _IO_file_close_mmap (FILE *fp)
  1015. {
  1016. /* In addition to closing the file descriptor we have to unmap the file. */
  1017. (void) __munmap (fp->_IO_buf_base, fp->_IO_buf_end - fp->_IO_buf_base);
  1018. fp->_IO_buf_base = fp->_IO_buf_end = NULL;
  1019. /* Cancelling close should be avoided if possible since it leaves an
  1020. unrecoverable state behind. */
  1021. return __close_nocancel (fp->_fileno);
  1022. }
  1023. int
  1024. _IO_file_close (FILE *fp)
  1025. {
  1026. /* Cancelling close should be avoided if possible since it leaves an
  1027. unrecoverable state behind. */
  1028. return __close_nocancel (fp->_fileno);
  1029. }
  1030. libc_hidden_def (_IO_file_close)
  1031. ssize_t
  1032. _IO_new_file_write (FILE *f, const void *data, ssize_t n)
  1033. {
  1034. ssize_t to_do = n;
  1035. while (to_do > 0)
  1036. {
  1037. ssize_t count = (__builtin_expect (f->_flags2
  1038. & _IO_FLAGS2_NOTCANCEL, 0)
  1039. ? __write_nocancel (f->_fileno, data, to_do)
  1040. : __write (f->_fileno, data, to_do));
  1041. if (count < 0)
  1042. {
  1043. f->_flags |= _IO_ERR_SEEN;
  1044. break;
  1045. }
  1046. to_do -= count;
  1047. data = (void *) ((char *) data + count);
  1048. }
  1049. n -= to_do;
  1050. if (f->_offset >= 0)
  1051. f->_offset += n;
  1052. return n;
  1053. }
  1054. size_t
  1055. _IO_new_file_xsputn (FILE *f, const void *data, size_t n)
  1056. {
  1057. const char *s = (const char *) data;
  1058. size_t to_do = n;
  1059. int must_flush = 0;
  1060. size_t count = 0;
  1061. if (n <= 0)
  1062. return 0;
  1063. /* This is an optimized implementation.
  1064. If the amount to be written straddles a block boundary
  1065. (or the filebuf is unbuffered), use sys_write directly. */
  1066. /* First figure out how much space is available in the buffer. */
  1067. if ((f->_flags & _IO_LINE_BUF) && (f->_flags & _IO_CURRENTLY_PUTTING))
  1068. {
  1069. count = f->_IO_buf_end - f->_IO_write_ptr;
  1070. if (count >= n)
  1071. {
  1072. const char *p;
  1073. for (p = s + n; p > s; )
  1074. {
  1075. if (*--p == '\n')
  1076. {
  1077. count = p - s + 1;
  1078. must_flush = 1;
  1079. break;
  1080. }
  1081. }
  1082. }
  1083. }
  1084. else if (f->_IO_write_end > f->_IO_write_ptr)
  1085. count = f->_IO_write_end - f->_IO_write_ptr; /* Space available. */
  1086. /* Then fill the buffer. */
  1087. if (count > 0)
  1088. {
  1089. if (count > to_do)
  1090. count = to_do;
  1091. f->_IO_write_ptr = __mempcpy (f->_IO_write_ptr, s, count);
  1092. s += count;
  1093. to_do -= count;
  1094. }
  1095. if (to_do + must_flush > 0)
  1096. {
  1097. size_t block_size, do_write;
  1098. /* Next flush the (full) buffer. */
  1099. if (_IO_OVERFLOW (f, EOF) == EOF)
  1100. /* If nothing else has to be written we must not signal the
  1101. caller that everything has been written. */
  1102. return to_do == 0 ? EOF : n - to_do;
  1103. /* Try to maintain alignment: write a whole number of blocks. */
  1104. block_size = f->_IO_buf_end - f->_IO_buf_base;
  1105. do_write = to_do - (block_size >= 128 ? to_do % block_size : 0);
  1106. if (do_write)
  1107. {
  1108. count = new_do_write (f, s, do_write);
  1109. to_do -= count;
  1110. if (count < do_write)
  1111. return n - to_do;
  1112. }
  1113. /* Now write out the remainder. Normally, this will fit in the
  1114. buffer, but it's somewhat messier for line-buffered files,
  1115. so we let _IO_default_xsputn handle the general case. */
  1116. if (to_do)
  1117. to_do -= _IO_default_xsputn (f, s+do_write, to_do);
  1118. }
  1119. return n - to_do;
  1120. }
  1121. libc_hidden_ver (_IO_new_file_xsputn, _IO_file_xsputn)
  1122. size_t
  1123. _IO_file_xsgetn (FILE *fp, void *data, size_t n)
  1124. {
  1125. size_t want, have;
  1126. ssize_t count;
  1127. char *s = data;
  1128. want = n;
  1129. if (fp->_IO_buf_base == NULL)
  1130. {
  1131. /* Maybe we already have a push back pointer. */
  1132. if (fp->_IO_save_base != NULL)
  1133. {
  1134. free (fp->_IO_save_base);
  1135. fp->_flags &= ~_IO_IN_BACKUP;
  1136. }
  1137. _IO_doallocbuf (fp);
  1138. }
  1139. while (want > 0)
  1140. {
  1141. have = fp->_IO_read_end - fp->_IO_read_ptr;
  1142. if (want <= have)
  1143. {
  1144. memcpy (s, fp->_IO_read_ptr, want);
  1145. fp->_IO_read_ptr += want;
  1146. want = 0;
  1147. }
  1148. else
  1149. {
  1150. if (have > 0)
  1151. {
  1152. s = __mempcpy (s, fp->_IO_read_ptr, have);
  1153. want -= have;
  1154. fp->_IO_read_ptr += have;
  1155. }
  1156. /* Check for backup and repeat */
  1157. if (_IO_in_backup (fp))
  1158. {
  1159. _IO_switch_to_main_get_area (fp);
  1160. continue;
  1161. }
  1162. /* If we now want less than a buffer, underflow and repeat
  1163. the copy. Otherwise, _IO_SYSREAD directly to
  1164. the user buffer. */
  1165. if (fp->_IO_buf_base
  1166. && want < (size_t) (fp->_IO_buf_end - fp->_IO_buf_base))
  1167. {
  1168. if (__underflow (fp) == EOF)
  1169. break;
  1170. continue;
  1171. }
  1172. /* These must be set before the sysread as we might longjmp out
  1173. waiting for input. */
  1174. _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base, fp->_IO_buf_base);
  1175. _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base);
  1176. /* Try to maintain alignment: read a whole number of blocks. */
  1177. count = want;
  1178. if (fp->_IO_buf_base)
  1179. {
  1180. size_t block_size = fp->_IO_buf_end - fp->_IO_buf_base;
  1181. if (block_size >= 128)
  1182. count -= want % block_size;
  1183. }
  1184. count = _IO_SYSREAD (fp, s, count);
  1185. if (count <= 0)
  1186. {
  1187. if (count == 0)
  1188. fp->_flags |= _IO_EOF_SEEN;
  1189. else
  1190. fp->_flags |= _IO_ERR_SEEN;
  1191. break;
  1192. }
  1193. s += count;
  1194. want -= count;
  1195. if (fp->_offset != _IO_pos_BAD)
  1196. _IO_pos_adjust (fp->_offset, count);
  1197. }
  1198. }
  1199. return n - want;
  1200. }
  1201. libc_hidden_def (_IO_file_xsgetn)
  1202. static size_t
  1203. _IO_file_xsgetn_mmap (FILE *fp, void *data, size_t n)
  1204. {
  1205. size_t have;
  1206. char *read_ptr = fp->_IO_read_ptr;
  1207. char *s = (char *) data;
  1208. have = fp->_IO_read_end - fp->_IO_read_ptr;
  1209. if (have < n)
  1210. {
  1211. if (__glibc_unlikely (_IO_in_backup (fp)))
  1212. {
  1213. s = __mempcpy (s, read_ptr, have);
  1214. n -= have;
  1215. _IO_switch_to_main_get_area (fp);
  1216. read_ptr = fp->_IO_read_ptr;
  1217. have = fp->_IO_read_end - fp->_IO_read_ptr;
  1218. }
  1219. if (have < n)
  1220. {
  1221. /* Check that we are mapping all of the file, in case it grew. */
  1222. if (__glibc_unlikely (mmap_remap_check (fp)))
  1223. /* We punted mmap, so complete with the vanilla code. */
  1224. return s - (char *) data + _IO_XSGETN (fp, data, n);
  1225. read_ptr = fp->_IO_read_ptr;
  1226. have = fp->_IO_read_end - read_ptr;
  1227. }
  1228. }
  1229. if (have < n)
  1230. fp->_flags |= _IO_EOF_SEEN;
  1231. if (have != 0)
  1232. {
  1233. have = MIN (have, n);
  1234. s = __mempcpy (s, read_ptr, have);
  1235. fp->_IO_read_ptr = read_ptr + have;
  1236. }
  1237. return s - (char *) data;
  1238. }
  1239. static size_t
  1240. _IO_file_xsgetn_maybe_mmap (FILE *fp, void *data, size_t n)
  1241. {
  1242. /* We only get here if this is the first attempt to read something.
  1243. Decide which operations to use and then punt to the chosen one. */
  1244. decide_maybe_mmap (fp);
  1245. return _IO_XSGETN (fp, data, n);
  1246. }
  1247. versioned_symbol (libc, _IO_new_do_write, _IO_do_write, GLIBC_2_1);
  1248. versioned_symbol (libc, _IO_new_file_attach, _IO_file_attach, GLIBC_2_1);
  1249. versioned_symbol (libc, _IO_new_file_close_it, _IO_file_close_it, GLIBC_2_1);
  1250. versioned_symbol (libc, _IO_new_file_finish, _IO_file_finish, GLIBC_2_1);
  1251. versioned_symbol (libc, _IO_new_file_fopen, _IO_file_fopen, GLIBC_2_1);
  1252. versioned_symbol (libc, _IO_new_file_init, _IO_file_init, GLIBC_2_1);
  1253. versioned_symbol (libc, _IO_new_file_setbuf, _IO_file_setbuf, GLIBC_2_1);
  1254. versioned_symbol (libc, _IO_new_file_sync, _IO_file_sync, GLIBC_2_1);
  1255. versioned_symbol (libc, _IO_new_file_overflow, _IO_file_overflow, GLIBC_2_1);
  1256. versioned_symbol (libc, _IO_new_file_seekoff, _IO_file_seekoff, GLIBC_2_1);
  1257. versioned_symbol (libc, _IO_new_file_underflow, _IO_file_underflow, GLIBC_2_1);
  1258. versioned_symbol (libc, _IO_new_file_write, _IO_file_write, GLIBC_2_1);
  1259. versioned_symbol (libc, _IO_new_file_xsputn, _IO_file_xsputn, GLIBC_2_1);
  1260. const struct _IO_jump_t _IO_file_jumps libio_vtable =
  1261. {
  1262. JUMP_INIT_DUMMY,
  1263. JUMP_INIT(finish, _IO_file_finish),
  1264. JUMP_INIT(overflow, _IO_file_overflow),
  1265. JUMP_INIT(underflow, _IO_file_underflow),
  1266. JUMP_INIT(uflow, _IO_default_uflow),
  1267. JUMP_INIT(pbackfail, _IO_default_pbackfail),
  1268. JUMP_INIT(xsputn, _IO_file_xsputn),
  1269. JUMP_INIT(xsgetn, _IO_file_xsgetn),
  1270. JUMP_INIT(seekoff, _IO_new_file_seekoff),
  1271. JUMP_INIT(seekpos, _IO_default_seekpos),
  1272. JUMP_INIT(setbuf, _IO_new_file_setbuf),
  1273. JUMP_INIT(sync, _IO_new_file_sync),
  1274. JUMP_INIT(doallocate, _IO_file_doallocate),
  1275. JUMP_INIT(read, _IO_file_read),
  1276. JUMP_INIT(write, _IO_new_file_write),
  1277. JUMP_INIT(seek, _IO_file_seek),
  1278. JUMP_INIT(close, _IO_file_close),
  1279. JUMP_INIT(stat, _IO_file_stat),
  1280. JUMP_INIT(showmanyc, _IO_default_showmanyc),
  1281. JUMP_INIT(imbue, _IO_default_imbue)
  1282. };
  1283. libc_hidden_data_def (_IO_file_jumps)
  1284. const struct _IO_jump_t _IO_file_jumps_mmap libio_vtable =
  1285. {
  1286. JUMP_INIT_DUMMY,
  1287. JUMP_INIT(finish, _IO_file_finish),
  1288. JUMP_INIT(overflow, _IO_file_overflow),
  1289. JUMP_INIT(underflow, _IO_file_underflow_mmap),
  1290. JUMP_INIT(uflow, _IO_default_uflow),
  1291. JUMP_INIT(pbackfail, _IO_default_pbackfail),
  1292. JUMP_INIT(xsputn, _IO_new_file_xsputn),
  1293. JUMP_INIT(xsgetn, _IO_file_xsgetn_mmap),
  1294. JUMP_INIT(seekoff, _IO_file_seekoff_mmap),
  1295. JUMP_INIT(seekpos, _IO_default_seekpos),
  1296. JUMP_INIT(setbuf, (_IO_setbuf_t) _IO_file_setbuf_mmap),
  1297. JUMP_INIT(sync, _IO_file_sync_mmap),
  1298. JUMP_INIT(doallocate, _IO_file_doallocate),
  1299. JUMP_INIT(read, _IO_file_read),
  1300. JUMP_INIT(write, _IO_new_file_write),
  1301. JUMP_INIT(seek, _IO_file_seek),
  1302. JUMP_INIT(close, _IO_file_close_mmap),
  1303. JUMP_INIT(stat, _IO_file_stat),
  1304. JUMP_INIT(showmanyc, _IO_default_showmanyc),
  1305. JUMP_INIT(imbue, _IO_default_imbue)
  1306. };
  1307. const struct _IO_jump_t _IO_file_jumps_maybe_mmap libio_vtable =
  1308. {
  1309. JUMP_INIT_DUMMY,
  1310. JUMP_INIT(finish, _IO_file_finish),
  1311. JUMP_INIT(overflow, _IO_file_overflow),
  1312. JUMP_INIT(underflow, _IO_file_underflow_maybe_mmap),
  1313. JUMP_INIT(uflow, _IO_default_uflow),
  1314. JUMP_INIT(pbackfail, _IO_default_pbackfail),
  1315. JUMP_INIT(xsputn, _IO_new_file_xsputn),
  1316. JUMP_INIT(xsgetn, _IO_file_xsgetn_maybe_mmap),
  1317. JUMP_INIT(seekoff, _IO_file_seekoff_maybe_mmap),
  1318. JUMP_INIT(seekpos, _IO_default_seekpos),
  1319. JUMP_INIT(setbuf, (_IO_setbuf_t) _IO_file_setbuf_mmap),
  1320. JUMP_INIT(sync, _IO_new_file_sync),
  1321. JUMP_INIT(doallocate, _IO_file_doallocate),
  1322. JUMP_INIT(read, _IO_file_read),
  1323. JUMP_INIT(write, _IO_new_file_write),
  1324. JUMP_INIT(seek, _IO_file_seek),
  1325. JUMP_INIT(close, _IO_file_close),
  1326. JUMP_INIT(stat, _IO_file_stat),
  1327. JUMP_INIT(showmanyc, _IO_default_showmanyc),
  1328. JUMP_INIT(imbue, _IO_default_imbue)
  1329. };