summaryrefslogtreecommitdiff
path: root/sys/src/ape/lib/ap/stdio/fwrite.c
blob: 5e871c06924180d2a6c4d79b89fff57932ef2865 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
 * pANS stdio -- fwrite
 */
#include "iolib.h"
#include <string.h>

#define BIGN (BUFSIZ/2)

size_t fwrite(const void *p, size_t recl, size_t 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;
			memcpy(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)
						lseek(f->fd, 0L, SEEK_END);
					if(write(f->fd, f->buf, d)!=d){
						f->state=ERR;
						goto ret;
					}
					f->wp=f->rp=f->buf;
				}
				if(f->flags&APPEND)
					lseek(f->fd, 0L, SEEK_END);
				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:
	if(recl)
		return (s-(char*)p)/recl;
	else
		return s-(char*)p;
}