daily pastebin goal
25%
SHARE
TWEET

Untitled

a guest Dec 16th, 2018 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <sys/wait.h>
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8.  
  9. int main(int argc, char *argv[]) {
  10.     char word[4097];
  11.     int counter = 0;
  12.     pid_t pid;
  13.     pid_t root = -1;
  14.     do {
  15.         if (scanf("%s", word) != EOF) {
  16.             counter++;
  17.             pid = fork();
  18.             if (counter == 1 && pid > 0) root = pid;
  19.         } else {
  20.             if (counter == 0) {
  21.                 printf("%d", counter);
  22.                 return 0;
  23.             }
  24.             return counter;
  25.             break;
  26.         }
  27.     } while (counter <= 255 && pid == 0);
  28.  
  29.     if (pid > 0) {
  30.         int status;
  31.         waitpid(pid, &status, 0);
  32.         int exit_status = WEXITSTATUS(status);
  33.         if (pid == root) {
  34.             printf("%d", exit_status); 
  35.             return 0;
  36.         }
  37.         return exit_status;
  38.     }
  39.  
  40. }
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