1 | /* $Id: command.c,v 1.1.1.2 2004-10-03 05:00:35 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 <glibtop/read.h> |
---|
25 | #include <glibtop/write.h> |
---|
26 | #include <glibtop/read_data.h> |
---|
27 | |
---|
28 | #include <glibtop/command.h> |
---|
29 | |
---|
30 | void * |
---|
31 | glibtop_call_l (glibtop *server, unsigned command, size_t send_size, |
---|
32 | const void *send_buf, size_t recv_size, void *recv_buf) |
---|
33 | { |
---|
34 | glibtop_command cmnd = {0}; |
---|
35 | glibtop_response response = {0}; |
---|
36 | |
---|
37 | glibtop_init_r (&server, 0, 0); |
---|
38 | |
---|
39 | cmnd.command = command; |
---|
40 | |
---|
41 | /* If send_size is less than _GLIBTOP_PARAM_SIZE (normally 16 Bytes), we |
---|
42 | * send it together with command, so we only need one system call instead |
---|
43 | * of two. */ |
---|
44 | |
---|
45 | if (send_size <= _GLIBTOP_PARAM_SIZE) { |
---|
46 | memcpy (cmnd.parameter, send_buf, send_size); |
---|
47 | cmnd.size = send_size; |
---|
48 | } else { |
---|
49 | cmnd.data_size = send_size; |
---|
50 | } |
---|
51 | |
---|
52 | glibtop_write_l (server, sizeof (glibtop_command), &cmnd); |
---|
53 | |
---|
54 | glibtop_read_l (server, sizeof (glibtop_response), &response); |
---|
55 | |
---|
56 | #ifdef DEBUG |
---|
57 | fprintf (stderr, "RESPONSE: %lu - %d\n", |
---|
58 | response.offset, response.data_size); |
---|
59 | #endif |
---|
60 | |
---|
61 | if (recv_buf) |
---|
62 | memcpy (recv_buf, ((char *) &response) + response.offset, |
---|
63 | recv_size); |
---|
64 | |
---|
65 | if (response.data_size) { |
---|
66 | void *ptr = g_malloc (response.data_size); |
---|
67 | |
---|
68 | glibtop_read_l (server, response.data_size, ptr); |
---|
69 | |
---|
70 | return ptr; |
---|
71 | } |
---|
72 | |
---|
73 | return NULL; |
---|
74 | } |
---|