Advertisement
wowonline

Untitled

Nov 21st, 2021
787
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <fcntl.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <sys/wait.h>
  5.  
  6. enum { NAME_SIZE = 23 };
  7.  
  8. int
  9. main(int argc, char *argv[])
  10. {
  11.     int fd_r = open("/dev/urandom", O_RDONLY);
  12.     if (fd_r == -1) {
  13.         return 1;
  14.     }
  15.     unsigned long long num_r;
  16.     read(fd_r, &num_r, sizeof(num_r));
  17.     close(fd_r);
  18.     char filename[NAME_SIZE];
  19.     snprintf(filename, NAME_SIZE, "%llu%s", num_r, ".c");
  20.  
  21.     pid_t pid;
  22.     char *input = NULL;
  23.     int fd = open(filename, O_WRONLY | O_CREAT | O_CLOEXEC, 0600);
  24.     if (fd == -1) {
  25.         return 1;
  26.     }
  27.     char *prog_beg =
  28.     "#include <stdio.h>\n"
  29.     "#include <stdlib.h>\n"
  30.     "int\n"
  31.     "main(int argc, char *argv[])\n"
  32.     "{\n"
  33.     "\tint x = strtol(argv[1], NULL, 10);\n"
  34.     "\tchar *summon = \"summon\";\n"
  35.     "\tchar *reject = \"reject\";\n"
  36.     "\tchar *disqualify = \"disqualify\";\n"
  37.     "\tprintf(\"%s\\n\", ";
  38.     char *prog_end =
  39.     ");\n"
  40.     "\treturn 0;\n"
  41.     "}";
  42.  
  43.     dprintf(fd, "%s", prog_beg);
  44.     dprintf(fd, "%s", argv[1]);
  45.     dprintf(fd, "%s", prog_end);
  46.  
  47.     char filename_exec[NAME_SIZE+2];
  48.     snprintf(filename_exec, sizeof(filename_exec), "%s.e", filename);
  49.     execlp("gcc", "gcc", filename, "-o", filename_exec, NULL);
  50.  
  51.     while (scanf("%s", input) == 1) {
  52.         pid = fork();
  53.         if (pid == -1) {
  54.             return 1;
  55.         } else if (!pid) {
  56.             execlp(filename_exec, filename_exec, input, NULL);
  57.             _exit(1);
  58.         }
  59.         wait(NULL);
  60.     }
  61.  
  62.     return 0;
  63. }
Advertisement
RAW Paste Data Copied
Advertisement