Advertisement
Geoff_Montee

execAndRedirect.cpp

Nov 1st, 2012
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 KB | None | 0 0
  1. bool execAndRedirect(std::string command, std::vector<std::string> args, std::string& output, int& status)
  2. {
  3.     int error;
  4.     int pipefd[2];
  5.     int localStatus;
  6.    
  7.     if (pipe(pipefd) == -1)
  8.     {
  9.         error = errno;
  10.         cerr << "Executing command '" << command << "' failed: " << strerror(error) << endl;
  11.         return false;
  12.     }
  13.  
  14.     pid_t pid = fork();
  15.  
  16.     if (pid == 0)
  17.     {      
  18.         char** argsC;
  19.        
  20.         argsC = new char*[args.size() + 2];
  21.    
  22.         argsC[0] = new char[command.size() + 1];
  23.        
  24.         strncpy(argsC[0], command.c_str(), command.size());
  25.        
  26.         argsC[0][command.size()] = '\0';
  27.        
  28.         for (size_t count = 0; count < args.size(); count++)
  29.         {
  30.             argsC[count + 1] = new char[args[count].size() + 1];
  31.            
  32.             strncpy(argsC[count + 1], args[count].c_str(), args[count].size());
  33.            
  34.             argsC[count + 1][args[count].size()] = '\0';           
  35.         }
  36.        
  37.         argsC[args.size() + 1] = NULL;
  38.        
  39.         close(pipefd[0]);
  40.  
  41.         if (dup2(pipefd[1], STDOUT_FILENO) == -1)
  42.         {
  43.             error = errno;
  44.             cerr << "Executing command '" << command << "' failed: " << strerror(error) << endl;
  45.             exit(1);       
  46.         }      
  47.        
  48.         if (dup2(pipefd[1], STDERR_FILENO) == -1)
  49.         {
  50.             error = errno;
  51.             cerr << "Executing command '" << command << "' failed: " << strerror(error) << endl;
  52.             exit(1);       
  53.         }      
  54.  
  55.         close(pipefd[1]);
  56.  
  57.         if (execvp(command.c_str(), argsC) == -1)
  58.         {
  59.             error = errno;
  60.             cerr << "Executing command '" << command << "' failed: " << strerror(error) << endl;
  61.             exit(1);
  62.         }
  63.     }
  64.    
  65.     else if (pid > 0)
  66.     {
  67.         size_t BUFFER_SIZE = 1024;
  68.         char buffer[BUFFER_SIZE + 1];
  69.  
  70.         close(pipefd[1]);
  71.        
  72.         ostringstream oss;
  73.  
  74.         ssize_t num_b;
  75.        
  76.         while ((num_b = read(pipefd[0], buffer, BUFFER_SIZE)) != 0)
  77.         {
  78.             buffer[num_b] = '\0';
  79.            
  80.             oss << buffer;
  81.         }
  82.        
  83.         output = oss.str();
  84.        
  85.         waitpid(pid, &localStatus, 0);
  86.        
  87.         close(pipefd[0]);
  88.     }
  89.    
  90.     else
  91.     {
  92.         error = errno;
  93.         cerr << "Executing command '" << command << "' failed: " << strerror(error) << endl;
  94.         return false;  
  95.     }
  96.    
  97.     if(WIFEXITED(localStatus))
  98.     {
  99.         status = WEXITSTATUS(localStatus);
  100.        
  101.         DateTime current = DateTime::now();
  102.        
  103.         if(status == 0)
  104.         {
  105.             return true;
  106.         }
  107.        
  108.         else
  109.         {
  110.             return false;
  111.         }
  112.     }
  113.    
  114.     else
  115.     {
  116.         error = errno;
  117.         cerr << "Executing command '" << command << "' failed: child didn't terminate normally" << endl;
  118.         return false;  
  119.     }  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement