Advertisement
wowonline

Untitled

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