/* include writen */
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>

/* Write "n" bytes to the file descriptor fd starting a the memory location 
 * referred to by vptr.  Performs the necessary error handling.
 */
ssize_t		
writen(int fd, const void *vptr, size_t n)
{
    size_t		nleft;
    ssize_t		nwritten;
    const char	*ptr;

    ptr = vptr;
    nleft = n;
    while (nleft > 0) {
	if ( (nwritten = write(fd, ptr, nleft)) <= 0) {
	    if (errno == EINTR)
		nwritten = 0;		/* and call write() again */
	    else
		return(-1);		/* error */
	}
	
	nleft -= nwritten;
	ptr   += nwritten;
    }
    return(n);
}

/* A public wrapper function that prints an error message if writen fails.
 */

int
Writen(int fd, void *ptr, size_t nbytes)
{
    ssize_t n;
    if ((n = writen(fd, ptr, nbytes)) != nbytes)
	perror("writen error");
    return(n);
}
