Advertisement
wowonline

Untitled

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