stdio.c 24 KB

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