Blame src/daemon/io.c

Packit d37888
/* Copyright (C) 1998-99 Martin Baulig
Packit d37888
   This file is part of LibGTop 1.0.
Packit d37888
Packit d37888
   Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998.
Packit d37888
Packit d37888
   LibGTop is free software; you can redistribute it and/or modify it
Packit d37888
   under the terms of the GNU General Public License as published by
Packit d37888
   the Free Software Foundation; either version 2 of the License,
Packit d37888
   or (at your option) any later version.
Packit d37888
Packit d37888
   LibGTop is distributed in the hope that it will be useful, but WITHOUT
Packit d37888
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
Packit d37888
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
Packit d37888
   for more details.
Packit d37888
Packit d37888
   You should have received a copy of the GNU General Public License
Packit d37888
   along with LibGTop; see the file COPYING. If not, write to the
Packit d37888
   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Packit d37888
   Boston, MA 02110-1301, USA.
Packit d37888
*/
Packit d37888
Packit d37888
#include <config.h>
Packit d37888
#include "daemon.h"
Packit d37888
#include <glibtop/error.h>
Packit d37888
Packit d37888
void
Packit d37888
do_output (int s, glibtop_response *resp, off_t offset,
Packit d37888
	   size_t data_size, const void *data)
Packit d37888
{
Packit d37888
	glibtop_debug ("Really writing %d bytes at offset %lu.",
Packit d37888
		 sizeof (glibtop_response), offset);
Packit d37888
Packit d37888
	resp->offset = offset;
Packit d37888
	resp->data_size = data_size;
Packit d37888
Packit d37888
	if (s == 0) {
Packit d37888
		if (write (1, (const void *) resp, sizeof (glibtop_response)) < 0)
Packit d37888
			glibtop_warn_io ("write");
Packit d37888
	} else {
Packit d37888
		if (send (s, (const void *) resp, sizeof (glibtop_response), 0) < 0)
Packit d37888
			glibtop_warn_io ("send");
Packit d37888
	}
Packit d37888
Packit d37888
	if (resp->data_size) {
Packit d37888
		glibtop_debug ("Writing %d bytes of data.", resp->data_size);
Packit d37888
Packit d37888
		if (s == 0) {
Packit d37888
			if (write (1, data, resp->data_size) < 0)
Packit d37888
				glibtop_warn_io ("write");
Packit d37888
		} else {
Packit d37888
			if (send (s, data, resp->data_size, 0) , 0)
Packit d37888
				glibtop_warn_io ("send");
Packit d37888
		}
Packit d37888
	}
Packit d37888
}
Packit d37888
Packit d37888
int
Packit d37888
do_read (int s, void *ptr, size_t total_size)
Packit d37888
{
Packit d37888
	int nread;
Packit d37888
	char *tmp_ptr;
Packit d37888
	size_t already_read = 0, remaining = total_size;
Packit d37888
Packit d37888
	while (already_read < total_size) {
Packit d37888
		if (s)
Packit d37888
			nread = recv (s, ptr, remaining, 0);
Packit d37888
		else
Packit d37888
			nread = read (0, ptr, remaining);
Packit d37888
Packit d37888
		if ((already_read == 0) && (nread == 0)) {
Packit d37888
			glibtop_warn ("pid %d received eof.", getpid ());
Packit d37888
			return 0;
Packit d37888
		}
Packit d37888
Packit d37888
		if (nread <= 0) {
Packit d37888
			glibtop_warn_io ("recv");
Packit d37888
			return 0;
Packit d37888
		}
Packit d37888
Packit d37888
		already_read += nread;
Packit d37888
		remaining -= nread;
Packit d37888
		/* (char *) ptr += nread; */
Packit d37888
		tmp_ptr = ptr;
Packit d37888
		tmp_ptr += nread;
Packit d37888
		ptr = tmp_ptr;
Packit d37888
Packit d37888
		glibtop_debug ("READ (%d): %d - %d - %d",
Packit d37888
			 nread, already_read, remaining, total_size);
Packit d37888
	}
Packit d37888
Packit d37888
	return already_read;
Packit d37888
}