(find_cells): check memory allocations
[heimdal.git] / include / bits.c
blobf390717fe36bd01b44a881e066d709a98acc5f19
1 /*
2 * Copyright (c) 1997 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. 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
36 * SUCH DAMAGE.
39 #ifdef HAVE_CONFIG_H
40 #include <config.h>
41 RCSID("$Id$");
42 #endif
43 #include <stdio.h>
44 #include <string.h>
45 #include <stdlib.h>
46 #include <ctype.h>
48 static void
49 strupr(char *s)
51 char *p = s;
52 while(*p){
53 if(islower(*p))
54 *p = toupper(*p);
55 p++;
60 #define BITSIZE(TYPE) \
61 { \
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=""; } \
65 if(b >= len){ \
66 int tabs; \
67 sprintf(tmp, "%sint%d_t" , pre, len); \
68 sprintf(tmp2, "typedef %s %s;", #TYPE, tmp); \
69 strupr(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); \
74 return; \
75 } \
78 static void
79 try_signed(FILE *f, int len)
81 BITSIZE(signed char);
82 BITSIZE(short);
83 BITSIZE(int);
84 BITSIZE(long);
85 #ifdef HAVE_LONG_LONG
86 BITSIZE(long long);
87 #endif
88 fprintf(f, "/* There is no %d bit type */\n", len);
91 static void
92 try_unsigned(FILE *f, int len)
94 BITSIZE(unsigned char);
95 BITSIZE(unsigned short);
96 BITSIZE(unsigned int);
97 BITSIZE(unsigned long);
98 #ifdef HAVE_LONG_LONG
99 BITSIZE(unsigned long long);
100 #endif
101 fprintf(f, "/* There is no %d bit type */\n", len);
104 static int
105 print_bt(FILE *f, int flag)
107 if(flag == 0){
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");
111 fprintf(f, "\n");
113 return 1;
116 int main(int argc, char **argv)
118 FILE *f;
119 int flag;
120 char *fn, *hb;
122 if(argc < 2){
123 fn = "bits.h";
124 hb = "__BITS_H__";
125 f = stdout;
126 } else {
127 char *p;
128 fn = argv[1];
129 hb = malloc(strlen(fn) + 5);
130 sprintf(hb, "__%s__", fn);
131 for(p = hb; *p; p++){
132 if(!isalnum(*p))
133 *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), "",
139 "$Id$");
140 fprintf(f, "#ifndef %s\n", hb);
141 fprintf(f, "#define %s\n", hb);
142 fprintf(f, "\n");
143 #ifdef HAVE_SYS_TYPES_H
144 fprintf(f, "#include <sys/types.h>\n");
145 #endif
146 #ifdef HAVE_INTTYPES_H
147 fprintf(f, "#include <inttypes.h>\n");
148 #endif
149 #ifdef HAVE_SYS_BITYPES_H
150 fprintf(f, "#include <sys/bitypes.h>\n");
151 #endif
152 #ifdef HAVE_NETINET_IN6_MACHTYPES_H
153 fprintf(f, "#include <netinet/in6_machtypes.h>\n");
154 #endif
155 fprintf(f, "\n");
157 flag = 0;
158 #ifndef HAVE_INT8_T
159 flag = print_bt(f, flag);
160 try_signed (f, 8);
161 #endif /* HAVE_INT8_T */
162 #ifndef HAVE_INT16_T
163 flag = print_bt(f, flag);
164 try_signed (f, 16);
165 #endif /* HAVE_INT16_T */
166 #ifndef HAVE_INT32_T
167 flag = print_bt(f, flag);
168 try_signed (f, 32);
169 #endif /* HAVE_INT32_T */
170 #if 0
171 #ifndef HAVE_INT64_T
172 flag = print_bt(f, flag);
173 try_signed (f, 64);
174 #endif /* HAVE_INT64_T */
175 #endif
177 #ifndef HAVE_U_INT8_T
178 flag = print_bt(f, flag);
179 try_unsigned (f, 8);
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 */
189 #if 0
190 #ifndef HAVE_U_INT64_T
191 flag = print_bt(f, flag);
192 try_unsigned (f, 64);
193 #endif /* HAVE_U_INT64_T */
194 #endif
196 if(flag){
197 fprintf(f, "\n");
198 fprintf(f, "#endif /* __BIT_TYPES_DEFINED__ */\n\n");
200 fprintf(f, "#endif /* %s */\n", hb);
201 return 0;