Use thread-safe libgcrypt and libgpg-error functions. Only call the new
[pwmd.git] / src / misc.c
blob3b861d5047c36795cc3ce092b760af2e8a3db7a1
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 <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>
27 #include <string.h>
28 #include <ctype.h>
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
34 #include "misc.h"
36 gboolean strv_printf(gchar ***array, const gchar *fmt, ...)
38 gchar **a;
39 va_list ap;
40 gchar *buf;
41 gint len = *array ? g_strv_length(*array) : 0;
42 gint ret;
44 if (!fmt)
45 return FALSE;
47 if ((a = g_realloc(*array, (len + 2) * sizeof(gchar *))) == NULL)
48 return FALSE;
50 va_start(ap, fmt);
51 ret = g_vasprintf(&buf, fmt, ap);
52 va_end(ap);
54 if (ret == -1)
55 return FALSE;
57 a[len++] = buf;
58 a[len] = NULL;
59 *array = a;
60 return TRUE;
63 gchar **strvcatv(gchar **dst, gchar **src)
65 gchar **p;
66 gint i;
67 gchar **d;
69 if (!src)
70 return NULL;
72 d = g_strdupv(dst);
74 if (!d)
75 return NULL;
77 i = g_strv_length(d);
79 for (p = src; *p; p++) {
80 gchar **pa;
82 pa = g_realloc(d, (i + 2) * sizeof(gchar *));
84 if (!pa) {
85 g_strfreev(d);
86 return NULL;
89 d = pa;
90 d[i] = g_strdup(*p);
92 if (!d[i]) {
93 g_strfreev(d);
94 return NULL;
97 d[++i] = NULL;
100 return d;
103 gboolean valid_filename(const gchar *filename)
105 const gchar *p;
107 if (!filename || !*filename)
108 return FALSE;
110 for (p = filename; *p; p++) {
111 if (*p == '/' || isspace(*p))
112 return FALSE;
115 return TRUE;
118 gint open_file(const gchar *filename, struct stat *st)
120 gint fd;
122 if ((fd = open(filename, O_RDONLY)) == -1)
123 return -1;
125 if (stat(filename, st) == -1) {
126 close(fd);
127 return -1;
130 return fd;
133 gchar *print_fmt(gchar *buf, gsize len, const char *fmt, ...)
135 va_list ap;
137 va_start(ap, fmt);
138 g_vsnprintf(buf, len, fmt, ap);
139 va_end(ap);
140 return buf;
143 gboolean contains_whitespace(const gchar *str)
145 const gchar *p = str;
146 gunichar c;
147 glong len;
149 len = g_utf8_strlen(p++, -1) -1;
151 while (len--) {
152 c = g_utf8_get_char(p++);
154 if (g_unichar_isspace(c))
155 return TRUE;
158 return FALSE;
161 gchar *expand_homedir(gchar *str)
163 gchar *p = str;
165 if (*p++ == '~')
166 return g_strdup_printf("%s%s", g_get_home_dir(), p);
168 return g_strdup(str);