diff options
Diffstat (limited to 'mdk-stage1/dietlibc/libstdio')
26 files changed, 521 insertions, 0 deletions
diff --git a/mdk-stage1/dietlibc/libstdio/clearerr.c b/mdk-stage1/dietlibc/libstdio/clearerr.c new file mode 100644 index 000000000..8bde8c702 --- /dev/null +++ b/mdk-stage1/dietlibc/libstdio/clearerr.c @@ -0,0 +1,5 @@ +#include "dietstdio.h" + +void clearerr( FILE *stream) { + stream->flags&=~(ERRORINDICATOR|EOFINDICATOR); +} diff --git a/mdk-stage1/dietlibc/libstdio/fclose.c b/mdk-stage1/dietlibc/libstdio/fclose.c new file mode 100644 index 000000000..6958a48e7 --- /dev/null +++ b/mdk-stage1/dietlibc/libstdio/fclose.c @@ -0,0 +1,24 @@ +#include "dietstdio.h" +#include <stdlib.h> +#include <unistd.h> + +int fclose(FILE *stream) { + int res; +#ifdef WANT_BUFFERED_STDIO + FILE *f,*fl; +#endif + fflush(stream); + res=close(stream->fd); +#ifdef WANT_BUFFERED_STDIO + for (fl=0,f=__stdio_root; f; fl=f,f=f->next) + if (f==stream) { + if (fl) + fl->next=f->next; + else + __stdio_root=f->next; + break; + } +#endif + free(stream); + return res; +} diff --git a/mdk-stage1/dietlibc/libstdio/fdglue.c b/mdk-stage1/dietlibc/libstdio/fdglue.c new file mode 100644 index 000000000..eeedd513d --- /dev/null +++ b/mdk-stage1/dietlibc/libstdio/fdglue.c @@ -0,0 +1,49 @@ +#include "dietstdio.h" +#include <unistd.h> +#include <fcntl.h> +#include <errno.h> +#include <stdlib.h> + +#ifdef WANT_BUFFERED_STDIO +extern int __stdio_atexit; +extern void __stdio_flushall(); +#endif + +int __stdio_parse_mode(const char *mode) { + int f=0; + for (;;) { + switch (*mode) { + case 0: return f; + case 'b': break; + case 'r': f=O_RDONLY; break; + case 'w': f=O_WRONLY|O_CREAT|O_TRUNC; break; + case 'a': f=O_WRONLY|O_CREAT|O_APPEND; break; + case '+': f=(f&(~O_WRONLY))|O_RDWR; break; + } + ++mode; + } +} + +FILE* __stdio_init_file(int fd) { + FILE *tmp=(FILE*)malloc(sizeof(FILE)); + if (!tmp) { + close(fd); + errno=ENOMEM; + return 0; + } + tmp->fd=fd; +#ifdef WANT_BUFFERED_STDIO + tmp->bm=0; + tmp->bs=0; +#endif + tmp->flags=0; +#ifdef WANT_BUFFERED_STDIO + if (__stdio_atexit==0) { + __stdio_atexit=1; + atexit(__stdio_flushall); + } + tmp->next=__stdio_root; + __stdio_root=tmp; +#endif + return tmp; +} diff --git a/mdk-stage1/dietlibc/libstdio/fdopen.c b/mdk-stage1/dietlibc/libstdio/fdopen.c new file mode 100644 index 000000000..aee14fb0c --- /dev/null +++ b/mdk-stage1/dietlibc/libstdio/fdopen.c @@ -0,0 +1,21 @@ +#include <sys/types.h> +#include <dietstdio.h> +#include <unistd.h> + +#define SEEK_END 2 + +int __stdio_parse_mode(const char *mode); +FILE* __stdio_init_file(int fd); + +FILE *fdopen (int filedes, const char *mode) { + int f=0; /* O_RDONLY, O_WRONLY or O_RDWR */ + int fd; + + f=__stdio_parse_mode(mode); + if ((fd=filedes)<0) return 0; + { + FILE * ret = __stdio_init_file(fd); + ret->flags |= UNSEEKABLE; + return ret; + } +} diff --git a/mdk-stage1/dietlibc/libstdio/feof.c b/mdk-stage1/dietlibc/libstdio/feof.c new file mode 100644 index 000000000..b7e86fe3b --- /dev/null +++ b/mdk-stage1/dietlibc/libstdio/feof.c @@ -0,0 +1,9 @@ +#include <dietstdio.h> + +int feof(FILE *stream) { +#ifdef WANT_UNGETC + /* yuck!!! */ + if (stream->ungotten) return 0; +#endif + return (stream->flags&EOFINDICATOR); +} diff --git a/mdk-stage1/dietlibc/libstdio/ferror.c b/mdk-stage1/dietlibc/libstdio/ferror.c new file mode 100644 index 000000000..e86873be0 --- /dev/null +++ b/mdk-stage1/dietlibc/libstdio/ferror.c @@ -0,0 +1,5 @@ +#include <dietstdio.h> + +int ferror(FILE *stream) { + return (stream->flags&ERRORINDICATOR); +} diff --git a/mdk-stage1/dietlibc/libstdio/fflush.c b/mdk-stage1/dietlibc/libstdio/fflush.c new file mode 100644 index 000000000..7078e04f4 --- /dev/null +++ b/mdk-stage1/dietlibc/libstdio/fflush.c @@ -0,0 +1,62 @@ +#include "dietstdio.h" +#include <unistd.h> +#include <stdlib.h> +#include "dietwarning.h" + +FILE *__stdio_root; + +#ifdef WANT_BUFFERED_STDIO +int __stdio_atexit=0; + +void __stdio_flushall() { + fflush(0); +} + +extern int __fflush_stdin(); +extern int __fflush_stdout(); +extern int __fflush_stderr(); + +int fflush(FILE *stream) { + if (stream==0) { + int res; + FILE *f; + __fflush_stdin(); + __fflush_stdout(); + __fflush_stderr(); + for (res=0, f=__stdio_root; f; f=f->next) + if (fflush(f)) + res=-1; + return res; + } + if (stream->flags&BUFINPUT) { + register int tmp; + if ((tmp=stream->bm-stream->bs)) lseek(stream->fd,tmp,SEEK_CUR); + } else + if (stream->bm && write(stream->fd,stream->buf,stream->bm)!=stream->bm) { + stream->flags|=ERRORINDICATOR; + return -1; + } + stream->bm=0; + return 0; +} + +int __fflush4(FILE *stream,int next) { + if (!__stdio_atexit) { + __stdio_atexit=1; + atexit(__stdio_flushall); + } + if ((stream->flags&BUFINPUT)!=next) { + int res=fflush(stream); + stream->flags=(stream->flags&~BUFINPUT)|next; + return res; + } + return 0; +} + +#else +int fflush(FILE *stream) { + return 0; +} +#endif + +link_warning("fflush","warning: your code uses stdio (several kilobytes of bloat).") diff --git a/mdk-stage1/dietlibc/libstdio/fgetc.c b/mdk-stage1/dietlibc/libstdio/fgetc.c new file mode 100644 index 000000000..90d83961a --- /dev/null +++ b/mdk-stage1/dietlibc/libstdio/fgetc.c @@ -0,0 +1,40 @@ +#include "dietstdio.h" +#include <unistd.h> + +extern int feof(FILE *stream); + +int fgetc(FILE *stream) { + unsigned char c; +#ifdef WANT_UNGETC + if (stream->ungotten) { + stream->ungotten=0; + return stream->ungetbuf; + } +#endif + if (feof(stream)) + return EOF; +#ifdef WANT_BUFFERED_STDIO + if (__fflush4(stream,BUFINPUT)) return EOF; + if (stream->bm>=stream->bs) { + int len=read(stream->fd,stream->buf,BUFSIZE); + if (len==0) { + stream->flags|=EOFINDICATOR; + return EOF; + } else if (len<0) { + stream->flags|=ERRORINDICATOR; + return EOF; + } + stream->bm=0; + stream->bs=len; + } + c=stream->buf[stream->bm]; + ++stream->bm; + return c; +#else + if (read(stream->fd,&c,1)!=1) { + stream->flags|=ERRORINDICATOR; + return EOF; + } + return c; +#endif +} diff --git a/mdk-stage1/dietlibc/libstdio/fgets.c b/mdk-stage1/dietlibc/libstdio/fgets.c new file mode 100644 index 000000000..c613d452d --- /dev/null +++ b/mdk-stage1/dietlibc/libstdio/fgets.c @@ -0,0 +1,20 @@ +#include "dietstdio.h" + +extern int ferror(FILE *stream); + +char *fgets(char *s, int size, FILE *stream) { + char *orig=s; + int l; + for (l=size; l>0;) { + int c=fgetc(stream); + if (c==EOF) break; + *s=c; + ++s; + --l; + if (c=='\n') break; + } + if (l==size || ferror(stream)) + return 0; + *s=0; + return orig; +} diff --git a/mdk-stage1/dietlibc/libstdio/fileno.c b/mdk-stage1/dietlibc/libstdio/fileno.c new file mode 100644 index 000000000..f021d3881 --- /dev/null +++ b/mdk-stage1/dietlibc/libstdio/fileno.c @@ -0,0 +1,5 @@ +#include <dietstdio.h> + +int fileno(FILE *stream) { + return stream->fd; +} diff --git a/mdk-stage1/dietlibc/libstdio/fopen.c b/mdk-stage1/dietlibc/libstdio/fopen.c new file mode 100644 index 000000000..aac17be42 --- /dev/null +++ b/mdk-stage1/dietlibc/libstdio/fopen.c @@ -0,0 +1,21 @@ +#include <sys/types.h> +#include <dietstdio.h> +#include <unistd.h> + +#define SEEK_END 2 + +extern int __stdio_atexit; +extern void __stdio_flushall(); + +extern int __stdio_parse_mode(const char *mode); +extern FILE* __stdio_init_file(int fd); + +FILE *fopen (const char *path, const char *mode) { + int f=0; /* O_RDONLY, O_WRONLY or O_RDWR */ + int fd; + + f=__stdio_parse_mode(mode); + if ((fd=open(path,f,0666))<0) + return 0; + return __stdio_init_file(fd); +} diff --git a/mdk-stage1/dietlibc/libstdio/fprintf.c b/mdk-stage1/dietlibc/libstdio/fprintf.c new file mode 100644 index 000000000..237fd1e51 --- /dev/null +++ b/mdk-stage1/dietlibc/libstdio/fprintf.c @@ -0,0 +1,23 @@ +#include <stdarg.h> +#include <linux/types.h> +#include <stdio.h> +#include <stdlib.h> + +extern int vsnprintf (char *str,size_t size,const char *format, va_list arg_ptr); + +int fprintf(FILE *f,const char *format,...) { + int n; + char *printf_buf; +/* char printf_buf[1024]; */ + va_list arg_ptr; + va_start(arg_ptr, format); + n=vsnprintf(0,1000000,format,arg_ptr); +/* write(1,printf_buf,strlen(printf_buf)); */ + va_end (arg_ptr); + va_start (arg_ptr, format); + printf_buf=alloca(n+2); + n=vsnprintf(printf_buf,n+1,format,arg_ptr); + va_end (arg_ptr); + fwrite(printf_buf,n,1,f); + return n; +} diff --git a/mdk-stage1/dietlibc/libstdio/fputc.c b/mdk-stage1/dietlibc/libstdio/fputc.c new file mode 100644 index 000000000..b816f7c4d --- /dev/null +++ b/mdk-stage1/dietlibc/libstdio/fputc.c @@ -0,0 +1,19 @@ +#include <dietstdio.h> +#ifndef WANT_BUFFERED_STDIO +#include <unistd.h> +#endif + +int fputc(int c, FILE *stream) { +#ifdef WANT_BUFFERED_STDIO + if (__fflush4(stream,0)) return EOF; + if (stream->bm>=BUFSIZE-1) + if (fflush(stream)) return EOF; + stream->buf[stream->bm]=c; + ++stream->bm; + if ((stream->flags&BUFLINEWISE) && c=='\n') /* puke */ + if (fflush(stream)) return EOF; +#else + write(stream->fd,&c,1); +#endif + return 0; +} diff --git a/mdk-stage1/dietlibc/libstdio/fputs.c b/mdk-stage1/dietlibc/libstdio/fputs.c new file mode 100644 index 000000000..51ee9a96e --- /dev/null +++ b/mdk-stage1/dietlibc/libstdio/fputs.c @@ -0,0 +1,6 @@ +#include "dietstdio.h" +#include <string.h> + +int fputs(const char *s, FILE *stream) { + return fwrite(s,strlen(s),1,stream); +} diff --git a/mdk-stage1/dietlibc/libstdio/fread.c b/mdk-stage1/dietlibc/libstdio/fread.c new file mode 100644 index 000000000..29afa1489 --- /dev/null +++ b/mdk-stage1/dietlibc/libstdio/fread.c @@ -0,0 +1,51 @@ +#include <sys/types.h> +#include "dietstdio.h" +#include <unistd.h> + +size_t fread( void *ptr, size_t size, size_t nmemb, FILE *stream) { + int res; +#ifdef WANT_BUFFERED_STDIO + unsigned long i,j; + j=size*nmemb; +#ifdef WANT_UNGETC + if (stream->ungotten) { + *(char*)ptr=stream->ungetbuf; + ptr=((char*)ptr)+1; + --j; + } + if (!j) return 1; +#endif + for (i=0; i<j; ++i) { + res=fgetc(stream); + if (res==EOF) + return i/size; + else + ((unsigned char*)ptr)[i]=(unsigned char)res; + } + return nmemb; +#else +#ifdef WANT_UNGETC + unsigned long j=size*nmemb; +#endif + fflush(stream); +#ifdef WANT_UNGETC + if (stream->ungotten) { + *(char*)ptr=stream->ungetbuf; + ptr=((char*)ptr)+1; + --j; + } + if (!j) return 1; + res=read(stream->fd,ptr,j); +#else + res=read(stream->fd,ptr,size*nmemb); +#endif + if (res<0) { + stream->flags|=ERRORINDICATOR; + return 0; + } else if (res<size*nmemb) + if (!(stream->flags & UNSEEKABLE) + || ((stream->flags & UNSEEKABLE) && res == 0)) + stream->flags|=EOFINDICATOR; + return res/size; +#endif +} diff --git a/mdk-stage1/dietlibc/libstdio/freopen.c b/mdk-stage1/dietlibc/libstdio/freopen.c new file mode 100644 index 000000000..8d4644a10 --- /dev/null +++ b/mdk-stage1/dietlibc/libstdio/freopen.c @@ -0,0 +1,9 @@ +#include <sys/types.h> +#include <dietstdio.h> +#include <unistd.h> + +FILE *freopen (const char *path, const char *mode, FILE *stream) { + fclose(stream); + stream=fopen(path,mode); + return stream; +} diff --git a/mdk-stage1/dietlibc/libstdio/fseek.c b/mdk-stage1/dietlibc/libstdio/fseek.c new file mode 100644 index 000000000..5bb96bc6e --- /dev/null +++ b/mdk-stage1/dietlibc/libstdio/fseek.c @@ -0,0 +1,11 @@ +#include <dietstdio.h> +#include <unistd.h> + +long fseek(FILE *stream, long offset, int whence) { + fflush(stream); +#ifdef WANT_BUFFERED_STDIO + stream->bm=0; stream->bs=0; +#endif + stream->flags&=~(ERRORINDICATOR|EOFINDICATOR); + return (lseek(stream->fd,offset,whence)); +} diff --git a/mdk-stage1/dietlibc/libstdio/ftell.c b/mdk-stage1/dietlibc/libstdio/ftell.c new file mode 100644 index 000000000..42d2c9f26 --- /dev/null +++ b/mdk-stage1/dietlibc/libstdio/ftell.c @@ -0,0 +1,7 @@ +#include <dietstdio.h> +#include <unistd.h> + +long ftell(FILE *stream) { + fflush(stream); + return (lseek(stream->fd,0,SEEK_CUR)); +} diff --git a/mdk-stage1/dietlibc/libstdio/fwrite.c b/mdk-stage1/dietlibc/libstdio/fwrite.c new file mode 100644 index 000000000..5691661a5 --- /dev/null +++ b/mdk-stage1/dietlibc/libstdio/fwrite.c @@ -0,0 +1,30 @@ +#include <sys/types.h> +#include "dietstdio.h" +#include <unistd.h> + +size_t fwrite( const void *ptr, size_t size, size_t nmemb, FILE *stream) { + int res; +#ifdef WANT_BUFFERED_STDIO + long len=size*nmemb; + long i; + if (len>BUFSIZE || (stream->flags&NOBUF)) { + if (!(stream->flags&NOBUF)) fflush(stream); + res=write(stream->fd,ptr,size*nmemb); + } else { + register const unsigned char *c=ptr; + for (i=size*nmemb; i>0; --i,++c) + if (fputc(*c,stream)) { + res=-1; + break; + } + res=size*nmemb; + } +#else + res=write(stream->fd,ptr,size*nmemb); +#endif + if (res<0) { + stream->flags|=ERRORINDICATOR; + return 0; + } + return size?res/size:0; +} diff --git a/mdk-stage1/dietlibc/libstdio/printf.c b/mdk-stage1/dietlibc/libstdio/printf.c new file mode 100644 index 000000000..571420dc3 --- /dev/null +++ b/mdk-stage1/dietlibc/libstdio/printf.c @@ -0,0 +1,29 @@ +#include <stdarg.h> +#include <linux/types.h> +#include <unistd.h> +#include <stdlib.h> +#include "dietstdio.h" + +int vsnprintf (char *str,size_t size,const char *format, va_list arg_ptr); + +int printf(const char *format,...) +{ + int n; + char *printf_buf; +/* char printf_buf[1024]; */ + va_list arg_ptr; + va_start(arg_ptr, format); + n=vsnprintf(0,1000000,format,arg_ptr); +/* write(1,printf_buf,strlen(printf_buf)); */ + va_end (arg_ptr); + va_start (arg_ptr, format); + printf_buf=alloca(n+2); + n=vsnprintf(printf_buf,n+1,format,arg_ptr); +#ifdef WANT_BUFFERED_STDIO + fwrite(printf_buf,n,1,stdout); +#else + write(1,printf_buf,n); +#endif + va_end (arg_ptr); + return n; +} diff --git a/mdk-stage1/dietlibc/libstdio/putchar.c b/mdk-stage1/dietlibc/libstdio/putchar.c new file mode 100644 index 000000000..7e5285a48 --- /dev/null +++ b/mdk-stage1/dietlibc/libstdio/putchar.c @@ -0,0 +1,6 @@ +#include <stdio.h> + +#undef putchar +int putchar(int c) { + return fputc(c,stdout); +} diff --git a/mdk-stage1/dietlibc/libstdio/setvbuf.c b/mdk-stage1/dietlibc/libstdio/setvbuf.c new file mode 100644 index 000000000..d0b1b7640 --- /dev/null +++ b/mdk-stage1/dietlibc/libstdio/setvbuf.c @@ -0,0 +1,18 @@ +#include <sys/types.h> +#include "dietstdio.h" +#include <unistd.h> +#include "dietwarning.h" + +int setvbuf(FILE *stream, char *buf, int flags , size_t size) { +#ifdef WANT_BUFFERED_STDIO + switch (flags) { + case _IONBF: stream->flags = (stream->flags & ~(BUFLINEWISE)) | NOBUF; break; + case _IOLBF: stream->flags = (stream->flags & ~(BUFLINEWISE|NOBUF)) | BUFLINEWISE; break; + case _IOFBF: stream->flags = stream->flags & ~(NOBUF | BUFLINEWISE); break; + default: return -1; + } +#endif + return 0; +} + +link_warning("setvbuf","setvbuf does not implement changing the buffer in diet libc.") diff --git a/mdk-stage1/dietlibc/libstdio/stderr.c b/mdk-stage1/dietlibc/libstdio/stderr.c new file mode 100644 index 000000000..a82b1b727 --- /dev/null +++ b/mdk-stage1/dietlibc/libstdio/stderr.c @@ -0,0 +1,13 @@ +#include <dietstdio.h> + +#ifdef WANT_BUFFERED_STDIO +static FILE __stderr = { 2, NOBUF, 0, 0 }; + +int __fflush_stderr() { + return fflush(stderr); +} +#else +static FILE __stderr = { 2, 0 }; +#endif + +FILE *stderr=&__stderr; diff --git a/mdk-stage1/dietlibc/libstdio/stdin.c b/mdk-stage1/dietlibc/libstdio/stdin.c new file mode 100644 index 000000000..2db82f9ab --- /dev/null +++ b/mdk-stage1/dietlibc/libstdio/stdin.c @@ -0,0 +1,13 @@ +#include <dietstdio.h> + +#ifdef WANT_BUFFERED_STDIO +static FILE __stdin = { 0, BUFINPUT, 0, 0 }; + +int __fflush_stdin() { + return fflush(stdin); +} +#else +static FILE __stdin = { 0, 0 }; +#endif + +FILE *stdin=&__stdin; diff --git a/mdk-stage1/dietlibc/libstdio/stdout.c b/mdk-stage1/dietlibc/libstdio/stdout.c new file mode 100644 index 000000000..481fb1014 --- /dev/null +++ b/mdk-stage1/dietlibc/libstdio/stdout.c @@ -0,0 +1,14 @@ +#include <dietstdio.h> + +#ifdef WANT_BUFFERED_STDIO +static FILE __stdout = { 1, BUFLINEWISE, 0, 0 }; + +int __fflush_stdout() { + return fflush(stdout); +} +#else +static FILE __stdout = { 1, 0 }; +#endif + +FILE *stdout=&__stdout; + diff --git a/mdk-stage1/dietlibc/libstdio/ungetc.c b/mdk-stage1/dietlibc/libstdio/ungetc.c new file mode 100644 index 000000000..ae03e9381 --- /dev/null +++ b/mdk-stage1/dietlibc/libstdio/ungetc.c @@ -0,0 +1,11 @@ +#include "dietstdio.h" + +#ifdef WANT_UNGETC +int ungetc(int c, FILE *stream) { + if (stream->ungotten) + return EOF; + stream->ungotten=1; + stream->ungetbuf=(char)(unsigned char)c; + return c; +} +#endif |