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