indent
[heimdal.git] / include / bits.c
blobf2ee2b9fa8ea03a3d730eb8e468bb9b4102889dc
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("$Id$");
37 #endif
38 #include <stdio.h>
39 #include <string.h>
40 #include <stdlib.h>
41 #include <ctype.h>
42 #ifdef WIN32
43 #include <ws2tcpip.h>
44 #endif
46 #define BITSIZE(TYPE) \
47 { \
48 int b = 0; TYPE x = 1, zero = 0; const char *pre = "u"; \
49 char tmp[128], tmp2[128]; \
50 while(x){ x <<= 1; b++; if(x < zero) pre=""; } \
51 if(b >= len){ \
52 int tabs; \
53 sprintf(tmp, "%sint%d_t" , pre, len); \
54 sprintf(tmp2, "typedef %s %s;", #TYPE, tmp); \
55 tabs = 5 - strlen(tmp2) / 8; \
56 fprintf(f, "%s", tmp2); \
57 while(tabs-- > 0) fprintf(f, "\t"); \
58 fprintf(f, "/* %2d bits */\n", b); \
59 return; \
60 } \
63 #ifndef HAVE___ATTRIBUTE__
64 #define __attribute__(x)
65 #endif
67 static void
68 try_signed(FILE *f, int len) __attribute__ ((unused));
70 static void
71 try_unsigned(FILE *f, int len) __attribute__ ((unused));
73 static int
74 print_bt(FILE *f, int flag) __attribute__ ((unused));
76 static void
77 try_signed(FILE *f, int len)
79 BITSIZE(signed char);
80 BITSIZE(short);
81 BITSIZE(int);
82 BITSIZE(long);
83 #ifdef HAVE_LONG_LONG
84 BITSIZE(long long);
85 #endif
86 fprintf(f, "/* There is no %d bit type */\n", len);
89 static void
90 try_unsigned(FILE *f, int len)
92 BITSIZE(unsigned char);
93 BITSIZE(unsigned short);
94 BITSIZE(unsigned int);
95 BITSIZE(unsigned long);
96 #ifdef HAVE_LONG_LONG
97 BITSIZE(unsigned long long);
98 #endif
99 fprintf(f, "/* There is no %d bit type */\n", len);
102 static int
103 print_bt(FILE *f, int flag)
105 if(flag == 0){
106 fprintf(f, "/* For compatibility with various type definitions */\n");
107 fprintf(f, "#ifndef __BIT_TYPES_DEFINED__\n");
108 fprintf(f, "#define __BIT_TYPES_DEFINED__\n");
109 fprintf(f, "\n");
111 return 1;
114 int main(int argc, char **argv)
116 FILE *f;
117 int flag;
118 const char *fn, *hb;
120 if (argc > 1 && strcmp(argv[1], "--version") == 0) {
121 printf("some version");
122 return 0;
125 if(argc < 2){
126 fn = "bits.h";
127 hb = "__BITS_H__";
128 f = stdout;
129 } else {
130 char *p;
131 fn = argv[1];
132 p = malloc(strlen(fn) + 5);
133 sprintf(p, "__%s__", fn);
134 hb = p;
135 for(; *p; p++){
136 if(!isalnum((unsigned char)*p))
137 *p = '_';
139 f = fopen(argv[1], "w");
141 fprintf(f, "/* %s -- this file was generated for %s by\n", fn, HOST);
142 fprintf(f, " %*s %s */\n\n", (int)strlen(fn), "",
143 "$Id$");
144 fprintf(f, "#ifndef %s\n", hb);
145 fprintf(f, "#define %s\n", hb);
146 fprintf(f, "\n");
147 #ifdef HAVE_INTTYPES_H
148 fprintf(f, "#include <inttypes.h>\n");
149 #endif
150 #ifdef HAVE_SYS_TYPES_H
151 fprintf(f, "#include <sys/types.h>\n");
152 #endif
153 #ifdef HAVE_SYS_BITYPES_H
154 fprintf(f, "#include <sys/bitypes.h>\n");
155 #endif
156 #ifdef HAVE_BIND_BITYPES_H
157 fprintf(f, "#include <bind/bitypes.h>\n");
158 #endif
159 #ifdef HAVE_NETINET_IN6_MACHTYPES_H
160 fprintf(f, "#include <netinet/in6_machtypes.h>\n");
161 #endif
162 #ifdef HAVE_SOCKLEN_T
163 #ifndef WIN32
164 fprintf(f, "#include <sys/socket.h>\n");
165 #else
166 fprintf(f, "#include <ws2tcpip.h>\n");
167 #endif
168 #endif
169 fprintf(f, "\n");
171 flag = 0;
172 #ifndef HAVE_INT8_T
173 flag = print_bt(f, flag);
174 try_signed (f, 8);
175 #endif /* HAVE_INT8_T */
176 #ifndef HAVE_INT16_T
177 flag = print_bt(f, flag);
178 try_signed (f, 16);
179 #endif /* HAVE_INT16_T */
180 #ifndef HAVE_INT32_T
181 flag = print_bt(f, flag);
182 try_signed (f, 32);
183 #endif /* HAVE_INT32_T */
184 #ifndef HAVE_INT64_T
185 flag = print_bt(f, flag);
186 try_signed (f, 64);
187 #endif /* HAVE_INT64_T */
189 #ifndef HAVE_UINT8_T
190 flag = print_bt(f, flag);
191 try_unsigned (f, 8);
192 #endif /* HAVE_UINT8_T */
193 #ifndef HAVE_UINT16_T
194 flag = print_bt(f, flag);
195 try_unsigned (f, 16);
196 #endif /* HAVE_UINT16_T */
197 #ifndef HAVE_UINT32_T
198 flag = print_bt(f, flag);
199 try_unsigned (f, 32);
200 #endif /* HAVE_UINT32_T */
201 #ifndef HAVE_UINT64_T
202 flag = print_bt(f, flag);
203 try_unsigned (f, 64);
204 #endif /* HAVE_UINT64_T */
206 #define X(S) fprintf(f, "typedef uint" #S "_t u_int" #S "_t;\n")
207 #ifndef HAVE_U_INT8_T
208 flag = print_bt(f, flag);
209 X(8);
210 #endif /* HAVE_U_INT8_T */
211 #ifndef HAVE_U_INT16_T
212 flag = print_bt(f, flag);
213 X(16);
214 #endif /* HAVE_U_INT16_T */
215 #ifndef HAVE_U_INT32_T
216 flag = print_bt(f, flag);
217 X(32);
218 #endif /* HAVE_U_INT32_T */
219 #ifndef HAVE_U_INT64_T
220 flag = print_bt(f, flag);
221 X(64);
222 #endif /* HAVE_U_INT64_T */
224 if(flag){
225 fprintf(f, "\n");
226 fprintf(f, "#endif /* __BIT_TYPES_DEFINED__ */\n\n");
228 #ifdef KRB5
229 fprintf(f, "\n");
230 #if defined(HAVE_SOCKLEN_T)
231 fprintf(f, "typedef socklen_t krb5_socklen_t;\n");
232 #else
233 fprintf(f, "typedef int krb5_socklen_t;\n");
234 #endif
235 #if defined(HAVE_SSIZE_T)
236 #ifdef HAVE_UNISTD_H
237 fprintf(f, "#include <unistd.h>\n");
238 #endif
239 fprintf(f, "typedef ssize_t krb5_ssize_t;\n");
240 #else
241 fprintf(f, "typedef int krb5_ssize_t;\n");
242 #endif
243 fprintf(f, "\n");
245 #if defined(_WIN32)
246 fprintf(f, "typedef SOCKET krb5_socket_t;\n");
247 #else
248 fprintf(f, "typedef int krb5_socket_t;\n");
249 #endif
250 fprintf(f, "\n");
252 #endif /* KRB5 */
253 fprintf(f, "#endif /* %s */\n", hb);
255 if (f != stdout)
256 fclose(f);
257 return 0;