Fix.
[shishi.git] / tests / utils.c
blobe1eb8c876d0246ab3de6d32987696e4b4d0264a1
1 /* utils.c Shishi self tests utilities.
2 * Copyright (C) 2002, 2003 Simon Josefsson
4 * This file is part of Shishi.
6 * Shishi is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * Shishi is distributed in the hope that it will be useful,
12 * but 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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Shishi; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #if HAVE_CONFIG_H
23 #include "config.h"
24 #endif
26 #ifdef STDC_HEADERS
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <ctype.h>
31 #endif
33 #if HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
37 #ifdef HAVE_NETDB_H
38 #include <netdb.h>
39 #endif
41 #if defined HAVE_DECL_H_ERRNO && !HAVE_DECL_H_ERRNO
42 /* extern int h_errno; */
43 #endif
45 #ifdef HAVE_PWD_H
46 #include <pwd.h>
47 #endif
49 #ifdef HAVE_SYS_TYPES_H
50 #include <sys/types.h>
51 #endif
53 #ifdef HAVE_SYS_SELECT_H
54 #include <sys/select.h>
55 #endif
57 #ifdef HAVE_SYS_SOCKET_H
58 #include <sys/socket.h>
59 #endif
61 #ifdef HAVE_SYS_IOCTL_H
62 #include <sys/ioctl.h>
63 #endif
65 #ifdef HAVE_ERRNO_H
66 #include <errno.h>
67 #endif
69 #if HAVE_INTTYPES_H
70 # include <inttypes.h>
71 #else
72 # if HAVE_STDINT_H
73 # include <stdint.h>
74 # endif
75 #endif
77 #if TIME_WITH_SYS_TIME
78 # include <sys/time.h>
79 # include <time.h>
80 #else
81 # if HAVE_SYS_TIME_H
82 # include <sys/time.h>
83 # else
84 # include <time.h>
85 # endif
86 #endif
88 #if HAVE_STRING_H
89 # if !STDC_HEADERS && HAVE_MEMORY_H
90 # include <memory.h>
91 # endif
92 # include <string.h>
93 #endif
94 #if HAVE_STRINGS_H
95 # include <strings.h>
96 #endif
98 #ifdef HAVE_SIGNAL_H
99 #include <signal.h>
100 #endif
102 #ifdef HAVE_NETINET_IN_H
103 #include <netinet/in.h>
104 #endif
105 #ifdef HAVE_NETINET_IN6_H
106 #include <netinet/in6.h>
107 #endif
109 const char *program_name = PACKAGE;
111 static int verbose = 0;
112 static int debug = 0;
113 static int error_count = 0;
114 static int break_on_error = 0;
116 static void
117 fail (const char *format, ...)
119 va_list arg_ptr;
121 va_start (arg_ptr, format);
122 vfprintf (stderr, format, arg_ptr);
123 va_end (arg_ptr);
124 error_count++;
125 if (break_on_error)
126 exit (1);
129 static void
130 success (const char *format, ...)
132 va_list arg_ptr;
134 va_start (arg_ptr, format);
135 if (verbose)
136 vfprintf (stdout, format, arg_ptr);
137 va_end (arg_ptr);
140 static void
141 escapeprint (const unsigned char *str, int len)
143 int i;
145 if (!str || !len)
146 return;
148 printf ("\t ;; `");
149 for (i = 0; i < len; i++)
150 if ((str[i] >= 'A' && str[i] <= 'Z') ||
151 (str[i] >= 'a' && str[i] <= 'z') ||
152 (str[i] >= '0' && str[i] <= '9') || str[i] == '.')
153 printf ("%c", str[i]);
154 else
155 printf ("\\x%02x", str[i]);
156 printf ("' (length %d bytes)\n", len);
159 static void
160 hexprint (const unsigned char *str, int len)
162 int i;
164 if (!str || !len)
165 return;
167 printf ("\t ;; ");
168 for (i = 0; i < len; i++)
170 printf ("%02x ", str[i]);
171 if ((i + 1) % 8 == 0)
172 printf (" ");
173 if ((i + 1) % 16 == 0 && i + 1 < len)
174 printf ("\n\t ;; ");
178 static void
179 binprint (const unsigned char *str, int len)
181 int i;
183 if (!str || !len)
184 return;
186 printf ("\t ;; ");
187 for (i = 0; i < len; i++)
189 printf ("%d%d%d%d%d%d%d%d ",
190 str[i] & 0x80 ? 1 : 0,
191 str[i] & 0x40 ? 1 : 0,
192 str[i] & 0x20 ? 1 : 0,
193 str[i] & 0x10 ? 1 : 0,
194 str[i] & 0x08 ? 1 : 0,
195 str[i] & 0x04 ? 1 : 0,
196 str[i] & 0x02 ? 1 : 0, str[i] & 0x01 ? 1 : 0);
197 if ((i + 1) % 3 == 0)
198 printf (" ");
199 if ((i + 1) % 6 == 0 && i + 1 < len)
200 printf ("\n\t ;; ");