Advertisement
--sas

ExpressionCompiler

Nov 27th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.52 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5.  
  6. #include <fcntl.h>
  7. #include <sys/stat.h>
  8. #include <sys/mman.h>
  9. #include <string.h>
  10. #include <sys/wait.h>
  11.  
  12. #define ER(number) {          \
  13.     printf("E%d\n", number);  \
  14.     exit(-1);                 \
  15. }
  16.  
  17. #define MAXLEN 5000
  18.  
  19. int main() {
  20.     umask(0000);
  21.     int fd = creat("program.c", 0666);
  22.     if (fd < 0) ER(1);
  23.  
  24.     char* init = "#include <stdio.h>\n int main() { printf(\"%d\\n\", (";
  25.     size_t initlen = strlen(init);
  26.     if (write(fd, init, initlen) != initlen) ER(2);
  27.  
  28.     char input[MAXLEN];
  29.     fgets(input, MAXLEN, stdin);
  30.     size_t inputlen = strlen(input);
  31.     if (write(fd, input, inputlen) != inputlen) ER(3);
  32.  
  33.     char* fin = ")); return 0; }";
  34.     size_t finlen = strlen(fin);
  35.     if (write(fd, fin, finlen) != finlen) ER(4);
  36.     if (close(fd) < 0) ER(45);
  37.  
  38.     int res = fork();
  39.  
  40.     if (res > 0) {
  41.  
  42.         int* wstatus;
  43.         wait(wstatus);
  44.  
  45.         int res1 = fork();
  46.  
  47.         if (res1 > 0) {
  48.  
  49.             int* wstatus1;
  50.             wait(wstatus1);
  51.  
  52.             if (unlink("program.out") < 0) ER(46);
  53.             if (unlink("program.c") < 0) ER(47);
  54.         }
  55.         else if (res1 == 0) {
  56.  
  57.             execlp("./program.out", "./program.out", 0);
  58.             ER(5);
  59.         }
  60.         else if (res1 < 0) ER(6);
  61.     }
  62.     else if (res == 0) {
  63.  
  64.         execlp("gcc", "gcc", "program.c", "-o", "program.out", 0);
  65.         ER(9);
  66.     }
  67.     else if (res < 0) ER(10);
  68.  
  69.     return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement