#include <9pm/windows.h>
#include <9pm/u.h>
#include <9pm/libc.h>
#include "syscall.h"

long
pwrite(int fd, void *buf, long n, vlong off)
{
	long a, m, m2;
	Syscallmem *c;

	c = _getsyscallmem();
	c->arg[0] = fd;
	c->arg[1] = 0;
	c->arg[2] = n;
	c->nr = Spwrite;

	for(a=0; a<n; a+=m){
		m = n-a;
		if(m > CallMem)
			m = CallMem;
		*(vlong*)(&c->arg[3]) = off+a;
		memmove(&c[1], (uchar*)buf+a, m);
		m2 = _dosyscall(c);
		if(m2 < 0){
			if(a==0)
				return -1;
			return a;
		}
		if(m2 < m)
			return a+m2;
	}
	return a;
}
