Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <errno.h>
- #include <vector>
- vector<string> Pars(string s)
- {
- int last = 1;
- vector<string> ans;
- for(int i = 1;i < s.size();++i )
- {
- if(s[i] == ' ' || i + 1 == s.size())
- {
- if(s[i-1] != ' ')
- if(i + 1 != s.size() || s[i] == ' ')
- {
- ans.push_back(s.substr(last,i - last));
- last = i + 1;
- } else
- {
- ans.push_back(s.substr(last,i - last + 1));
- }
- }
- }
- return ans;
- }
- int main(int argc, char * argv[])
- {
- int pid, status;
- if (argc < 2) {
- printf("Where are arguments?\n");
- return EXIT_FAILURE;
- }
- ifstream in;
- in.open("lol.txt");
- vector<string> arg;
- string param;
- std::getline(in,param);
- arg = Pars(param);
- for (int i = 0; i < arg.size(); i++ ) {
- pid = fork();
- if (pid == 0) {
- execvp(arg[i], &arg[i]);
- perror("execvp");
- return EXIT_FAILURE;
- } else {
- if (wait(&status) == -1) {
- perror("wait");
- return EXIT_FAILURE;
- }
- if (WIFEXITED(status))
- printf("Child terminated normally with exit code %i\n",
- WEXITSTATUS(status));
- if (WIFSIGNALED(status))
- printf("Child was terminated by a signal #%i\n", WTERMSIG(status));
- if (WCOREDUMP(status))
- printf("Child dumped core\n");
- if (WIFSTOPPED(status))
- printf("Child was stopped by a signal #%i\n", WSTOPSIG(status));
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement