1 /* 2 * Copyright (c) 1997 - 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.16 1999/12/02 17:04:57 joda Exp $"); 37 #endif 38 #include <stdio.h> 39 #include <string.h> 40 #include <stdlib.h> 41 #include <ctype.h> 42 43 static void 44 my_strupr(char *s) 45 { 46 char *p = s; 47 48 while(*p){ 49 if(islower((unsigned char)*p)) 50 *p = toupper((unsigned char)*p); 51 p++; 52 } 53 } 54 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 my_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 static void 75 try_signed(FILE *f, int len) 76 { 77 BITSIZE(signed char); 78 BITSIZE(short); 79 BITSIZE(int); 80 BITSIZE(long); 81 #ifdef HAVE_LONG_LONG 82 BITSIZE(long long); 83 #endif 84 fprintf(f, "/* There is no %d bit type */\n", len); 85 } 86 87 static void 88 try_unsigned(FILE *f, int len) 89 { 90 BITSIZE(unsigned char); 91 BITSIZE(unsigned short); 92 BITSIZE(unsigned int); 93 BITSIZE(unsigned long); 94 #ifdef HAVE_LONG_LONG 95 BITSIZE(unsigned long long); 96 #endif 97 fprintf(f, "/* There is no %d bit type */\n", len); 98 } 99 100 static int 101 print_bt(FILE *f, int flag) 102 { 103 if(flag == 0){ 104 fprintf(f, "/* For compatibility with various type definitions */\n"); 105 fprintf(f, "#ifndef __BIT_TYPES_DEFINED__\n"); 106 fprintf(f, "#define __BIT_TYPES_DEFINED__\n"); 107 fprintf(f, "\n"); 108 } 109 return 1; 110 } 111 112 int main(int argc, char **argv) 113 { 114 FILE *f; 115 int flag; 116 char *fn, *hb; 117 118 if(argc < 2){ 119 fn = "bits.h"; 120 hb = "__BITS_H__"; 121 f = stdout; 122 } else { 123 char *p; 124 fn = argv[1]; 125 hb = malloc(strlen(fn) + 5); 126 sprintf(hb, "__%s__", fn); 127 for(p = hb; *p; p++){ 128 if(!isalnum((unsigned char)*p)) 129 *p = '_'; 130 } 131 f = fopen(argv[1], "w"); 132 } 133 fprintf(f, "/* %s -- this file was generated for %s by\n", fn, HOST); 134 fprintf(f, " %*s %s */\n\n", (int)strlen(fn), "", 135 "$Id: bits.c,v 1.16 1999/12/02 17:04:57 joda Exp $"); 136 fprintf(f, "#ifndef %s\n", hb); 137 fprintf(f, "#define %s\n", hb); 138 fprintf(f, "\n"); 139 #ifdef HAVE_SYS_TYPES_H 140 fprintf(f, "#include <sys/types.h>\n"); 141 #endif 142 #ifdef HAVE_INTTYPES_H 143 fprintf(f, "#include <inttypes.h>\n"); 144 #endif 145 #ifdef HAVE_SYS_BITYPES_H 146 fprintf(f, "#include <sys/bitypes.h>\n"); 147 #endif 148 #ifdef HAVE_BIND_BITYPES_H 149 fprintf(f, "#include <bind/bitypes.h>\n"); 150 #endif 151 #ifdef HAVE_NETINET_IN6_MACHTYPES_H 152 fprintf(f, "#include <netinet/in6_machtypes.h>\n"); 153 #endif 154 fprintf(f, "\n"); 155 156 flag = 0; 157 #ifndef HAVE_INT8_T 158 flag = print_bt(f, flag); 159 try_signed (f, 8); 160 #endif /* HAVE_INT8_T */ 161 #ifndef HAVE_INT16_T 162 flag = print_bt(f, flag); 163 try_signed (f, 16); 164 #endif /* HAVE_INT16_T */ 165 #ifndef HAVE_INT32_T 166 flag = print_bt(f, flag); 167 try_signed (f, 32); 168 #endif /* HAVE_INT32_T */ 169 #if 0 170 #ifndef HAVE_INT64_T 171 flag = print_bt(f, flag); 172 try_signed (f, 64); 173 #endif /* HAVE_INT64_T */ 174 #endif 175 176 #ifndef HAVE_U_INT8_T 177 flag = print_bt(f, flag); 178 try_unsigned (f, 8); 179 #endif /* HAVE_INT8_T */ 180 #ifndef HAVE_U_INT16_T 181 flag = print_bt(f, flag); 182 try_unsigned (f, 16); 183 #endif /* HAVE_U_INT16_T */ 184 #ifndef HAVE_U_INT32_T 185 flag = print_bt(f, flag); 186 try_unsigned (f, 32); 187 #endif /* HAVE_U_INT32_T */ 188 #if 0 189 #ifndef HAVE_U_INT64_T 190 flag = print_bt(f, flag); 191 try_unsigned (f, 64); 192 #endif /* HAVE_U_INT64_T */ 193 #endif 194 195 if(flag){ 196 fprintf(f, "\n"); 197 fprintf(f, "#endif /* __BIT_TYPES_DEFINED__ */\n\n"); 198 } 199 fprintf(f, "#endif /* %s */\n", hb); 200 return 0; 201 } 202