stdio.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  1. /* Copyright (C) 1996 Robert de Bath <rdebath@cix.compulink.co.uk>
  2. * This file is part of the Linux-8086 C library and is distributed
  3. * under the GNU Library General Public License.
  4. */
  5. /* This is an implementation of the C standard IO package.
  6. *
  7. * Updates:
  8. * 29-Sep-2000 W. Greathouse 1. fgetc copying beyond end of buffer
  9. * 2. stdout needs flushed when input requested on
  10. * stdin.
  11. * 3. bufend was set incorrectly to 4 bytes beyond
  12. * bufin (sizeof a pointer) instead of BUFSIZ.
  13. * This resulted in 4 byte buffers for full
  14. * buffered stdin and stdout and an 8 byte
  15. * buffer for the unbuffered stderr!
  16. */
  17. /*
  18. * Feb 27, 2001 Manuel Novoa III
  19. *
  20. * Most of the core functionality has been completely rewritten.
  21. * A number of functions have been added as well, as mandated by C89.
  22. *
  23. * An extension function "fsfopen" has been added:
  24. * Open a file using an automatically (stack) or statically allocated FILE.
  25. * The FILE * returned behaves just as any other FILE * with respect to the
  26. * stdio functions, but be aware of the following:
  27. * NOTE: The buffer used for the file is FILE's builtin 2-byte buffer, so
  28. * setting a new buffer is probably advisable.
  29. * NOTE: This function is primarily intended to be used for stack-allocated
  30. * FILEs when uClibc stdio has no dynamic memory support.
  31. * For the statically allocated case, it is probably better to increase
  32. * the value of FIXED_STREAMS in stdio.c.
  33. * WARNING: If allocated on the stack, make sure you call fclose before the
  34. * stack memory is reclaimed!
  35. */
  36. #include <stdlib.h>
  37. #include <stdio.h>
  38. #include <unistd.h>
  39. #include <fcntl.h>
  40. #include <sys/types.h>
  41. #include <malloc.h>
  42. #include <errno.h>
  43. #include <string.h>
  44. #include <assert.h>
  45. #include <limits.h>
  46. extern off_t _uClibc_fwrite(const unsigned char *buf, off_t bytes, FILE *fp);
  47. extern off_t _uClibc_fread(unsigned char *buf, off_t bytes, FILE *fp);
  48. /* Note: This def of READING is ok since 1st ungetc puts in buf. */
  49. #if 0
  50. #define READING(fp) (fp->bufpos < fp->bufread)
  51. #else
  52. #define READING(fp) (fp->bufstart < fp->bufread)
  53. #endif
  54. #if 1
  55. #define WRITING(fp) (fp->bufwrite > fp->bufstart)
  56. #else
  57. #define WRITING(fp) ((fp->bufpos > fp->bufread) && (fp->bufpos > fp->bufstart))
  58. #endif
  59. #define READABLE(fp) (fp->bufread != 0)
  60. #define WRITEABLE(fp) (fp->bufwrite != 0)
  61. #define EOF_OR_ERROR(fp) (fp->mode & (__MODE_EOF | __MODE_ERR))
  62. /***********************************************************************/
  63. /* BUILD TIME OPTIONS */
  64. /***********************************************************************/
  65. /*
  66. * FIXED_STREAMS must be >= 3 and FIXED_BUFFERS must be >= 2.
  67. * As a feature, these can be increased, although this is probably
  68. * only useful if DISABLE_DYNAMIC is set to 1 below.
  69. */
  70. #define FIXED_STREAMS 3
  71. #define FIXED_BUFFERS 2
  72. /*
  73. * As a feature, you can build uClibc with no dynamic allocation done
  74. * by the stdio package. Just set DISABLE_DYNAMIC to nonzero. Note that
  75. * use of asprintf, getdelim, or getline will pull malloc into the link.
  76. *
  77. * Note: You can't trust FOPEN_MAX if DISABLE_DYNAMIC != 0.
  78. */
  79. #define DISABLE_DYNAMIC 0
  80. /*
  81. * As a feature, you can try to allow setvbuf calls after file operations.
  82. * Setting FLEXIBLE_SETVBUF to nonzero will cause setvbuf to try to fflush
  83. * any buffered writes or sync the file position for buffered reads. If it
  84. * is successful, the buffer change can then take place.
  85. */
  86. #define FLEXIBLE_SETVBUF 0
  87. /***********************************************************************/
  88. #if DISABLE_DYNAMIC != 0
  89. #undef malloc
  90. #undef free
  91. #define malloc(x) 0
  92. #define free(x)
  93. #endif
  94. extern FILE *__IO_list; /* For fflush. */
  95. extern FILE *_free_file_list;
  96. extern char _free_buffer_index;
  97. extern FILE _stdio_streams[FIXED_STREAMS];
  98. extern unsigned char _fixed_buffers[FIXED_BUFFERS * BUFSIZ];
  99. extern unsigned char *_alloc_stdio_buffer(size_t size);
  100. extern void _free_stdio_buffer_of_file(FILE *fp);
  101. extern void _free_stdio_stream(FILE *fp);
  102. #ifdef L__alloc_stdio_buffer
  103. unsigned char *_alloc_stdio_buffer(size_t size)
  104. {
  105. unsigned char *buf;
  106. if ((size == BUFSIZ) && (_free_buffer_index < FIXED_BUFFERS)) {
  107. buf = _fixed_buffers + ((unsigned int)_free_buffer_index) * BUFSIZ;
  108. _free_buffer_index = *buf;
  109. return buf;
  110. }
  111. return malloc(size);
  112. }
  113. #endif
  114. #ifdef L__free_stdio_buffer_of_file
  115. void _free_stdio_buffer_of_file(FILE *fp)
  116. {
  117. unsigned char *buf;
  118. if (!(fp->mode & __MODE_FREEBUF)) {
  119. return;
  120. }
  121. fp->mode &= ~(__MODE_FREEBUF);
  122. buf = fp->bufstart;
  123. if ((buf >= _fixed_buffers)
  124. && (buf < _fixed_buffers + (FIXED_BUFFERS * BUFSIZ))) {
  125. *buf = _free_buffer_index;
  126. _free_buffer_index = (buf - _fixed_buffers)/BUFSIZ;
  127. return;
  128. }
  129. free(buf);
  130. }
  131. #endif
  132. #ifdef L__stdio_init
  133. #if FIXED_BUFFERS < 2
  134. #error FIXED_BUFFERS must be >= 2
  135. #endif
  136. #if FIXED_BUFFERS >= UCHAR_MAX
  137. #error FIXED_BUFFERS must be < UCHAR_MAX
  138. #endif
  139. #define bufin (_fixed_buffers)
  140. #define bufout (_fixed_buffers + BUFSIZ)
  141. #define buferr (_stdio_streams[2].unbuf) /* Stderr is unbuffered */
  142. unsigned char _fixed_buffers[FIXED_BUFFERS * BUFSIZ];
  143. #if FIXED_STREAMS < 3
  144. #error FIXED_STREAMS must be >= 3
  145. #endif
  146. FILE _stdio_streams[FIXED_STREAMS] = {
  147. {bufin, bufin, 0, bufin, bufin + BUFSIZ,
  148. _stdio_streams + 1,
  149. 0, _IOFBF | __MODE_FREEFIL | __MODE_FREEBUF | __MODE_TIED },
  150. {bufout, 0, bufout, bufout, bufout + BUFSIZ,
  151. _stdio_streams + 2,
  152. 1, _IOFBF | __MODE_FREEFIL | __MODE_FREEBUF | __MODE_TIED },
  153. {buferr, 0, buferr, buferr, buferr + 1,
  154. NULL,
  155. 2, _IONBF | __MODE_FREEFIL }
  156. };
  157. FILE *_stdin = _stdio_streams + 0;
  158. FILE *_stdout = _stdio_streams + 1;
  159. FILE *_stderr = _stdio_streams + 2;
  160. /*
  161. * Note: the following forces linking of the __init_stdio function if
  162. * any of the stdio functions are used since they all call fflush directly
  163. * or indirectly.
  164. */
  165. FILE *__IO_list = _stdio_streams; /* For fflush. */
  166. FILE *_free_file_list = 0;
  167. char _free_buffer_index = FIXED_BUFFERS;
  168. /*
  169. * __stdio_close_all is automatically when exiting if stdio is used.
  170. * See misc/internals/__uClibc_main.c and and stdlib/atexit.c.
  171. */
  172. void __stdio_close_all(void)
  173. {
  174. fflush(NULL); /* Files will be closed on _exit call. */
  175. }
  176. /*
  177. * __init_stdio is automatically by __uClibc_main if stdio is used.
  178. */
  179. void __init_stdio(void)
  180. {
  181. #if (FIXED_BUFFERS > 2) || (FIXED_STREAMS > 3)
  182. int i;
  183. #endif
  184. #if FIXED_BUFFERS > 2
  185. _free_buffer_index = 2;
  186. for ( i = 2 ; i < FIXED_BUFFERS ; i++ ) {
  187. _fixed_buffers[i*BUFSIZ] = i;
  188. }
  189. #endif
  190. #if FIXED_STREAMS > 3
  191. _free_file_list = _stdio_streams + 3;
  192. for ( i = 3 ; i < FIXED_STREAMS-1 ; i++ ) {
  193. _stdio_streams[i].next = _stdio_streams + i + 1;
  194. }
  195. _stdio_streams[i].next = 0;
  196. #endif
  197. #if _IOFBF != 0 || _IOLBF != 1
  198. #error Assumption violated -- values of _IOFBF and/or _IOLBF
  199. /* This asssumption is also made in _fopen. */
  200. #endif
  201. /* stdout uses line buffering when connected to a tty. */
  202. _stdio_streams[1].mode |= isatty(1);
  203. }
  204. #endif
  205. #ifdef L_fputc
  206. int fputc(int c, FILE *fp)
  207. {
  208. unsigned char buf[1];
  209. *buf = (unsigned char) c;
  210. if (_uClibc_fwrite(buf, 1, fp)) {
  211. return (unsigned char) c;
  212. }
  213. return EOF;
  214. }
  215. #endif
  216. #ifdef L_fgetc
  217. int fgetc(FILE *fp)
  218. {
  219. unsigned char buf[1];
  220. if (_uClibc_fread(buf, 1, fp)) {
  221. return *buf;
  222. }
  223. return EOF;
  224. }
  225. #endif
  226. #ifdef L_fflush
  227. int fflush(FILE *fp)
  228. {
  229. int rv;
  230. rv = 0;
  231. if (fp == NULL) { /* On NULL flush the lot. */
  232. for (fp = __IO_list; fp; fp = fp->next) {
  233. if (WRITEABLE(fp) && fflush(fp)) {
  234. rv = EOF;
  235. }
  236. }
  237. } else if (WRITING(fp)) { /* Output buffer contents. */
  238. _uClibc_fwrite(NULL, 0, fp);
  239. if (fp->mode & __MODE_ERR) {
  240. rv = -1;
  241. }
  242. } else if (!WRITEABLE(fp)) { /* File opened read-only!!! */
  243. /*
  244. * According to info, glibc returns an error when the file is opened
  245. * in read-only mode.
  246. * ANSI says behavior in this case is undefined but also says you
  247. * shouldn't flush a stream you were reading from.
  248. */
  249. errno = EBADF; /* Should we set stream error indicator? */
  250. rv = -1;
  251. }
  252. return rv;
  253. }
  254. #endif
  255. #ifdef L_fgets
  256. /* Nothing special here ... */
  257. char *fgets(char *s, int count, FILE *fp)
  258. {
  259. int ch;
  260. char *p;
  261. p = s;
  262. while (count-- > 1) { /* Guard against count arg == INT_MIN. */
  263. ch = getc(fp);
  264. if (ch == EOF) {
  265. break;
  266. }
  267. *p++ = ch;
  268. if (ch == '\n') {
  269. break;
  270. }
  271. }
  272. if (ferror(fp) || (s == p)) {
  273. return 0;
  274. }
  275. *p = 0;
  276. return s;
  277. }
  278. #endif
  279. #ifdef L_gets
  280. char *gets(char *str) /* This is an UNSAFE function! */
  281. {
  282. /*
  283. * Strictly speaking, this implementation is incorrect as the number
  284. * of chars gets can read should be unlimited. However, I can't
  285. * imagine anyone wanting to gets() into a buffer bigger than INT_MAX.
  286. *
  287. * Besides, this function is inherently unsafe and shouldn't be used.
  288. */
  289. return fgets(str, INT_MAX, _stdin);
  290. }
  291. #endif
  292. #ifdef L_fputs
  293. int fputs(const char *str, FILE *fp)
  294. {
  295. int n;
  296. n = strlen(str);
  297. _uClibc_fwrite((const unsigned char *)str, n, fp);
  298. if (fp->mode & __MODE_ERR) {
  299. n = EOF;
  300. }
  301. return n;
  302. }
  303. #endif
  304. #ifdef L_puts
  305. int puts(const char *str)
  306. {
  307. int n;
  308. n = fputs(str, _stdout); /* Let next fputc handle EOF or error. */
  309. if (fputc('\n', _stdout) == EOF) { /* Don't use putc since we want to */
  310. return EOF; /* fflush stdout if it is line buffered. */
  311. }
  312. return n + 1;
  313. }
  314. #endif
  315. #ifdef L_fread
  316. /*
  317. * fread will often be used to read in large chunks of data calling read()
  318. * directly can be a big win in this case. Beware also fgetc calls this
  319. * function to fill the buffer.
  320. */
  321. size_t fread(buf, size, nelm, fp)
  322. void *buf;
  323. size_t size;
  324. size_t nelm;
  325. FILE *fp;
  326. {
  327. off_t bytes;
  328. #warning TODO: handle possible overflow for bytes
  329. bytes = size * nelm; /* How many bytes do we want? */
  330. bytes = _uClibc_fread((unsigned char *)buf, bytes, fp);
  331. return bytes/size;
  332. }
  333. #endif
  334. #ifdef L__uClibc_fread
  335. off_t _uClibc_fread(unsigned char *buf, off_t bytes, FILE *fp)
  336. {
  337. unsigned char *p;
  338. off_t len;
  339. if (!READABLE(fp)) {
  340. fp->mode |= __MODE_ERR;
  341. } else if (WRITING(fp)) {
  342. fflush(fp);
  343. } else if (fp->mode & _stdout->mode & __MODE_TIED) {
  344. fflush(_stdout);
  345. }
  346. if (EOF_OR_ERROR(fp) || (bytes <= 0)) {
  347. return 0;
  348. }
  349. p = (unsigned char *) buf;
  350. if (fp->mode & __MODE_UNGOT) { /* If we had an ungetc'd char, */
  351. fp->mode ^= __MODE_UNGOT; /* reset the flag and return it. */
  352. *p++ = fp->ungot;
  353. --bytes;
  354. }
  355. FROM_BUF:
  356. len = fp->bufread - fp->bufpos;
  357. if (len > bytes) { /* Enough buffered */
  358. len = bytes;
  359. }
  360. bytes -= len;
  361. while (len--) {
  362. *p++ = *fp->bufpos++;
  363. }
  364. if (bytes && !EOF_OR_ERROR(fp)) { /* More requested but buffer empty. */
  365. if (bytes < fp->bufend - fp->bufstart) {
  366. fp->bufpos = fp->bufread = fp->bufstart; /* Reset pointers. */
  367. fp->bufread += _uClibc_fread(fp->bufstart,
  368. fp->bufend - fp->bufstart, fp);
  369. goto FROM_BUF;
  370. }
  371. len = read(fp->fd, p, (unsigned) bytes);
  372. if (len < 0) {
  373. fp->mode |= __MODE_ERR;
  374. } else {
  375. p += len;
  376. if (len == 0) {
  377. fp->mode |= __MODE_EOF;
  378. }
  379. }
  380. }
  381. return (p - (unsigned char *)buf);
  382. }
  383. #endif
  384. #ifdef L_fwrite
  385. /*
  386. * Like fread, fwrite will often be used to write out large chunks of
  387. * data; calling write() directly can be a big win in this case.
  388. *
  389. * But first we check to see if there's space in the buffer.
  390. */
  391. size_t fwrite(buf, size, nelm, fp)
  392. const void *buf;
  393. size_t size;
  394. size_t nelm;
  395. FILE *fp;
  396. {
  397. off_t bytes;
  398. #warning TODO: handle possible overflow for bytes
  399. bytes = size * nelm; /* How many bytes do we want? */
  400. bytes = _uClibc_fwrite((const unsigned char *)buf, bytes, fp);
  401. return bytes/size;
  402. }
  403. #endif
  404. #ifdef L__uClibc_fwrite
  405. /*
  406. * If buf == NULL, fflush.
  407. * If buf != NULL, (fflush and) write
  408. * Returns number of chars written from fp buffer _OR_ from buf.
  409. */
  410. off_t _uClibc_fwrite(const unsigned char *buf, off_t bytes, FILE *fp)
  411. {
  412. unsigned char *p;
  413. int rv, had_newline;
  414. /*
  415. * Fail if stream isn't writable, if we were reading and get an error
  416. * changing over to write mode (ie. can't update stream position),
  417. * or if the stream was already in an error state.
  418. */
  419. if (!WRITEABLE(fp)) { /* Fail if stream isn't writable. */
  420. fp->mode |= __MODE_ERR;
  421. } else if (READING(fp)) { /* If read buffer isn't empty, */
  422. fseek(fp, 0, SEEK_CUR); /* stop reading and update position. */
  423. } else if (READABLE(fp)) {
  424. fp->bufread = fp->bufstart; /* Reset start of read buffer. */
  425. }
  426. if (EOF_OR_ERROR(fp)) {
  427. return 0;
  428. }
  429. p = (unsigned char *)buf;
  430. if (p && (fp->bufpos + bytes <= fp->bufend)) { /* Enough buffer space? */
  431. had_newline = 0;
  432. while (bytes--) {
  433. if (*p == '\n') {
  434. had_newline = 1;
  435. }
  436. *fp->bufpos++ = *p++;
  437. }
  438. if (fp->bufpos < fp->bufend) { /* Buffer is not full. */
  439. fp->bufwrite = fp->bufend;
  440. if ((fp->mode & __MODE_BUF) == _IOLBF) {
  441. fp->bufwrite = fp->bufpos;
  442. if (had_newline) {
  443. goto FFLUSH;
  444. }
  445. }
  446. goto DONE;
  447. }
  448. FFLUSH:
  449. /* If we get here, either buffer is full or we need to flush anyway. */
  450. p = NULL;
  451. }
  452. if (!p) { /* buf == NULL means fflush */
  453. p = fp->bufstart;
  454. bytes = fp->bufpos - p;
  455. buf = fp->bufpos = fp->bufwrite = p;
  456. } else if (fp->bufpos > fp->bufstart) { /* If there are buffered chars, */
  457. _uClibc_fwrite(NULL, 0, fp); /* write them. */
  458. if (ferror(fp)) {
  459. return 0;
  460. }
  461. }
  462. while (bytes) {
  463. if ((rv = write(fp->fd, p, bytes)) < 0) {
  464. rv = 0;
  465. if (errno != EINTR) {
  466. break;
  467. }
  468. }
  469. p += rv;
  470. bytes -= rv;
  471. }
  472. if (bytes) {
  473. fp->mode |= __MODE_ERR;
  474. }
  475. DONE:
  476. return (p - (unsigned char *)buf);
  477. }
  478. #endif
  479. #ifdef L_rewind
  480. void rewind(fp)
  481. FILE *fp;
  482. {
  483. clearerr(fp); /* Clear errors first, then seek in case */
  484. fseek(fp, 0, SEEK_SET); /* there is an error seeking. */
  485. }
  486. #endif
  487. #ifdef L_fseek
  488. int fseek(FILE *fp, long int offset, int ref)
  489. {
  490. #if SEEK_SET != 0 || SEEK_CUR != 1 || SEEK_END != 2
  491. #error Assumption violated -- values of SEEK_SET, SEEK_CUR, SEEK_END
  492. #endif
  493. if ((ref < 0) || (ref > 2)) {
  494. errno = EINVAL;
  495. return -1;
  496. }
  497. if (WRITING(fp)) {
  498. fflush(fp); /* We'll deal with errors below. */
  499. /* After fflush, bufpos is at CUR. */
  500. } else if (READING(fp)) {
  501. if (ref == SEEK_CUR) {
  502. /* Correct offset to take into account position in buffer. */
  503. offset -= (fp->bufread - fp->bufpos);
  504. if (fp->mode & __MODE_UNGOT) { /* If we had an ungetc'd char, */
  505. --offset; /* adjust offset (clear flag below). */
  506. }
  507. }
  508. fp->bufpos = fp->bufread = fp->bufstart;
  509. }
  510. if ((fp->mode & __MODE_ERR) ||
  511. (((ref != SEEK_CUR) || offset) && (lseek(fp->fd, offset, ref) < 0))) {
  512. fp->mode |= __MODE_ERR; /* Possibly redundant, but doesn't hurt. */
  513. return -1;
  514. }
  515. fp->mode &= ~(__MODE_EOF | __MODE_UNGOT);
  516. return 0;
  517. }
  518. #endif
  519. #ifdef L_ftell
  520. long ftell(fp)
  521. FILE *fp;
  522. {
  523. /* Note: can't do fflush here since it would discard any ungetc's. */
  524. off_t pos;
  525. pos = lseek(fp->fd, 0, SEEK_CUR); /* Get kernels idea of position. */
  526. if (pos < 0) {
  527. return -1;
  528. }
  529. if (WRITING(fp)) {
  530. pos += (fp->bufpos - fp->bufstart); /* Adjust for buffer position. */
  531. } else if (READING(fp)) {
  532. pos -= (fp->bufread - fp->bufpos); /* Adjust for buffer position. */
  533. if (fp->mode & __MODE_UNGOT) {
  534. --pos;
  535. }
  536. if (pos < 0) { /* ungetcs at start of file? */
  537. errno = EIO;
  538. pos = -1;
  539. }
  540. }
  541. return pos;
  542. }
  543. #endif
  544. #ifdef L__fopen
  545. /*
  546. * This Fopen is all three of fopen, fdopen and freopen. The macros in
  547. * stdio.h show the other names.
  548. */
  549. static __inline FILE *_alloc_stdio_stream(void)
  550. {
  551. FILE *fp;
  552. if (_free_file_list) {
  553. fp = _free_file_list;
  554. _free_file_list = fp->next;
  555. } else if (!(fp = malloc(sizeof(FILE)))) {
  556. return 0;
  557. }
  558. fp->mode = __MODE_FREEFIL | _IOFBF;
  559. /* Initially set to use builtin buffer of FILE structure. */
  560. fp->bufstart = fp->unbuf;
  561. fp->bufend = fp->unbuf + sizeof(fp->unbuf);
  562. return fp;
  563. }
  564. FILE *__fopen(fname, fd, fp, mode)
  565. const char *fname;
  566. int fd;
  567. FILE *fp;
  568. const char *mode;
  569. {
  570. FILE *nfp;
  571. int open_mode;
  572. int cur_mode;
  573. nfp = fp;
  574. /* Parse the mode string arg. */
  575. switch (*mode++) {
  576. case 'r': /* read */
  577. open_mode = O_RDONLY;
  578. break;
  579. case 'w': /* write (create or truncate)*/
  580. open_mode = (O_WRONLY | O_CREAT | O_TRUNC);
  581. break;
  582. case 'a': /* write (create or append) */
  583. open_mode = (O_WRONLY | O_CREAT | O_APPEND);
  584. break;
  585. default: /* illegal mode */
  586. errno = EINVAL;
  587. goto _fopen_ERROR;
  588. }
  589. if ((*mode == 'b')) { /* binary mode (nop for uClibc) */
  590. ++mode;
  591. }
  592. #if O_RDONLY != 0 || O_WRONLY != 1 || O_RDWR != 2
  593. #error Assumption violated concerning open mode constants!
  594. #endif
  595. if (*mode == '+') { /* read-write */
  596. ++mode;
  597. open_mode &= ~(O_RDONLY | O_WRONLY);
  598. open_mode |= O_RDWR;
  599. }
  600. while (*mode) { /* ignore everything else except ... */
  601. if (*mode == 'x') { /* open exclusive -- GNU extension */
  602. open_mode |= O_EXCL;
  603. }
  604. ++mode;
  605. }
  606. if (fp == 0) { /* We need a FILE so allocate it before */
  607. if (!(nfp = _alloc_stdio_stream())) {
  608. return 0;
  609. }
  610. }
  611. if (fname) { /* Open the file itself */
  612. fd = open(fname, open_mode, 0666);
  613. } else { /* fdopen -- check mode is compatible. */
  614. cur_mode = fcntl(fd, F_GETFL);
  615. if ((cur_mode == -1) || ((cur_mode ^ open_mode) & O_ACCMODE)) {
  616. fd = -1;
  617. }
  618. }
  619. if (fd < 0) { /* Error from open or bad arg passed. */
  620. _fopen_ERROR:
  621. if (nfp) {
  622. _free_stdio_stream(nfp);
  623. }
  624. return 0;
  625. }
  626. nfp->fd = fd; /* Set FILE's fd before adding to open list. */
  627. if (fp == 0) { /* Not freopen so... */
  628. nfp->next = __IO_list; /* use newly created FILE and */
  629. __IO_list = nfp; /* add it to the list of open files. */
  630. if ((nfp->bufstart = _alloc_stdio_buffer(BUFSIZ)) != 0) {
  631. nfp->bufend = nfp->bufstart + BUFSIZ;
  632. nfp->mode |= __MODE_FREEBUF;
  633. }
  634. }
  635. /* Ok, file's ready clear the buffer and save important bits */
  636. nfp->bufpos = nfp->bufstart;
  637. nfp->mode |= isatty(fd);
  638. nfp->bufread = nfp->bufwrite = 0;
  639. if (!(open_mode & O_WRONLY)) {
  640. nfp->bufread = nfp->bufstart;
  641. }
  642. if (open_mode & (O_WRONLY | O_RDWR)) {
  643. nfp->bufwrite = nfp->bufstart;
  644. }
  645. return nfp;
  646. }
  647. #endif
  648. #ifdef L_fclose
  649. int fclose(fp)
  650. FILE *fp;
  651. {
  652. FILE *prev;
  653. FILE *ptr;
  654. int rv;
  655. rv = 0;
  656. if (WRITING(fp)) { /* If there are buffered chars to write... */
  657. rv = fflush(fp); /* write them. */
  658. }
  659. if (close(fp->fd)) { /* Need to close even if fflush fails. */
  660. rv = EOF;
  661. }
  662. prev = 0; /* Remove file from open list. */
  663. for (ptr = __IO_list; ptr ; ptr = ptr->next) {
  664. if (ptr == fp) {
  665. if (prev == 0) {
  666. __IO_list = fp->next;
  667. } else {
  668. prev->next = fp->next;
  669. }
  670. break;
  671. }
  672. prev = ptr;
  673. }
  674. _free_stdio_stream(fp); /* Finally free the stream if necessary. */
  675. return rv;
  676. }
  677. #endif
  678. #ifdef L__free_stdio_stream
  679. /* The following is only called by fclose and _fopen. */
  680. void _free_stdio_stream(FILE *fp)
  681. {
  682. _free_stdio_buffer_of_file(fp); /* Free buffer if necessary. */
  683. if (!(fp->mode & __MODE_FREEFIL)) {
  684. return;
  685. }
  686. /* Note: we generally won't bother checking for bad pointers here. */
  687. if ((fp >= _stdio_streams) && (fp < _stdio_streams + FIXED_STREAMS)) {
  688. assert( (fp - _stdio_streams) % sizeof(_stdio_streams[0]) == 0 );
  689. fp->next = _free_file_list;
  690. _free_file_list = fp;
  691. return;
  692. }
  693. free(fp);
  694. }
  695. #endif
  696. #ifdef L_setbuffer
  697. void setbuffer(FILE *fp, char *buf, size_t size)
  698. {
  699. int mode;
  700. mode = _IOFBF;
  701. if (!buf) {
  702. mode = _IONBF;
  703. }
  704. setvbuf(fp, buf, mode, size);
  705. }
  706. #endif
  707. #ifdef L_setvbuf
  708. int setvbuf(FILE *fp, char *buf, int mode, size_t size)
  709. {
  710. int allocated_buf_flag;
  711. if ((mode < 0) || (mode > 2)) { /* Illegal mode. */
  712. return EOF;
  713. }
  714. #if FLEXIBLE_SETVBUF
  715. /* C89 standard requires no ops before setvbuf, but we can be flexible. */
  716. /* NOTE: This will trash any chars ungetc'd!!! */
  717. if (fseek(fp, 0, SEEK_CUR)) {
  718. return EOF;
  719. }
  720. #endif
  721. /* Note: If size == 2 we could use FILE's builting buffer as well, but */
  722. /* I don't think the benefit is worth the code size increase. */
  723. if ((mode == _IONBF) || (size < 1)) {
  724. size = 1; /* size == 1 _REQUIRED_ for _IONBF!!! */
  725. buf = fp->unbuf;
  726. }
  727. fp->mode &= ~(__MODE_BUF); /* Clear current mode */
  728. fp->mode |= mode; /* and set new one. */
  729. allocated_buf_flag = 0;
  730. if ((!buf) && (size != (fp->bufend - fp->bufstart))) {
  731. /* No buffer supplied and requested size different from current. */
  732. allocated_buf_flag = __MODE_FREEBUF;
  733. if (!(buf = _alloc_stdio_buffer(size))) {
  734. return EOF; /* Keep current buffer. */
  735. }
  736. }
  737. if (buf && (buf != (char *) fp->bufstart)) { /* Want different buffer. */
  738. _free_stdio_buffer_of_file(fp); /* Free the old buffer. */
  739. fp->mode |= allocated_buf_flag; /* Allocated? or FILE's builtin. */
  740. fp->bufstart = buf;
  741. fp->bufend = buf + size;
  742. fp->bufpos = fp->bufstart;
  743. if (READABLE(fp)) {
  744. fp->bufread = fp->bufstart;
  745. }
  746. if (WRITEABLE(fp)) {
  747. fp->bufwrite = fp->bufstart;
  748. }
  749. }
  750. return 0;
  751. }
  752. #endif
  753. #ifdef L_setbuf
  754. void setbuf(FILE *fp, char *buf)
  755. {
  756. int mode;
  757. mode = _IOFBF;
  758. if (!buf) {
  759. mode = _IONBF;
  760. }
  761. setvbuf(fp, buf, mode, BUFSIZ);
  762. }
  763. #endif
  764. #ifdef L_setlinebuf
  765. void setlinebuf(FILE *fp)
  766. {
  767. setvbuf(fp, NULL, _IOLBF, BUFSIZ);
  768. }
  769. #endif
  770. #ifdef L_ungetc
  771. /*
  772. * NOTE: Only one character of pushback is guaranteed, although sometimes
  773. * it is possible to do more. You have 1 plus as many characters of pushback
  774. * as have been read since that last buffer-fill.
  775. */
  776. int ungetc(c, fp)
  777. int c;
  778. FILE *fp;
  779. {
  780. unsigned char *p;
  781. /* If can't read or there's been an error, or c == EOF, or ungot slot
  782. * already filled, then return EOF */
  783. /*
  784. * This can only happen if an fgetc triggered a read (that filled
  785. * the buffer for case 2 above) and then we ungetc 3 chars.
  786. */
  787. if (!READABLE(fp) || (fp->mode & (__MODE_UNGOT | __MODE_ERR))
  788. || (c == EOF) ) {
  789. return EOF;
  790. }
  791. if (WRITING(fp)) { /* Commit any write-buffered chars. */
  792. fflush(fp);
  793. }
  794. if (fp->bufpos > fp->bufstart) { /* We have space before bufpos. */
  795. p = --fp->bufpos;
  796. } else if (fp->bufread == fp->bufpos) { /* Buffer is empty. */
  797. p = fp->bufread++;
  798. } else {
  799. fp->mode |= __MODE_UNGOT;
  800. p = &(fp->ungot);
  801. }
  802. fp->mode &= ~(__MODE_EOF); /* Clear EOF indicator. */
  803. if (*p != (unsigned char) c) { /* Don't store if same, because could */
  804. *p = (unsigned char) c; /* be sscanf from a const string!!! */
  805. }
  806. return c;
  807. }
  808. #endif
  809. #ifdef L_fopen
  810. #undef fopen
  811. FILE *fopen(const char *__restrict filename,
  812. const char *__restrict mode)
  813. {
  814. return __fopen(filename, -1, NULL, mode);
  815. }
  816. #endif
  817. #ifdef L_freopen
  818. FILE *freopen(__const char *__restrict filename,
  819. __const char *__restrict mode, FILE *__restrict fp)
  820. {
  821. /* fflush file, close the old fd, and reset modes. */
  822. if (WRITING(fp)) { /* If anything in the write buffer... */
  823. fflush(fp); /* write it. */
  824. }
  825. close(fp->fd); /* Close the file. */
  826. fp->mode &= (__MODE_FREEFIL | __MODE_FREEBUF); /* Reset the FILE modes. */
  827. fp->mode |= _IOFBF;
  828. return __fopen(filename, -1, fp, mode);
  829. }
  830. #endif
  831. #ifdef L_fsfopen
  832. FILE *fsfopen(__const char *__restrict filename,
  833. __const char *__restrict mode, FILE *__restrict fp)
  834. {
  835. fp->mode = _IOFBF;
  836. fp->bufstart = fp->unbuf;
  837. fp->bufend = fp->unbuf + sizeof(fp->unbuf);
  838. return __fopen(filename, -1, fp, mode);
  839. }
  840. #endif
  841. #ifdef L_fdopen
  842. #undef fdopen
  843. FILE *fdopen(int fd, __const char *mode)
  844. {
  845. return __fopen(NULL, fd, NULL, mode);
  846. }
  847. #endif
  848. #ifdef L_getchar
  849. #undef getchar
  850. int getchar(void)
  851. {
  852. return getc(_stdin);
  853. }
  854. #endif
  855. #ifdef L_putchar
  856. #undef putchar
  857. int putchar(int c)
  858. {
  859. return putc(c, _stdout);
  860. }
  861. #endif
  862. #ifdef L_clearerr
  863. #undef clearerr
  864. void clearerr(FILE *fp)
  865. {
  866. fp->mode &= ~(__MODE_EOF | __MODE_ERR);
  867. }
  868. #endif
  869. #ifdef L_feof
  870. #undef feof
  871. int feof(FILE *fp)
  872. {
  873. return fp->mode & __MODE_EOF;
  874. }
  875. #endif
  876. #ifdef L_ferror
  877. #undef ferror
  878. int ferror(FILE *fp)
  879. {
  880. return fp->mode & __MODE_ERR;
  881. }
  882. #endif
  883. #ifdef L_fileno
  884. int fileno(FILE *fp)
  885. {
  886. return fp->fd;
  887. }
  888. #endif
  889. #ifdef L_fgetpos
  890. int fgetpos(FILE *fp, fpos_t *pos)
  891. {
  892. fpos_t p;
  893. if (!pos) { /* NULL pointer. */
  894. errno = EINVAL;
  895. return -1;
  896. }
  897. if ((p = ftell(fp)) < 0) { /* ftell failed. */
  898. return -1; /* errno set by ftell. */
  899. }
  900. *pos = p;
  901. return 0;
  902. }
  903. #endif
  904. #ifdef L_fsetpos
  905. int fsetpos(FILE *fp, __const fpos_t *pos)
  906. {
  907. if (pos) { /* Pointer ok. */
  908. return fseek(fp, *pos, SEEK_SET);
  909. }
  910. errno = EINVAL; /* NULL pointer. */
  911. return EOF;
  912. }
  913. #endif