Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // <poll.h> demo
- //
- // in response to: https://boards.4channel.org/g/thread/70651197#p70656634
- //
- // see: http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/poll.h.html
- // http://man7.org/linux/man-pages/man2/poll.2.html (more helpful)
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h> // strchr(), strerror()
- #include <errno.h>
- #include <poll.h>
- #define BUFFSZ 1024
- char buffer[BUFFSZ];
- #define len(array) (sizeof(array) / sizeof(array[0]))
- #define HIGHLIGHT "\033[2m"
- #define NORMVIDEO "\033[0m"
- int main() {
- struct pollfd p[1];
- int whatever;
- p[0].fd = fileno(stdin);
- p[0].events = (POLLIN | 0);
- whatever = 0;
- while(1) {
- poll(p, len(p), 3); // 3 milli-second timeout
- if(p[0].revents == POLLIN) {
- char *line = fgets(buffer, sizeof(buffer)-1, stdin);
- // error handling...
- if(!line) {
- int err = errno;
- fprintf(stderr, "[err=%d] msg=%s\n", err, strerror(err));
- exit(1);
- }
- // process input...
- char *n = strchr(line, '\n');
- if(n) *n = '\0';
- printf("You entered: '%s%s%s', cycle == %d\n",
- HIGHLIGHT, line, NORMVIDEO, whatever);
- }
- else {
- // other non-input activities...
- whatever++;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement