2 * Copyright (c) 1997, 1998, 1999 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
36 RCSID("$Id: bits.c,v 1.6 1999/12/02 16:58:36 joda Exp $");
47 unsigned char *p = (unsigned char *)s;
54 #endif /* HAVE_STRUPR */
56 #define BITSIZE(TYPE) \
58 int b = 0; TYPE x = 1, zero = 0; char *pre = "u_"; \
59 char tmp[128], tmp2[128]; \
60 while(x){ x <<= 1; b++; if(x < zero) pre=""; } \
63 sprintf(tmp, "%sint%d_t" , pre, len); \
64 sprintf(tmp2, "typedef %s %s;", #TYPE, tmp); \
66 tabs = 5 - strlen(tmp2) / 8; \
67 fprintf(f, "%s", tmp2); \
68 while(tabs-- > 0) fprintf(f, "\t"); \
69 fprintf(f, "/* %2d bits */\n", b); \
74 #ifndef HAVE___ATTRIBUTE__
75 #define __attribute__(x)
79 try_signed(FILE *f, int len) __attribute__ ((unused));
82 try_unsigned(FILE *f, int len) __attribute__ ((unused));
85 try_signed(FILE *f, int len)
94 fprintf(f, "/* There is no %d bit type */\n", len);
98 try_unsigned(FILE *f, int len)
100 BITSIZE(unsigned char);
101 BITSIZE(unsigned short);
102 BITSIZE(unsigned int);
103 BITSIZE(unsigned long);
104 #ifdef HAVE_LONG_LONG
105 BITSIZE(unsigned long long);
107 fprintf(f, "/* There is no %d bit type */\n", len);
111 print_bt(FILE *f, int flag)
114 fprintf(f, "/* For compatibility with various type definitions */\n");
115 fprintf(f, "#ifndef __BIT_TYPES_DEFINED__\n");
116 fprintf(f, "#define __BIT_TYPES_DEFINED__\n");
122 int main(int argc, char **argv)
135 hb = malloc(strlen(fn) + 5);
136 sprintf(hb, "__%s__", fn);
137 for(p = hb; *p; p++){
138 if(!isalnum((unsigned char)*p))
141 f = fopen(argv[1], "w");
143 fprintf(f, "/* %s -- this file was generated for %s by\n", fn, HOST);
144 fprintf(f, " %*s %s */\n\n", (int)strlen(fn), "",
145 "$Id: bits.c,v 1.6 1999/12/02 16:58:36 joda Exp $");
146 fprintf(f, "#ifndef %s\n", hb);
147 fprintf(f, "#define %s\n", hb);
149 #ifdef HAVE_SYS_TYPES_H
150 fprintf(f, "#include <sys/types.h>\n");
152 #ifdef HAVE_INTTYPES_H
153 fprintf(f, "#include <inttypes.h>\n");
155 #ifdef HAVE_SYS_BITYPES_H
156 fprintf(f, "#include <sys/bitypes.h>\n");
158 #ifdef HAVE_NETINET_IN6_MACHTYPES_H
159 fprintf(f, "#include <netinet/in6_machtypes.h>\n");
165 flag = print_bt(f, flag);
167 #endif /* HAVE_INT8_T */
169 flag = print_bt(f, flag);
171 #endif /* HAVE_INT16_T */
173 flag = print_bt(f, flag);
175 #endif /* HAVE_INT32_T */
178 flag = print_bt(f, flag);
180 #endif /* HAVE_INT64_T */
183 #ifndef HAVE_U_INT8_T
184 flag = print_bt(f, flag);
186 #endif /* HAVE_INT8_T */
187 #ifndef HAVE_U_INT16_T
188 flag = print_bt(f, flag);
189 try_unsigned (f, 16);
190 #endif /* HAVE_U_INT16_T */
191 #ifndef HAVE_U_INT32_T
192 flag = print_bt(f, flag);
193 try_unsigned (f, 32);
194 #endif /* HAVE_U_INT32_T */
196 #ifndef HAVE_U_INT64_T
197 flag = print_bt(f, flag);
198 try_unsigned (f, 64);
199 #endif /* HAVE_U_INT64_T */
204 fprintf(f, "#endif /* __BIT_TYPES_DEFINED__ */\n\n");
206 fprintf(f, "#endif /* %s */\n", hb);