stdio.c 25 KB

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