Advertisement
wowonline

Untitled

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