fork(3) download
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <limits>
  5.  
  6. void cat(std::string filename);
  7. void echo(std::string args);
  8. void cp(std::string source, std::string destination);
  9.  
  10. int main()
  11. {
  12. std::string command = "", prompt = "$ ";
  13. std::cout << std::endl << prompt;
  14.  
  15. while (std::cin >> command)
  16. {
  17. if (command == "cat")
  18. {
  19. std::string source = "", arg = "";
  20. std::cin >> source;
  21.  
  22. if (std::getline(std::cin, arg) && arg.size() > 0)
  23. {
  24. std::cerr << "cat only accepts one argument" << std::endl << prompt;
  25. continue;
  26. }
  27. else
  28. std::cin.clear();
  29.  
  30. cat(source);
  31. }
  32. else if (command == "echo")
  33. {
  34. std::string args;
  35. while (std::cin.peek() == ' ')
  36. std::cin.get();
  37. std::getline(std::cin, args);
  38.  
  39. echo(args);
  40. }
  41. else if (command == "cp")
  42. {
  43. std::string source = "", destination = "", arg = "";
  44.  
  45. std::cin >> source >> destination;
  46. if (std::getline(std::cin, arg) && arg.size() > 0)
  47. {
  48. std::cerr << "cp only accepts two arguments" << std::endl << prompt;
  49. continue;
  50. }
  51. else
  52. std::cin.clear();
  53.  
  54. cp(source, destination);
  55. }
  56. else if (command == "PS1")
  57. {
  58. while (std::cin.peek() == ' ')
  59. std::cin.get();
  60.  
  61. std::getline(std::cin, prompt);
  62. }
  63. else if (command == "exit")
  64. {
  65. break;
  66. }
  67. else if (command == "ls")
  68. {
  69. system("ls");
  70. }
  71. else
  72. {
  73. std::cout << "Usage: " << std::endl
  74. << "cat [filename]" << std::endl
  75. << "\tDisplays file contents." << std::endl << std::endl
  76. << "echo [-n] [args ...]" << std::endl
  77. << "\tWrite arguments to the standard output." << std::endl << std::endl
  78. << "PS1 [prompt]" << std::endl
  79. << "\tChanges the prompt symbol(s)." << std::endl << std::endl
  80. << "cp [source] [destination]" << std::endl
  81. << "\tCopies the source file to the destination file." << std::endl << std::endl
  82. << "exit" << std::endl
  83. << "\tExits this prompt." << std::endl << std::endl;
  84. }
  85.  
  86. std::cout << prompt;
  87. }
  88.  
  89. return 0;
  90. }
  91.  
  92. void cat(std::string filename)
  93. {
  94. std::ifstream file;
  95. file.open(filename);
  96. std::string line = "";
  97. if (!file)
  98. {
  99. std::cerr << "File open error" << std::endl;
  100. return;
  101. }
  102.  
  103. while (std::getline(file, line))
  104. std::cout << line << std::endl;
  105. }
  106.  
  107. void echo(std::string args)
  108. {
  109. if (args.size() > 2 && args.substr(0, 3) == "-n ")
  110. {
  111. args = args.substr(3);
  112. std::cout << args;
  113. }
  114. else if (args == "-n")
  115. return;
  116. else
  117. std::cout << args << std::endl;
  118. }
  119.  
  120. void cp(std::string source, std::string destination)
  121. {
  122. std::ifstream sourceFile;
  123. std::ofstream destinationFile;
  124. sourceFile.open(source);
  125. destinationFile.open(destination);
  126. std::string line = "";
  127. while (std::getline(sourceFile, line))
  128. destinationFile << line << std::endl;
  129. }
Success #stdin #stdout 0s 3044KB
stdin
Standard input is empty
stdout
$