Debian Codegraph @

tests/common.h

raw
1 /* common.h - Common functions for the tests.
2 * Copyright (C) 2006 Free Software Foundation, Inc.
3 *
4 * This file is part of Assuan.
5 *
6 * Assuan is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 3 of
9 * the License, or (at your option) any later version.
10 *
11 * Assuan is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <stdarg.h>
21
22 #if __GNUC__ >= 4
23 # define MY_GCC_A_SENTINEL(a) __attribute__ ((sentinel(a)))
24 #else
25 # define MY_GCC_A_SENTINEL(a)
26 #endif
27
28
29 #if HAVE_W64_SYSTEM
30 #define SOCKET2HANDLE(s) ((void *)(s))
31 #define HANDLE2SOCKET(h) ((uintptr_t)(h))
32 CRITICAL_SECTION _log_critsect;
33 #define _log_enter() do { EnterCriticalSection (&_log_critsect); } while (0)
34 #define _log_leave() do { LeaveCriticalSection (&_log_critsect); } while (0)
35 #elif HAVE_W32_SYSTEM
36 #define SOCKET2HANDLE(s) ((void *)(s))
37 #define HANDLE2SOCKET(h) ((unsigned int)(h))
38 CRITICAL_SECTION _log_critsect;
39 #define _log_enter() do { EnterCriticalSection (&_log_critsect); } while (0)
40 #define _log_leave() do { LeaveCriticalSection (&_log_critsect); } while (0)
41 #else
42 #define SOCKET2HANDLE(s) (s)
43 #define HANDLE2SOCKET(h) (h)
44 #define _log_enter() do { } while (0)
45 #define _log_leave() do { } while (0)
46 #endif
47
48 #define DIM(v) (sizeof(v)/sizeof((v)[0]))
49 #define DIMof(type,member) DIM(((type *)0)->member)
50
51
52 char *xstrconcat (const char *s1, ...) MY_GCC_A_SENTINEL(0);
53
54
55 static const char *log_prefix;
56 static int errorcount;
57 static int verbose;
58 static int debug;
59
60 void *
61 xmalloc (size_t n)
62 {
63 char *p = malloc (n);
64 if (!p)
65 {
66 if (log_prefix)
67 fprintf (stderr, "%s[%u]: ", log_prefix, (unsigned int)getpid ());
68 fprintf (stderr, "out of core\n");
69 exit (1);
70 }
71 return p;
72 }
73
74 void *
75 xcalloc (size_t n, size_t m)
76 {
77 char *p = calloc (n, m);
78 if (!p)
79 {
80 _log_enter ();
81 if (log_prefix)
82 fprintf (stderr, "%s[%u]: ", log_prefix, (unsigned int)getpid ());
83 fprintf (stderr, "out of core\n");
84 _log_leave ();
85 exit (1);
86 }
87 return p;
88 }
89
90 void
91 xfree (void *a)
92 {
93 if (a)
94 free (a);
95 }
96
97 void *
98 xstrdup (const char *string)
99 {
100 char *p = xmalloc (strlen (string) + 1);
101 strcpy (p, string);
102 return p;
103 }
104
105
106 void
107 log_set_prefix (const char *s)
108 {
109 #ifdef HAVE_W32_SYSTEM
110 InitializeCriticalSection (&_log_critsect);
111 log_prefix = strrchr (s, '\\');
112 #else
113 log_prefix = strrchr (s, '/');
114 #endif
115 if (log_prefix)
116 log_prefix++;
117 else
118 log_prefix = s;
119 }
120
121
122 const char *
123 log_get_prefix (void)
124 {
125 return log_prefix? log_prefix:"";
126 }
127
128
129 void
130 log_info (const char *format, ...)
131 {
132 va_list arg_ptr ;
133
134 if (!verbose)
135 return;
136
137 va_start (arg_ptr, format) ;
138 _log_enter ();
139 if (log_prefix)
140 fprintf (stderr, "%s[%u]: ", log_prefix, (unsigned int)getpid ());
141 vfprintf (stderr, format, arg_ptr );
142 _log_leave ();
143 va_end (arg_ptr);
144 }
145
146
147 void
148 log_error (const char *format, ...)
149 {
150 va_list arg_ptr ;
151
152 va_start (arg_ptr, format) ;
153 _log_enter ();
154 if (log_prefix)
155 fprintf (stderr, "%s[%u]: ", log_prefix, (unsigned int)getpid ());
156 vfprintf (stderr, format, arg_ptr );
157 _log_leave ();
158 va_end (arg_ptr);
159 errorcount++;
160 }
161
162
163 void
164 log_fatal (const char *format, ...)
165 {
166 va_list arg_ptr ;
167
168 va_start (arg_ptr, format) ;
169 _log_enter ();
170 if (log_prefix)
171 fprintf (stderr, "%s[%u]: ", log_prefix, (unsigned int)getpid ());
172 vfprintf (stderr, format, arg_ptr );
173 _log_leave ();
174 va_end (arg_ptr);
175 exit (2);
176 }
177
178
179 void
180 log_printhex (const char *text, const void *buffer, size_t length)
181 {
182 const unsigned char *s;
183
184 _log_enter ();
185 if (log_prefix)
186 fprintf (stderr, "%s[%u]: ", log_prefix, (unsigned int)getpid ());
187 fputs (text, stderr);
188 for (s=buffer; length; s++, length--)
189 fprintf (stderr, "%02X", *s);
190 putc ('\n', stderr);
191 _log_leave ();
192 }
193
194
195 /* Prepend FNAME with the srcdir environment variable's value and
196 return an allocated filename. */
197 char *
198 prepend_srcdir (const char *fname)
199 {
200 static const char *srcdir;
201 char *result;
202
203 if (!srcdir && !(srcdir = getenv ("srcdir")))
204 srcdir = ".";
205
206 result = xmalloc (strlen (srcdir) + 1 + strlen (fname) + 1);
207 strcpy (result, srcdir);
208 strcat (result, "/");
209 strcat (result, fname);
210 return result;
211 }
212
213
214 #ifndef HAVE_STPCPY
215 #undef __stpcpy
216 #undef stpcpy
217 #ifndef weak_alias
218 # define __stpcpy stpcpy
219 #endif
220 char *
221 __stpcpy (char *a,const char *b)
222 {
223 while (*b)
224 *a++ = *b++;
225 *a = 0;
226 return (char*)a;
227 }
228 #ifdef libc_hidden_def
229 libc_hidden_def (__stpcpy)
230 #endif
231 #ifdef weak_alias
232 weak_alias (__stpcpy, stpcpy)
233 #endif
234 #ifdef libc_hidden_builtin_def
235 libc_hidden_builtin_def (stpcpy)
236 #endif
237 #endif
238
239
240 static char *
241 do_strconcat (const char *s1, va_list arg_ptr)
242 {
243 const char *argv[48];
244 size_t argc;
245 size_t needed;
246 char *buffer, *p;
247
248 argc = 0;
249 argv[argc++] = s1;
250 needed = strlen (s1);
251 while (((argv[argc] = va_arg (arg_ptr, const char *))))
252 {
253 needed += strlen (argv[argc]);
254 if (argc >= DIM (argv)-1)
255 {
256 fprintf (stderr, "too many args in strconcat\n");
257 exit (1);
258 }
259 argc++;
260 }
261 needed++;
262 buffer = xmalloc (needed);
263 if (buffer)
264 {
265 for (p = buffer, argc=0; argv[argc]; argc++)
266 p = stpcpy (p, argv[argc]);
267 }
268 return buffer;
269 }
270
271
272 /* Concatenate the string S1 with all the following strings up to a
273 NULL. Returns a malloced buffer or dies on malloc error. */
274 char *
275 xstrconcat (const char *s1, ...)
276 {
277 va_list arg_ptr;
278 char *result;
279
280 if (!s1)
281 result = xstrdup ("");
282 else
283 {
284 va_start (arg_ptr, s1);
285 result = do_strconcat (s1, arg_ptr);
286 va_end (arg_ptr);
287 }
288 return result;
289 }
290