2 * Copyright (c) 1997 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. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Kungliga Tekniska
20 * Högskolan and its contributors.
22 * 4. Neither the name of the Institute nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 #define BITSIZE(TYPE) \
62 int b = 0; TYPE x = 1, zero = 0; char *pre = "u_"; \
63 char tmp[128], tmp2[128]; \
64 while(x){ x <<= 1; b++; if(x < zero) pre=""; } \
67 sprintf(tmp, "%sint%d_t" , pre, len); \
68 sprintf(tmp2, "typedef %s %s;", #TYPE, tmp); \
70 tabs = 5 - strlen(tmp2) / 8; \
71 fprintf(f, "%s", tmp2); \
72 while(tabs-- > 0) fprintf(f, "\t"); \
73 fprintf(f, "/* %2d bits */\n", b); \
79 try_signed(FILE *f
, int len
)
88 fprintf(f
, "/* There is no %d bit type */\n", len
);
92 try_unsigned(FILE *f
, int len
)
94 BITSIZE(unsigned char);
95 BITSIZE(unsigned short);
96 BITSIZE(unsigned int);
97 BITSIZE(unsigned long);
99 BITSIZE(unsigned long long);
101 fprintf(f
, "/* There is no %d bit type */\n", len
);
105 print_bt(FILE *f
, int flag
)
108 fprintf(f
, "/* For compatibility with various type definitions */\n");
109 fprintf(f
, "#ifndef __BIT_TYPES_DEFINED__\n");
110 fprintf(f
, "#define __BIT_TYPES_DEFINED__\n");
116 int main(int argc
, char **argv
)
129 hb
= malloc(strlen(fn
) + 5);
130 sprintf(hb
, "__%s__", fn
);
131 for(p
= hb
; *p
; p
++){
135 f
= fopen(argv
[1], "w");
137 fprintf(f
, "/* %s -- this file was generated for %s by\n", fn
, HOST
);
138 fprintf(f
, " %*s %s */\n\n", (int)strlen(fn
), "",
140 fprintf(f
, "#ifndef %s\n", hb
);
141 fprintf(f
, "#define %s\n", hb
);
143 #ifdef HAVE_SYS_TYPES_H
144 fprintf(f
, "#include <sys/types.h>\n");
146 #ifdef HAVE_INTTYPES_H
147 fprintf(f
, "#include <inttypes.h>\n");
149 #ifdef HAVE_SYS_BITYPES_H
150 fprintf(f
, "#include <sys/bitypes.h>\n");
152 #ifdef HAVE_NETINET_IN6_MACHTYPES_H
153 fprintf(f
, "#include <netinet/in6_machtypes.h>\n");
159 flag
= print_bt(f
, flag
);
161 #endif /* HAVE_INT8_T */
163 flag
= print_bt(f
, flag
);
165 #endif /* HAVE_INT16_T */
167 flag
= print_bt(f
, flag
);
169 #endif /* HAVE_INT32_T */
172 flag
= print_bt(f
, flag
);
174 #endif /* HAVE_INT64_T */
177 #ifndef HAVE_U_INT8_T
178 flag
= print_bt(f
, flag
);
180 #endif /* HAVE_INT8_T */
181 #ifndef HAVE_U_INT16_T
182 flag
= print_bt(f
, flag
);
183 try_unsigned (f
, 16);
184 #endif /* HAVE_U_INT16_T */
185 #ifndef HAVE_U_INT32_T
186 flag
= print_bt(f
, flag
);
187 try_unsigned (f
, 32);
188 #endif /* HAVE_U_INT32_T */
190 #ifndef HAVE_U_INT64_T
191 flag
= print_bt(f
, flag
);
192 try_unsigned (f
, 64);
193 #endif /* HAVE_U_INT64_T */
198 fprintf(f
, "#endif /* __BIT_TYPES_DEFINED__ */\n\n");
200 fprintf(f
, "#endif /* %s */\n", hb
);