Check the return value from pth_spawn().
[pwmd.git] / src / misc.c
blob3321713a40b3d93e0959c8123bc22cb5957dc8af
1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
2 /*
3 Copyright (C) 2006-2009 Ben Kibbey <bjk@luxsci.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02110-1301 USA
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <errno.h>
22 #include <unistd.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <glib.h>
27 #include <glib/gprintf.h>
28 #include <string.h>
29 #include <ctype.h>
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
35 #include "misc.h"
37 void log_write(const gchar *fmt, ...);
39 gboolean strv_printf(gchar ***array, const gchar *fmt, ...)
41 gchar **a;
42 va_list ap;
43 gchar *buf;
44 gint len = *array ? g_strv_length(*array) : 0;
45 gint ret;
47 if (!fmt)
48 return FALSE;
50 if ((a = g_realloc(*array, (len + 2) * sizeof(gchar *))) == NULL) {
51 log_write("%s(%i): %s", __FILE__, __LINE__, strerror(ENOMEM));
52 return FALSE;
55 va_start(ap, fmt);
56 ret = g_vasprintf(&buf, fmt, ap);
57 va_end(ap);
59 if (ret == -1) {
60 log_write("%s(%i): %s", __FILE__, __LINE__, strerror(ENOMEM));
61 return FALSE;
64 a[len++] = buf;
65 a[len] = NULL;
66 *array = a;
67 return TRUE;
70 gchar **strvcatv(gchar **dst, gchar **src)
72 gchar **p;
73 gint i;
74 gchar **d;
76 if (!src)
77 return NULL;
79 d = g_strdupv(dst);
81 if (!d)
82 return NULL;
84 i = g_strv_length(d);
86 for (p = src; *p; p++) {
87 gchar **pa;
89 pa = g_realloc(d, (i + 2) * sizeof(gchar *));
91 if (!pa) {
92 g_strfreev(d);
93 return NULL;
96 d = pa;
97 d[i] = g_strdup(*p);
99 if (!d[i]) {
100 g_strfreev(d);
101 return NULL;
104 d[++i] = NULL;
107 return d;
110 gboolean valid_filename(const gchar *filename)
112 const gchar *p;
114 if (!filename || !*filename)
115 return FALSE;
117 if (!strcmp(filename, "-"))
118 return FALSE;
120 for (p = filename; *p; p++) {
121 if (*p == '/' || isspace(*p))
122 return FALSE;
125 return TRUE;
128 gchar *print_fmt(gchar *buf, gsize len, const char *fmt, ...)
130 va_list ap;
132 va_start(ap, fmt);
133 g_vsnprintf(buf, len, fmt, ap);
134 va_end(ap);
135 return buf;
138 gboolean contains_whitespace(const gchar *str)
140 const gchar *p = str;
141 gunichar c;
142 glong len;
144 len = g_utf8_strlen(p++, -1) -1;
146 while (len--) {
147 c = g_utf8_get_char(p++);
149 if (g_unichar_isspace(c))
150 return TRUE;
153 return FALSE;
156 gchar *expand_homedir(gchar *str)
158 gchar *p = str;
160 if (*p++ == '~')
161 return g_strdup_printf("%s%s", g_get_home_dir(), p);
163 return g_strdup(str);
166 gchar *strip_char(gchar *str, gchar c)
168 gchar *p, *p2;
170 for (p = p2 = str; *p; p++) {
171 if (*p == c)
172 continue;
174 *p2++ = *p;
177 *p2 = 0;
178 return str;
181 gchar *tohex(const guchar *str, gsize len)
183 gchar *p;
184 gint i;
186 if (!str || len <= 0)
187 return NULL;
189 p = g_malloc0(len*2+1);
191 if (!p)
192 return NULL;
194 for (i = 0; i < len; i++) {
195 gchar buf[3];
197 g_sprintf(buf, "%02x", str[i]);
198 strcat(p, buf);
201 return p;