Advertisement
codemonkey

Untitled

Oct 12th, 2011
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.10 KB | None | 0 0
  1.  
  2. int extcommand(char** const param)
  3. {
  4.     // Split path by colons
  5.     char** paths = strsplit(getenv("PATH"), ':');
  6.  
  7.     // This holds the full path to the command if it exists
  8.     char* cmdpath = NULL;
  9.  
  10.     // The length of the command
  11.     int cmdlen = strlen(*(param));
  12.     int cmdfound = 0;
  13.  
  14.     // For various loops
  15.     int i;
  16.  
  17. #ifdef DEBUG
  18.     // In debug mode, print out all the paths in PATH
  19.     printf("~ Path:\n");
  20.     for(i = 0; i < arraylen((void**)paths); i++)
  21.         printf("~\t%s\n", *(paths + i));
  22. #endif
  23.  
  24.     // Try to find the requested command in PATH
  25.     for(i = 0; i < arraylen((void**)paths); i++)
  26.     {
  27.         // The length of the final path is the length of the path + a dash +
  28.         // the command length and the nullbyte
  29.         int len = strlen(*(paths + i)) + cmdlen + 2;
  30.  
  31.         // This will hold the concatenated string
  32.         char* c = malloc(sizeof(char) * len);
  33.  
  34.         // Concatenate the path with the command
  35.         sprintf(c, "%s/%s", *(paths + i), *(param));
  36.  
  37.         // Check if the file exists in this path
  38.         if(fileexists(c))
  39.         {
  40.             // Set cmdpath to the path and break out of the loop
  41.             cmdpath = c;
  42.             break;
  43.         }
  44.         else
  45.         {
  46.             // TODO: Figure out why this free crashes
  47.             // Free the temporary variable
  48.             //free(c);
  49.         }
  50.     }
  51.  
  52.     // Check if the command was found
  53.     if(cmdpath)
  54.         cmdfound = 1;
  55.     else
  56.     {
  57.         cmdfound = 0;
  58.  
  59.         // Jump to the end of the function
  60.         goto end;
  61.     }
  62.  
  63.     /*
  64.      * Fork
  65.      */
  66.  
  67. #ifdef DEBUG
  68.     printf("~ Found command at path: %s\n", cmdpath);
  69.     return (1);
  70. #endif
  71.    
  72.     // Fork the process using safefork
  73.     pid_t pid = safefork();
  74.  
  75.     // In case the fork couldn't be completed
  76.     if(pid == -1)
  77.         error("Fork failed");
  78.  
  79.     // Child process
  80.     else if(pid == 0)
  81.     {
  82.         int result = execve(param[0], param, NULL);
  83.  
  84.         printf("FAIL!!! Returned %d\n", result);
  85.         exit(1);
  86.     }
  87.  
  88.     // Parent process
  89.     else
  90.     {
  91.         printf("Child process created on pid %d\n", pid);
  92.     }
  93.  
  94.     /*
  95.      * End of function
  96.      */
  97.     end:
  98.     // Free the array of paths
  99.     arrayfree((void**)paths);
  100.  
  101.     // Free the string holding the full path
  102.     if(cmdpath) free(cmdpath);
  103.  
  104.     // Return 0 if the command couldn't be found, otherwise 1
  105.     return cmdfound;
  106. }
  107.  
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement