xref: /onnv-gate/usr/src/cmd/hal/hald-runner/utils.c (revision 2912:85ea316d9c18)
1*2912Sartem /***************************************************************************
2*2912Sartem  * CVSID: $Id$
3*2912Sartem  *
4*2912Sartem  * utils.c - Some utils for the hald runner
5*2912Sartem  *
6*2912Sartem  * Copyright (C) 2006 Sjoerd Simons, <sjoerd@luon.net>
7*2912Sartem  *
8*2912Sartem  * Licensed under the Academic Free License version 2.1
9*2912Sartem  *
10*2912Sartem  * This program is free software; you can redistribute it and/or modify
11*2912Sartem  * it under the terms of the GNU General Public License as published by
12*2912Sartem  * the Free Software Foundation; either version 2 of the License, or
13*2912Sartem  * (at your option) any later version.
14*2912Sartem  *
15*2912Sartem  * This program is distributed in the hope that it will be useful,
16*2912Sartem  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*2912Sartem  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*2912Sartem  * GNU General Public License for more details.
19*2912Sartem  *
20*2912Sartem  * You should have received a copy of the GNU General Public License
21*2912Sartem  * along with this program; if not, write to the Free Software
22*2912Sartem  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23*2912Sartem  *
24*2912Sartem  **************************************************************************/
25*2912Sartem #include <stdio.h>
26*2912Sartem #include <stdlib.h>
27*2912Sartem #define DBUS_API_SUBJECT_TO_CHANGE
28*2912Sartem #include <dbus/dbus-glib-lowlevel.h>
29*2912Sartem #include <glib.h>
30*2912Sartem 
31*2912Sartem #include "utils.h"
32*2912Sartem 
33*2912Sartem char **
get_string_array(DBusMessageIter * iter,char * extra)34*2912Sartem get_string_array(DBusMessageIter *iter, char *extra)
35*2912Sartem {
36*2912Sartem 	GArray *array;
37*2912Sartem 	char **result;
38*2912Sartem 	array = g_array_new(TRUE, FALSE, sizeof(char *));
39*2912Sartem 
40*2912Sartem 	while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING) {
41*2912Sartem 		const char *value;
42*2912Sartem 		char *t;
43*2912Sartem 		dbus_message_iter_get_basic(iter, &value);
44*2912Sartem 		t = g_strdup(value);
45*2912Sartem 		g_array_append_vals(array, &t, 1);
46*2912Sartem 		dbus_message_iter_next(iter);
47*2912Sartem 	}
48*2912Sartem 	if (extra != NULL)
49*2912Sartem 		g_array_append_vals(array, &extra, 1);
50*2912Sartem 	result = (char **) array->data;
51*2912Sartem 	g_array_free(array, FALSE);
52*2912Sartem 	return result;
53*2912Sartem }
54*2912Sartem 
55*2912Sartem char **
get_string_array_from_fd(int fd)56*2912Sartem get_string_array_from_fd(int fd)
57*2912Sartem {
58*2912Sartem 	GArray *array;
59*2912Sartem 	char **result;
60*2912Sartem 	GString *str;
61*2912Sartem 	gsize pos;
62*2912Sartem 	GIOChannel *io;
63*2912Sartem 	int i = 0;
64*2912Sartem 
65*2912Sartem 	array = g_array_new(TRUE, FALSE, sizeof(char *));
66*2912Sartem 	str = g_string_new("");
67*2912Sartem 	io = g_io_channel_unix_new(fd);
68*2912Sartem 	while (g_io_channel_read_line_string(io, str, &pos, NULL) == G_IO_STATUS_NORMAL && (i++ < 128)) {
69*2912Sartem 		char *t;
70*2912Sartem 
71*2912Sartem 		/* Remove the terminting char aka \n */
72*2912Sartem 		g_string_erase(str, pos, 1);
73*2912Sartem 		t = g_strdup(str->str);
74*2912Sartem 		g_array_append_vals(array, &t, 1);
75*2912Sartem 	}
76*2912Sartem 	g_string_free(str, TRUE);
77*2912Sartem 	g_io_channel_unref(io);
78*2912Sartem 	result = (char **) array->data;
79*2912Sartem 	g_array_free(array, FALSE);
80*2912Sartem 	return result;
81*2912Sartem }
82*2912Sartem 
83*2912Sartem void
free_string_array(char ** array)84*2912Sartem free_string_array(char **array)
85*2912Sartem {
86*2912Sartem 	char **p;
87*2912Sartem 
88*2912Sartem 	for (p = array; p != NULL && *p != NULL; p++)
89*2912Sartem 		g_free(*p);
90*2912Sartem 	g_free(array);
91*2912Sartem }
92