fork(2) download
  1. #include <cstring>
  2. #include <iostream>
  3. #include <sstream>
  4. #include <string>
  5. #include <unistd.h>
  6. int main()
  7. {
  8. while (true)
  9. {
  10. int argc = 0;
  11. std::istringstream iss;
  12. std::string command;
  13. std::cout << "$> ";
  14. getline(std::cin, command);
  15. iss.str(command);
  16. for (unsigned i = 0; i <= command.length(); i++)
  17. {
  18. if (command[i] == ' ' || command[i] == '\0')
  19. {
  20. argc++;
  21. }
  22. }
  23. std::string arr[argc+1];
  24. for (int i = 0; i < argc; i++)
  25. {
  26. iss >> arr[i];
  27. }
  28. if (!arr[0].compare("quit"))
  29. {
  30. std::cout << "break:" << std::endl;
  31. break;
  32. }
  33. else
  34. {
  35. char* argv[argc+1];
  36. for (int i = 0; i < argc; i++)
  37. {
  38. argv[i] = const_cast<char*>(arr[i].c_str()); //This line is wrong
  39. }
  40. argv[argc] = NULL;
  41. execvp(argv[0], argv);
  42. }
  43. }
  44. return 0;
  45. }
Success #stdin #stdout 0s 3024KB
stdin
ls -l
stdout
$> total 12
-rwxr-xr-x 1 root 1001 8260 Feb 25 05:29 prog