Advertisement
wowonline

Untitled

Nov 21st, 2021
882
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[NAME_SIZE];
  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.  
  50.     pid = fork();
  51.     if (pid < -1) {
  52.         return 1;
  53.     } else if (!pid) {
  54.         execlp("gcc", "gcc", filename, "-o", filename_exec, NULL);
  55.         _exit(1);
  56.     }
  57.     wait(NULL);
  58.  
  59.     while (scanf("%s", input) == 1) {
  60.         pid = fork();
  61.         if (pid == -1) {
  62.             return 1;
  63.         } else if (!pid) {
  64.             execlp(filename_exec, filename_exec, input, NULL);
  65.             _exit(1);
  66.         }
  67.         wait(NULL);
  68.     }
  69.  
  70.     return 0;
  71. }
Advertisement
RAW Paste Data Copied
Advertisement