Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. vector<string> args;
  2.  
  3. /* code that correctly parses args from user input */
  4.  
  5. pid_t kidpid = fork();
  6. if (kidpid < 0)
  7. {
  8. perror("Internal error: cannot fork.");
  9. return -1;
  10. }
  11. else if (kidpid == 0)
  12. {
  13. // I am the child.
  14.  
  15. vector<char*>argcs;
  16. for(int i=1;i<args.size();i++)
  17. {
  18. char * temp = new char[args.at(i).length()];
  19. for(int k=0;k<args.at(i).length();k++)
  20. {
  21. temp[k] = args.at(i).at(k);
  22. }
  23. argcs.push_back(temp);
  24. }
  25.  
  26. char** argv = new char*[argcs.size() + 1];
  27. for (int i = 0; i < argcs.size(); i++)
  28. {
  29. argv[i] = argcs[i];
  30. }
  31. argv[args.size()] = NULL;
  32.  
  33. execvp(program, args);
  34.  
  35. return -1;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement