memset right size
[heimdal.git] / include / bits.c
blob7e76828b4e96d8832359eef5d8841559406b6687
1 /*
2 * Copyright (c) 1997-2002 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Portions Copyright (c) 2010 Apple Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 RCSID("$Id$");
39 #endif
40 #include <stdio.h>
41 #include <string.h>
42 #include <stdlib.h>
43 #include <ctype.h>
44 #ifdef WIN32
45 #include <winsock2.h>
46 #include <ws2tcpip.h>
47 #endif
49 #define BITSIZE(TYPE) \
50 { \
51 int b = 0; TYPE x = 1, zero = 0; const char *pre = "u"; \
52 char tmp[128], tmp2[128]; \
53 while(x){ x <<= 1; b++; if(x < zero) pre=""; } \
54 if(b >= len){ \
55 size_t tabs; \
56 sprintf(tmp, "%sint%d_t" , pre, len); \
57 sprintf(tmp2, "typedef %s %s;", #TYPE, tmp); \
58 tabs = 5 - strlen(tmp2) / 8; \
59 fprintf(f, "%s", tmp2); \
60 while(tabs-- > 0) fprintf(f, "\t"); \
61 fprintf(f, "/* %2d bits */\n", b); \
62 return; \
63 } \
66 #ifndef HAVE___ATTRIBUTE__
67 #define __attribute__(x)
68 #endif
70 static void
71 try_signed(FILE *f, int len) __attribute__ ((unused));
73 static void
74 try_unsigned(FILE *f, int len) __attribute__ ((unused));
76 static int
77 print_bt(FILE *f, int flag) __attribute__ ((unused));
79 static void
80 try_signed(FILE *f, int len)
82 BITSIZE(signed char);
83 BITSIZE(short);
84 BITSIZE(int);
85 BITSIZE(long);
86 #ifdef HAVE_LONG_LONG
87 BITSIZE(long long);
88 #endif
89 fprintf(f, "/* There is no %d bit type */\n", len);
92 static void
93 try_unsigned(FILE *f, int len)
95 BITSIZE(unsigned char);
96 BITSIZE(unsigned short);
97 BITSIZE(unsigned int);
98 BITSIZE(unsigned long);
99 #ifdef HAVE_LONG_LONG
100 BITSIZE(unsigned long long);
101 #endif
102 fprintf(f, "/* There is no %d bit type */\n", len);
105 static int
106 print_bt(FILE *f, int flag)
108 if(flag == 0){
109 fprintf(f, "/* For compatibility with various type definitions */\n");
110 fprintf(f, "#ifndef __BIT_TYPES_DEFINED__\n");
111 fprintf(f, "#define __BIT_TYPES_DEFINED__\n");
112 fprintf(f, "\n");
114 return 1;
117 int main(int argc, char **argv)
119 FILE *f;
120 int flag;
121 const char *fn, *hb;
123 if (argc > 1 && strcmp(argv[1], "--version") == 0) {
124 printf("some version");
125 return 0;
128 if(argc < 2){
129 fn = "bits.h";
130 hb = "__BITS_H__";
131 f = stdout;
132 } else {
133 char *p;
134 fn = argv[1];
135 p = malloc(strlen(fn) + 5);
136 sprintf(p, "__%s__", fn);
137 hb = p;
138 for(; *p; p++){
139 if(!isalnum((unsigned char)*p))
140 *p = '_';
142 f = fopen(argv[1], "w");
144 fprintf(f, "/* %s -- this file was generated for %s by\n", fn, HOST);
145 fprintf(f, " %*s %s */\n\n", (int)strlen(fn), "",
146 "$Id$");
147 fprintf(f, "#ifndef %s\n", hb);
148 fprintf(f, "#define %s\n", hb);
149 fprintf(f, "\n");
150 #ifdef HAVE_INTTYPES_H
151 fprintf(f, "#include <inttypes.h>\n");
152 #endif
153 #ifdef HAVE_SYS_TYPES_H
154 fprintf(f, "#include <sys/types.h>\n");
155 #endif
156 #ifdef HAVE_SYS_BITYPES_H
157 fprintf(f, "#include <sys/bitypes.h>\n");
158 #endif
159 #ifdef HAVE_BIND_BITYPES_H
160 fprintf(f, "#include <bind/bitypes.h>\n");
161 #endif
162 #ifdef HAVE_NETINET_IN6_MACHTYPES_H
163 fprintf(f, "#include <netinet/in6_machtypes.h>\n");
164 #endif
165 #ifdef HAVE_SOCKLEN_T
166 #ifndef WIN32
167 fprintf(f, "#include <sys/socket.h>\n");
168 #else
169 fprintf(f, "#include <winsock2.h>\n");
170 fprintf(f, "#include <ws2tcpip.h>\n");
171 #endif
172 #endif
173 fprintf(f, "\n");
175 flag = 0;
176 #ifndef HAVE_INT8_T
177 flag = print_bt(f, flag);
178 try_signed (f, 8);
179 #endif /* HAVE_INT8_T */
180 #ifndef HAVE_INT16_T
181 flag = print_bt(f, flag);
182 try_signed (f, 16);
183 #endif /* HAVE_INT16_T */
184 #ifndef HAVE_INT32_T
185 flag = print_bt(f, flag);
186 try_signed (f, 32);
187 #endif /* HAVE_INT32_T */
188 #ifndef HAVE_INT64_T
189 flag = print_bt(f, flag);
190 try_signed (f, 64);
191 #endif /* HAVE_INT64_T */
193 #ifndef HAVE_UINT8_T
194 flag = print_bt(f, flag);
195 try_unsigned (f, 8);
196 #endif /* HAVE_UINT8_T */
197 #ifndef HAVE_UINT16_T
198 flag = print_bt(f, flag);
199 try_unsigned (f, 16);
200 #endif /* HAVE_UINT16_T */
201 #ifndef HAVE_UINT32_T
202 flag = print_bt(f, flag);
203 try_unsigned (f, 32);
204 #endif /* HAVE_UINT32_T */
205 #ifndef HAVE_UINT64_T
206 flag = print_bt(f, flag);
207 try_unsigned (f, 64);
208 #endif /* HAVE_UINT64_T */
210 #define X(S) fprintf(f, "typedef uint" #S "_t u_int" #S "_t;\n")
211 #ifndef HAVE_U_INT8_T
212 flag = print_bt(f, flag);
213 X(8);
214 #endif /* HAVE_U_INT8_T */
215 #ifndef HAVE_U_INT16_T
216 flag = print_bt(f, flag);
217 X(16);
218 #endif /* HAVE_U_INT16_T */
219 #ifndef HAVE_U_INT32_T
220 flag = print_bt(f, flag);
221 X(32);
222 #endif /* HAVE_U_INT32_T */
223 #ifndef HAVE_U_INT64_T
224 flag = print_bt(f, flag);
225 X(64);
226 #endif /* HAVE_U_INT64_T */
228 if(flag){
229 fprintf(f, "\n");
230 fprintf(f, "#endif /* __BIT_TYPES_DEFINED__ */\n\n");
232 #ifdef KRB5
233 fprintf(f, "\n");
234 #if defined(HAVE_SOCKLEN_T)
235 fprintf(f, "typedef socklen_t krb5_socklen_t;\n");
236 #else
237 fprintf(f, "typedef int krb5_socklen_t;\n");
238 #endif
239 #if defined(HAVE_SSIZE_T)
240 #ifdef HAVE_UNISTD_H
241 fprintf(f, "#include <unistd.h>\n");
242 #endif
243 fprintf(f, "typedef ssize_t krb5_ssize_t;\n");
244 #else
245 fprintf(f, "typedef int krb5_ssize_t;\n");
246 #endif
247 fprintf(f, "\n");
249 #if defined(_WIN32)
250 fprintf(f, "typedef SOCKET krb5_socket_t;\n");
251 #else
252 fprintf(f, "typedef int krb5_socket_t;\n");
253 #endif
254 fprintf(f, "\n");
256 #endif /* KRB5 */
258 fprintf(f, "#ifndef HEIMDAL_DEPRECATED\n");
259 fprintf(f, "#if defined(__GNUC__) && ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1 )))\n");
260 fprintf(f, "#define HEIMDAL_DEPRECATED __attribute__((deprecated))\n");
261 fprintf(f, "#elif defined(_MSC_VER) && (_MSC_VER>1200)\n");
262 fprintf(f, "#define HEIMDAL_DEPRECATED __declspec(deprecated)\n");
263 fprintf(f, "#else\n");
264 fprintf(f, "#define HEIMDAL_DEPRECATED\n");
265 fprintf(f, "#endif\n");
266 fprintf(f, "#endif\n");
268 fprintf(f, "#ifndef HEIMDAL_PRINTF_ATTRIBUTE\n");
269 fprintf(f, "#if defined(__GNUC__) && ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1 )))\n");
270 fprintf(f, "#define HEIMDAL_PRINTF_ATTRIBUTE(x) __attribute__((format x))\n");
271 fprintf(f, "#else\n");
272 fprintf(f, "#define HEIMDAL_PRINTF_ATTRIBUTE(x)\n");
273 fprintf(f, "#endif\n");
274 fprintf(f, "#endif\n");
276 fprintf(f, "#ifndef HEIMDAL_NORETURN_ATTRIBUTE\n");
277 fprintf(f, "#if defined(__GNUC__) && ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1 )))\n");
278 fprintf(f, "#define HEIMDAL_NORETURN_ATTRIBUTE __attribute__((noreturn))\n");
279 fprintf(f, "#else\n");
280 fprintf(f, "#define HEIMDAL_NORETURN_ATTRIBUTE\n");
281 fprintf(f, "#endif\n");
282 fprintf(f, "#endif\n");
284 fprintf(f, "#ifndef HEIMDAL_UNUSED_ATTRIBUTE\n");
285 fprintf(f, "#if defined(__GNUC__) && ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1 )))\n");
286 fprintf(f, "#define HEIMDAL_UNUSED_ATTRIBUTE __attribute__((unused))\n");
287 fprintf(f, "#else\n");
288 fprintf(f, "#define HEIMDAL_UNUSED_ATTRIBUTE\n");
289 fprintf(f, "#endif\n");
290 fprintf(f, "#endif\n");
292 fprintf(f, "#endif /* %s */\n", hb);
294 if (f != stdout)
295 fclose(f);
296 return 0;