bits.c revision 55682
155682Smarkm/* 255682Smarkm * Copyright (c) 1997 - 1999 Kungliga Tekniska H�gskolan 355682Smarkm * (Royal Institute of Technology, Stockholm, Sweden). 455682Smarkm * All rights reserved. 555682Smarkm * 655682Smarkm * Redistribution and use in source and binary forms, with or without 755682Smarkm * modification, are permitted provided that the following conditions 855682Smarkm * are met: 955682Smarkm * 1055682Smarkm * 1. Redistributions of source code must retain the above copyright 1155682Smarkm * notice, this list of conditions and the following disclaimer. 1255682Smarkm * 1355682Smarkm * 2. Redistributions in binary form must reproduce the above copyright 1455682Smarkm * notice, this list of conditions and the following disclaimer in the 1555682Smarkm * documentation and/or other materials provided with the distribution. 1655682Smarkm * 1755682Smarkm * 3. Neither the name of the Institute nor the names of its contributors 1855682Smarkm * may be used to endorse or promote products derived from this software 1955682Smarkm * without specific prior written permission. 2055682Smarkm * 2155682Smarkm * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 2255682Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2355682Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2455682Smarkm * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 2555682Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2655682Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2755682Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2855682Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2955682Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3055682Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3155682Smarkm * SUCH DAMAGE. 3255682Smarkm */ 3355682Smarkm 3455682Smarkm#ifdef HAVE_CONFIG_H 3555682Smarkm#include <config.h> 3655682SmarkmRCSID("$Id: bits.c,v 1.16 1999/12/02 17:04:57 joda Exp $"); 3755682Smarkm#endif 3855682Smarkm#include <stdio.h> 3955682Smarkm#include <string.h> 4055682Smarkm#include <stdlib.h> 4155682Smarkm#include <ctype.h> 4255682Smarkm 4355682Smarkmstatic void 4455682Smarkmmy_strupr(char *s) 4555682Smarkm{ 4655682Smarkm char *p = s; 4755682Smarkm 4855682Smarkm while(*p){ 4955682Smarkm if(islower((unsigned char)*p)) 5055682Smarkm *p = toupper((unsigned char)*p); 5155682Smarkm p++; 5255682Smarkm } 5355682Smarkm} 5455682Smarkm 5555682Smarkm 5655682Smarkm#define BITSIZE(TYPE) \ 5755682Smarkm{ \ 5855682Smarkm int b = 0; TYPE x = 1, zero = 0; char *pre = "u_"; \ 5955682Smarkm char tmp[128], tmp2[128]; \ 6055682Smarkm while(x){ x <<= 1; b++; if(x < zero) pre=""; } \ 6155682Smarkm if(b >= len){ \ 6255682Smarkm int tabs; \ 6355682Smarkm sprintf(tmp, "%sint%d_t" , pre, len); \ 6455682Smarkm sprintf(tmp2, "typedef %s %s;", #TYPE, tmp); \ 6555682Smarkm my_strupr(tmp); \ 6655682Smarkm tabs = 5 - strlen(tmp2) / 8; \ 6755682Smarkm fprintf(f, "%s", tmp2); \ 6855682Smarkm while(tabs-- > 0) fprintf(f, "\t"); \ 6955682Smarkm fprintf(f, "/* %2d bits */\n", b); \ 7055682Smarkm return; \ 7155682Smarkm } \ 7255682Smarkm} 7355682Smarkm 7455682Smarkmstatic void 7555682Smarkmtry_signed(FILE *f, int len) 7655682Smarkm{ 7755682Smarkm BITSIZE(signed char); 7855682Smarkm BITSIZE(short); 7955682Smarkm BITSIZE(int); 8055682Smarkm BITSIZE(long); 8155682Smarkm#ifdef HAVE_LONG_LONG 8255682Smarkm BITSIZE(long long); 8355682Smarkm#endif 8455682Smarkm fprintf(f, "/* There is no %d bit type */\n", len); 8555682Smarkm} 8655682Smarkm 8755682Smarkmstatic void 8855682Smarkmtry_unsigned(FILE *f, int len) 8955682Smarkm{ 9055682Smarkm BITSIZE(unsigned char); 9155682Smarkm BITSIZE(unsigned short); 9255682Smarkm BITSIZE(unsigned int); 9355682Smarkm BITSIZE(unsigned long); 9455682Smarkm#ifdef HAVE_LONG_LONG 9555682Smarkm BITSIZE(unsigned long long); 9655682Smarkm#endif 9755682Smarkm fprintf(f, "/* There is no %d bit type */\n", len); 9855682Smarkm} 9955682Smarkm 10055682Smarkmstatic int 10155682Smarkmprint_bt(FILE *f, int flag) 10255682Smarkm{ 10355682Smarkm if(flag == 0){ 10455682Smarkm fprintf(f, "/* For compatibility with various type definitions */\n"); 10555682Smarkm fprintf(f, "#ifndef __BIT_TYPES_DEFINED__\n"); 10655682Smarkm fprintf(f, "#define __BIT_TYPES_DEFINED__\n"); 10755682Smarkm fprintf(f, "\n"); 10855682Smarkm } 10955682Smarkm return 1; 11055682Smarkm} 11155682Smarkm 11255682Smarkmint main(int argc, char **argv) 11355682Smarkm{ 11455682Smarkm FILE *f; 11555682Smarkm int flag; 11655682Smarkm char *fn, *hb; 11755682Smarkm 11855682Smarkm if(argc < 2){ 11955682Smarkm fn = "bits.h"; 12055682Smarkm hb = "__BITS_H__"; 12155682Smarkm f = stdout; 12255682Smarkm } else { 12355682Smarkm char *p; 12455682Smarkm fn = argv[1]; 12555682Smarkm hb = malloc(strlen(fn) + 5); 12655682Smarkm sprintf(hb, "__%s__", fn); 12755682Smarkm for(p = hb; *p; p++){ 12855682Smarkm if(!isalnum((unsigned char)*p)) 12955682Smarkm *p = '_'; 13055682Smarkm } 13155682Smarkm f = fopen(argv[1], "w"); 13255682Smarkm } 13355682Smarkm fprintf(f, "/* %s -- this file was generated for %s by\n", fn, HOST); 13455682Smarkm fprintf(f, " %*s %s */\n\n", (int)strlen(fn), "", 13555682Smarkm "$Id: bits.c,v 1.16 1999/12/02 17:04:57 joda Exp $"); 13655682Smarkm fprintf(f, "#ifndef %s\n", hb); 13755682Smarkm fprintf(f, "#define %s\n", hb); 13855682Smarkm fprintf(f, "\n"); 13955682Smarkm#ifdef HAVE_SYS_TYPES_H 14055682Smarkm fprintf(f, "#include <sys/types.h>\n"); 14155682Smarkm#endif 14255682Smarkm#ifdef HAVE_INTTYPES_H 14355682Smarkm fprintf(f, "#include <inttypes.h>\n"); 14455682Smarkm#endif 14555682Smarkm#ifdef HAVE_SYS_BITYPES_H 14655682Smarkm fprintf(f, "#include <sys/bitypes.h>\n"); 14755682Smarkm#endif 14855682Smarkm#ifdef HAVE_BIND_BITYPES_H 14955682Smarkm fprintf(f, "#include <bind/bitypes.h>\n"); 15055682Smarkm#endif 15155682Smarkm#ifdef HAVE_NETINET_IN6_MACHTYPES_H 15255682Smarkm fprintf(f, "#include <netinet/in6_machtypes.h>\n"); 15355682Smarkm#endif 15455682Smarkm fprintf(f, "\n"); 15555682Smarkm 15655682Smarkm flag = 0; 15755682Smarkm#ifndef HAVE_INT8_T 15855682Smarkm flag = print_bt(f, flag); 15955682Smarkm try_signed (f, 8); 16055682Smarkm#endif /* HAVE_INT8_T */ 16155682Smarkm#ifndef HAVE_INT16_T 16255682Smarkm flag = print_bt(f, flag); 16355682Smarkm try_signed (f, 16); 16455682Smarkm#endif /* HAVE_INT16_T */ 16555682Smarkm#ifndef HAVE_INT32_T 16655682Smarkm flag = print_bt(f, flag); 16755682Smarkm try_signed (f, 32); 16855682Smarkm#endif /* HAVE_INT32_T */ 16955682Smarkm#if 0 17055682Smarkm#ifndef HAVE_INT64_T 17155682Smarkm flag = print_bt(f, flag); 17255682Smarkm try_signed (f, 64); 17355682Smarkm#endif /* HAVE_INT64_T */ 17455682Smarkm#endif 17555682Smarkm 17655682Smarkm#ifndef HAVE_U_INT8_T 17755682Smarkm flag = print_bt(f, flag); 17855682Smarkm try_unsigned (f, 8); 17955682Smarkm#endif /* HAVE_INT8_T */ 18055682Smarkm#ifndef HAVE_U_INT16_T 18155682Smarkm flag = print_bt(f, flag); 18255682Smarkm try_unsigned (f, 16); 18355682Smarkm#endif /* HAVE_U_INT16_T */ 18455682Smarkm#ifndef HAVE_U_INT32_T 18555682Smarkm flag = print_bt(f, flag); 18655682Smarkm try_unsigned (f, 32); 18755682Smarkm#endif /* HAVE_U_INT32_T */ 18855682Smarkm#if 0 18955682Smarkm#ifndef HAVE_U_INT64_T 19055682Smarkm flag = print_bt(f, flag); 19155682Smarkm try_unsigned (f, 64); 19255682Smarkm#endif /* HAVE_U_INT64_T */ 19355682Smarkm#endif 19455682Smarkm 19555682Smarkm if(flag){ 19655682Smarkm fprintf(f, "\n"); 19755682Smarkm fprintf(f, "#endif /* __BIT_TYPES_DEFINED__ */\n\n"); 19855682Smarkm } 19955682Smarkm fprintf(f, "#endif /* %s */\n", hb); 20055682Smarkm return 0; 20155682Smarkm} 202