This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
fix .gitignore: only ignore 'perl' in root of repo, not U/perl subdir
[metaconfig.git] / U / perl / selectminbits.U
1 ?RCS: $Id$
2 ?RCS:
3 ?RCS: Copyright (c) 1998, Jarkko Hietaniemi
4 ?RCS:
5 ?RCS: You may redistribute only under the terms of the Artistic License,
6 ?RCS: as specified in the README file that comes with the distribution.
7 ?RCS: You may reuse parts of this distribution only within the terms of
8 ?RCS: that same Artistic License; a copy of which may be found at the root
9 ?RCS: of the source tree for dist 3.0.
10 ?RCS:
11 ?MAKE:selectminbits: Compile cat rm_try Oldconfig d_select \
12         i_time i_systime i_systimek i_sysselct selecttype d_socket \
13         byteorder Myread Setvar run i_stdlib ptrsize
14 ?MAKE:  -pick add $@ %<
15 ?S:selectminbits:
16 ?S:     This variable holds the minimum number of bits operated by select.
17 ?S:     That is, if you do select(n, ...), how many bits at least will be
18 ?S:     cleared in the masks if some activity is detected.  Usually this
19 ?S:     is either n or 32*ceil(n/32), especially many little-endians do
20 ?S:     the latter.  This is only useful if you have select(), naturally.
21 ?S:.
22 ?C:SELECT_MIN_BITS:
23 ?C:     This symbol holds the minimum number of bits operated by select.
24 ?C:     That is, if you do select(n, ...), how many bits at least will be
25 ?C:     cleared in the masks if some activity is detected.  Usually this
26 ?C:     is either n or 32*ceil(n/32), especially many little-endians do
27 ?C:     the latter.  This is only useful if you have select(), naturally.
28 ?C:.
29 ?H:#define SELECT_MIN_BITS      $selectminbits  /**/
30 ?H:.
31 ?T:bits
32 ?T:safebits
33 ?F:!try
34 : check for the select 'width'
35 case "$selectminbits" in
36 '') safebits=`expr $ptrsize \* 8`
37     case "$d_select" in
38         $define)
39                 $cat <<EOM
40
41 Checking to see on how many bits at a time your select() operates...
42 EOM
43                 $cat >try.c <<EOCP
44 #include <sys/types.h>
45 #$i_time I_TIME
46 #$i_systime I_SYS_TIME
47 #$i_systimek I_SYS_TIME_KERNEL
48 #ifdef I_TIME
49 #   include <time.h>
50 #endif
51 #ifdef I_SYS_TIME
52 #   ifdef I_SYS_TIME_KERNEL
53 #       define KERNEL
54 #   endif
55 #   include <sys/time.h>
56 #   ifdef I_SYS_TIME_KERNEL
57 #       undef KERNEL
58 #   endif
59 #endif
60 #$i_sysselct I_SYS_SELECT
61 #ifdef I_SYS_SELECT
62 #include <sys/select.h>
63 #endif
64 #$d_socket HAS_SOCKET
65 #ifdef HAS_SOCKET
66 #   include <sys/socket.h> /* Might include <sys/bsdtypes.h> */
67 #endif
68 #include <stdio.h>
69 #$i_stdlib I_STDLIB
70 #ifdef I_STDLIB
71 #include <stdlib.h>
72 #endif
73 $selecttype b;
74 #define S sizeof(*(b))
75 #define MINBITS 64
76 #define NBYTES (S * 8 > MINBITS ? S : MINBITS/8)
77 #define NBITS  (NBYTES * 8)
78 int main() {
79     char *s = (char *)malloc(NBYTES);
80     struct timeval t;
81     int i;
82     FILE* fp;
83     int fd;
84
85     if (!s)
86         exit(1);
87     fclose(stdin);
88     fp = fopen("try.c", "r");
89     if (fp == 0)
90       exit(2);
91     fd = fileno(fp);
92     if (fd < 0)
93       exit(3);
94     b = ($selecttype)s;
95     for (i = 0; i < NBITS; i++)
96         FD_SET(i, b);
97     t.tv_sec  = 0;
98     t.tv_usec = 0;
99     select(fd + 1, b, 0, 0, &t);
100     for (i = NBITS - 1; i > fd && FD_ISSET(i, b); i--);
101     free(s);
102     printf("%d\n", i + 1);
103     return 0;
104 }
105 EOCP
106                 set try
107                 if eval $compile_ok; then
108                         selectminbits=`$run ./try 2>/dev/null`
109                         case "$selectminbits" in
110                         '')     cat >&4 <<EOM
111 Cannot figure out on how many bits at a time your select() operates.
112 I'll play safe and guess it is $safebits bits.
113 EOM
114                                 selectminbits=$safebits
115                                 bits="$safebits bits"
116                                 ;;
117                         1)      bits="1 bit" ;;
118                         *)      bits="$selectminbits bits" ;;
119                         esac
120                         echo "Your select() operates on $bits at a time." >&4
121                 else
122                         rp='What is the minimum number of bits your select() operates on?'
123                         case "$byteorder" in
124                         12345678)       dflt=64 ;;
125                         1234)           dflt=32 ;;
126                         *)              dflt=1  ;;
127                         esac
128                         . ./myread
129                         val=$ans
130                         selectminbits="$val"
131                 fi
132                 $rm_try
133                 ;;
134         *)      : no select, so pick a harmless default
135                 selectminbits=$safebits
136                 ;;
137         esac
138         ;;
139 esac
140