Advertisement
wowonline

Untitled

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