Change __signed to signed.
[dragonfly.git] / crypto / kerberosIV / include / bits.c
1 /*
2  * Copyright (c) 1997, 1998, 1999 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden). 
4  * All rights reserved. 
5  *
6  * Redistribution and use in source and binary forms, with or without 
7  * modification, are permitted provided that the following conditions 
8  * are met: 
9  *
10  * 1. Redistributions of source code must retain the above copyright 
11  *    notice, this list of conditions and the following disclaimer. 
12  *
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. 
16  *
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. 
20  *
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 
31  * SUCH DAMAGE. 
32  */
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 RCSID("$Id: bits.c,v 1.6 1999/12/02 16:58:36 joda Exp $");
37 #endif
38 #include <stdio.h>
39 #include <string.h>
40 #include <stdlib.h>
41 #include <ctype.h>
42
43 #ifndef HAVE_STRUPR
44 static void
45 strupr(char *s)
46 {
47     unsigned char *p = (unsigned char *)s;
48     while(*p){
49         if(islower(*p))
50             *p = toupper(*p);
51         p++;
52     }
53 }
54 #endif /* HAVE_STRUPR */
55
56 #define BITSIZE(TYPE)                                           \
57 {                                                               \
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=""; }              \
61     if(b >= len){                                               \
62         int tabs;                                               \
63         sprintf(tmp, "%sint%d_t" , pre, len);                   \
64         sprintf(tmp2, "typedef %s %s;", #TYPE, tmp);            \
65         strupr(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);                      \
70         return;                                                 \
71     }                                                           \
72 }
73
74 #ifndef HAVE___ATTRIBUTE__
75 #define __attribute__(x)
76 #endif
77
78 static void
79 try_signed(FILE *f, int len)  __attribute__ ((unused));
80
81 static void
82 try_unsigned(FILE *f, int len) __attribute__ ((unused));
83
84 static void
85 try_signed(FILE *f, int len)
86 {
87     BITSIZE(signed char);
88     BITSIZE(short);
89     BITSIZE(int);
90     BITSIZE(long);
91 #ifdef HAVE_LONG_LONG
92     BITSIZE(long long);
93 #endif
94     fprintf(f, "/* There is no %d bit type */\n", len);
95 }
96
97 static void
98 try_unsigned(FILE *f, int len)
99 {
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);
106 #endif
107     fprintf(f, "/* There is no %d bit type */\n", len);
108 }
109
110 static int
111 print_bt(FILE *f, int flag)
112 {
113     if(flag == 0){
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");
117         fprintf(f, "\n");
118     }
119     return 1;
120 }
121
122 int main(int argc, char **argv)
123 {
124     FILE *f;
125     int flag;
126     char *fn, *hb;
127     
128     if(argc < 2){
129         fn = "bits.h";
130         hb = "__BITS_H__";
131         f = stdout;
132     } else {
133         char *p;
134         fn = argv[1];
135         hb = malloc(strlen(fn) + 5);
136         sprintf(hb, "__%s__", fn);
137         for(p = hb; *p; p++){
138             if(!isalnum((unsigned char)*p))
139                 *p = '_';
140         }
141         f = fopen(argv[1], "w");
142     }
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);
148     fprintf(f, "\n");
149 #ifdef HAVE_SYS_TYPES_H
150     fprintf(f, "#include <sys/types.h>\n");
151 #endif
152 #ifdef HAVE_INTTYPES_H
153     fprintf(f, "#include <inttypes.h>\n");
154 #endif
155 #ifdef HAVE_SYS_BITYPES_H
156     fprintf(f, "#include <sys/bitypes.h>\n");
157 #endif
158 #ifdef HAVE_NETINET_IN6_MACHTYPES_H
159     fprintf(f, "#include <netinet/in6_machtypes.h>\n");
160 #endif
161     fprintf(f, "\n");
162
163     flag = 0;
164 #ifndef HAVE_INT8_T
165     flag = print_bt(f, flag);
166     try_signed (f, 8);
167 #endif /* HAVE_INT8_T */
168 #ifndef HAVE_INT16_T
169     flag = print_bt(f, flag);
170     try_signed (f, 16);
171 #endif /* HAVE_INT16_T */
172 #ifndef HAVE_INT32_T
173     flag = print_bt(f, flag);
174     try_signed (f, 32);
175 #endif /* HAVE_INT32_T */
176 #if 0
177 #ifndef HAVE_INT64_T
178     flag = print_bt(f, flag);
179     try_signed (f, 64);
180 #endif /* HAVE_INT64_T */
181 #endif
182
183 #ifndef HAVE_U_INT8_T
184     flag = print_bt(f, flag);
185     try_unsigned (f, 8);
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 */
195 #if 0
196 #ifndef HAVE_U_INT64_T
197     flag = print_bt(f, flag);
198     try_unsigned (f, 64);
199 #endif /* HAVE_U_INT64_T */
200 #endif
201
202     if(flag){
203         fprintf(f, "\n");
204         fprintf(f, "#endif /* __BIT_TYPES_DEFINED__ */\n\n");
205     }
206     fprintf(f, "#endif /* %s */\n", hb);
207     return 0;
208 }