bits.c revision 178825
1/* 2 * Copyright (c) 1997-2002 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> 36RCSID("$Id: bits.c 18703 2006-10-20 20:33:58Z lha $"); 37#endif 38#include <stdio.h> 39#include <string.h> 40#include <stdlib.h> 41#include <ctype.h> 42 43#define BITSIZE(TYPE) \ 44{ \ 45 int b = 0; TYPE x = 1, zero = 0; const char *pre = "u"; \ 46 char tmp[128], tmp2[128]; \ 47 while(x){ x <<= 1; b++; if(x < zero) pre=""; } \ 48 if(b >= len){ \ 49 int tabs; \ 50 sprintf(tmp, "%sint%d_t" , pre, len); \ 51 sprintf(tmp2, "typedef %s %s;", #TYPE, tmp); \ 52 tabs = 5 - strlen(tmp2) / 8; \ 53 fprintf(f, "%s", tmp2); \ 54 while(tabs-- > 0) fprintf(f, "\t"); \ 55 fprintf(f, "/* %2d bits */\n", b); \ 56 return; \ 57 } \ 58} 59 60#ifndef HAVE___ATTRIBUTE__ 61#define __attribute__(x) 62#endif 63 64static void 65try_signed(FILE *f, int len) __attribute__ ((unused)); 66 67static void 68try_unsigned(FILE *f, int len) __attribute__ ((unused)); 69 70static int 71print_bt(FILE *f, int flag) __attribute__ ((unused)); 72 73static void 74try_signed(FILE *f, int len) 75{ 76 BITSIZE(signed char); 77 BITSIZE(short); 78 BITSIZE(int); 79 BITSIZE(long); 80#ifdef HAVE_LONG_LONG 81 BITSIZE(long long); 82#endif 83 fprintf(f, "/* There is no %d bit type */\n", len); 84} 85 86static void 87try_unsigned(FILE *f, int len) 88{ 89 BITSIZE(unsigned char); 90 BITSIZE(unsigned short); 91 BITSIZE(unsigned int); 92 BITSIZE(unsigned long); 93#ifdef HAVE_LONG_LONG 94 BITSIZE(unsigned long long); 95#endif 96 fprintf(f, "/* There is no %d bit type */\n", len); 97} 98 99static int 100print_bt(FILE *f, int flag) 101{ 102 if(flag == 0){ 103 fprintf(f, "/* For compatibility with various type definitions */\n"); 104 fprintf(f, "#ifndef __BIT_TYPES_DEFINED__\n"); 105 fprintf(f, "#define __BIT_TYPES_DEFINED__\n"); 106 fprintf(f, "\n"); 107 } 108 return 1; 109} 110 111int main(int argc, char **argv) 112{ 113 FILE *f; 114 int flag; 115 const char *fn, *hb; 116 117 if (argc > 1 && strcmp(argv[1], "--version") == 0) { 118 printf("some version"); 119 return 0; 120 } 121 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 p = malloc(strlen(fn) + 5); 130 sprintf(p, "__%s__", fn); 131 hb = p; 132 for(; *p; p++){ 133 if(!isalnum((unsigned char)*p)) 134 *p = '_'; 135 } 136 f = fopen(argv[1], "w"); 137 } 138 fprintf(f, "/* %s -- this file was generated for %s by\n", fn, HOST); 139 fprintf(f, " %*s %s */\n\n", (int)strlen(fn), "", 140 "$Id: bits.c 18703 2006-10-20 20:33:58Z lha $"); 141 fprintf(f, "#ifndef %s\n", hb); 142 fprintf(f, "#define %s\n", hb); 143 fprintf(f, "\n"); 144#ifdef HAVE_INTTYPES_H 145 fprintf(f, "#include <inttypes.h>\n"); 146#endif 147#ifdef HAVE_SYS_TYPES_H 148 fprintf(f, "#include <sys/types.h>\n"); 149#endif 150#ifdef HAVE_SYS_BITYPES_H 151 fprintf(f, "#include <sys/bitypes.h>\n"); 152#endif 153#ifdef HAVE_BIND_BITYPES_H 154 fprintf(f, "#include <bind/bitypes.h>\n"); 155#endif 156#ifdef HAVE_NETINET_IN6_MACHTYPES_H 157 fprintf(f, "#include <netinet/in6_machtypes.h>\n"); 158#endif 159#ifdef HAVE_SOCKLEN_T 160 fprintf(f, "#include <sys/socket.h>\n"); 161#endif 162 fprintf(f, "\n"); 163 164 flag = 0; 165#ifndef HAVE_INT8_T 166 flag = print_bt(f, flag); 167 try_signed (f, 8); 168#endif /* HAVE_INT8_T */ 169#ifndef HAVE_INT16_T 170 flag = print_bt(f, flag); 171 try_signed (f, 16); 172#endif /* HAVE_INT16_T */ 173#ifndef HAVE_INT32_T 174 flag = print_bt(f, flag); 175 try_signed (f, 32); 176#endif /* HAVE_INT32_T */ 177#ifndef HAVE_INT64_T 178 flag = print_bt(f, flag); 179 try_signed (f, 64); 180#endif /* HAVE_INT64_T */ 181 182#ifndef HAVE_UINT8_T 183 flag = print_bt(f, flag); 184 try_unsigned (f, 8); 185#endif /* HAVE_UINT8_T */ 186#ifndef HAVE_UINT16_T 187 flag = print_bt(f, flag); 188 try_unsigned (f, 16); 189#endif /* HAVE_UINT16_T */ 190#ifndef HAVE_UINT32_T 191 flag = print_bt(f, flag); 192 try_unsigned (f, 32); 193#endif /* HAVE_UINT32_T */ 194#ifndef HAVE_UINT64_T 195 flag = print_bt(f, flag); 196 try_unsigned (f, 64); 197#endif /* HAVE_UINT64_T */ 198 199#define X(S) fprintf(f, "typedef uint" #S "_t u_int" #S "_t;\n") 200#ifndef HAVE_U_INT8_T 201 flag = print_bt(f, flag); 202 X(8); 203#endif /* HAVE_U_INT8_T */ 204#ifndef HAVE_U_INT16_T 205 flag = print_bt(f, flag); 206 X(16); 207#endif /* HAVE_U_INT16_T */ 208#ifndef HAVE_U_INT32_T 209 flag = print_bt(f, flag); 210 X(32); 211#endif /* HAVE_U_INT32_T */ 212#ifndef HAVE_U_INT64_T 213 flag = print_bt(f, flag); 214 X(64); 215#endif /* HAVE_U_INT64_T */ 216 217 if(flag){ 218 fprintf(f, "\n"); 219 fprintf(f, "#endif /* __BIT_TYPES_DEFINED__ */\n\n"); 220 } 221#ifdef KRB5 222 fprintf(f, "\n"); 223#if defined(HAVE_SOCKLEN_T) 224 fprintf(f, "typedef socklen_t krb5_socklen_t;\n"); 225#else 226 fprintf(f, "typedef int krb5_socklen_t;\n"); 227#endif 228#if defined(HAVE_SSIZE_T) 229#ifdef HAVE_UNISTD_H 230 fprintf(f, "#include <unistd.h>\n"); 231#endif 232 fprintf(f, "typedef ssize_t krb5_ssize_t;\n"); 233#else 234 fprintf(f, "typedef int krb5_ssize_t;\n"); 235#endif 236 fprintf(f, "\n"); 237#endif /* KRB5 */ 238 fprintf(f, "#endif /* %s */\n", hb); 239 return 0; 240} 241