SHARE
TWEET

Untitled

a guest May 19th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <sys/signalfd.h>
  2. #include <signal.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6.  
  7. static void
  8. transfer_line(FILE* from) {
  9.     char buffer[4096];
  10.     memset(buffer, 0, sizeof(buffer));
  11.     fgets(buffer, sizeof(buffer), from);
  12.     fputs(buffer, stdout);
  13.     fflush(stdout);
  14. }
  15.  
  16. int main(int argc, char** argv) {
  17.     FILE* files[argc-1];
  18.     for (int i = 1; i < argc; i++) {
  19.         files[i - 1] = fopen(argv[i], "r");
  20.     }
  21.     sigset_t block, catch;
  22.     sigfillset(&block);
  23.     sigemptyset(&catch);
  24.  
  25.     for (int i = 0; i < argc; i++) {
  26.         sigaddset(&catch, SIGRTMIN + i);
  27.     }
  28.     sigprocmask(SIG_BLOCK, &block, NULL);
  29.     int fd = signalfd(-1, &catch, 0);
  30.     struct signalfd_siginfo si;
  31.     int index;
  32.  
  33.     for(;;) {
  34.         read(fd, &si, sizeof(si));
  35.         index = si.ssi_signo - SIGRTMIN;
  36.         if (0 == index) {
  37.             break;
  38.         } else {
  39.             transfer_line(files[index-1]);
  40.         }
  41.     }
  42.     return 0;
  43. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top