1 /* Copyright (C) 1998-99 Martin Baulig
2    This file is part of LibGTop 1.0.
3 
4    Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998.
5 
6    LibGTop is free software; you can redistribute it and/or modify it
7    under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License,
9    or (at your option) any later version.
10 
11    LibGTop is distributed in the hope that it will be useful, but WITHOUT
12    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14    for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with LibGTop; see the file COPYING. If not, write to the
18    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19    Boston, MA 02110-1301, USA.
20 */
21 
22 #include <config.h>
23 
24 #include <glibtop/error.h>
25 #include <glibtop/read_data.h>
26 #include <glib/gi18n-lib.h>
27 
28 /* Reads some data from server. */
29 
30 void *
glibtop_read_data_l(glibtop * server)31 glibtop_read_data_l (glibtop *server)
32 {
33 	size_t size;
34 	void *ptr;
35 	int ret;
36 
37 	glibtop_init_r (&server, 0, 0);
38 
39 	glibtop_debug ("LIBRARY: reading %lu data bytes.",
40 		 (unsigned long) sizeof (size_t));
41 
42 	if (server->socket) {
43 		ret = recv (server->socket, &size, sizeof (size_t), 0);
44 	} else {
45 		ret = read (server->input [0], &size, sizeof (size_t));
46 	}
47 
48 	if (ret < 0)
49 		glibtop_error_io_r (server, _("read data size"));
50 
51 	glibtop_debug ("LIBRARY: really reading %lu data bytes (ret = %d).",
52 		 (unsigned long) size, ret);
53 
54 	if (!size) return NULL;
55 
56 	ptr = g_malloc (size);
57 
58 	if (server->socket) {
59 		ret = recv (server->socket, ptr, size, 0);
60 	} else {
61 		ret = read (server->input [0], ptr, size);
62 	}
63 
64 	if (ret < 0)
65 		glibtop_error_io_r (server,
66 				    ngettext ("read %lu byte of data",
67 					      "read %lu bytes of data",
68 					      (unsigned long) size),
69 				    (unsigned long) size);
70 
71 	return ptr;
72 }
73