diff options
author | Taru Karttunen <taruti@taruti.net> | 2011-03-30 15:46:40 +0300 |
---|---|---|
committer | Taru Karttunen <taruti@taruti.net> | 2011-03-30 15:46:40 +0300 |
commit | e5888a1ffdae813d7575f5fb02275c6bb07e5199 (patch) | |
tree | d8d51eac403f07814b9e936eed0c9a79195e2450 /sys/src/libstdio/fwrite.c |
Import sources from 2011-03-30 iso image
Diffstat (limited to 'sys/src/libstdio/fwrite.c')
-rwxr-xr-x | sys/src/libstdio/fwrite.c | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/sys/src/libstdio/fwrite.c b/sys/src/libstdio/fwrite.c new file mode 100755 index 000000000..d77679b22 --- /dev/null +++ b/sys/src/libstdio/fwrite.c @@ -0,0 +1,51 @@ +/* + * pANS stdio -- fwrite + */ +#include "iolib.h" + +#define BIGN (BUFSIZ/2) + +long fwrite(const void *p, long recl, long nrec, FILE *f){ + char *s; + int n, d; + + s=(char *)p; + n=recl*nrec; + while(n>0){ + d=f->rp-f->wp; + if(d>0){ + if(d>n) + d=n; + memmove(f->wp, s, d); + f->wp+=d; + }else{ + if(n>=BIGN && f->state==WR && !(f->flags&(STRING|LINEBUF)) && f->buf!=f->unbuf){ + d=f->wp-f->buf; + if(d>0){ + if(f->flags&APPEND) + seek(f->fd, 0L, 2); + if(write(f->fd, f->buf, d)!=d){ + f->state=ERR; + goto ret; + } + f->wp=f->rp=f->buf; + } + if(f->flags&APPEND) + seek(f->fd, 0L, 2); + d=write(f->fd, s, n); + if(d<=0){ + f->state=ERR; + goto ret; + } + }else{ + if(_IO_putc(*s, f)==EOF) + goto ret; + d=1; + } + } + s+=d; + n-=d; + } + ret: + return (s-(char *)p)/(recl?recl:1); +} |