1 #include <fcntl.h> 2 #include <stdio.h> 3 #include <sys/select.h> 4 #include <unistd.h> 5 6 #include "test_helpers.h" 7 8 int file_test(void) { 9 int fd = open("select.c", 0, 0); 10 if (fd < 0) { 11 perror("open"); 12 return -1; 13 } 14 15 printf("Testing select on file\n"); 16 17 fd_set read; 18 FD_ZERO(&read); 19 FD_SET(fd, &read); 20 21 printf("Is set before? %d\n", FD_ISSET(fd, &read)); 22 23 int nfds = select(fd + 1, &read, NULL, NULL, NULL); 24 if (nfds < 0) { 25 perror("select"); 26 return 1; 27 } 28 printf("Amount of things ready: %d\n", nfds); 29 30 printf("Is set after? %d\n", FD_ISSET(fd, &read)); 31 32 close(fd); 33 34 return 0; 35 } 36 37 int pipe_test(void) { 38 int pipefd[2]; 39 if (pipe2(pipefd, O_NONBLOCK) < 0) { 40 perror("pipe"); 41 return 1; 42 } 43 44 char c = 'c'; 45 if (write(pipefd[1], &c, sizeof(c)) < 0) { 46 perror("write"); 47 return 1; 48 } 49 50 printf("Testing select on pipe\n"); 51 52 fd_set read; 53 FD_ZERO(&read); 54 FD_SET(pipefd[0], &read); 55 56 printf("Is set before? %d\n", FD_ISSET(pipefd[0], &read)); 57 58 int nfds = select(pipefd[0] + 1, &read, NULL, NULL, NULL); 59 if (nfds < 0) { 60 perror("select"); 61 return 1; 62 } 63 printf("Amount of things ready: %d\n", nfds); 64 65 printf("Is set after? %d\n", FD_ISSET(pipefd[0], &read)); 66 67 close(pipefd[0]); 68 close(pipefd[1]); 69 70 return 0; 71 } 72 73 int main(void) { 74 if (file_test()) { 75 return 1; 76 } 77 78 if (pipe_test()) { 79 return 1; 80 } 81 82 return 0; 83 } 84