1 /* $NetBSD: bits.c,v 1.1.1.3 2014/04/24 12:45:27 pettai Exp $ */
4 * Copyright (c) 1997-2002 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Portions Copyright (c) 2010 Apple Inc. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the Institute nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 #define BITSIZE(TYPE) \
53 int b = 0; TYPE x = 1, zero = 0; const char *pre = "u"; \
54 char tmp[128], tmp2[128]; \
55 while(x){ x <<= 1; b++; if(x < zero) pre=""; } \
58 sprintf(tmp, "%sint%d_t" , pre, len); \
59 sprintf(tmp2, "typedef %s %s;", #TYPE, tmp); \
60 tabs = 5 - strlen(tmp2) / 8; \
61 fprintf(f, "%s", tmp2); \
62 while(tabs-- > 0) fprintf(f, "\t"); \
63 fprintf(f, "/* %2d bits */\n", b); \
68 #ifndef HAVE___ATTRIBUTE__
69 #define __attribute__(x)
73 try_signed(FILE *f
, int len
) __attribute__ ((unused
));
76 try_unsigned(FILE *f
, int len
) __attribute__ ((unused
));
79 print_bt(FILE *f
, int flag
) __attribute__ ((unused
));
82 try_signed(FILE *f
, int len
)
91 fprintf(f
, "/* There is no %d bit type */\n", len
);
95 try_unsigned(FILE *f
, int len
)
97 BITSIZE(unsigned char);
98 BITSIZE(unsigned short);
99 BITSIZE(unsigned int);
100 BITSIZE(unsigned long);
101 #ifdef HAVE_LONG_LONG
102 BITSIZE(unsigned long long);
104 fprintf(f
, "/* There is no %d bit type */\n", len
);
108 print_bt(FILE *f
, int flag
)
111 fprintf(f
, "/* For compatibility with various type definitions */\n");
112 fprintf(f
, "#ifndef __BIT_TYPES_DEFINED__\n");
113 fprintf(f
, "#define __BIT_TYPES_DEFINED__\n");
119 int main(int argc
, char **argv
)
125 if (argc
> 1 && strcmp(argv
[1], "--version") == 0) {
126 printf("some version");
137 p
= malloc(strlen(fn
) + 5);
138 sprintf(p
, "__%s__", fn
);
141 if(!isalnum((unsigned char)*p
))
144 f
= fopen(argv
[1], "w");
146 fprintf(f
, "/* %s -- this file was generated for %s by\n", fn
, HOST
);
147 fprintf(f
, " %*s %s */\n\n", (int)strlen(fn
), "",
149 fprintf(f
, "#ifndef %s\n", hb
);
150 fprintf(f
, "#define %s\n", hb
);
152 #ifdef HAVE_INTTYPES_H
153 fprintf(f
, "#include <inttypes.h>\n");
155 #ifdef HAVE_SYS_TYPES_H
156 fprintf(f
, "#include <sys/types.h>\n");
158 #ifdef HAVE_SYS_BITYPES_H
159 fprintf(f
, "#include <sys/bitypes.h>\n");
161 #ifdef HAVE_BIND_BITYPES_H
162 fprintf(f
, "#include <bind/bitypes.h>\n");
164 #ifdef HAVE_NETINET_IN6_MACHTYPES_H
165 fprintf(f
, "#include <netinet/in6_machtypes.h>\n");
167 #ifdef HAVE_SOCKLEN_T
169 fprintf(f
, "#include <sys/socket.h>\n");
171 fprintf(f
, "#include <winsock2.h>\n");
172 fprintf(f
, "#include <ws2tcpip.h>\n");
179 flag
= print_bt(f
, flag
);
181 #endif /* HAVE_INT8_T */
183 flag
= print_bt(f
, flag
);
185 #endif /* HAVE_INT16_T */
187 flag
= print_bt(f
, flag
);
189 #endif /* HAVE_INT32_T */
191 flag
= print_bt(f
, flag
);
193 #endif /* HAVE_INT64_T */
196 flag
= print_bt(f
, flag
);
198 #endif /* HAVE_UINT8_T */
199 #ifndef HAVE_UINT16_T
200 flag
= print_bt(f
, flag
);
201 try_unsigned (f
, 16);
202 #endif /* HAVE_UINT16_T */
203 #ifndef HAVE_UINT32_T
204 flag
= print_bt(f
, flag
);
205 try_unsigned (f
, 32);
206 #endif /* HAVE_UINT32_T */
207 #ifndef HAVE_UINT64_T
208 flag
= print_bt(f
, flag
);
209 try_unsigned (f
, 64);
210 #endif /* HAVE_UINT64_T */
212 #define X(S) fprintf(f, "typedef uint" #S "_t u_int" #S "_t;\n")
213 #ifndef HAVE_U_INT8_T
214 flag
= print_bt(f
, flag
);
216 #endif /* HAVE_U_INT8_T */
217 #ifndef HAVE_U_INT16_T
218 flag
= print_bt(f
, flag
);
220 #endif /* HAVE_U_INT16_T */
221 #ifndef HAVE_U_INT32_T
222 flag
= print_bt(f
, flag
);
224 #endif /* HAVE_U_INT32_T */
225 #ifndef HAVE_U_INT64_T
226 flag
= print_bt(f
, flag
);
228 #endif /* HAVE_U_INT64_T */
232 fprintf(f
, "#endif /* __BIT_TYPES_DEFINED__ */\n\n");
236 #if defined(HAVE_SOCKLEN_T)
237 fprintf(f
, "typedef socklen_t krb5_socklen_t;\n");
239 fprintf(f
, "typedef int krb5_socklen_t;\n");
241 #if defined(HAVE_SSIZE_T)
243 fprintf(f
, "#include <unistd.h>\n");
245 fprintf(f
, "typedef ssize_t krb5_ssize_t;\n");
247 fprintf(f
, "typedef int krb5_ssize_t;\n");
252 fprintf(f
, "typedef SOCKET krb5_socket_t;\n");
254 fprintf(f
, "typedef int krb5_socket_t;\n");
260 fprintf(f
, "#ifndef HEIMDAL_DEPRECATED\n");
261 fprintf(f
, "#if defined(__GNUC__) && ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1 )))\n");
262 fprintf(f
, "#define HEIMDAL_DEPRECATED __attribute__((deprecated))\n");
263 fprintf(f
, "#elif defined(_MSC_VER) && (_MSC_VER>1200)\n");
264 fprintf(f
, "#define HEIMDAL_DEPRECATED __declspec(deprecated)\n");
265 fprintf(f
, "#else\n");
266 fprintf(f
, "#define HEIMDAL_DEPRECATED\n");
267 fprintf(f
, "#endif\n");
268 fprintf(f
, "#endif\n");
270 fprintf(f
, "#ifndef HEIMDAL_PRINTF_ATTRIBUTE\n");
271 fprintf(f
, "#if defined(__GNUC__) && ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1 )))\n");
272 fprintf(f
, "#define HEIMDAL_PRINTF_ATTRIBUTE(x) __attribute__((format x))\n");
273 fprintf(f
, "#else\n");
274 fprintf(f
, "#define HEIMDAL_PRINTF_ATTRIBUTE(x)\n");
275 fprintf(f
, "#endif\n");
276 fprintf(f
, "#endif\n");
278 fprintf(f
, "#ifndef HEIMDAL_NORETURN_ATTRIBUTE\n");
279 fprintf(f
, "#if defined(__GNUC__) && ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1 )))\n");
280 fprintf(f
, "#define HEIMDAL_NORETURN_ATTRIBUTE __attribute__((noreturn))\n");
281 fprintf(f
, "#else\n");
282 fprintf(f
, "#define HEIMDAL_NORETURN_ATTRIBUTE\n");
283 fprintf(f
, "#endif\n");
284 fprintf(f
, "#endif\n");
286 fprintf(f
, "#ifndef HEIMDAL_UNUSED_ATTRIBUTE\n");
287 fprintf(f
, "#if defined(__GNUC__) && ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1 )))\n");
288 fprintf(f
, "#define HEIMDAL_UNUSED_ATTRIBUTE __attribute__((unused))\n");
289 fprintf(f
, "#else\n");
290 fprintf(f
, "#define HEIMDAL_UNUSED_ATTRIBUTE\n");
291 fprintf(f
, "#endif\n");
292 fprintf(f
, "#endif\n");
294 fprintf(f
, "#endif /* %s */\n", hb
);