No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / include / bits.c
blob0a80631adb398214b611298d360496df5826512b
1 /*
2 * Copyright (c) 1997-2002 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 __RCSID("$Heimdal: bits.c 18703 2006-10-20 20:33:58Z lha $"
37 "$NetBSD$");
38 #endif
39 #include <stdio.h>
40 #include <string.h>
41 #include <stdlib.h>
42 #include <ctype.h>
44 #define BITSIZE(TYPE) \
45 { \
46 int b = 0; TYPE x = 1, zero = 0; const char *pre = "u"; \
47 char tmp[128], tmp2[128]; \
48 while(x){ x <<= 1; b++; if(x < zero) pre=""; } \
49 if(b >= len){ \
50 int tabs; \
51 sprintf(tmp, "%sint%d_t" , pre, len); \
52 sprintf(tmp2, "typedef %s %s;", #TYPE, tmp); \
53 tabs = 5 - strlen(tmp2) / 8; \
54 fprintf(f, "%s", tmp2); \
55 while(tabs-- > 0) fprintf(f, "\t"); \
56 fprintf(f, "/* %2d bits */\n", b); \
57 return; \
58 } \
61 #ifndef HAVE___ATTRIBUTE__
62 #define __attribute__(x)
63 #endif
65 static void
66 try_signed(FILE *f, int len) __attribute__ ((unused));
68 static void
69 try_unsigned(FILE *f, int len) __attribute__ ((unused));
71 static int
72 print_bt(FILE *f, int flag) __attribute__ ((unused));
74 static void
75 try_signed(FILE *f, int len)
77 BITSIZE(signed char);
78 BITSIZE(short);
79 BITSIZE(int);
80 BITSIZE(long);
81 #ifdef HAVE_LONG_LONG
82 BITSIZE(long long);
83 #endif
84 fprintf(f, "/* There is no %d bit type */\n", len);
87 static void
88 try_unsigned(FILE *f, int len)
90 BITSIZE(unsigned char);
91 BITSIZE(unsigned short);
92 BITSIZE(unsigned int);
93 BITSIZE(unsigned long);
94 #ifdef HAVE_LONG_LONG
95 BITSIZE(unsigned long long);
96 #endif
97 fprintf(f, "/* There is no %d bit type */\n", len);
100 static int
101 print_bt(FILE *f, int flag)
103 if(flag == 0){
104 fprintf(f, "/* For compatibility with various type definitions */\n");
105 fprintf(f, "#ifndef __BIT_TYPES_DEFINED__\n");
106 fprintf(f, "#define __BIT_TYPES_DEFINED__\n");
107 fprintf(f, "\n");
109 return 1;
112 int main(int argc, char **argv)
114 FILE *f;
115 int flag;
116 const char *fn, *hb;
118 if (argc > 1 && strcmp(argv[1], "--version") == 0) {
119 printf("some version");
120 return 0;
123 if(argc < 2){
124 fn = "bits.h";
125 hb = "__BITS_H__";
126 f = stdout;
127 } else {
128 char *p;
129 fn = argv[1];
130 p = malloc(strlen(fn) + 5);
131 sprintf(p, "__%s__", fn);
132 hb = p;
133 for(; *p; p++){
134 if(!isalnum((unsigned char)*p))
135 *p = '_';
137 f = fopen(argv[1], "w");
139 fprintf(f, "/* %s -- this file was generated for %s by\n", fn, HOST);
140 fprintf(f, " %*s %s */\n\n", (int)strlen(fn), "",
141 "$Id: bits.c,v 1.2 2008/03/22 08:37:02 mlelstv Exp $");
142 fprintf(f, "#ifndef %s\n", hb);
143 fprintf(f, "#define %s\n", hb);
144 fprintf(f, "\n");
145 #ifdef HAVE_INTTYPES_H
146 fprintf(f, "#include <inttypes.h>\n");
147 #endif
148 #ifdef HAVE_SYS_TYPES_H
149 fprintf(f, "#include <sys/types.h>\n");
150 #endif
151 #ifdef HAVE_SYS_BITYPES_H
152 fprintf(f, "#include <sys/bitypes.h>\n");
153 #endif
154 #ifdef HAVE_BIND_BITYPES_H
155 fprintf(f, "#include <bind/bitypes.h>\n");
156 #endif
157 #ifdef HAVE_NETINET_IN6_MACHTYPES_H
158 fprintf(f, "#include <netinet/in6_machtypes.h>\n");
159 #endif
160 #ifdef HAVE_SOCKLEN_T
161 fprintf(f, "#include <sys/socket.h>\n");
162 #endif
163 fprintf(f, "\n");
165 flag = 0;
166 #ifndef HAVE_INT8_T
167 flag = print_bt(f, flag);
168 try_signed (f, 8);
169 #endif /* HAVE_INT8_T */
170 #ifndef HAVE_INT16_T
171 flag = print_bt(f, flag);
172 try_signed (f, 16);
173 #endif /* HAVE_INT16_T */
174 #ifndef HAVE_INT32_T
175 flag = print_bt(f, flag);
176 try_signed (f, 32);
177 #endif /* HAVE_INT32_T */
178 #ifndef HAVE_INT64_T
179 flag = print_bt(f, flag);
180 try_signed (f, 64);
181 #endif /* HAVE_INT64_T */
183 #ifndef HAVE_UINT8_T
184 flag = print_bt(f, flag);
185 try_unsigned (f, 8);
186 #endif /* HAVE_UINT8_T */
187 #ifndef HAVE_UINT16_T
188 flag = print_bt(f, flag);
189 try_unsigned (f, 16);
190 #endif /* HAVE_UINT16_T */
191 #ifndef HAVE_UINT32_T
192 flag = print_bt(f, flag);
193 try_unsigned (f, 32);
194 #endif /* HAVE_UINT32_T */
195 #ifndef HAVE_UINT64_T
196 flag = print_bt(f, flag);
197 try_unsigned (f, 64);
198 #endif /* HAVE_UINT64_T */
200 #define X(S) fprintf(f, "typedef uint" #S "_t u_int" #S "_t;\n")
201 #ifndef HAVE_U_INT8_T
202 flag = print_bt(f, flag);
203 X(8);
204 #endif /* HAVE_U_INT8_T */
205 #ifndef HAVE_U_INT16_T
206 flag = print_bt(f, flag);
207 X(16);
208 #endif /* HAVE_U_INT16_T */
209 #ifndef HAVE_U_INT32_T
210 flag = print_bt(f, flag);
211 X(32);
212 #endif /* HAVE_U_INT32_T */
213 #ifndef HAVE_U_INT64_T
214 flag = print_bt(f, flag);
215 X(64);
216 #endif /* HAVE_U_INT64_T */
218 if(flag){
219 fprintf(f, "\n");
220 fprintf(f, "#endif /* __BIT_TYPES_DEFINED__ */\n\n");
222 #ifdef KRB5
223 fprintf(f, "\n");
224 #if defined(HAVE_SOCKLEN_T)
225 fprintf(f, "typedef socklen_t krb5_socklen_t;\n");
226 #else
227 fprintf(f, "typedef int krb5_socklen_t;\n");
228 #endif
229 #if defined(HAVE_SSIZE_T)
230 #ifdef HAVE_UNISTD_H
231 fprintf(f, "#include <unistd.h>\n");
232 #endif
233 fprintf(f, "typedef ssize_t krb5_ssize_t;\n");
234 #else
235 fprintf(f, "typedef int krb5_ssize_t;\n");
236 #endif
237 fprintf(f, "\n");
238 #endif /* KRB5 */
239 fprintf(f, "#endif /* %s */\n", hb);
240 return 0;