SHARE
TWEET

Untitled

SeGaR Dec 9th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <unistd.h>
  7. #include <vector>
  8. #include <string>
  9.  
  10. using namespace std;
  11.  
  12. void Exec(vector<string>& command)
  13. {
  14.     int size = command.size();
  15.     char** args = new char* [size + 1];
  16.     args[size] = NULL;
  17.     for (int i = 0; i < size; i++)
  18.         args[i] = (char*)command[i].c_str();
  19.  
  20.     execvp(args[0], args);
  21. }
  22.  
  23. vector< vector<string> > ParseCommands(char* buffer, int size)
  24. {
  25.     vector< vector<string> > commands;
  26.     vector<string> empty;
  27.     commands.push_back(empty);
  28.     commands[0].push_back("");
  29.     int k = 0, l = 0;
  30.     for (int i = 0; i < size; i++)
  31.     {
  32.         if (buffer[i] == ' ')
  33.         {
  34.             if (commands[k][l].size() > 0)
  35.             {
  36.                 commands[k].push_back("");
  37.                 l++;
  38.             }
  39.         }
  40.         else if (buffer[i] == '|')
  41.         {
  42.             if (commands[k][l].size() <= 0)
  43.                 commands[k].pop_back();
  44.             commands.push_back(empty);
  45.             k++;
  46.             commands[k].push_back("");
  47.             l = 0;
  48.         }
  49.         else
  50.             commands[k][l] += buffer[i];
  51.     }
  52.     return commands;
  53. }
  54.  
  55. int main(int argc, char** argv)
  56. {
  57.     char buffer[1024];
  58.     int size = read(STDIN_FILENO, buffer, 1024);
  59.  
  60.     vector< vector<string> > commands = ParseCommands(buffer, size);
  61.     int k = 0;
  62.     while (true)
  63.     {
  64.         if (k >= commands.size() - 1)
  65.         {
  66.             int fd = open("/home/box/result.out", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
  67.             close(STDOUT_FILENO);
  68.             dup2(fd, STDOUT_FILENO);
  69.             close(fd);
  70.             Exec(commands[k]);
  71.             break;
  72.         }
  73.         int pfd[2];
  74.         pipe(pfd);
  75.         if ((pid = fork()))
  76.         {
  77.             close(STDOUT_FILENO);
  78.             dup2(pfd[1], STDOUT_FILENO);
  79.             close(pfd[1]);
  80.             close(pfd[0]);
  81.            
  82.             waitpid(pid, NULL, 0);
  83.             Exec(commands[k]);
  84.             break;
  85.         }
  86.         else
  87.         {
  88.             close(STDIN_FILENO);
  89.             dup2(pfd[0], STDIN_FILENO);
  90.             close(pfd[1]);
  91.             close(pfd[0]);
  92.             k++;
  93.         }
  94.     }
  95.     return 0;
  96. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top