stdio.c 25 KB

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