Fix compile time warnings.
[pwmd.git] / src / misc.c
blob803821fb253fb26a13b42d3c16c0e0ff15630cd8
1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
2 /*
3 Copyright (C) 2006-2011 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 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <errno.h>
26 #include <unistd.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <glib.h>
31 #include <glib/gprintf.h>
32 #include <string.h>
33 #include <ctype.h>
34 #include <gpg-error.h>
35 #include "misc.h"
36 #include "pwmd_error.h"
38 extern void log_write(const gchar *fmt, ...);
40 gboolean strv_printf(gchar ***array, const gchar *fmt, ...)
42 gchar **a;
43 va_list ap;
44 gchar *buf;
45 gint len = *array ? g_strv_length(*array) : 0;
46 gint ret;
48 if (!fmt)
49 return FALSE;
51 if ((a = g_realloc(*array, (len + 2) * sizeof(gchar *))) == NULL) {
52 log_write("%s(%i): %s", __FILE__, __LINE__, pwmd_strerror(GPG_ERR_ENOMEM));
53 return FALSE;
56 va_start(ap, fmt);
57 ret = g_vasprintf(&buf, fmt, ap);
58 va_end(ap);
60 if (ret == -1) {
61 log_write("%s(%i): %s", __FILE__, __LINE__, pwmd_strerror(GPG_ERR_ENOMEM));
62 return FALSE;
65 a[len++] = buf;
66 a[len] = NULL;
67 *array = a;
68 return TRUE;
71 gchar **strvcatv(gchar **dst, gchar **src)
73 gchar **p;
74 gint i;
75 gchar **d;
77 if (!src)
78 return NULL;
80 d = g_strdupv(dst);
82 if (!d)
83 return NULL;
85 i = g_strv_length(d);
87 for (p = src; *p; p++) {
88 gchar **pa;
90 pa = g_realloc(d, (i + 2) * sizeof(gchar *));
92 if (!pa) {
93 g_strfreev(d);
94 return NULL;
97 d = pa;
98 d[i] = g_strdup(*p);
100 if (!d[i]) {
101 g_strfreev(d);
102 return NULL;
105 d[++i] = NULL;
108 return d;
111 gboolean valid_filename(const gchar *filename)
113 const gchar *p;
115 if (!filename || !*filename)
116 return FALSE;
118 if (!g_strcmp0(filename, "-"))
119 return FALSE;
121 for (p = filename; *p; p++) {
122 if (*p == '/' || isspace(*p))
123 return FALSE;
126 return TRUE;
129 gchar *print_fmt(gchar *buf, gsize len, const char *fmt, ...)
131 va_list ap;
133 va_start(ap, fmt);
134 g_vsnprintf(buf, len, fmt, ap);
135 va_end(ap);
136 return buf;
139 gchar *expand_homedir(gchar *str)
141 gchar *p = str;
143 if (*p++ == '~')
144 return g_strdup_printf("%s%s", g_get_home_dir(), p);
146 return g_strdup(str);
149 gchar **split_input_line(gchar *str, gchar *delim, gint n)
151 if (!str || !*str)
152 return NULL;
154 return g_strsplit(str, delim, n);
157 gpg_error_t parse_options(gchar **line, struct argv_s *args[], gpointer data)
159 gchar *p = *line;
160 gpg_error_t rc = 0;
162 for (; p && *p; p++) {
163 while (g_ascii_isspace(*p))
164 p++;
166 if (!*p)
167 break;
169 if (*p == '-' && *(p+1) == '-') {
170 p += 2;
171 gchar opt[255] = {0}, value[255] = {0};
172 gchar *tp;
173 guint len;
175 if (!*p || *p == ' ') {
176 if (*p)
177 p++;
179 break;
182 for (tp = opt, len = 0; *p; p++, len++) {
183 if (len+1 == 255)
184 return GPG_ERR_LINE_TOO_LONG;
186 if (*p == ' ' || *p == '=') {
187 gboolean inquote = FALSE;
189 *tp = 0;
191 if (*p == '=') {
192 p++;
194 for (tp = value, len = 0; *p; p++, len++) {
195 if (len+1 == 255)
196 return GPG_ERR_LINE_TOO_LONG;
198 if (*p == '\"') {
199 inquote = !inquote;
200 continue;
202 else if (*p == ' ' && !inquote)
203 break;
205 *tp++ = *p;
208 *tp = 0;
211 break;
214 *tp++ = *p;
217 *tp = 0;
218 gboolean match = FALSE;
220 for (int i = 0; args[i]; i++) {
221 if (!g_strcmp0(args[i]->opt, opt)) {
222 if (args[i]->type == OPTION_TYPE_NOARG && *value)
223 return GPG_ERR_SYNTAX;
224 else if (args[i]->type == OPTION_TYPE_ARG && !*value)
225 return GPG_ERR_SYNTAX;
227 rc = args[i]->func(data, value);
229 if (rc)
230 return rc;
232 log_write2("OPTION='%s' VALUE='%s'", opt, value);
233 match = TRUE;
234 break;
238 if (!match)
239 return GPG_ERR_UNKNOWN_OPTION;
241 if (!*p)
242 break;
244 continue;
247 break;
250 *line = p;
251 return rc;