Plan 9 from Bell Labs’s /usr/web/sources/plan9/sys/src/libstdio/fwrite.c

Copyright © 2009 Alcatel-Lucent.
Distributed under the Lucent Public License version 1.02.
Download the Plan 9 distribution.


/*
 * 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);
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2009 Alcatel-Lucent. All Rights Reserved.
Comments to webmaster@plan9.bell-labs.com.