view src/lib/write-full.c @ 4020:fcfd44f56b04 HEAD

While casting const pointers to something else, the const was often unneededly dropped out in the middle of casts.
author Timo Sirainen <tss@iki.fi>
date Tue, 14 Feb 2006 20:59:09 +0200
parents e1443315294c
children 65c69a53a7be
line wrap: on
line source

/* Copyright (c) 2002-2003 Timo Sirainen */

#include "lib.h"
#include "write-full.h"

#include <unistd.h>

int write_full(int fd, const void *data, size_t size)
{
	ssize_t ret;

	while (size > 0) {
		ret = write(fd, data, size < SSIZE_T_MAX ? size : SSIZE_T_MAX);
		if (ret < 0)
			return -1;

		if (ret == 0) {
			/* nothing was written, only reason for this should
			   be out of disk space */
			errno = ENOSPC;
			return -1;
		}

		data = CONST_PTR_OFFSET(data, ret);
		size -= ret;
	}

	return 0;
}

int pwrite_full(int fd, const void *data, size_t size, off_t offset)
{
	ssize_t ret;

	while (size > 0) {
		ret = pwrite(fd, data, size < SSIZE_T_MAX ?
			     size : SSIZE_T_MAX, offset);
		if (ret < 0)
			return -1;

		if (ret == 0) {
			/* nothing was written, only reason for this should
			   be out of disk space */
			errno = ENOSPC;
			return -1;
		}

		data = CONST_PTR_OFFSET(data, ret);
		size -= ret;
		offset += ret;
	}

	return 0;
}