stdio.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  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. if (fp->bufread - fp->bufstart >= bytes) { /* If we read all */
  363. fp->mode |= ~__MODE_EOF; /* that was requested, make sure */
  364. } /* EOF flag is clear. */
  365. goto FROM_BUF;
  366. }
  367. while (bytes) {
  368. if ((len = read(fp->fd, p, (unsigned) bytes)) < 0) {
  369. if (errno != EINTR) { /* We weren't interrupted, so error. */
  370. fp->mode |= __MODE_ERR;
  371. break;
  372. }
  373. } else {
  374. if (len == 0) {
  375. fp->mode |= __MODE_EOF;
  376. break;
  377. }
  378. bytes -= len;
  379. p += len;
  380. }
  381. }
  382. }
  383. return (p - (unsigned char *)buf);
  384. }
  385. #endif
  386. #ifdef L_fwrite
  387. /*
  388. * Like fread, fwrite will often be used to write out large chunks of
  389. * data; calling write() directly can be a big win in this case.
  390. *
  391. * But first we check to see if there's space in the buffer.
  392. */
  393. size_t fwrite(buf, size, nelm, fp)
  394. const void *buf;
  395. size_t size;
  396. size_t nelm;
  397. FILE *fp;
  398. {
  399. off_t bytes;
  400. #warning TODO: handle possible overflow for bytes
  401. bytes = size * nelm; /* How many bytes do we want? */
  402. bytes = _uClibc_fwrite((const unsigned char *)buf, bytes, fp);
  403. return bytes/size;
  404. }
  405. #endif
  406. #ifdef L__uClibc_fwrite
  407. /*
  408. * If buf == NULL, fflush.
  409. * If buf != NULL, (fflush and) write
  410. * Returns number of chars written from fp buffer _OR_ from buf.
  411. */
  412. off_t _uClibc_fwrite(const unsigned char *buf, off_t bytes, FILE *fp)
  413. {
  414. unsigned char *p;
  415. int rv, had_newline;
  416. /*
  417. * Fail if stream isn't writable, if we were reading and get an error
  418. * changing over to write mode (ie. can't update stream position),
  419. * or if the stream was already in an error state.
  420. */
  421. if (!WRITEABLE(fp)) { /* Fail if stream isn't writable. */
  422. fp->mode |= __MODE_ERR;
  423. } else if (READING(fp)) { /* If read buffer isn't empty, */
  424. fseek(fp, 0, SEEK_CUR); /* stop reading and update position. */
  425. } else if (READABLE(fp)) {
  426. fp->bufread = fp->bufstart; /* Reset start of read buffer. */
  427. }
  428. if (EOF_OR_ERROR(fp)) {
  429. return 0;
  430. }
  431. p = (unsigned char *)buf;
  432. if (p && (fp->bufpos + bytes <= fp->bufend)) { /* Enough buffer space? */
  433. had_newline = 0;
  434. while (bytes--) {
  435. if (*p == '\n') {
  436. had_newline = 1;
  437. }
  438. *fp->bufpos++ = *p++;
  439. }
  440. if (fp->bufpos < fp->bufend) { /* Buffer is not full. */
  441. fp->bufwrite = fp->bufend;
  442. if ((fp->mode & __MODE_BUF) == _IOLBF) {
  443. fp->bufwrite = fp->bufpos;
  444. if (had_newline) {
  445. goto FFLUSH;
  446. }
  447. }
  448. goto DONE;
  449. }
  450. FFLUSH:
  451. /* If we get here, either buffer is full or we need to flush anyway. */
  452. buf = fp->bufpos - (p - (unsigned char *)buf);
  453. p = NULL;
  454. }
  455. if (!p) { /* buf == NULL means fflush */
  456. p = fp->bufstart;
  457. bytes = fp->bufpos - p;
  458. fp->bufpos = fp->bufwrite = p;
  459. } else if (fp->bufpos > fp->bufstart) { /* If there are buffered chars, */
  460. _uClibc_fwrite(NULL, 0, fp); /* write them. */
  461. if (ferror(fp)) {
  462. return 0;
  463. }
  464. }
  465. while (bytes) {
  466. if ((rv = write(fp->fd, p, bytes)) < 0) {
  467. rv = 0;
  468. if (errno != EINTR) {
  469. break;
  470. }
  471. }
  472. p += rv;
  473. bytes -= rv;
  474. }
  475. if (bytes) {
  476. fp->mode |= __MODE_ERR;
  477. }
  478. DONE:
  479. return (p - (unsigned char *)buf);
  480. }
  481. #endif
  482. #ifdef L_rewind
  483. void rewind(fp)
  484. FILE *fp;
  485. {
  486. clearerr(fp); /* Clear errors first, then seek in case */
  487. fseek(fp, 0, SEEK_SET); /* there is an error seeking. */
  488. }
  489. #endif
  490. #ifdef L_fseek
  491. int fseek(FILE *fp, long int offset, int ref)
  492. {
  493. #if SEEK_SET != 0 || SEEK_CUR != 1 || SEEK_END != 2
  494. #error Assumption violated -- values of SEEK_SET, SEEK_CUR, SEEK_END
  495. #endif
  496. if ((ref < 0) || (ref > 2)) {
  497. __set_errno(EINVAL);
  498. return -1;
  499. }
  500. if (WRITING(fp)) {
  501. fflush(fp); /* We'll deal with errors below. */
  502. /* After fflush, bufpos is at CUR. */
  503. } else if (READING(fp)) {
  504. if (ref == SEEK_CUR) {
  505. /* Correct offset to take into account position in buffer. */
  506. offset -= (fp->bufread - fp->bufpos);
  507. if (fp->mode & __MODE_UNGOT) { /* If we had an ungetc'd char, */
  508. --offset; /* adjust offset (clear flag below). */
  509. }
  510. }
  511. fp->bufpos = fp->bufread = fp->bufstart;
  512. }
  513. if ((fp->mode & __MODE_ERR) ||
  514. (((ref != SEEK_CUR) || offset) && (lseek(fp->fd, offset, ref) < 0))) {
  515. fp->mode |= __MODE_ERR; /* Possibly redundant, but doesn't hurt. */
  516. return -1;
  517. }
  518. fp->mode &= ~(__MODE_EOF | __MODE_UNGOT);
  519. return 0;
  520. }
  521. #endif
  522. #ifdef L_ftell
  523. long ftell(fp)
  524. FILE *fp;
  525. {
  526. /* Note: can't do fflush here since it would discard any ungetc's. */
  527. off_t pos;
  528. pos = lseek(fp->fd, 0, SEEK_CUR); /* Get kernels idea of position. */
  529. if (pos < 0) {
  530. return -1;
  531. }
  532. if (WRITING(fp)) {
  533. pos += (fp->bufpos - fp->bufstart); /* Adjust for buffer position. */
  534. } else if (READING(fp)) {
  535. pos -= (fp->bufread - fp->bufpos); /* Adjust for buffer position. */
  536. if (fp->mode & __MODE_UNGOT) {
  537. --pos;
  538. }
  539. if (pos < 0) { /* ungetcs at start of file? */
  540. __set_errno(EIO);
  541. pos = -1;
  542. }
  543. }
  544. return pos;
  545. }
  546. #endif
  547. #ifdef L__fopen
  548. /*
  549. * This Fopen is all three of fopen, fdopen and freopen. The macros in
  550. * stdio.h show the other names.
  551. */
  552. static __inline FILE *_alloc_stdio_stream(void)
  553. {
  554. FILE *fp;
  555. if (_free_file_list) {
  556. fp = _free_file_list;
  557. _free_file_list = fp->next;
  558. } else if (!(fp = malloc(sizeof(FILE)))) {
  559. return 0;
  560. }
  561. fp->mode = __MODE_FREEFIL | _IOFBF;
  562. /* Initially set to use builtin buffer of FILE structure. */
  563. fp->bufstart = fp->unbuf;
  564. fp->bufend = fp->unbuf + sizeof(fp->unbuf);
  565. return fp;
  566. }
  567. FILE *__fopen(fname, fd, fp, mode)
  568. const char *fname;
  569. int fd;
  570. FILE *fp;
  571. const char *mode;
  572. {
  573. FILE *nfp;
  574. unsigned char *p;
  575. int open_mode;
  576. int cur_mode;
  577. nfp = fp;
  578. /* Parse the mode string arg. */
  579. switch (*mode++) {
  580. case 'r': /* read */
  581. open_mode = O_RDONLY;
  582. break;
  583. case 'w': /* write (create or truncate)*/
  584. open_mode = (O_WRONLY | O_CREAT | O_TRUNC);
  585. break;
  586. case 'a': /* write (create or append) */
  587. open_mode = (O_WRONLY | O_CREAT | O_APPEND);
  588. break;
  589. default: /* illegal mode */
  590. __set_errno(EINVAL);
  591. goto _fopen_ERROR;
  592. }
  593. if ((*mode == 'b')) { /* binary mode (nop for uClibc) */
  594. ++mode;
  595. }
  596. #if O_RDONLY != 0 || O_WRONLY != 1 || O_RDWR != 2
  597. #error Assumption violated concerning open mode constants!
  598. #endif
  599. if (*mode == '+') { /* read-write */
  600. ++mode;
  601. open_mode &= ~(O_RDONLY | O_WRONLY);
  602. open_mode |= O_RDWR;
  603. }
  604. while (*mode) { /* ignore everything else except ... */
  605. if (*mode == 'x') { /* open exclusive -- GNU extension */
  606. open_mode |= O_EXCL;
  607. }
  608. ++mode;
  609. }
  610. if (fp == 0) { /* We need a FILE so allocate it before */
  611. if (!(nfp = _alloc_stdio_stream())) {
  612. return 0;
  613. }
  614. }
  615. if (fname) { /* Open the file itself */
  616. fd = open(fname, open_mode, 0666);
  617. } else { /* fdopen -- check mode is compatible. */
  618. #if O_ACCMODE != 3 || O_RDONLY != 0 || O_WRONLY != 1 || O_RDWR != 2
  619. #error Assumption violated - mode constants
  620. #endif
  621. cur_mode = fcntl(fd, F_GETFL);
  622. if (cur_mode == -1) {
  623. fd = -1;
  624. } else if (!(cur_mode & O_RDWR)
  625. && ((cur_mode ^ open_mode) & O_ACCMODE)) {
  626. __set_errno(EINVAL);
  627. fd = -1;
  628. }
  629. }
  630. if (fd < 0) { /* Error from open or bad arg passed. */
  631. _fopen_ERROR:
  632. if (nfp) {
  633. _free_stdio_stream(nfp);
  634. }
  635. return 0;
  636. }
  637. nfp->fd = fd; /* Set FILE's fd before adding to open list. */
  638. if (fp == 0) { /* Not freopen so... */
  639. nfp->next = __IO_list; /* use newly created FILE and */
  640. __IO_list = nfp; /* add it to the list of open files. */
  641. if ((p = _alloc_stdio_buffer(BUFSIZ)) != 0) {
  642. nfp->bufstart = p;
  643. nfp->bufend = p + BUFSIZ;
  644. nfp->mode |= __MODE_FREEBUF;
  645. }
  646. }
  647. /* Ok, file's ready clear the buffer and save important bits */
  648. nfp->bufpos = nfp->bufstart;
  649. nfp->mode |= isatty(fd);
  650. nfp->bufread = nfp->bufwrite = 0;
  651. if (!(open_mode & O_WRONLY)) {
  652. nfp->bufread = nfp->bufstart;
  653. }
  654. if (open_mode & (O_WRONLY | O_RDWR)) {
  655. nfp->bufwrite = nfp->bufstart;
  656. }
  657. return nfp;
  658. }
  659. #endif
  660. #ifdef L_fclose
  661. int fclose(fp)
  662. FILE *fp;
  663. {
  664. FILE *prev;
  665. FILE *ptr;
  666. int rv;
  667. rv = 0;
  668. if (WRITING(fp)) { /* If there are buffered chars to write... */
  669. rv = fflush(fp); /* write them. */
  670. }
  671. if (close(fp->fd)) { /* Need to close even if fflush fails. */
  672. rv = EOF;
  673. }
  674. prev = 0; /* Remove file from open list. */
  675. for (ptr = __IO_list; ptr ; ptr = ptr->next) {
  676. if (ptr == fp) {
  677. if (prev == 0) {
  678. __IO_list = fp->next;
  679. } else {
  680. prev->next = fp->next;
  681. }
  682. break;
  683. }
  684. prev = ptr;
  685. }
  686. _free_stdio_stream(fp); /* Finally free the stream if necessary. */
  687. return rv;
  688. }
  689. #endif
  690. #ifdef L__free_stdio_stream
  691. /* The following is only called by fclose and _fopen. */
  692. void _free_stdio_stream(FILE *fp)
  693. {
  694. _free_stdio_buffer_of_file(fp); /* Free buffer if necessary. */
  695. if (!(fp->mode & __MODE_FREEFIL)) {
  696. return;
  697. }
  698. /* Note: we generally won't bother checking for bad pointers here. */
  699. if ((fp >= _stdio_streams) && (fp < _stdio_streams + FIXED_STREAMS)) {
  700. assert( (fp - _stdio_streams) % ((_stdio_streams+1) -_stdio_streams)
  701. == 0 );
  702. fp->next = _free_file_list;
  703. _free_file_list = fp;
  704. return;
  705. }
  706. free(fp);
  707. }
  708. #endif
  709. #ifdef L_setbuffer
  710. void setbuffer(FILE *fp, char *buf, size_t size)
  711. {
  712. int mode;
  713. mode = _IOFBF;
  714. if (!buf) {
  715. mode = _IONBF;
  716. }
  717. setvbuf(fp, buf, mode, size);
  718. }
  719. #endif
  720. #ifdef L_setvbuf
  721. int setvbuf(FILE *fp, char *buf, int mode, size_t size)
  722. {
  723. int allocated_buf_flag;
  724. if ((mode < 0) || (mode > 2)) { /* Illegal mode. */
  725. return EOF;
  726. }
  727. #if FLEXIBLE_SETVBUF
  728. /* C89 standard requires no ops before setvbuf, but we can be flexible. */
  729. /* NOTE: This will trash any chars ungetc'd!!! */
  730. if (fseek(fp, 0, SEEK_CUR)) {
  731. return EOF;
  732. }
  733. #endif
  734. /* Note: If size == 2 we could use FILE's builting buffer as well, but */
  735. /* I don't think the benefit is worth the code size increase. */
  736. if ((mode == _IONBF) || (size < 1)) {
  737. size = 1; /* size == 1 _REQUIRED_ for _IONBF!!! */
  738. buf = fp->unbuf;
  739. }
  740. fp->mode &= ~(__MODE_BUF); /* Clear current mode */
  741. fp->mode |= mode; /* and set new one. */
  742. allocated_buf_flag = 0;
  743. if ((!buf) && (size != (fp->bufend - fp->bufstart))) {
  744. /* No buffer supplied and requested size different from current. */
  745. allocated_buf_flag = __MODE_FREEBUF;
  746. if (!(buf = _alloc_stdio_buffer(size))) {
  747. return EOF; /* Keep current buffer. */
  748. }
  749. }
  750. if (buf && (buf != (char *) fp->bufstart)) { /* Want different buffer. */
  751. _free_stdio_buffer_of_file(fp); /* Free the old buffer. */
  752. fp->mode |= allocated_buf_flag; /* Allocated? or FILE's builtin. */
  753. fp->bufstart = buf;
  754. fp->bufend = buf + size;
  755. fp->bufpos = fp->bufstart;
  756. if (READABLE(fp)) {
  757. fp->bufread = fp->bufstart;
  758. }
  759. if (WRITEABLE(fp)) {
  760. fp->bufwrite = fp->bufstart;
  761. }
  762. }
  763. return 0;
  764. }
  765. #endif
  766. #ifdef L_setbuf
  767. void setbuf(FILE *fp, char *buf)
  768. {
  769. int mode;
  770. mode = _IOFBF;
  771. if (!buf) {
  772. mode = _IONBF;
  773. }
  774. setvbuf(fp, buf, mode, BUFSIZ);
  775. }
  776. #endif
  777. #ifdef L_setlinebuf
  778. void setlinebuf(FILE *fp)
  779. {
  780. setvbuf(fp, NULL, _IOLBF, BUFSIZ);
  781. }
  782. #endif
  783. #ifdef L_ungetc
  784. /*
  785. * NOTE: Only one character of pushback is guaranteed, although sometimes
  786. * it is possible to do more. You have 1 plus as many characters of pushback
  787. * as have been read since that last buffer-fill.
  788. */
  789. int ungetc(c, fp)
  790. int c;
  791. FILE *fp;
  792. {
  793. unsigned char *p;
  794. /* If can't read or there's been an error, or c == EOF, or ungot slot
  795. * already filled, then return EOF */
  796. /*
  797. * This can only happen if an fgetc triggered a read (that filled
  798. * the buffer for case 2 above) and then we ungetc 3 chars.
  799. */
  800. if (!READABLE(fp) || (fp->mode & (__MODE_UNGOT | __MODE_ERR))
  801. || (c == EOF) ) {
  802. return EOF;
  803. }
  804. if (WRITING(fp)) { /* Commit any write-buffered chars. */
  805. fflush(fp);
  806. }
  807. if (fp->bufpos > fp->bufstart) { /* We have space before bufpos. */
  808. p = --fp->bufpos;
  809. } else if (fp->bufread == fp->bufpos) { /* Buffer is empty. */
  810. p = fp->bufread++;
  811. } else {
  812. fp->mode |= __MODE_UNGOT;
  813. p = &(fp->ungot);
  814. }
  815. fp->mode &= ~(__MODE_EOF); /* Clear EOF indicator. */
  816. if (*p != (unsigned char) c) { /* Don't store if same, because could */
  817. *p = (unsigned char) c; /* be sscanf from a const string!!! */
  818. }
  819. return c;
  820. }
  821. #endif
  822. #ifdef L_fopen
  823. #undef fopen
  824. FILE *fopen(const char *__restrict filename,
  825. const char *__restrict mode)
  826. {
  827. return __fopen(filename, -1, NULL, mode);
  828. }
  829. #endif
  830. #ifdef L_freopen
  831. FILE *freopen(__const char *__restrict filename,
  832. __const char *__restrict mode, FILE *__restrict fp)
  833. {
  834. /* fflush file, close the old fd, and reset modes. */
  835. if (WRITING(fp)) { /* If anything in the write buffer... */
  836. fflush(fp); /* write it. */
  837. }
  838. close(fp->fd); /* Close the file. */
  839. fp->mode &= (__MODE_FREEFIL | __MODE_FREEBUF); /* Reset the FILE modes. */
  840. fp->mode |= _IOFBF;
  841. return __fopen(filename, -1, fp, mode);
  842. }
  843. #endif
  844. #ifdef L_fsfopen
  845. FILE *fsfopen(__const char *__restrict filename,
  846. __const char *__restrict mode, FILE *__restrict fp)
  847. {
  848. fp->mode = _IOFBF;
  849. fp->bufstart = fp->unbuf;
  850. fp->bufend = fp->unbuf + sizeof(fp->unbuf);
  851. return __fopen(filename, -1, fp, mode);
  852. }
  853. #endif
  854. #ifdef L_fdopen
  855. #undef fdopen
  856. FILE *fdopen(int fd, __const char *mode)
  857. {
  858. return __fopen(NULL, fd, NULL, mode);
  859. }
  860. #endif
  861. #ifdef L_getchar
  862. #undef getchar
  863. int getchar(void)
  864. {
  865. return getc(_stdin);
  866. }
  867. #endif
  868. #ifdef L_putchar
  869. #undef putchar
  870. int putchar(int c)
  871. {
  872. return putc(c, _stdout);
  873. }
  874. #endif
  875. #ifdef L_clearerr
  876. #undef clearerr
  877. void clearerr(FILE *fp)
  878. {
  879. fp->mode &= ~(__MODE_EOF | __MODE_ERR);
  880. }
  881. #endif
  882. #ifdef L_feof
  883. #undef feof
  884. int feof(FILE *fp)
  885. {
  886. return fp->mode & __MODE_EOF;
  887. }
  888. #endif
  889. #ifdef L_ferror
  890. #undef ferror
  891. int ferror(FILE *fp)
  892. {
  893. return fp->mode & __MODE_ERR;
  894. }
  895. #endif
  896. #ifdef L_fileno
  897. int fileno(FILE *fp)
  898. {
  899. return fp->fd;
  900. }
  901. #endif
  902. #ifdef L_fgetpos
  903. int fgetpos(FILE *fp, fpos_t *pos)
  904. {
  905. fpos_t p;
  906. if (!pos) { /* NULL pointer. */
  907. __set_errno(EINVAL);
  908. return -1;
  909. }
  910. if ((p = ftell(fp)) < 0) { /* ftell failed. */
  911. return -1; /* errno set by ftell. */
  912. }
  913. *pos = p;
  914. return 0;
  915. }
  916. #endif
  917. #ifdef L_fsetpos
  918. int fsetpos(FILE *fp, __const fpos_t *pos)
  919. {
  920. if (pos) { /* Pointer ok. */
  921. return fseek(fp, *pos, SEEK_SET);
  922. }
  923. __set_errno(EINVAL); /* NULL pointer. */
  924. return EOF;
  925. }
  926. #endif