Advertisement
riking

Untitled

May 17th, 2014
553
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.46 KB | None | 0 0
  1.  
  2. static int CompileProgram(Program *prog) {
  3.     int makefile = CheckExists("Makefile");
  4.     int pid;
  5.    
  6.     if (!(pid = fork())) {
  7.         char **argBlock;
  8.  
  9.         if (makefile) {
  10.             argBlock = calloc(2, sizeof(char *));
  11.             CHECK_MALLOC(argBlock);
  12.             argBlock[0] = "/usr/bin/make";
  13.             argBlock[1] = prog->executableName;
  14.         } else {
  15.             argBlock = calloc(1 + prog->sourceFiles->length + 2, sizeof(char *));
  16.             CHECK_MALLOC(argBlock);
  17.             argBlock[0] = "/usr/bin/gcc";
  18.             argBlock[1] = "-o";
  19.             argBlock[2] = prog->executableName;
  20.             int i = 3;
  21.             ListNode *argNode;
  22.             Argument *arg = NULL;
  23.             LIST_FOR(prog->sourceFiles, argNode, Argument, arg) {
  24.                 argBlock[i++] = arg->value;
  25.             }
  26.  
  27.             assert(i == 1 + prog->sourceFiles->length + 2);
  28.         }
  29.        
  30.         printf("%s\n", *argBlock);
  31.        
  32.         execv(*argBlock, argBlock);
  33.         if (errno == EFAULT) {
  34.             printf("efault\n");
  35.         }
  36.         perror("Failed to exec");
  37.         exit(1);
  38.     } else {
  39.         // Parent
  40.         if (pid == -1) {
  41.             perror("Failed to fork");
  42.             exit(EXIT_SYSCALL);
  43.         }
  44.        
  45.         int status;
  46.         wait(&status);
  47.         // TODO
  48.     }
  49.  
  50.     return 1;
  51. }
  52.  
  53. ---
  54. /usr/bin/make
  55. efault
  56. Failed to exec: Bad address
  57. /usr/bin/make
  58. efault
  59. Failed to exec: Bad address
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement