Advertisement
akela43

forks

Mar 20th, 2023 (edited)
545
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.15 KB | Help | 0 0
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <cstdio>
  4. #include <fcntl.h>
  5. #include <iostream>
  6. #include <sys/wait.h>
  7. #include <vector>
  8. #include <sstream>
  9. using VS = std::vector<std::string>;
  10. using VVS = std::vector<VS>;
  11. using VChars = std::vector<const char*>;
  12. using VVChars= std::vector<VChars>;
  13. struct VectorCmd
  14. {
  15.         VVS vCmds;
  16.         VVChars cmd;
  17.         VectorCmd(const std::string inp)
  18.         {
  19.           VS segPipes = split(inp, '|');
  20.           for (unsigned i = 0; i < segPipes.size(); ++i)
  21.           {   vCmds.push_back(split(segPipes[i], ' '));  }
  22.  
  23.           for (unsigned i = 0; i <vCmds.size(); ++i)
  24.           {
  25.              VChars v;
  26.              for (unsigned k = 0; k < vCmds[i].size(); ++k)
  27.              {
  28.                  v.push_back(vCmds[i][k].c_str());
  29.              }
  30.                  v.push_back((char*)NULL);
  31.              cmd.push_back(v);
  32.           }
  33.         }
  34.         VS split(const std::string &s, const char delimiter)
  35.         {
  36.             std::stringstream ss(s);
  37.             std::string segment;
  38.             VS lst;
  39.             while(std::getline(ss, segment, delimiter))
  40.                 if (segment.size())
  41.                    lst.push_back(segment);
  42.             return lst;
  43.  
  44.         }
  45.         void print() {
  46.           for (auto & v:cmd)
  47.           {
  48.              for(auto &s:v) if (s) std::cout << s<< " ";
  49.                 else std::cout << "NULL ";
  50.  
  51.              std::cout <<"|" <<  std::endl;
  52.           }
  53.  
  54.         }
  55. };
  56. void closepfd(std::vector<int> &pfd)
  57. {
  58.         for (unsigned i = 0; i < pfd.size(); ++i)
  59.                 close(pfd[i]);
  60. }
  61. int main()
  62. {
  63.         std::string inp;
  64.         std::getline(std::cin, inp);
  65.         VectorCmd vc(inp);
  66.         std::vector<int> pfd (vc.cmd.size()*2);
  67.         for (int i = 0; i < vc.cmd.size(); ++i)
  68.                 pipe(&pfd[2*i]);
  69.         for (unsigned i = 0; i < vc.cmd.size(); ++i)
  70.         {
  71. //              pipe(&pfd[2*i]);
  72.                 int pid = fork();
  73.                 if (pid == 0) {
  74.                         if (i != 0) {
  75.                                 dup2(pfd[i*2-2], STDIN_FILENO);
  76.                         }
  77.                         dup2(pfd[i*2+1], STDOUT_FILENO);
  78.                         closepfd(pfd);
  79.                         const std::vector<const char*> &pcmd = vc.cmd[i];
  80. //                      std::cerr <<i << std::endl;
  81.                         execvp(pcmd[0], const_cast<char*const*>(pcmd.data()));
  82.                 }
  83.                 else
  84.                 {
  85.                         int status;
  86.                         if (i == vc.cmd.size()-1) {
  87.                                 dup2(pfd[i*2], STDIN_FILENO);
  88.                                 closepfd(pfd);
  89.                                 freopen("/home/box/result.out", "w", stdout);
  90.                                 std::cout << std::cin.rdbuf();
  91.                         }
  92.                         else
  93.                         {
  94.                                 //closepfd(pfd);
  95.         //                      waitpid(pid, &status, 0);
  96.                         }
  97.                 //aitpid(pid, &status, 0);
  98.  
  99.                 }
  100.         }
  101.         wait(0);
  102.  
  103.         return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement