Forgot to commit the Debian changelog in the v1.13 release.
[pwmd.git] / src / misc.c
blob207a7a5dcb40ba2e46a1d2ad071ddd86c24a2d16
1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
2 /*
3 Copyright (C) 2006-2008 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 <unistd.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <glib.h>
26 #include <glib/gprintf.h>
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
32 #include "misc.h"
34 gboolean strv_printf(gchar ***array, const gchar *fmt, ...)
36 gchar **a;
37 va_list ap;
38 gchar *buf;
39 gint len = *array ? g_strv_length(*array) : 0;
40 gint ret;
42 if (!fmt)
43 return FALSE;
45 if ((a = g_realloc(*array, (len + 2) * sizeof(gchar *))) == NULL)
46 return FALSE;
48 va_start(ap, fmt);
49 ret = g_vasprintf(&buf, fmt, ap);
50 va_end(ap);
52 if (ret == -1)
53 return FALSE;
55 a[len++] = buf;
56 a[len] = NULL;
57 *array = a;
58 return TRUE;
61 gchar **strvcatv(gchar **dst, gchar **src)
63 gchar **p;
64 gint i;
65 gchar **d;
67 if (!src)
68 return NULL;
70 d = g_strdupv(dst);
72 if (!d)
73 return NULL;
75 i = g_strv_length(d);
77 for (p = src; *p; p++) {
78 gchar **pa;
80 pa = g_realloc(d, (i + 2) * sizeof(gchar *));
82 if (!pa) {
83 g_strfreev(d);
84 return NULL;
87 d = pa;
88 d[i] = g_strdup(*p);
90 if (!d[i]) {
91 g_strfreev(d);
92 return NULL;
95 d[++i] = NULL;
98 return d;
101 gboolean valid_filename(const gchar *filename)
103 const gchar *p;
105 if (!filename || !*filename)
106 return FALSE;
108 for (p = filename; *p; p++) {
109 if (g_ascii_isalnum(*p) == FALSE && *p != '-' && *p != '_' && *p != '.')
110 return FALSE;
113 return TRUE;
116 gint open_file(const gchar *filename, struct stat *st)
118 gint fd;
120 if ((fd = open(filename, O_RDONLY)) == -1)
121 return -1;
123 if (stat(filename, st) == -1) {
124 close(fd);
125 return -1;
128 return fd;
131 gchar *print_fmt(gchar *buf, gsize len, const char *fmt, ...)
133 va_list ap;
135 va_start(ap, fmt);
136 g_vsnprintf(buf, len, fmt, ap);
137 va_end(ap);
138 return buf;
141 gboolean contains_whitespace(const gchar *str)
143 const gchar *p = str;
144 gunichar c;
145 glong len;
147 len = g_utf8_strlen(p++, -1) -1;
149 while (len--) {
150 c = g_utf8_get_char(p++);
152 if (g_unichar_isspace(c))
153 return TRUE;
156 return FALSE;
159 gchar *expand_homedir(gchar *str)
161 gchar *p = str;
163 if (*p++ == '~')
164 return g_strdup_printf("%s%s", g_get_home_dir(), p);
166 return g_strdup(str);