#include <sys/types.h>
#include <dirent.h>
#include <sys/time.h> 
#include <unistd.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <sys/socket.h>

#define NODEFINE
#include <u.h>
#include <libc.h>
#include <fcall.h>
#include "fd.h"

long
pm_pwrite(int fd, void *a, long n, vlong offset)
{
	off_t off;
	File *f;

	if(offset == -1)
		return pm_write(fd, a, n);
		
	if(offset < 0){
		pm_werrstr("negative pwrite offset");
		return -1;
	}
	off = offset;
	if((vlong)off != offset){
		pm_werrstr("pwrite offset does not fit in off_t");
		return -1;
	}
	if((f = pm_lockfile(fd, 0)) == nil)
		return -1;
	n = pwrite(f->ufd, a, n, off);
	pm_qunlock(&f->lk);
	if(n < 0){
		pm_oserror();
		return -1;
	}
	return n;
}
