Advertisement
peetaur

gcc wrapper in C - works (but has unfixable memory leak)

Oct 10th, 2012
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.66 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5.  
  6. /*
  7. This version has an array instead of a string for the command
  8. */
  9.  
  10. int main(int argc, char* argv[]) {
  11.     int i;
  12.     int ret = 0;
  13.     int skipped = 0;
  14.  
  15.     // the new gcc args to pass
  16.     char** gccargs;
  17.     gccargs = (char **)malloc( sizeof(char*) * (argc+1) );
  18.     int gccargc = 0;
  19.  
  20.     char realcmd[] = "/usr/bin/gcc.real";
  21.  
  22.     //first pass through args and copy the values if they aren't filtered out
  23.     gccargs[0] = argv[0];
  24.     gccargc++;
  25.     for(i=1; i<argc; i++) {
  26.         char* arg = argv[i];
  27.         if( strcmp(arg, "-Werror") == 0 ) {
  28.             printf("PM gccwrap4: skipping arg: %s\n", arg);
  29.             skipped++;
  30.             continue;
  31.         }
  32.         printf("PM gccwrap4:    \"%s\"\n", arg);
  33.  
  34.         gccargs[gccargc] = arg;
  35.         gccargc++;
  36.     }
  37.     //next set the next one to 0 so execv knows where to stop
  38.     gccargs[gccargc] = 0;
  39.  
  40.  
  41.     printf("PM gccwrap4: final array:\n");
  42.     for(i=0; i<gccargc; i++) {
  43.         printf("PM gccwrap4:     %s\n", gccargs[i]);
  44.     }
  45.  
  46.     // just for testing; must be commented out because execv does not return if it is successful, so it won't run the real command
  47. //    printf("PM gccwrap4: final command:\n    %s ", realcmd);
  48. //    ret = execv("/bin/echo", gccargs);
  49.  
  50.     ret = execv(realcmd, gccargs);
  51.  
  52.     // execv replaces this process, and does not return if successful ... does that mean this free can never be called? does that mean there is no way to free gccargs?
  53.     free(gccargs);
  54.    
  55.     // don't free gccargs[i] because those are just pointers to argv's values
  56.  
  57.     return ret;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement