Blame snprintf/patient_write.c

Packit c04fcb
/*
Packit c04fcb
 * Copyright (C) 2009  Pierre-Marc Fournier
Packit c04fcb
 * Copyright (C) 2011  Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Packit c04fcb
 *
Packit c04fcb
 * This library is free software; you can redistribute it and/or
Packit c04fcb
 * modify it under the terms of the GNU Lesser General Public
Packit c04fcb
 * License as published by the Free Software Foundation; either
Packit c04fcb
 * version 2.1 of the License, or (at your option) any later version.
Packit c04fcb
 *
Packit c04fcb
 * This library is distributed in the hope that it will be useful,
Packit c04fcb
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit c04fcb
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit c04fcb
 * Lesser General Public License for more details.
Packit c04fcb
 *
Packit c04fcb
 * You should have received a copy of the GNU Lesser General Public
Packit c04fcb
 * License along with this library; if not, write to the Free Software
Packit c04fcb
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
Packit c04fcb
 */
Packit c04fcb
Packit c04fcb
/* write() */
Packit c04fcb
#include <unistd.h>
Packit c04fcb
Packit c04fcb
/* send() */
Packit c04fcb
#include <sys/types.h>
Packit c04fcb
#include <sys/socket.h>
Packit c04fcb
Packit c04fcb
#include <errno.h>
Packit c04fcb
Packit c04fcb
#include <share.h>
Packit c04fcb
Packit c04fcb
/*
Packit c04fcb
 * This write is patient because it restarts if it was incomplete.
Packit c04fcb
 */
Packit c04fcb
Packit c04fcb
ssize_t patient_write(int fd, const void *buf, size_t count)
Packit c04fcb
{
Packit c04fcb
	const char *bufc = (const char *) buf;
Packit c04fcb
	int result;
Packit c04fcb
Packit c04fcb
	for(;;) {
Packit c04fcb
		result = write(fd, bufc, count);
Packit c04fcb
		if (result == -1 && errno == EINTR) {
Packit c04fcb
			continue;
Packit c04fcb
		}
Packit c04fcb
		if (result <= 0) {
Packit c04fcb
			return result;
Packit c04fcb
		}
Packit c04fcb
		count -= result;
Packit c04fcb
		bufc += result;
Packit c04fcb
Packit c04fcb
		if (count == 0) {
Packit c04fcb
			break;
Packit c04fcb
		}
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	return bufc-(const char *)buf;
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
ssize_t patient_send(int fd, const void *buf, size_t count, int flags)
Packit c04fcb
{
Packit c04fcb
	const char *bufc = (const char *) buf;
Packit c04fcb
	int result;
Packit c04fcb
Packit c04fcb
	for(;;) {
Packit c04fcb
		result = send(fd, bufc, count, flags);
Packit c04fcb
		if (result == -1 && errno == EINTR) {
Packit c04fcb
			continue;
Packit c04fcb
		}
Packit c04fcb
		if (result <= 0) {
Packit c04fcb
			return result;
Packit c04fcb
		}
Packit c04fcb
		count -= result;
Packit c04fcb
		bufc += result;
Packit c04fcb
Packit c04fcb
		if (count == 0) {
Packit c04fcb
			break;
Packit c04fcb
		}
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	return bufc - (const char *) buf;
Packit c04fcb
}