lib/krb5: krb5_init_creds_set_service fail if set_realm fails
[heimdal.git] / include / bits.c
blob6abdb15c9113069464d7a8f207582ae8b3691a9f
1 /*
2 * Copyright (c) 1997-2002 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Portions Copyright (c) 2010 Apple Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 RCSID("$Id$");
39 #endif
40 #include <stdio.h>
41 #include <string.h>
42 #include <stdlib.h>
43 #include <ctype.h>
44 #ifdef WIN32
45 #include <winsock2.h>
46 #include <ws2tcpip.h>
47 #endif
49 #ifdef HAVE_SNPRINTF
50 #define BITSIZE(TYPE) \
51 { \
52 int b = 0; TYPE x = 1, zero = 0; const char *pre = "u"; \
53 char tmp[128]; \
54 while(x){ x <<= 1; b++; if(x < zero) pre=""; } \
55 if(b >= len){ \
56 size_t tabs; \
57 snprintf(tmp, sizeof(tmp), "typedef %s %sint%d_t;", #TYPE, \
58 pre, len); \
59 tabs = 5 - strlen(tmp) / 8; \
60 fprintf(f, "%s", tmp); \
61 while(tabs-- > 0) fprintf(f, "\t"); \
62 fprintf(f, "/* %2d bits */\n", b); \
63 return; \
64 } \
66 #else
67 #define BITSIZE(TYPE) \
68 { \
69 int b = 0; TYPE x = 1, zero = 0; const char *pre = "u"; \
70 char tmp[128], tmp2[128]; \
71 while(x){ x <<= 1; b++; if(x < zero) pre=""; } \
72 if(b >= len){ \
73 size_t tabs; \
74 sprintf(tmp, "%sint%d_t" , pre, len); \
75 sprintf(tmp2, "typedef %s %s;", #TYPE, tmp); \
76 tabs = 5 - strlen(tmp2) / 8; \
77 fprintf(f, "%s", tmp2); \
78 while(tabs-- > 0) \
79 fprintf(f, "\t"); \
80 fprintf(f, "/* %2d bits */\n", b); \
81 return; \
82 } \
84 #endif
85 #ifndef HAVE___ATTRIBUTE__
86 #define __attribute__(x)
87 #endif
89 static void
90 try_signed(FILE *f, int len) __attribute__ ((__unused__));
92 static void
93 try_unsigned(FILE *f, int len) __attribute__ ((__unused__));
95 static int
96 print_bt(FILE *f, int flag) __attribute__ ((__unused__));
98 static void
99 try_signed(FILE *f, int len)
101 BITSIZE(signed char);
102 BITSIZE(short);
103 BITSIZE(int);
104 BITSIZE(long);
105 #ifdef HAVE_LONG_LONG
106 BITSIZE(long long);
107 #endif
108 fprintf(f, "/* There is no %d bit type */\n", len);
111 static void
112 try_unsigned(FILE *f, int len)
114 BITSIZE(unsigned char);
115 BITSIZE(unsigned short);
116 BITSIZE(unsigned int);
117 BITSIZE(unsigned long);
118 #ifdef HAVE_LONG_LONG
119 BITSIZE(unsigned long long);
120 #endif
121 fprintf(f, "/* There is no %d bit type */\n", len);
124 static int
125 print_bt(FILE *f, int flag)
127 if(flag == 0){
128 fprintf(f, "/* For compatibility with various type definitions */\n");
129 fprintf(f, "#ifndef __BIT_TYPES_DEFINED__\n");
130 fprintf(f, "#define __BIT_TYPES_DEFINED__\n");
131 fprintf(f, "\n");
133 return 1;
136 int main(int argc, char **argv)
138 FILE *f;
139 int flag;
140 char *p = NULL;
141 const char *hb;
143 if (argc > 1 && strcmp(argv[1], "--version") == 0) {
144 printf("some version");
145 return 0;
148 if(argc < 2){
149 hb = "__BITS_H__";
150 f = stdout;
151 } else {
152 p = malloc(strlen(argv[1]) + 5);
153 sprintf(p, "__%s__", argv[1]);
154 hb = p;
155 for(; *p; p++){
156 if(!isalnum((unsigned char)*p))
157 *p = '_';
159 f = fopen(argv[1], "w");
161 fprintf(f, "#ifndef %s\n", hb);
162 fprintf(f, "#define %s\n", hb);
163 fprintf(f, "\n");
164 #ifdef HAVE_INTTYPES_H
165 fprintf(f, "#include <inttypes.h>\n");
166 #endif
167 #ifdef HAVE_SYS_TYPES_H
168 fprintf(f, "#include <sys/types.h>\n");
169 #endif
170 #ifdef HAVE_SYS_BITYPES_H
171 fprintf(f, "#include <sys/bitypes.h>\n");
172 #endif
173 #ifdef HAVE_BIND_BITYPES_H
174 fprintf(f, "#include <bind/bitypes.h>\n");
175 #endif
176 #ifdef HAVE_NETINET_IN6_MACHTYPES_H
177 fprintf(f, "#include <netinet/in6_machtypes.h>\n");
178 #endif
179 #ifdef HAVE_SOCKLEN_T
180 #ifndef WIN32
181 fprintf(f, "#include <sys/socket.h>\n");
182 #else
183 fprintf(f, "#include <winsock2.h>\n");
184 fprintf(f, "#include <ws2tcpip.h>\n");
185 #endif
186 #endif
187 fprintf(f, "\n");
189 flag = 0;
190 #ifndef HAVE_INT8_T
191 flag = print_bt(f, flag);
192 try_signed (f, 8);
193 #endif /* HAVE_INT8_T */
194 #ifndef HAVE_INT16_T
195 flag = print_bt(f, flag);
196 try_signed (f, 16);
197 #endif /* HAVE_INT16_T */
198 #ifndef HAVE_INT32_T
199 flag = print_bt(f, flag);
200 try_signed (f, 32);
201 #endif /* HAVE_INT32_T */
202 #ifndef HAVE_INT64_T
203 flag = print_bt(f, flag);
204 try_signed (f, 64);
205 #endif /* HAVE_INT64_T */
207 #ifndef HAVE_UINT8_T
208 flag = print_bt(f, flag);
209 try_unsigned (f, 8);
210 #endif /* HAVE_UINT8_T */
211 #ifndef HAVE_UINT16_T
212 flag = print_bt(f, flag);
213 try_unsigned (f, 16);
214 #endif /* HAVE_UINT16_T */
215 #ifndef HAVE_UINT32_T
216 flag = print_bt(f, flag);
217 try_unsigned (f, 32);
218 #endif /* HAVE_UINT32_T */
219 #ifndef HAVE_UINT64_T
220 flag = print_bt(f, flag);
221 try_unsigned (f, 64);
222 #endif /* HAVE_UINT64_T */
224 #define X(S) fprintf(f, "typedef uint" #S "_t u_int" #S "_t;\n")
225 #ifndef HAVE_U_INT8_T
226 flag = print_bt(f, flag);
227 X(8);
228 #endif /* HAVE_U_INT8_T */
229 #ifndef HAVE_U_INT16_T
230 flag = print_bt(f, flag);
231 X(16);
232 #endif /* HAVE_U_INT16_T */
233 #ifndef HAVE_U_INT32_T
234 flag = print_bt(f, flag);
235 X(32);
236 #endif /* HAVE_U_INT32_T */
237 #ifndef HAVE_U_INT64_T
238 flag = print_bt(f, flag);
239 X(64);
240 #endif /* HAVE_U_INT64_T */
242 if(flag){
243 fprintf(f, "\n");
244 fprintf(f, "#endif /* __BIT_TYPES_DEFINED__ */\n\n");
246 #ifdef KRB5
247 fprintf(f, "\n");
248 #if defined(HAVE_SOCKLEN_T)
249 fprintf(f, "typedef socklen_t krb5_socklen_t;\n");
250 #else
251 fprintf(f, "typedef int krb5_socklen_t;\n");
252 #endif
253 #if defined(HAVE_SSIZE_T)
254 #ifdef HAVE_UNISTD_H
255 fprintf(f, "#include <unistd.h>\n");
256 #endif
257 fprintf(f, "typedef ssize_t krb5_ssize_t;\n");
258 #else
259 fprintf(f, "typedef int krb5_ssize_t;\n");
260 #endif
261 fprintf(f, "\n");
263 #if defined(_WIN32)
264 fprintf(f, "typedef SOCKET krb5_socket_t;\n");
265 #else
266 fprintf(f, "typedef int krb5_socket_t;\n");
267 #endif
268 fprintf(f, "\n");
270 #endif /* KRB5 */
272 fprintf(f, "#if !defined(__has_extension)\n");
273 fprintf(f, "#define __has_extension(x) 0\n");
274 fprintf(f, "#endif\n\n");
276 fprintf(f, "#ifndef KRB5TYPES_REQUIRE_GNUC\n");
277 fprintf(f, "#define KRB5TYPES_REQUIRE_GNUC(m,n,p) \\\n");
278 fprintf(f, " (((__GNUC__ * 10000) + (__GNUC_MINOR__ * 100) + __GNUC_PATCHLEVEL__) >= \\\n");
279 fprintf(f, " (((m) * 10000) + ((n) * 100) + (p)))\n");
280 fprintf(f, "#endif\n\n");
282 fprintf(f, "#ifndef HEIMDAL_DEPRECATED\n");
283 fprintf(f, "#if __has_extension(deprecated) || KRB5TYPES_REQUIRE_GNUC(3,1,0)\n");
284 fprintf(f, "#define HEIMDAL_DEPRECATED __attribute__ ((__deprecated__))\n");
285 fprintf(f, "#elif defined(_MSC_VER) && (_MSC_VER>1200)\n");
286 fprintf(f, "#define HEIMDAL_DEPRECATED __declspec(deprecated)\n");
287 fprintf(f, "#else\n");
288 fprintf(f, "#define HEIMDAL_DEPRECATED\n");
289 fprintf(f, "#endif\n");
290 fprintf(f, "#endif\n\n");
292 fprintf(f, "#ifndef HEIMDAL_PRINTF_ATTRIBUTE\n");
293 fprintf(f, "#if __has_extension(format) || KRB5TYPES_REQUIRE_GNUC(3,1,0)\n");
294 fprintf(f, "#define HEIMDAL_PRINTF_ATTRIBUTE(x) __attribute__ ((__format__ x))\n");
295 fprintf(f, "#else\n");
296 fprintf(f, "#define HEIMDAL_PRINTF_ATTRIBUTE(x)\n");
297 fprintf(f, "#endif\n");
298 fprintf(f, "#endif\n\n");
300 fprintf(f, "#ifndef HEIMDAL_NORETURN_ATTRIBUTE\n");
301 fprintf(f, "#if __has_extension(noreturn) || KRB5TYPES_REQUIRE_GNUC(3,1,0)\n");
302 fprintf(f, "#define HEIMDAL_NORETURN_ATTRIBUTE __attribute__ ((__noreturn__))\n");
303 fprintf(f, "#else\n");
304 fprintf(f, "#define HEIMDAL_NORETURN_ATTRIBUTE\n");
305 fprintf(f, "#endif\n");
306 fprintf(f, "#endif\n\n");
308 fprintf(f, "#ifndef HEIMDAL_UNUSED_ATTRIBUTE\n");
309 fprintf(f, "#if __has_extension(unused) || KRB5TYPES_REQUIRE_GNUC(3,1,0)\n");
310 fprintf(f, "#define HEIMDAL_UNUSED_ATTRIBUTE __attribute__ ((__unused__))\n");
311 fprintf(f, "#else\n");
312 fprintf(f, "#define HEIMDAL_UNUSED_ATTRIBUTE\n");
313 fprintf(f, "#endif\n");
314 fprintf(f, "#endif\n\n");
316 fprintf(f, "#ifndef HEIMDAL_WARN_UNUSED_RESULT_ATTRIBUTE\n");
317 fprintf(f, "#if __has_extension(warn_unused_result) || KRB5TYPES_REQUIRE_GNUC(3,3,0)\n");
318 fprintf(f, "#define HEIMDAL_WARN_UNUSED_RESULT_ATTRIBUTE __attribute__ ((__warn_unused_result__))\n");
319 fprintf(f, "#else\n");
320 fprintf(f, "#define HEIMDAL_WARN_UNUSED_RESULT_ATTRIBUTE\n");
321 fprintf(f, "#endif\n");
322 fprintf(f, "#endif\n\n");
324 fprintf(f, "#endif /* %s */\n", hb);
326 if (f != stdout)
327 fclose(f);
328 return 0;