stdio.c 24 KB

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