This commit was manufactured by cvs2svn to create tag 'heimdal-0-7'.
[heimdal.git] / include / bits.c
blobf5630766ca494ae23f2839db3bc67770204d7a8d
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>
43 #define BITSIZE(TYPE) \
44 { \
45 int b = 0; TYPE x = 1, zero = 0; const char *pre = "u"; \
46 char tmp[128], tmp2[128]; \
47 while(x){ x <<= 1; b++; if(x < zero) pre=""; } \
48 if(b >= len){ \
49 int tabs; \
50 sprintf(tmp, "%sint%d_t" , pre, len); \
51 sprintf(tmp2, "typedef %s %s;", #TYPE, tmp); \
52 tabs = 5 - strlen(tmp2) / 8; \
53 fprintf(f, "%s", tmp2); \
54 while(tabs-- > 0) fprintf(f, "\t"); \
55 fprintf(f, "/* %2d bits */\n", b); \
56 return; \
57 } \
60 #ifndef HAVE___ATTRIBUTE__
61 #define __attribute__(x)
62 #endif
64 static void
65 try_signed(FILE *f, int len) __attribute__ ((unused));
67 static void
68 try_unsigned(FILE *f, int len) __attribute__ ((unused));
70 static int
71 print_bt(FILE *f, int flag) __attribute__ ((unused));
73 static void
74 try_signed(FILE *f, int len)
76 BITSIZE(signed char);
77 BITSIZE(short);
78 BITSIZE(int);
79 BITSIZE(long);
80 #ifdef HAVE_LONG_LONG
81 BITSIZE(long long);
82 #endif
83 fprintf(f, "/* There is no %d bit type */\n", len);
86 static void
87 try_unsigned(FILE *f, int len)
89 BITSIZE(unsigned char);
90 BITSIZE(unsigned short);
91 BITSIZE(unsigned int);
92 BITSIZE(unsigned long);
93 #ifdef HAVE_LONG_LONG
94 BITSIZE(unsigned long long);
95 #endif
96 fprintf(f, "/* There is no %d bit type */\n", len);
99 static int
100 print_bt(FILE *f, int flag)
102 if(flag == 0){
103 fprintf(f, "/* For compatibility with various type definitions */\n");
104 fprintf(f, "#ifndef __BIT_TYPES_DEFINED__\n");
105 fprintf(f, "#define __BIT_TYPES_DEFINED__\n");
106 fprintf(f, "\n");
108 return 1;
111 int main(int argc, char **argv)
113 FILE *f;
114 int flag;
115 const char *fn, *hb;
117 if(argc < 2){
118 fn = "bits.h";
119 hb = "__BITS_H__";
120 f = stdout;
121 } else {
122 char *p;
123 fn = argv[1];
124 p = malloc(strlen(fn) + 5);
125 sprintf(p, "__%s__", fn);
126 hb = p;
127 for(; *p; p++){
128 if(!isalnum((unsigned char)*p))
129 *p = '_';
131 f = fopen(argv[1], "w");
133 fprintf(f, "/* %s -- this file was generated for %s by\n", fn, HOST);
134 fprintf(f, " %*s %s */\n\n", (int)strlen(fn), "",
135 "$Id$");
136 fprintf(f, "#ifndef %s\n", hb);
137 fprintf(f, "#define %s\n", hb);
138 fprintf(f, "\n");
139 #ifdef HAVE_INTTYPES_H
140 fprintf(f, "#include <inttypes.h>\n");
141 #endif
142 #ifdef HAVE_SYS_TYPES_H
143 fprintf(f, "#include <sys/types.h>\n");
144 #endif
145 #ifdef HAVE_SYS_BITYPES_H
146 fprintf(f, "#include <sys/bitypes.h>\n");
147 #endif
148 #ifdef HAVE_BIND_BITYPES_H
149 fprintf(f, "#include <bind/bitypes.h>\n");
150 #endif
151 #ifdef HAVE_NETINET_IN6_MACHTYPES_H
152 fprintf(f, "#include <netinet/in6_machtypes.h>\n");
153 #endif
154 #ifdef HAVE_SOCKLEN_T
155 fprintf(f, "#include <sys/socket.h>\n");
156 #endif
157 fprintf(f, "\n");
159 flag = 0;
160 #ifndef HAVE_INT8_T
161 flag = print_bt(f, flag);
162 try_signed (f, 8);
163 #endif /* HAVE_INT8_T */
164 #ifndef HAVE_INT16_T
165 flag = print_bt(f, flag);
166 try_signed (f, 16);
167 #endif /* HAVE_INT16_T */
168 #ifndef HAVE_INT32_T
169 flag = print_bt(f, flag);
170 try_signed (f, 32);
171 #endif /* HAVE_INT32_T */
172 #if 0
173 #ifndef HAVE_INT64_T
174 flag = print_bt(f, flag);
175 try_signed (f, 64);
176 #endif /* HAVE_INT64_T */
177 #endif
179 #ifndef HAVE_UINT8_T
180 flag = print_bt(f, flag);
181 try_unsigned (f, 8);
182 #endif /* HAVE_UINT8_T */
183 #ifndef HAVE_UINT16_T
184 flag = print_bt(f, flag);
185 try_unsigned (f, 16);
186 #endif /* HAVE_UINT16_T */
187 #ifndef HAVE_UINT32_T
188 flag = print_bt(f, flag);
189 try_unsigned (f, 32);
190 #endif /* HAVE_UINT32_T */
191 #if 0
192 #ifndef HAVE_UINT64_T
193 flag = print_bt(f, flag);
194 try_unsigned (f, 64);
195 #endif /* HAVE_UINT64_T */
196 #endif
198 #define X(S) fprintf(f, "typedef uint" #S "_t u_int" #S "_t;\n")
199 #ifndef HAVE_U_INT8_T
200 flag = print_bt(f, flag);
201 X(8);
202 #endif /* HAVE_U_INT8_T */
203 #ifndef HAVE_U_INT16_T
204 flag = print_bt(f, flag);
205 X(16);
206 #endif /* HAVE_U_INT16_T */
207 #ifndef HAVE_U_INT32_T
208 flag = print_bt(f, flag);
209 X(32);
210 #endif /* HAVE_U_INT32_T */
211 #if 0
212 #ifndef HAVE_U_INT64_T
213 flag = print_bt(f, flag);
214 X(64);
215 #endif /* HAVE_U_INT64_T */
216 #endif
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;