Home | History | Annotate | Line # | Download | only in include
      1 /*	$NetBSD: bits.c,v 1.4 2023/06/19 21:41:41 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997-2002 Kungliga Tekniska Hgskolan
      5  * (Royal Institute of Technology, Stockholm, Sweden).
      6  * All rights reserved.
      7  *
      8  * Portions Copyright (c) 2010 Apple Inc. All rights reserved.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  *
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  *
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  *
     21  * 3. Neither the name of the Institute nor the names of its contributors
     22  *    may be used to endorse or promote products derived from this software
     23  *    without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
     29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  * SUCH DAMAGE.
     36  */
     37 
     38 #ifdef HAVE_CONFIG_H
     39 #include <config.h>
     40 __RCSID("$NetBSD: bits.c,v 1.4 2023/06/19 21:41:41 christos Exp $");
     41 #endif
     42 #include <stdio.h>
     43 #include <string.h>
     44 #include <stdlib.h>
     45 #include <ctype.h>
     46 #ifdef WIN32
     47 #include <winsock2.h>
     48 #include <ws2tcpip.h>
     49 #endif
     50 
     51 #define BITSIZE(TYPE)						\
     52 {								\
     53     int b = 0; TYPE x = 1, zero = 0; const char *pre = "u";	\
     54     char tmp[128], tmp2[192];					\
     55     while(x){ x <<= 1; b++; if(x < zero) pre=""; }		\
     56     if(b >= len){						\
     57         size_t tabs;						\
     58 	sprintf(tmp, "%sint%d_t" , pre, len);			\
     59 	sprintf(tmp2, "typedef %s %s;", #TYPE, tmp);		\
     60 	tabs = 5 - strlen(tmp2) / 8;				\
     61         fprintf(f, "%s", tmp2);					\
     62 	while(tabs-- > 0) fprintf(f, "\t");			\
     63 	fprintf(f, "/* %2d bits */\n", b);			\
     64         return;                                                 \
     65     }								\
     66 }
     67 
     68 #ifndef HAVE___ATTRIBUTE__
     69 #define __attribute__(x)
     70 #endif
     71 
     72 static void
     73 try_signed(FILE *f, int len)  __attribute__ ((__unused__));
     74 
     75 static void
     76 try_unsigned(FILE *f, int len) __attribute__ ((__unused__));
     77 
     78 static int
     79 print_bt(FILE *f, int flag) __attribute__ ((__unused__));
     80 
     81 static void
     82 try_signed(FILE *f, int len)
     83 {
     84     BITSIZE(signed char);
     85     BITSIZE(short);
     86     BITSIZE(int);
     87     BITSIZE(long);
     88 #ifdef HAVE_LONG_LONG
     89     BITSIZE(long long);
     90 #endif
     91     fprintf(f, "/* There is no %d bit type */\n", len);
     92 }
     93 
     94 static void
     95 try_unsigned(FILE *f, int len)
     96 {
     97     BITSIZE(unsigned char);
     98     BITSIZE(unsigned short);
     99     BITSIZE(unsigned int);
    100     BITSIZE(unsigned long);
    101 #ifdef HAVE_LONG_LONG
    102     BITSIZE(unsigned long long);
    103 #endif
    104     fprintf(f, "/* There is no %d bit type */\n", len);
    105 }
    106 
    107 static int
    108 print_bt(FILE *f, int flag)
    109 {
    110     if(flag == 0){
    111 	fprintf(f, "/* For compatibility with various type definitions */\n");
    112 	fprintf(f, "#ifndef __BIT_TYPES_DEFINED__\n");
    113 	fprintf(f, "#define __BIT_TYPES_DEFINED__\n");
    114 	fprintf(f, "\n");
    115     }
    116     return 1;
    117 }
    118 
    119 int main(int argc, char **argv)
    120 {
    121     FILE *f;
    122     int flag;
    123     char *p = NULL;
    124     const char *hb;
    125 
    126     if (argc > 1 && strcmp(argv[1], "--version") == 0) {
    127 	printf("some version");
    128 	return 0;
    129     }
    130 
    131     if(argc < 2){
    132 	hb = "__BITS_H__";
    133 	f = stdout;
    134     } else {
    135 	p = malloc(strlen(argv[1]) + 5);
    136 	sprintf(p, "__%s__", argv[1]);
    137 	hb = p;
    138 	for(; *p; p++){
    139 	    if(!isalnum((unsigned char)*p))
    140 		*p = '_';
    141 	}
    142 	f = fopen(argv[1], "w");
    143     }
    144     fprintf(f, "#ifndef %s\n", hb);
    145     fprintf(f, "#define %s\n", hb);
    146     fprintf(f, "\n");
    147 #ifdef HAVE_INTTYPES_H
    148     fprintf(f, "#include <inttypes.h>\n");
    149 #endif
    150 #ifdef HAVE_SYS_TYPES_H
    151     fprintf(f, "#include <sys/types.h>\n");
    152 #endif
    153 #ifdef HAVE_SYS_BITYPES_H
    154     fprintf(f, "#include <sys/bitypes.h>\n");
    155 #endif
    156 #ifdef HAVE_BIND_BITYPES_H
    157     fprintf(f, "#include <bind/bitypes.h>\n");
    158 #endif
    159 #ifdef HAVE_NETINET_IN6_MACHTYPES_H
    160     fprintf(f, "#include <netinet/in6_machtypes.h>\n");
    161 #endif
    162 #ifdef HAVE_SOCKLEN_T
    163 #ifndef WIN32
    164     fprintf(f, "#include <sys/socket.h>\n");
    165 #else
    166     fprintf(f, "#include <winsock2.h>\n");
    167     fprintf(f, "#include <ws2tcpip.h>\n");
    168 #endif
    169 #endif
    170     fprintf(f, "\n");
    171 
    172     flag = 0;
    173 #ifndef HAVE_INT8_T
    174     flag = print_bt(f, flag);
    175     try_signed (f, 8);
    176 #endif /* HAVE_INT8_T */
    177 #ifndef HAVE_INT16_T
    178     flag = print_bt(f, flag);
    179     try_signed (f, 16);
    180 #endif /* HAVE_INT16_T */
    181 #ifndef HAVE_INT32_T
    182     flag = print_bt(f, flag);
    183     try_signed (f, 32);
    184 #endif /* HAVE_INT32_T */
    185 #ifndef HAVE_INT64_T
    186     flag = print_bt(f, flag);
    187     try_signed (f, 64);
    188 #endif /* HAVE_INT64_T */
    189 
    190 #ifndef HAVE_UINT8_T
    191     flag = print_bt(f, flag);
    192     try_unsigned (f, 8);
    193 #endif /* HAVE_UINT8_T */
    194 #ifndef HAVE_UINT16_T
    195     flag = print_bt(f, flag);
    196     try_unsigned (f, 16);
    197 #endif /* HAVE_UINT16_T */
    198 #ifndef HAVE_UINT32_T
    199     flag = print_bt(f, flag);
    200     try_unsigned (f, 32);
    201 #endif /* HAVE_UINT32_T */
    202 #ifndef HAVE_UINT64_T
    203     flag = print_bt(f, flag);
    204     try_unsigned (f, 64);
    205 #endif /* HAVE_UINT64_T */
    206 
    207 #define X(S) fprintf(f, "typedef uint" #S "_t u_int" #S "_t;\n")
    208 #ifndef HAVE_U_INT8_T
    209     flag = print_bt(f, flag);
    210     X(8);
    211 #endif /* HAVE_U_INT8_T */
    212 #ifndef HAVE_U_INT16_T
    213     flag = print_bt(f, flag);
    214     X(16);
    215 #endif /* HAVE_U_INT16_T */
    216 #ifndef HAVE_U_INT32_T
    217     flag = print_bt(f, flag);
    218     X(32);
    219 #endif /* HAVE_U_INT32_T */
    220 #ifndef HAVE_U_INT64_T
    221     flag = print_bt(f, flag);
    222     X(64);
    223 #endif /* HAVE_U_INT64_T */
    224 
    225     if(flag){
    226 	fprintf(f, "\n");
    227 	fprintf(f, "#endif /* __BIT_TYPES_DEFINED__ */\n\n");
    228     }
    229 #ifdef KRB5
    230     fprintf(f, "\n");
    231 #if defined(HAVE_SOCKLEN_T)
    232     fprintf(f, "typedef socklen_t krb5_socklen_t;\n");
    233 #else
    234     fprintf(f, "typedef int krb5_socklen_t;\n");
    235 #endif
    236 #if defined(HAVE_SSIZE_T)
    237 #ifdef HAVE_UNISTD_H
    238     fprintf(f, "#include <unistd.h>\n");
    239 #endif
    240     fprintf(f, "typedef ssize_t krb5_ssize_t;\n");
    241 #else
    242     fprintf(f, "typedef int krb5_ssize_t;\n");
    243 #endif
    244     fprintf(f, "\n");
    245 
    246 #if defined(_WIN32)
    247     fprintf(f, "typedef SOCKET krb5_socket_t;\n");
    248 #else
    249     fprintf(f, "typedef int krb5_socket_t;\n");
    250 #endif
    251     fprintf(f, "\n");
    252 
    253 #endif /* KRB5 */
    254 
    255     fprintf(f, "#if !defined(__has_extension)\n");
    256     fprintf(f, "#define __has_extension(x) 0\n");
    257     fprintf(f, "#endif\n\n");
    258 
    259     fprintf(f, "#ifndef KRB5TYPES_REQUIRE_GNUC\n");
    260     fprintf(f, "#define KRB5TYPES_REQUIRE_GNUC(m,n,p) \\\n");
    261     fprintf(f, "    (((__GNUC__ * 10000) + (__GNUC_MINOR__ * 100) + __GNUC_PATCHLEVEL__) >= \\\n");
    262     fprintf(f, "     (((m) * 10000) + ((n) * 100) + (p)))\n");
    263     fprintf(f, "#endif\n\n");
    264 
    265     fprintf(f, "#ifndef HEIMDAL_DEPRECATED\n");
    266     fprintf(f, "#if __has_extension(deprecated) || KRB5TYPES_REQUIRE_GNUC(3,1,0)\n");
    267     fprintf(f, "#define HEIMDAL_DEPRECATED __attribute__ ((__deprecated__))\n");
    268     fprintf(f, "#elif defined(_MSC_VER) && (_MSC_VER>1200)\n");
    269     fprintf(f, "#define HEIMDAL_DEPRECATED __declspec(deprecated)\n");
    270     fprintf(f, "#else\n");
    271     fprintf(f, "#define HEIMDAL_DEPRECATED\n");
    272     fprintf(f, "#endif\n");
    273     fprintf(f, "#endif\n\n");
    274 
    275     fprintf(f, "#ifndef HEIMDAL_PRINTF_ATTRIBUTE\n");
    276     fprintf(f, "#if __has_extension(format) || KRB5TYPES_REQUIRE_GNUC(3,1,0)\n");
    277     fprintf(f, "#define HEIMDAL_PRINTF_ATTRIBUTE(x) __attribute__ ((__format__ x))\n");
    278     fprintf(f, "#else\n");
    279     fprintf(f, "#define HEIMDAL_PRINTF_ATTRIBUTE(x)\n");
    280     fprintf(f, "#endif\n");
    281     fprintf(f, "#endif\n\n");
    282 
    283     fprintf(f, "#ifndef HEIMDAL_NORETURN_ATTRIBUTE\n");
    284     fprintf(f, "#if __has_extension(noreturn) || KRB5TYPES_REQUIRE_GNUC(3,1,0)\n");
    285     fprintf(f, "#define HEIMDAL_NORETURN_ATTRIBUTE __attribute__ ((__noreturn__))\n");
    286     fprintf(f, "#else\n");
    287     fprintf(f, "#define HEIMDAL_NORETURN_ATTRIBUTE\n");
    288     fprintf(f, "#endif\n");
    289     fprintf(f, "#endif\n\n");
    290 
    291     fprintf(f, "#ifndef HEIMDAL_UNUSED_ATTRIBUTE\n");
    292     fprintf(f, "#if __has_extension(unused) || KRB5TYPES_REQUIRE_GNUC(3,1,0)\n");
    293     fprintf(f, "#define HEIMDAL_UNUSED_ATTRIBUTE __attribute__ ((__unused__))\n");
    294     fprintf(f, "#else\n");
    295     fprintf(f, "#define HEIMDAL_UNUSED_ATTRIBUTE\n");
    296     fprintf(f, "#endif\n");
    297     fprintf(f, "#endif\n\n");
    298 
    299     fprintf(f, "#ifndef HEIMDAL_WARN_UNUSED_RESULT_ATTRIBUTE\n");
    300     fprintf(f, "#if __has_extension(warn_unused_result) || KRB5TYPES_REQUIRE_GNUC(3,3,0)\n");
    301     fprintf(f, "#define HEIMDAL_WARN_UNUSED_RESULT_ATTRIBUTE __attribute__ ((__warn_unused_result__))\n");
    302     fprintf(f, "#else\n");
    303     fprintf(f, "#define HEIMDAL_WARN_UNUSED_RESULT_ATTRIBUTE\n");
    304     fprintf(f, "#endif\n");
    305     fprintf(f, "#endif\n\n");
    306 
    307     fprintf(f, "#endif /* %s */\n", hb);
    308 
    309     if (f != stdout)
    310 	fclose(f);
    311     return 0;
    312 }
    313