stdio.c 24 KB

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