Advertisement
Guest User

Untitled

a guest
May 25th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/wait.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <errno.h>
  6. #include <vector>
  7.  
  8. vector<string> Pars(string s)
  9. {
  10.     int last = 1;
  11.     vector<string> ans;
  12.     for(int i = 1;i < s.size();++i )
  13.     {
  14.         if(s[i] == ' ' || i + 1 == s.size())
  15.         {
  16.             if(s[i-1] != ' ')
  17.                 if(i + 1 != s.size() || s[i] == ' ')
  18.                 {
  19.                 ans.push_back(s.substr(last,i - last));
  20.                 last = i + 1;
  21.                 } else
  22.                 {
  23.                     ans.push_back(s.substr(last,i - last + 1));
  24.                 }
  25.         }
  26.     }
  27.     return ans;
  28. }
  29.  
  30. int main(int argc, char * argv[])
  31. {
  32.     int pid, status;
  33.  
  34.     if (argc < 2) {
  35.         printf("Where are arguments?\n");
  36.         return EXIT_FAILURE;
  37.     }
  38.     ifstream in;
  39.     in.open("lol.txt");
  40.     vector<string>  arg;
  41.     string param;
  42.    
  43.     for (int i = 0; i < arg.size(); i++ ) {
  44.         std::getline(in,param);
  45.         arg = Pars(param);
  46.         pid = fork();
  47.     if (pid == 0) {
  48.         execvp(arg[i], &arg[i]);
  49.         perror("execvp");
  50.         return EXIT_FAILURE;
  51.     } else {
  52.         if (wait(&status) == -1) {
  53.         perror("wait");
  54.         return EXIT_FAILURE;
  55.     }
  56.     if (WIFEXITED(status))
  57.         printf("Child terminated normally with exit code %i\n",
  58.         WEXITSTATUS(status));
  59.     if (WIFSIGNALED(status))
  60.         printf("Child was terminated by a signal #%i\n", WTERMSIG(status));
  61.     if (WCOREDUMP(status))
  62.         printf("Child dumped core\n");
  63.     if (WIFSTOPPED(status))
  64.     printf("Child was stopped by a signal #%i\n", WSTOPSIG(status));
  65.     }
  66.     }
  67.  
  68.     return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement