The_Law

Untitled

Dec 7th, 2018
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.23 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <signal.h>
  5. #include <sys/wait.h>
  6. #include <limits.h>
  7.  
  8. int min(int a, int b) {
  9.     if (a < b) {
  10.         return a;
  11.     }
  12.     return b;
  13. }
  14.  
  15. int
  16. main(int argc, char *argv[])
  17. {
  18.     int n;
  19.     int res = 0;
  20.     sscanf(argv[1], "%d", &n);
  21.     for (int i = 2; i < min(n + 2, argc); ++i) {
  22.         int pid;
  23.         if (!(pid = fork())) {
  24.             char buf[PATH_MAX];
  25.             FILE *f = fopen(argv[i], "r");
  26.             fscanf(f, "%s", buf);
  27.             fclose(f);
  28.             execlp(buf, buf, NULL);
  29.             exit(1);
  30.         }
  31.     }
  32.     int status;
  33.     while (wait(&status) != -1) {
  34.         res = !(WIFEXITED(status) && WEXITSTATUS(status)) ? res + 1 : res;
  35.     }
  36.     for (int i = n + 2; i < argc; ++i) {
  37.         int pid;
  38.         if (!(pid= fork())) {
  39.             char buf[PATH_MAX];
  40.             FILE *f = fopen(argv[i], "r");
  41.             fscanf(f, "%s", buf);
  42.             fclose(f);
  43.             execlp(buf, buf, NULL);
  44.             exit(1);
  45.         } else {
  46.             wait(&status);
  47.             res = !(WIFEXITED(status) && WEXITSTATUS(status)) ? res + 1 : res;
  48.         }
  49.     }
  50.     printf("%d\n", res);
  51.     fflush(stdout);
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment