Last active
July 26, 2017 05:28
-
-
Save jdavidberger/f4b5bb255311cc8b65a7fd8772948035 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <signal.h> | |
#include <sys/types.h> | |
#include <errno.h> | |
#include <cstdlib> | |
#include <cstdio> | |
#include <unistd.h> | |
volatile int stop = 0; | |
void handler (int) | |
{ | |
printf("Exiting...\n"); | |
stop = 1; | |
} | |
int main(void) { | |
struct sigaction s; | |
s.sa_handler = handler; | |
sigemptyset(&s.sa_mask); | |
s.sa_flags = 0; | |
sigaction(SIGINT, &s, NULL); | |
fd_set set; | |
FD_ZERO(&set); | |
FD_SET(0, &set); | |
while(!stop) { | |
while (((select(1, &set, 0, 0, 0)) < 0) && (errno == EINTR) && !stop); | |
if (stop) | |
exit(0); | |
char buffer[1024] = {}; | |
int n = read(0, buffer, 1024); | |
buffer[n] = 0; | |
printf("%s\n", buffer); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment