stdio.c 25 KB

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