1 #include <errno.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 #include <sys/select.h> 6 #include <unistd.h> 7 8 9 int 10 main(int argc, const char* const* argv) 11 { 12 const char* config[] = { 13 argc >= 2 ? argv[1] : "rwe", 14 argc >= 3 ? argv[2] : "rwe", 15 argc >= 4 ? argv[3] : "rwe" 16 }; 17 18 fd_set readSet; 19 fd_set writeSet; 20 fd_set errorSet; 21 22 FD_ZERO(&readSet); 23 FD_ZERO(&writeSet); 24 FD_ZERO(&errorSet); 25 26 for (int fd = 0; fd < 3; fd++) { 27 if (strchr(config[fd], 'r')) 28 FD_SET(fd, &readSet); 29 if (strchr(config[fd], 'w')) 30 FD_SET(fd, &writeSet); 31 if (strchr(config[fd], 'e')) 32 FD_SET(fd, &errorSet); 33 } 34 35 int result = select(3, &readSet, &writeSet, &errorSet, NULL); 36 fprintf(stderr, "select(): %d\n", result); 37 38 for (int fd = 0; fd < 3; fd++) { 39 fprintf(stderr, "fd %d: %s%s%s\n", fd, 40 FD_ISSET(fd, &readSet) ? "r" : " ", 41 FD_ISSET(fd, &writeSet) ? "w" : " ", 42 FD_ISSET(fd, &errorSet) ? "e" : " "); 43 } 44 45 return 0; 46 } 47