123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119 |
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/types.h>
- #include <malloc.h>
- #include <errno.h>
- #include <string.h>
- #include <assert.h>
- #include <limits.h>
- extern off_t _uClibc_fwrite(const unsigned char *buf, off_t bytes, FILE *fp);
- extern off_t _uClibc_fread(unsigned char *buf, off_t bytes, FILE *fp);
- extern FILE *__fopen __P((__const char *__restrict __filename, int __fd,
- FILE *__restrict __stream, __const char *__restrict __mode,
- int extra_modes));
- #define READING(fp) (fp->bufstart < fp->bufread)
- #define WRITING(fp) (fp->bufwrite > fp->bufstart)
- #define READABLE(fp) (fp->bufread != 0)
- #define WRITEABLE(fp) (fp->bufwrite != 0)
- #define EOF_OR_ERROR(fp) (fp->mode & (__MODE_EOF | __MODE_ERR))
- #define FIXED_STREAMS 3
- #define FIXED_BUFFERS 2
- #define DISABLE_DYNAMIC 0
- #define FLEXIBLE_SETVBUF 0
- #if DISABLE_DYNAMIC != 0
- #undef malloc
- #undef free
- #define malloc(x) 0
- #define free(x)
- #endif
- extern FILE *__IO_list;
- extern FILE *_free_file_list;
- extern char _free_buffer_index;
- extern FILE _stdio_streams[FIXED_STREAMS];
- extern unsigned char _fixed_buffers[FIXED_BUFFERS * BUFSIZ];
- extern unsigned char *_alloc_stdio_buffer(size_t size);
- extern void _free_stdio_buffer_of_file(FILE *fp);
- extern void _free_stdio_stream(FILE *fp);
- #ifdef L__alloc_stdio_buffer
- unsigned char *_alloc_stdio_buffer(size_t size)
- {
- unsigned char *buf;
- if ((size == BUFSIZ) && (_free_buffer_index < FIXED_BUFFERS)) {
- buf = _fixed_buffers + ((unsigned int)_free_buffer_index) * BUFSIZ;
- _free_buffer_index = *buf;
- return buf;
- }
- return malloc(size);
- }
- #endif
- #ifdef L__free_stdio_buffer_of_file
- void _free_stdio_buffer_of_file(FILE *fp)
- {
- unsigned char *buf;
- if (!(fp->mode & __MODE_FREEBUF)) {
- return;
- }
- fp->mode &= ~(__MODE_FREEBUF);
- buf = fp->bufstart;
- if ((buf >= _fixed_buffers)
- && (buf < _fixed_buffers + (FIXED_BUFFERS * BUFSIZ))) {
- *buf = _free_buffer_index;
- _free_buffer_index = (buf - _fixed_buffers)/BUFSIZ;
- return;
- }
- free(buf);
- }
- #endif
- #ifdef L__stdio_init
- #if FIXED_BUFFERS < 2
- #error FIXED_BUFFERS must be >= 2
- #endif
- #if FIXED_BUFFERS >= UCHAR_MAX
- #error FIXED_BUFFERS must be < UCHAR_MAX
- #endif
- #define bufin (_fixed_buffers)
- #define bufout (_fixed_buffers + BUFSIZ)
- #define buferr (_stdio_streams[2].unbuf)
- unsigned char _fixed_buffers[FIXED_BUFFERS * BUFSIZ];
- #if FIXED_STREAMS < 3
- #error FIXED_STREAMS must be >= 3
- #endif
- FILE _stdio_streams[FIXED_STREAMS] = {
- {bufin, bufin, 0, bufin, bufin + BUFSIZ,
- _stdio_streams + 1,
- 0, _IOFBF | __MODE_FREEFIL | __MODE_FREEBUF | __MODE_TIED },
- {bufout, 0, bufout, bufout, bufout + BUFSIZ,
- _stdio_streams + 2,
- 1, _IOFBF | __MODE_FREEFIL | __MODE_FREEBUF | __MODE_TIED },
- {buferr, 0, buferr, buferr, buferr + 1,
- NULL,
- 2, _IONBF | __MODE_FREEFIL }
- };
- FILE *stdin = _stdio_streams + 0;
- FILE *stdout = _stdio_streams + 1;
- FILE *stderr = _stdio_streams + 2;
- FILE *__IO_list = _stdio_streams;
- FILE *_free_file_list = 0;
- char _free_buffer_index = FIXED_BUFFERS;
- void __stdio_flush_buffers(void)
- {
- FILE *fp;
- for (fp = __IO_list; fp; fp = fp->next) {
- if (WRITEABLE(fp)) {
-
- fcntl(fp->fd, F_SETFL, O_NONBLOCK);
- fflush(fp);
- }
- }
- }
- void __init_stdio(void)
- {
- #if (FIXED_BUFFERS > 2) || (FIXED_STREAMS > 3)
- int i;
- #endif
- #if FIXED_BUFFERS > 2
- _free_buffer_index = 2;
- for ( i = 2 ; i < FIXED_BUFFERS ; i++ ) {
- _fixed_buffers[i*BUFSIZ] = i;
- }
- #endif
- #if FIXED_STREAMS > 3
- _free_file_list = _stdio_streams + 3;
- for ( i = 3 ; i < FIXED_STREAMS-1 ; i++ ) {
- _stdio_streams[i].next = _stdio_streams + i + 1;
- }
- _stdio_streams[i].next = 0;
- #endif
- #if _IOFBF != 0 || _IOLBF != 1
- #error Assumption violated -- values of _IOFBF and/or _IOLBF
- #endif
-
- _stdio_streams[1].mode |= isatty(1);
- }
- #endif
- #ifdef L_fputc
- int fputc(int c, FILE *fp)
- {
- unsigned char buf[1];
- *buf = (unsigned char) c;
- if (_uClibc_fwrite(buf, 1, fp)) {
- return (unsigned char) c;
- }
- return EOF;
- }
- #endif
- #ifdef L_fgetc
- int fgetc(FILE *fp)
- {
- unsigned char buf[1];
- if (_uClibc_fread(buf, 1, fp)) {
- return *buf;
- }
- return EOF;
- }
- #endif
- #ifdef L_fflush
- int fflush(FILE *fp)
- {
- int rv;
- rv = 0;
- if (fp == NULL) {
- for (fp = __IO_list; fp; fp = fp->next) {
- if (WRITEABLE(fp) && fflush(fp)) {
- rv = EOF;
- }
- }
- } else if (WRITING(fp)) {
- _uClibc_fwrite(NULL, 0, fp);
- if (fp->mode & __MODE_ERR) {
- rv = -1;
- }
- } else if (!WRITEABLE(fp)) {
-
- __set_errno(EBADF);
- rv = -1;
- }
- return rv;
- }
- #endif
- #ifdef L_fgets
- char *fgets(char *s, int count, FILE *fp)
- {
- int ch;
- char *p;
-
- p = s;
- while (count-- > 1) {
- ch = getc(fp);
- if (ch == EOF) {
- break;
- }
- *p++ = ch;
- if (ch == '\n') {
- break;
- }
- }
- if (ferror(fp) || (s == p)) {
- return 0;
- }
- *p = 0;
- return s;
- }
- #endif
- #ifdef L_gets
- link_warning (gets, "the `gets' function is dangerous and should not be used.")
- char *gets(char *str)
- {
-
- return fgets(str, INT_MAX, stdin);
- }
- #endif
- #ifdef L_fputs
- int fputs(const char *str, FILE *fp)
- {
- int n;
- n = strlen(str);
- _uClibc_fwrite((const unsigned char *)str, n, fp);
- if (fp->mode & __MODE_ERR) {
- n = EOF;
- }
- return n;
- }
- #endif
- #ifdef L_puts
- int puts(const char *str)
- {
- int n;
- n = fputs(str, stdout);
- if (fputc('\n', stdout) == EOF) {
- return EOF;
- }
- return n + 1;
- }
- #endif
- #ifdef L_fread
- size_t fread(buf, size, nelm, fp)
- void *buf;
- size_t size;
- size_t nelm;
- FILE *fp;
- {
- unsigned char *p;
- unsigned char *q;
- #warning TODO: handle possible overflow of size * nelm
- p = (unsigned char *) buf;
- q = p + (size * nelm);
- while ((p < q) && !EOF_OR_ERROR(fp)) {
- p += _uClibc_fread(p, q - p, fp);
- }
- return (p - (unsigned char *) buf)/size;
- }
- #endif
- #ifdef L__uClibc_fread
- off_t _uClibc_fread(unsigned char *buf, off_t bytes, FILE *fp)
- {
- unsigned char *p;
- off_t len;
- if (!READABLE(fp)) {
- fp->mode |= __MODE_ERR;
- } else if (WRITING(fp)) {
- fflush(fp);
- } else if (fp->mode & stdout->mode & __MODE_TIED) {
- fflush(stdout);
- }
- if (EOF_OR_ERROR(fp) || (bytes <= 0)) {
- return 0;
- }
- p = (unsigned char *) buf;
- if (fp->mode & __MODE_UNGOT) {
- fp->mode ^= __MODE_UNGOT;
- *p++ = fp->ungot;
- --bytes;
- }
- FROM_BUF:
- len = fp->bufread - fp->bufpos;
- if (len > bytes) {
- len = bytes;
- }
-
- bytes -= len;
- while (len--) {
- *p++ = *fp->bufpos++;
- }
- if (bytes && !EOF_OR_ERROR(fp)) {
- if (bytes < fp->bufend - fp->bufstart) {
- fp->bufpos = fp->bufread = fp->bufstart;
- fp->bufread += _uClibc_fread(fp->bufstart,
- fp->bufend - fp->bufstart, fp);
- goto FROM_BUF;
- }
- TRY_READ:
- len = read(fp->fd, p, (unsigned) bytes);
- if (len < 0) {
- if (errno == EINTR) {
- goto TRY_READ;
- }
- fp->mode |= __MODE_ERR;
- } else {
- p += len;
- if (len == 0) {
- fp->mode |= __MODE_EOF;
- }
- }
- }
- return (p - (unsigned char *)buf);
- }
- #endif
- #ifdef L_fwrite
- size_t fwrite(buf, size, nelm, fp)
- const void *buf;
- size_t size;
- size_t nelm;
- FILE *fp;
- {
- off_t bytes;
- #warning TODO: handle possible overflow for bytes
- bytes = size * nelm;
- bytes = _uClibc_fwrite((const unsigned char *)buf, bytes, fp);
- return bytes/size;
- }
- #endif
- #ifdef L__uClibc_fwrite
- off_t _uClibc_fwrite(const unsigned char *buf, off_t bytes, FILE *fp)
- {
- unsigned char *p;
- int rv, had_newline;
-
- if (!WRITEABLE(fp)) {
- fp->mode |= __MODE_ERR;
- } else if (READING(fp)) {
- fseek(fp, 0, SEEK_CUR);
- } else if (READABLE(fp)) {
- fp->bufread = fp->bufstart;
- }
- if (EOF_OR_ERROR(fp)) {
- return 0;
- }
- p = (unsigned char *)buf;
- if (p && (fp->bufpos + bytes <= fp->bufend)) {
- had_newline = 0;
- while (bytes--) {
- if (*p == '\n') {
- had_newline = 1;
- }
- *fp->bufpos++ = *p++;
- }
- if (fp->bufpos < fp->bufend) {
- fp->bufwrite = fp->bufend;
- if ((fp->mode & __MODE_BUF) == _IOLBF) {
- fp->bufwrite = fp->bufpos;
- if (had_newline) {
- goto FFLUSH;
- }
- }
- goto DONE;
- }
- FFLUSH:
-
- buf = fp->bufpos - (p - (unsigned char *)buf);
- p = NULL;
- }
- if (!p) {
- p = fp->bufstart;
- bytes = fp->bufpos - p;
- fp->bufpos = fp->bufwrite = p;
- } else if (fp->bufpos > fp->bufstart) {
- _uClibc_fwrite(NULL, 0, fp);
- if (ferror(fp)) {
- return 0;
- }
- }
- while (bytes) {
- if ((rv = write(fp->fd, p, bytes)) < 0) {
- rv = 0;
- if (errno != EINTR) {
- break;
- }
- }
- p += rv;
- bytes -= rv;
- }
- if (bytes) {
- fp->mode |= __MODE_ERR;
- }
- DONE:
- return (p - (unsigned char *)buf);
- }
- #endif
- #ifdef L_rewind
- void rewind(fp)
- FILE *fp;
- {
- clearerr(fp);
- fseek(fp, 0, SEEK_SET);
- }
- #endif
- #ifdef L_fseek
- int fseek(FILE *fp, long int offset, int ref)
- {
- #if SEEK_SET != 0 || SEEK_CUR != 1 || SEEK_END != 2
- #error Assumption violated -- values of SEEK_SET, SEEK_CUR, SEEK_END
- #endif
- if ((ref < 0) || (ref > 2)) {
- __set_errno(EINVAL);
- return -1;
- }
- if (WRITING(fp)) {
- fflush(fp);
-
- } else if (READING(fp)) {
- if (ref == SEEK_CUR) {
-
- offset -= (fp->bufread - fp->bufpos);
- if (fp->mode & __MODE_UNGOT) {
- --offset;
- }
- }
- }
- if ((fp->mode & __MODE_ERR) ||
- (((ref != SEEK_CUR) || offset) && (lseek(fp->fd, offset, ref) < 0))) {
- return -1;
- }
- if (READING(fp)) {
- fp->bufpos = fp->bufread = fp->bufstart;
- }
- fp->mode &= ~(__MODE_EOF | __MODE_UNGOT);
- return 0;
- }
- #endif
- #ifdef L_ftell
- long ftell(fp)
- FILE *fp;
- {
-
- off_t pos;
- pos = lseek(fp->fd, 0, SEEK_CUR);
- if (pos < 0) {
- return -1;
- }
- if (WRITING(fp)) {
- pos += (fp->bufpos - fp->bufstart);
- } else if (READING(fp)) {
- pos -= (fp->bufread - fp->bufpos);
- if (fp->mode & __MODE_UNGOT) {
- --pos;
- }
- if (pos < 0) {
- __set_errno(EIO);
- pos = -1;
- }
- }
- return pos;
- }
- #endif
- #ifdef L__fopen
- static __inline FILE *_alloc_stdio_stream(void)
- {
- FILE *fp;
- if (_free_file_list) {
- fp = _free_file_list;
- _free_file_list = fp->next;
- } else if (!(fp = malloc(sizeof(FILE)))) {
- return 0;
- }
- fp->mode = __MODE_FREEFIL | _IOFBF;
-
- fp->bufstart = fp->unbuf;
- fp->bufend = fp->unbuf + sizeof(fp->unbuf);
- return fp;
- }
- FILE *__fopen(fname, fd, fp, mode, extra_modes)
- const char *fname;
- int fd;
- FILE *fp;
- const char *mode;
- int extra_modes;
- {
- FILE *nfp;
- unsigned char *p;
- int open_mode;
- int cur_mode;
- nfp = fp;
-
- switch (*mode++) {
- case 'r':
- open_mode = O_RDONLY | extra_modes;
- break;
- case 'w':
- open_mode = (O_WRONLY | O_CREAT | O_TRUNC | extra_modes);
- break;
- case 'a':
- open_mode = (O_WRONLY | O_CREAT | O_APPEND | extra_modes);
- break;
- default:
- __set_errno(EINVAL);
- goto _fopen_ERROR;
- }
- if ((*mode == 'b')) {
- ++mode;
- }
- #if O_RDONLY != 0 || O_WRONLY != 1 || O_RDWR != 2
- #error Assumption violated concerning open mode constants!
- #endif
- if (*mode == '+') {
- ++mode;
- open_mode &= ~(O_RDONLY | O_WRONLY);
- open_mode |= O_RDWR;
- }
- while (*mode) {
- if (*mode == 'x') {
- open_mode |= O_EXCL;
- }
- ++mode;
- }
- if (fp == 0) {
- if (!(nfp = _alloc_stdio_stream())) {
- return 0;
- }
- }
- if (fname) {
- fd = open(fname, open_mode, 0666);
- } else {
- #if O_ACCMODE != 3 || O_RDONLY != 0 || O_WRONLY != 1 || O_RDWR != 2
- #error Assumption violated - mode constants
- #endif
- cur_mode = fcntl(fd, F_GETFL);
- if (cur_mode == -1) {
- fd = -1;
- } else if (!(cur_mode & O_RDWR)
- && ((cur_mode ^ open_mode) & O_ACCMODE)) {
- __set_errno(EINVAL);
- fd = -1;
- }
- }
- if (fd < 0) {
- _fopen_ERROR:
- if (nfp) {
- _free_stdio_stream(nfp);
- }
- return 0;
- }
- nfp->fd = fd;
- if (fp == 0) {
- nfp->next = __IO_list;
- __IO_list = nfp;
- if ((p = _alloc_stdio_buffer(BUFSIZ)) != 0) {
- nfp->bufstart = p;
- nfp->bufend = p + BUFSIZ;
- nfp->mode |= __MODE_FREEBUF;
- }
- }
-
- nfp->bufpos = nfp->bufstart;
- nfp->mode |= isatty(fd);
- nfp->bufread = nfp->bufwrite = 0;
- if (!(open_mode & O_WRONLY)) {
- nfp->bufread = nfp->bufstart;
- }
- if (open_mode & (O_WRONLY | O_RDWR)) {
- nfp->bufwrite = nfp->bufstart;
- }
- return nfp;
- }
- #endif
- #ifdef L_fclose
- int fclose(fp)
- FILE *fp;
- {
- FILE *prev;
- FILE *ptr;
- int rv;
- rv = 0;
- if (WRITING(fp)) {
- rv = fflush(fp);
- }
- if (close(fp->fd)) {
- rv = EOF;
- }
- prev = 0;
- for (ptr = __IO_list; ptr ; ptr = ptr->next) {
- if (ptr == fp) {
- if (prev == 0) {
- __IO_list = fp->next;
- } else {
- prev->next = fp->next;
- }
- break;
- }
- prev = ptr;
- }
- _free_stdio_stream(fp);
- return rv;
- }
- #endif
- #ifdef L__free_stdio_stream
- void _free_stdio_stream(FILE *fp)
- {
- _free_stdio_buffer_of_file(fp);
- if (!(fp->mode & __MODE_FREEFIL)) {
- return;
- }
-
- if ((fp >= _stdio_streams) && (fp < _stdio_streams + FIXED_STREAMS)) {
- assert( (fp - _stdio_streams) % ((_stdio_streams+1) -_stdio_streams)
- == 0 );
- fp->next = _free_file_list;
- _free_file_list = fp;
- return;
- }
- free(fp);
- }
- #endif
- #ifdef L_setbuffer
- void setbuffer(FILE *fp, char *buf, size_t size)
- {
- int mode;
- mode = _IOFBF;
- if (!buf) {
- mode = _IONBF;
- }
- setvbuf(fp, buf, mode, size);
- }
- #endif
- #ifdef L_setvbuf
- int setvbuf(FILE *fp, char *buf, int mode, size_t size)
- {
- int allocated_buf_flag;
- if ((mode < 0) || (mode > 2)) {
- return EOF;
- }
- #if FLEXIBLE_SETVBUF
-
-
- if (fseek(fp, 0, SEEK_CUR)) {
- return EOF;
- }
- #endif
-
-
- if ((mode == _IONBF) || (size < 1)) {
- size = 1;
- buf = fp->unbuf;
- }
- fp->mode &= ~(__MODE_BUF);
- fp->mode |= mode;
- allocated_buf_flag = 0;
- if ((!buf) && (size != (fp->bufend - fp->bufstart))) {
-
- allocated_buf_flag = __MODE_FREEBUF;
- if (!(buf = _alloc_stdio_buffer(size))) {
- return EOF;
- }
- }
- if (buf && (buf != (char *) fp->bufstart)) {
- _free_stdio_buffer_of_file(fp);
- fp->mode |= allocated_buf_flag;
- fp->bufstart = buf;
- fp->bufend = buf + size;
- fp->bufpos = fp->bufstart;
- if (READABLE(fp)) {
- fp->bufread = fp->bufstart;
- }
- if (WRITEABLE(fp)) {
- fp->bufwrite = fp->bufstart;
- }
- }
- return 0;
- }
- #endif
- #ifdef L_setbuf
- void setbuf(FILE *fp, char *buf)
- {
- int mode;
- mode = _IOFBF;
- if (!buf) {
- mode = _IONBF;
- }
- setvbuf(fp, buf, mode, BUFSIZ);
- }
- #endif
- #ifdef L_setlinebuf
- void setlinebuf(FILE *fp)
- {
- setvbuf(fp, NULL, _IOLBF, BUFSIZ);
- }
- #endif
- #ifdef L_ungetc
- int ungetc(c, fp)
- int c;
- FILE *fp;
- {
- unsigned char *p;
-
-
- if (!READABLE(fp) || (fp->mode & (__MODE_UNGOT | __MODE_ERR))
- || (c == EOF) ) {
- return EOF;
- }
- if (WRITING(fp)) {
- fflush(fp);
- }
- if (fp->bufpos > fp->bufstart) {
- p = --fp->bufpos;
- } else if (fp->bufread == fp->bufpos) {
- p = fp->bufread++;
- } else {
- fp->mode |= __MODE_UNGOT;
- p = &(fp->ungot);
- }
- fp->mode &= ~(__MODE_EOF);
- if (*p != (unsigned char) c) {
- *p = (unsigned char) c;
- }
- return c;
- }
- #endif
- #ifdef L_fopen
- #undef fopen
- FILE *fopen(const char *__restrict filename,
- const char *__restrict mode)
- {
- return __fopen(filename, -1, NULL, mode, 0);
- }
- #endif
- #ifdef L_freopen
- FILE *freopen(__const char *__restrict filename,
- __const char *__restrict mode, FILE *__restrict fp)
- {
-
- if (WRITING(fp)) {
- fflush(fp);
- }
- close(fp->fd);
- fp->mode &= (__MODE_FREEFIL | __MODE_FREEBUF);
- fp->mode |= _IOFBF;
- return __fopen(filename, -1, fp, mode, 0);
- }
- #endif
- #ifdef L_fsfopen
- FILE *fsfopen(__const char *__restrict filename,
- __const char *__restrict mode, FILE *__restrict fp)
- {
- fp->mode = _IOFBF;
- fp->bufstart = fp->unbuf;
- fp->bufend = fp->unbuf + sizeof(fp->unbuf);
- return __fopen(filename, -1, fp, mode, 0);
- }
- #endif
- #ifdef L_fdopen
- #undef fdopen
- FILE *fdopen(int fd, __const char *mode)
- {
- return __fopen(NULL, fd, NULL, mode, 0);
- }
- #endif
- #ifdef L_getc
- #undef getc
- int getc(FILE *stream)
- {
- return(((stream)->bufpos >= (stream)->bufread)? fgetc(stream) :
- (*(stream)->bufpos++));
- }
- #endif
- #ifdef L_putc
- #undef putc
- int putc(int c, FILE *stream)
- {
- return(((stream)->bufpos >= (stream)->bufwrite)? fputc((c), (stream)) :
- (unsigned char) (*(stream)->bufpos++ = (c)) );
- }
- #endif
- #ifdef L_getchar
- #undef getchar
- int getchar(void)
- {
- return getc(stdin);
- }
- #endif
- #ifdef L_putchar
- #undef putchar
- int putchar(int c)
- {
- return putc(c, stdout);
- }
- #endif
- #ifdef L_clearerr
- #undef clearerr
- void clearerr(FILE *fp)
- {
- fp->mode &= ~(__MODE_EOF | __MODE_ERR);
- }
- #endif
- #ifdef L_feof
- #undef feof
- int feof(FILE *fp)
- {
- return fp->mode & __MODE_EOF;
- }
- #endif
- #ifdef L_ferror
- #undef ferror
- int ferror(FILE *fp)
- {
- return fp->mode & __MODE_ERR;
- }
- #endif
- #ifdef L_fileno
- int fileno(FILE *fp)
- {
- return fp->fd;
- }
- #endif
- #ifdef L_fgetpos
- int fgetpos(FILE *fp, fpos_t *pos)
- {
- fpos_t p;
- if (!pos) {
- __set_errno(EINVAL);
- return -1;
- }
- if ((p = ftell(fp)) < 0) {
- return -1;
- }
- *pos = p;
- return 0;
- }
- #endif
- #ifdef L_fsetpos
- int fsetpos(FILE *fp, __const fpos_t *pos)
- {
- if (pos) {
- return fseek(fp, *pos, SEEK_SET);
- }
- __set_errno(EINVAL);
- return EOF;
- }
- #endif
- #ifdef L_fopen64
- #ifdef __UCLIBC_HAVE_LFS__
- #ifndef O_LARGEFILE
- #define O_LARGEFILE 0100000
- #endif
- FILE *fopen64(const char *__restrict filename,
- const char *__restrict mode)
- {
- return __fopen(filename, -1, NULL, mode, O_LARGEFILE);
- }
- #endif
- #endif
|