stdio.c 25 KB

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