1 | /* $Id: read.c,v 1.1.1.2 2004-10-03 04:59:56 ghudson Exp $ */ |
---|
2 | |
---|
3 | /* Copyright (C) 1998-99 Martin Baulig |
---|
4 | This file is part of LibGTop 1.0. |
---|
5 | |
---|
6 | Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998. |
---|
7 | |
---|
8 | LibGTop is free software; you can redistribute it and/or modify it |
---|
9 | under the terms of the GNU General Public License as published by |
---|
10 | the Free Software Foundation; either version 2 of the License, |
---|
11 | or (at your option) any later version. |
---|
12 | |
---|
13 | LibGTop is distributed in the hope that it will be useful, but WITHOUT |
---|
14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
15 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
---|
16 | for more details. |
---|
17 | |
---|
18 | You should have received a copy of the GNU General Public License |
---|
19 | along with LibGTop; see the file COPYING. If not, write to the |
---|
20 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
---|
21 | Boston, MA 02111-1307, USA. |
---|
22 | */ |
---|
23 | |
---|
24 | #include <config.h> |
---|
25 | #include <glibtop/read.h> |
---|
26 | #include "libgtop-i18n.h" |
---|
27 | |
---|
28 | |
---|
29 | /* Reads some data from server. */ |
---|
30 | |
---|
31 | static void |
---|
32 | do_read (int s, void *ptr, size_t total_size) |
---|
33 | { |
---|
34 | ssize_t nread; |
---|
35 | |
---|
36 | if(!total_size) return; |
---|
37 | |
---|
38 | while (total_size && (nread = recv (s, ptr, total_size, 0)) > 0) { |
---|
39 | total_size -= nread; |
---|
40 | ptr = (char*)ptr + nread; |
---|
41 | } |
---|
42 | |
---|
43 | if(nread == 0) |
---|
44 | close (s); |
---|
45 | |
---|
46 | if (nread < 0) |
---|
47 | glibtop_error_io ("recv"); |
---|
48 | } |
---|
49 | |
---|
50 | void |
---|
51 | glibtop_read_l (glibtop *server, size_t size, void *buf) |
---|
52 | { |
---|
53 | glibtop_init_r (&server, 0, 0); |
---|
54 | |
---|
55 | #ifdef DEBUG |
---|
56 | fprintf (stderr, "LIBRARY: really reading %d bytes.\n", size); |
---|
57 | #endif |
---|
58 | |
---|
59 | if (server->socket) { |
---|
60 | do_read (server->socket, buf, size); |
---|
61 | } else { |
---|
62 | if(read (server->input [0], buf, size) < 0) |
---|
63 | glibtop_error_io_r ( |
---|
64 | server, |
---|
65 | ngettext ("read %d byte", |
---|
66 | "read %d bytes", size), |
---|
67 | size); |
---|
68 | } |
---|
69 | } |
---|