1*433d6423SLionel Sambuc /* t40a.c
2*433d6423SLionel Sambuc *
3*433d6423SLionel Sambuc * Test FD_* macros
4*433d6423SLionel Sambuc *
5*433d6423SLionel Sambuc * Select works on regular files, (pseudo) terminal devices, streams-based
6*433d6423SLionel Sambuc * files, FIFOs, pipes, and sockets. This test verifies the FD_* macros.
7*433d6423SLionel Sambuc *
8*433d6423SLionel Sambuc * This test is part of a bigger select test. It expects as argument which sub-
9*433d6423SLionel Sambuc * test it is.
10*433d6423SLionel Sambuc */
11*433d6423SLionel Sambuc
12*433d6423SLionel Sambuc #include <stdio.h>
13*433d6423SLionel Sambuc #include <stdlib.h>
14*433d6423SLionel Sambuc #include <unistd.h>
15*433d6423SLionel Sambuc #include <sys/select.h>
16*433d6423SLionel Sambuc #include <errno.h>
17*433d6423SLionel Sambuc #include <limits.h>
18*433d6423SLionel Sambuc
19*433d6423SLionel Sambuc #ifndef OPEN_MAX
20*433d6423SLionel Sambuc # define OPEN_MAX 1024
21*433d6423SLionel Sambuc #endif
22*433d6423SLionel Sambuc
23*433d6423SLionel Sambuc #define MAX_ERROR 5
24*433d6423SLionel Sambuc
25*433d6423SLionel Sambuc #include "common.h"
26*433d6423SLionel Sambuc
main(int argc,char ** argv)27*433d6423SLionel Sambuc int main(int argc, char **argv) {
28*433d6423SLionel Sambuc fd_set fds;
29*433d6423SLionel Sambuc int i;
30*433d6423SLionel Sambuc
31*433d6423SLionel Sambuc /* Get subtest number */
32*433d6423SLionel Sambuc if(argc != 2) {
33*433d6423SLionel Sambuc printf("Usage: %s subtest_no\n", argv[0]);
34*433d6423SLionel Sambuc exit(-1);
35*433d6423SLionel Sambuc } else if(sscanf(argv[1], "%d", &subtest) != 1) {
36*433d6423SLionel Sambuc printf("Usage: %s subtest_no\n", argv[0]);
37*433d6423SLionel Sambuc exit(-1);
38*433d6423SLionel Sambuc }
39*433d6423SLionel Sambuc
40*433d6423SLionel Sambuc /* FD_ZERO */
41*433d6423SLionel Sambuc FD_ZERO(&fds);
42*433d6423SLionel Sambuc for(i = 0; i < OPEN_MAX; i++) {
43*433d6423SLionel Sambuc if(FD_ISSET(i, &fds)) {
44*433d6423SLionel Sambuc em(1, "fd set should be completely empty");
45*433d6423SLionel Sambuc break;
46*433d6423SLionel Sambuc }
47*433d6423SLionel Sambuc }
48*433d6423SLionel Sambuc
49*433d6423SLionel Sambuc /* FD_SET */
50*433d6423SLionel Sambuc for(i = 0; i < OPEN_MAX; i++) FD_SET(i, &fds);
51*433d6423SLionel Sambuc for(i = 0; i < OPEN_MAX; i++) {
52*433d6423SLionel Sambuc if(!FD_ISSET(i, &fds)) {
53*433d6423SLionel Sambuc em(2, "fd set should be completely filled");
54*433d6423SLionel Sambuc break;
55*433d6423SLionel Sambuc }
56*433d6423SLionel Sambuc }
57*433d6423SLionel Sambuc
58*433d6423SLionel Sambuc /* Reset to empty set and verify it's really empty */
59*433d6423SLionel Sambuc FD_ZERO(&fds);
60*433d6423SLionel Sambuc for(i = 0; i < OPEN_MAX; i++) {
61*433d6423SLionel Sambuc if(FD_ISSET(i, &fds)) {
62*433d6423SLionel Sambuc em(3, "fd set should be completely empty");
63*433d6423SLionel Sambuc break;
64*433d6423SLionel Sambuc }
65*433d6423SLionel Sambuc }
66*433d6423SLionel Sambuc
67*433d6423SLionel Sambuc /* Let's try a variation on filling the set */
68*433d6423SLionel Sambuc for(i = 0; i < OPEN_MAX; i += 2) FD_SET(i, &fds);
69*433d6423SLionel Sambuc for(i = 0; i < OPEN_MAX - 1; i+= 2 ) {
70*433d6423SLionel Sambuc if(!(FD_ISSET(i, &fds) && !FD_ISSET(i+1, &fds))) {
71*433d6423SLionel Sambuc em(4, "bit pattern does not match");
72*433d6423SLionel Sambuc break;
73*433d6423SLionel Sambuc }
74*433d6423SLionel Sambuc }
75*433d6423SLionel Sambuc
76*433d6423SLionel Sambuc /* Reset to empty set and verify it's really empty */
77*433d6423SLionel Sambuc FD_ZERO(&fds);
78*433d6423SLionel Sambuc for(i = 0; i < OPEN_MAX; i++) {
79*433d6423SLionel Sambuc if(FD_ISSET(i, &fds)) {
80*433d6423SLionel Sambuc em(5,"fd set should be completely empty");
81*433d6423SLionel Sambuc break;
82*433d6423SLionel Sambuc }
83*433d6423SLionel Sambuc }
84*433d6423SLionel Sambuc
85*433d6423SLionel Sambuc /* Let's try another variation on filling the set */
86*433d6423SLionel Sambuc for(i = 0; i < OPEN_MAX - 1; i += 2) FD_SET(i+1, &fds);
87*433d6423SLionel Sambuc for(i = 0; i < OPEN_MAX - 1; i+= 2 ) {
88*433d6423SLionel Sambuc if(!(FD_ISSET(i+1, &fds) && !FD_ISSET(i, &fds))) {
89*433d6423SLionel Sambuc em(6, "bit pattern does not match");
90*433d6423SLionel Sambuc break;
91*433d6423SLionel Sambuc }
92*433d6423SLionel Sambuc }
93*433d6423SLionel Sambuc
94*433d6423SLionel Sambuc /* Reset to empty set and verify it's really empty */
95*433d6423SLionel Sambuc FD_ZERO(&fds);
96*433d6423SLionel Sambuc for(i = 0; i < OPEN_MAX; i++) {
97*433d6423SLionel Sambuc if(FD_ISSET(i, &fds)) {
98*433d6423SLionel Sambuc em(7, "fd set should be completely empty");
99*433d6423SLionel Sambuc break;
100*433d6423SLionel Sambuc }
101*433d6423SLionel Sambuc }
102*433d6423SLionel Sambuc
103*433d6423SLionel Sambuc /* FD_CLR */
104*433d6423SLionel Sambuc for(i = 0; i < OPEN_MAX; i++) FD_SET(i, &fds); /* Set all bits */
105*433d6423SLionel Sambuc for(i = 0; i < OPEN_MAX; i++) FD_CLR(i, &fds); /* Clear all bits */
106*433d6423SLionel Sambuc for(i = 0; i < OPEN_MAX; i++) {
107*433d6423SLionel Sambuc if(FD_ISSET(i, &fds)) {
108*433d6423SLionel Sambuc em(8, "all bits in fd set should be cleared");
109*433d6423SLionel Sambuc break;
110*433d6423SLionel Sambuc }
111*433d6423SLionel Sambuc }
112*433d6423SLionel Sambuc
113*433d6423SLionel Sambuc /* Reset to empty set and verify it's really empty */
114*433d6423SLionel Sambuc FD_ZERO(&fds);
115*433d6423SLionel Sambuc for(i = 0; i < OPEN_MAX; i++) {
116*433d6423SLionel Sambuc if(FD_ISSET(i, &fds)) {
117*433d6423SLionel Sambuc em(9, "fd set should be completely empty");
118*433d6423SLionel Sambuc break;
119*433d6423SLionel Sambuc }
120*433d6423SLionel Sambuc }
121*433d6423SLionel Sambuc
122*433d6423SLionel Sambuc for(i = 0; i < OPEN_MAX; i++) FD_SET(i, &fds); /* Set all bits */
123*433d6423SLionel Sambuc for(i = 0; i < OPEN_MAX; i += 2) FD_CLR(i, &fds); /* Clear all bits */
124*433d6423SLionel Sambuc for(i = 0; i < OPEN_MAX; i += 2) {
125*433d6423SLionel Sambuc if(FD_ISSET(i, &fds)) {
126*433d6423SLionel Sambuc em(10, "all even bits in fd set should be cleared");
127*433d6423SLionel Sambuc break;
128*433d6423SLionel Sambuc }
129*433d6423SLionel Sambuc }
130*433d6423SLionel Sambuc
131*433d6423SLionel Sambuc exit(errct);
132*433d6423SLionel Sambuc }
133