stdio.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  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. }
  514. if ((fp->mode & __MODE_ERR) ||
  515. (((ref != SEEK_CUR) || offset) && (lseek(fp->fd, offset, ref) < 0))) {
  516. return -1;
  517. }
  518. if (READING(fp)) {
  519. fp->bufpos = fp->bufread = fp->bufstart;
  520. }
  521. fp->mode &= ~(__MODE_EOF | __MODE_UNGOT);
  522. return 0;
  523. }
  524. #endif
  525. #ifdef L_ftell
  526. long ftell(fp)
  527. FILE *fp;
  528. {
  529. /* Note: can't do fflush here since it would discard any ungetc's. */
  530. off_t pos;
  531. pos = lseek(fp->fd, 0, SEEK_CUR); /* Get kernels idea of position. */
  532. if (pos < 0) {
  533. return -1;
  534. }
  535. if (WRITING(fp)) {
  536. pos += (fp->bufpos - fp->bufstart); /* Adjust for buffer position. */
  537. } else if (READING(fp)) {
  538. pos -= (fp->bufread - fp->bufpos); /* Adjust for buffer position. */
  539. if (fp->mode & __MODE_UNGOT) {
  540. --pos;
  541. }
  542. if (pos < 0) { /* ungetcs at start of file? */
  543. __set_errno(EIO);
  544. pos = -1;
  545. }
  546. }
  547. return pos;
  548. }
  549. #endif
  550. #ifdef L__fopen
  551. /*
  552. * This Fopen is all three of fopen, fdopen and freopen. The macros in
  553. * stdio.h show the other names.
  554. */
  555. static __inline FILE *_alloc_stdio_stream(void)
  556. {
  557. FILE *fp;
  558. if (_free_file_list) {
  559. fp = _free_file_list;
  560. _free_file_list = fp->next;
  561. } else if (!(fp = malloc(sizeof(FILE)))) {
  562. return 0;
  563. }
  564. fp->mode = __MODE_FREEFIL | _IOFBF;
  565. /* Initially set to use builtin buffer of FILE structure. */
  566. fp->bufstart = fp->unbuf;
  567. fp->bufend = fp->unbuf + sizeof(fp->unbuf);
  568. return fp;
  569. }
  570. FILE *__fopen(fname, fd, fp, mode)
  571. const char *fname;
  572. int fd;
  573. FILE *fp;
  574. const char *mode;
  575. {
  576. FILE *nfp;
  577. unsigned char *p;
  578. int open_mode;
  579. int cur_mode;
  580. nfp = fp;
  581. /* Parse the mode string arg. */
  582. switch (*mode++) {
  583. case 'r': /* read */
  584. open_mode = O_RDONLY;
  585. break;
  586. case 'w': /* write (create or truncate)*/
  587. open_mode = (O_WRONLY | O_CREAT | O_TRUNC);
  588. break;
  589. case 'a': /* write (create or append) */
  590. open_mode = (O_WRONLY | O_CREAT | O_APPEND);
  591. break;
  592. default: /* illegal mode */
  593. __set_errno(EINVAL);
  594. goto _fopen_ERROR;
  595. }
  596. if ((*mode == 'b')) { /* binary mode (nop for uClibc) */
  597. ++mode;
  598. }
  599. #if O_RDONLY != 0 || O_WRONLY != 1 || O_RDWR != 2
  600. #error Assumption violated concerning open mode constants!
  601. #endif
  602. if (*mode == '+') { /* read-write */
  603. ++mode;
  604. open_mode &= ~(O_RDONLY | O_WRONLY);
  605. open_mode |= O_RDWR;
  606. }
  607. while (*mode) { /* ignore everything else except ... */
  608. if (*mode == 'x') { /* open exclusive -- GNU extension */
  609. open_mode |= O_EXCL;
  610. }
  611. ++mode;
  612. }
  613. if (fp == 0) { /* We need a FILE so allocate it before */
  614. if (!(nfp = _alloc_stdio_stream())) {
  615. return 0;
  616. }
  617. }
  618. if (fname) { /* Open the file itself */
  619. fd = open(fname, open_mode, 0666);
  620. } else { /* fdopen -- check mode is compatible. */
  621. #if O_ACCMODE != 3 || O_RDONLY != 0 || O_WRONLY != 1 || O_RDWR != 2
  622. #error Assumption violated - mode constants
  623. #endif
  624. cur_mode = fcntl(fd, F_GETFL);
  625. if (cur_mode == -1) {
  626. fd = -1;
  627. } else if (!(cur_mode & O_RDWR)
  628. && ((cur_mode ^ open_mode) & O_ACCMODE)) {
  629. __set_errno(EINVAL);
  630. fd = -1;
  631. }
  632. }
  633. if (fd < 0) { /* Error from open or bad arg passed. */
  634. _fopen_ERROR:
  635. if (nfp) {
  636. _free_stdio_stream(nfp);
  637. }
  638. return 0;
  639. }
  640. nfp->fd = fd; /* Set FILE's fd before adding to open list. */
  641. if (fp == 0) { /* Not freopen so... */
  642. nfp->next = __IO_list; /* use newly created FILE and */
  643. __IO_list = nfp; /* add it to the list of open files. */
  644. if ((p = _alloc_stdio_buffer(BUFSIZ)) != 0) {
  645. nfp->bufstart = p;
  646. nfp->bufend = p + BUFSIZ;
  647. nfp->mode |= __MODE_FREEBUF;
  648. }
  649. }
  650. /* Ok, file's ready clear the buffer and save important bits */
  651. nfp->bufpos = nfp->bufstart;
  652. nfp->mode |= isatty(fd);
  653. nfp->bufread = nfp->bufwrite = 0;
  654. if (!(open_mode & O_WRONLY)) {
  655. nfp->bufread = nfp->bufstart;
  656. }
  657. if (open_mode & (O_WRONLY | O_RDWR)) {
  658. nfp->bufwrite = nfp->bufstart;
  659. }
  660. return nfp;
  661. }
  662. #endif
  663. #ifdef L_fclose
  664. int fclose(fp)
  665. FILE *fp;
  666. {
  667. FILE *prev;
  668. FILE *ptr;
  669. int rv;
  670. rv = 0;
  671. if (WRITING(fp)) { /* If there are buffered chars to write... */
  672. rv = fflush(fp); /* write them. */
  673. }
  674. if (close(fp->fd)) { /* Need to close even if fflush fails. */
  675. rv = EOF;
  676. }
  677. prev = 0; /* Remove file from open list. */
  678. for (ptr = __IO_list; ptr ; ptr = ptr->next) {
  679. if (ptr == fp) {
  680. if (prev == 0) {
  681. __IO_list = fp->next;
  682. } else {
  683. prev->next = fp->next;
  684. }
  685. break;
  686. }
  687. prev = ptr;
  688. }
  689. _free_stdio_stream(fp); /* Finally free the stream if necessary. */
  690. return rv;
  691. }
  692. #endif
  693. #ifdef L__free_stdio_stream
  694. /* The following is only called by fclose and _fopen. */
  695. void _free_stdio_stream(FILE *fp)
  696. {
  697. _free_stdio_buffer_of_file(fp); /* Free buffer if necessary. */
  698. if (!(fp->mode & __MODE_FREEFIL)) {
  699. return;
  700. }
  701. /* Note: we generally won't bother checking for bad pointers here. */
  702. if ((fp >= _stdio_streams) && (fp < _stdio_streams + FIXED_STREAMS)) {
  703. assert( (fp - _stdio_streams) % ((_stdio_streams+1) -_stdio_streams)
  704. == 0 );
  705. fp->next = _free_file_list;
  706. _free_file_list = fp;
  707. return;
  708. }
  709. free(fp);
  710. }
  711. #endif
  712. #ifdef L_setbuffer
  713. void setbuffer(FILE *fp, char *buf, size_t size)
  714. {
  715. int mode;
  716. mode = _IOFBF;
  717. if (!buf) {
  718. mode = _IONBF;
  719. }
  720. setvbuf(fp, buf, mode, size);
  721. }
  722. #endif
  723. #ifdef L_setvbuf
  724. int setvbuf(FILE *fp, char *buf, int mode, size_t size)
  725. {
  726. int allocated_buf_flag;
  727. if ((mode < 0) || (mode > 2)) { /* Illegal mode. */
  728. return EOF;
  729. }
  730. #if FLEXIBLE_SETVBUF
  731. /* C89 standard requires no ops before setvbuf, but we can be flexible. */
  732. /* NOTE: This will trash any chars ungetc'd!!! */
  733. if (fseek(fp, 0, SEEK_CUR)) {
  734. return EOF;
  735. }
  736. #endif
  737. /* Note: If size == 2 we could use FILE's builting buffer as well, but */
  738. /* I don't think the benefit is worth the code size increase. */
  739. if ((mode == _IONBF) || (size < 1)) {
  740. size = 1; /* size == 1 _REQUIRED_ for _IONBF!!! */
  741. buf = fp->unbuf;
  742. }
  743. fp->mode &= ~(__MODE_BUF); /* Clear current mode */
  744. fp->mode |= mode; /* and set new one. */
  745. allocated_buf_flag = 0;
  746. if ((!buf) && (size != (fp->bufend - fp->bufstart))) {
  747. /* No buffer supplied and requested size different from current. */
  748. allocated_buf_flag = __MODE_FREEBUF;
  749. if (!(buf = _alloc_stdio_buffer(size))) {
  750. return EOF; /* Keep current buffer. */
  751. }
  752. }
  753. if (buf && (buf != (char *) fp->bufstart)) { /* Want different buffer. */
  754. _free_stdio_buffer_of_file(fp); /* Free the old buffer. */
  755. fp->mode |= allocated_buf_flag; /* Allocated? or FILE's builtin. */
  756. fp->bufstart = buf;
  757. fp->bufend = buf + size;
  758. fp->bufpos = fp->bufstart;
  759. if (READABLE(fp)) {
  760. fp->bufread = fp->bufstart;
  761. }
  762. if (WRITEABLE(fp)) {
  763. fp->bufwrite = fp->bufstart;
  764. }
  765. }
  766. return 0;
  767. }
  768. #endif
  769. #ifdef L_setbuf
  770. void setbuf(FILE *fp, char *buf)
  771. {
  772. int mode;
  773. mode = _IOFBF;
  774. if (!buf) {
  775. mode = _IONBF;
  776. }
  777. setvbuf(fp, buf, mode, BUFSIZ);
  778. }
  779. #endif
  780. #ifdef L_setlinebuf
  781. void setlinebuf(FILE *fp)
  782. {
  783. setvbuf(fp, NULL, _IOLBF, BUFSIZ);
  784. }
  785. #endif
  786. #ifdef L_ungetc
  787. /*
  788. * NOTE: Only one character of pushback is guaranteed, although sometimes
  789. * it is possible to do more. You have 1 plus as many characters of pushback
  790. * as have been read since that last buffer-fill.
  791. */
  792. int ungetc(c, fp)
  793. int c;
  794. FILE *fp;
  795. {
  796. unsigned char *p;
  797. /* If can't read or there's been an error, or c == EOF, or ungot slot
  798. * already filled, then return EOF */
  799. /*
  800. * This can only happen if an fgetc triggered a read (that filled
  801. * the buffer for case 2 above) and then we ungetc 3 chars.
  802. */
  803. if (!READABLE(fp) || (fp->mode & (__MODE_UNGOT | __MODE_ERR))
  804. || (c == EOF) ) {
  805. return EOF;
  806. }
  807. if (WRITING(fp)) { /* Commit any write-buffered chars. */
  808. fflush(fp);
  809. }
  810. if (fp->bufpos > fp->bufstart) { /* We have space before bufpos. */
  811. p = --fp->bufpos;
  812. } else if (fp->bufread == fp->bufpos) { /* Buffer is empty. */
  813. p = fp->bufread++;
  814. } else {
  815. fp->mode |= __MODE_UNGOT;
  816. p = &(fp->ungot);
  817. }
  818. fp->mode &= ~(__MODE_EOF); /* Clear EOF indicator. */
  819. if (*p != (unsigned char) c) { /* Don't store if same, because could */
  820. *p = (unsigned char) c; /* be sscanf from a const string!!! */
  821. }
  822. return c;
  823. }
  824. #endif
  825. #ifdef L_fopen
  826. #undef fopen
  827. FILE *fopen(const char *__restrict filename,
  828. const char *__restrict mode)
  829. {
  830. return __fopen(filename, -1, NULL, mode);
  831. }
  832. #endif
  833. #ifdef L_freopen
  834. FILE *freopen(__const char *__restrict filename,
  835. __const char *__restrict mode, FILE *__restrict fp)
  836. {
  837. /* fflush file, close the old fd, and reset modes. */
  838. if (WRITING(fp)) { /* If anything in the write buffer... */
  839. fflush(fp); /* write it. */
  840. }
  841. close(fp->fd); /* Close the file. */
  842. fp->mode &= (__MODE_FREEFIL | __MODE_FREEBUF); /* Reset the FILE modes. */
  843. fp->mode |= _IOFBF;
  844. return __fopen(filename, -1, fp, mode);
  845. }
  846. #endif
  847. #ifdef L_fsfopen
  848. FILE *fsfopen(__const char *__restrict filename,
  849. __const char *__restrict mode, FILE *__restrict fp)
  850. {
  851. fp->mode = _IOFBF;
  852. fp->bufstart = fp->unbuf;
  853. fp->bufend = fp->unbuf + sizeof(fp->unbuf);
  854. return __fopen(filename, -1, fp, mode);
  855. }
  856. #endif
  857. #ifdef L_fdopen
  858. #undef fdopen
  859. FILE *fdopen(int fd, __const char *mode)
  860. {
  861. return __fopen(NULL, fd, NULL, mode);
  862. }
  863. #endif
  864. #ifdef L_getchar
  865. #undef getchar
  866. int getchar(void)
  867. {
  868. return getc(stdin);
  869. }
  870. #endif
  871. #ifdef L_putchar
  872. #undef putchar
  873. int putchar(int c)
  874. {
  875. return putc(c, stdout);
  876. }
  877. #endif
  878. #ifdef L_clearerr
  879. #undef clearerr
  880. void clearerr(FILE *fp)
  881. {
  882. fp->mode &= ~(__MODE_EOF | __MODE_ERR);
  883. }
  884. #endif
  885. #ifdef L_feof
  886. #undef feof
  887. int feof(FILE *fp)
  888. {
  889. return fp->mode & __MODE_EOF;
  890. }
  891. #endif
  892. #ifdef L_ferror
  893. #undef ferror
  894. int ferror(FILE *fp)
  895. {
  896. return fp->mode & __MODE_ERR;
  897. }
  898. #endif
  899. #ifdef L_fileno
  900. int fileno(FILE *fp)
  901. {
  902. return fp->fd;
  903. }
  904. #endif
  905. #ifdef L_fgetpos
  906. int fgetpos(FILE *fp, fpos_t *pos)
  907. {
  908. fpos_t p;
  909. if (!pos) { /* NULL pointer. */
  910. __set_errno(EINVAL);
  911. return -1;
  912. }
  913. if ((p = ftell(fp)) < 0) { /* ftell failed. */
  914. return -1; /* errno set by ftell. */
  915. }
  916. *pos = p;
  917. return 0;
  918. }
  919. #endif
  920. #ifdef L_fsetpos
  921. int fsetpos(FILE *fp, __const fpos_t *pos)
  922. {
  923. if (pos) { /* Pointer ok. */
  924. return fseek(fp, *pos, SEEK_SET);
  925. }
  926. __set_errno(EINVAL); /* NULL pointer. */
  927. return EOF;
  928. }
  929. #endif