summaryrefslogtreecommitdiff
path: root/src/freebsd/up-util.c
blob: 6e754076ac193fb8f6de907055f28baae6827611 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/***************************************************************************
 *
 * up-util.c : utilities
 *
 * Copyright (C) 2006, 2007 Jean-Yves Lefort <jylefort@FreeBSD.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 **************************************************************************/

#include "config.h"

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <glib.h>

#include "egg-debug.h"

#include "up-util.h"

gboolean
up_has_sysctl (const gchar *format, ...)
{
	va_list args;
	gchar *name;
	size_t value_len;
	gboolean status;

	g_return_val_if_fail (format != NULL, FALSE);

	va_start (args, format);
	name = g_strdup_vprintf (format, args);
	va_end (args);

	status = sysctlbyname (name, NULL, &value_len, NULL, 0) == 0;

	g_free (name);
	return status;
}

gboolean
up_get_int_sysctl (int *value, GError **err, const gchar *format, ...)
{
	va_list args;
	gchar *name;
	size_t value_len = sizeof(int);
	gboolean status;

	g_return_val_if_fail (value != NULL, FALSE);
	g_return_val_if_fail (format != NULL, FALSE);

	va_start (args, format);
	name = g_strdup_vprintf (format, args);
	va_end (args);

	status = sysctlbyname (name, value, &value_len, NULL, 0) == 0;
	if (!status)
		g_set_error(err, 0, 0, "%s", g_strerror (errno));

	g_free (name);
	return status;
}

gchar *
up_get_string_sysctl (GError **err, const gchar *format, ...)
{
	va_list args;
	gchar *name;
	size_t value_len;
	gchar *str = NULL;

	g_return_val_if_fail(format != NULL, FALSE);

	va_start (args, format);
	name = g_strdup_vprintf (format, args);
	va_end (args);

	if (sysctlbyname (name, NULL, &value_len, NULL, 0) == 0) {
		str = g_new (char, value_len + 1);
		if (sysctlbyname (name, str, &value_len, NULL, 0) == 0)
			str[value_len] = 0;
		else {
			g_free (str);
			str = NULL;
		}
	}

	if (!str)
		g_set_error (err, 0, 0, "%s", g_strerror(errno));

	g_free(name);
	return str;
}

/**
 * up_util_make_safe_string:
 *
 * This is adapted from linux/up-device-supply.c.
 **/
gchar *
up_make_safe_string (const gchar *text)
{
	guint i;
	guint idx = 0;
	gchar *ret;

	/* no point in checking */
	if (text == NULL)
		return NULL;

	ret = g_strdup (text);

	/* shunt up only safe chars */
	for (i = 0; ret[i] != '\0'; i++) {
		if (g_ascii_isprint (ret[i])) {
			/* only copy if the address is going to change */
			if (idx != i)
				ret[idx] = ret[i];
			idx++;
		} else {
			egg_debug ("invalid char '%c'", ret[i]);
		}
	}

	/* ensure null terminated */
	ret[idx] = '\0';

	return ret;
}