Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <cstring>
  3. #include <cerrno>
  4. #include <unistd.h>
  5. #include <fcntl.h>
  6. #include <string>
  7. #include <iostream>
  8.  
  9. using namespace std;
  10.  
  11. extern char ** environ;
  12. char * result[100];
  13. bool wasNotBuiltIn = true;
  14.  
  15. char ** convert(string s)
  16. {
  17.  
  18. char * cstr = new char [s.length()+1];
  19. strcpy (cstr, s.c_str());
  20.  
  21. // cstr now contains a c-string copy of str
  22.  
  23. int counter = 0;
  24. char * p = strtok (cstr," ");
  25. while (p!=0)
  26. {
  27. result[counter] = p;
  28. p = std::strtok(NULL," ");
  29. counter++;
  30. }
  31.  
  32. return result;
  33. }
  34.  
  35. void execute(char ** c)
  36. {
  37. if (execvp(c[0], c + 1) == -1) perror(c[0]);
  38. }
  39.  
  40. int main()
  41. {
  42. string line;
  43.  
  44. string homeDir = getenv("HOME");
  45. string presentDir = getenv("PWD");
  46. string toPrint;
  47. if(strcmp(homeDir.c_str(), presentDir.substr(0, homeDir.length()).c_str()) == 0)
  48. {
  49. toPrint = "~" + presentDir.substr(homeDir.length(), presentDir.length());
  50. }
  51. else
  52. {
  53. toPrint = presentDir;
  54. }
  55.  
  56. while(printf("1730sh:%s$ ", toPrint.c_str()) && getline(cin, line))
  57. {
  58. convert(line);
  59.  
  60. string exit = "exit";
  61. if(strcmp(result[0],exit.c_str()) == 0)
  62. {
  63. return EXIT_SUCCESS;
  64. }
  65.  
  66. string help = "help";
  67. string cd = "cd";
  68. if(strcmp(result[0],help.c_str()) == 0)
  69. {
  70. if(result[1] == nullptr)
  71. {
  72. cout << "Built-In Commands" << endl;
  73. cout << "cd [PATH] - Change the current directory to PATH. The environmental variable HOME is the default PATH." << endl;
  74. cout << "exit [N] - Cause the shell to exit with a status of N. If N is omitted, the exit status is that of the last job executed." << endl;
  75. cout << "help - Display helpful information about builtin commands." << endl;
  76. }
  77. else
  78. {
  79. if(strcmp(result[1],cd.c_str()) == 0)
  80. {
  81. cout << "Built-In Command " << result[1] << endl;
  82. cout << "cd [PATH] - Change the current directory to PATH. The environmental variable HOME is the default PATH." << endl;
  83. }
  84. else if(strcmp(result[1],exit.c_str()) == 0)
  85. {
  86. cout << "Built-In Command " << result[1] << endl;
  87. cout << "exit [N] - Cause the shell to exit with a status of N. If N is omitted, the exit status is that of the last job executed." << endl;
  88. }
  89. else if(strcmp(result[1],help.c_str()) == 0)
  90. {
  91. cout << "Built-In Command " << result[1] << endl;
  92. cout << "help - Display helpful information about builtin commands." << endl;
  93. }
  94. else
  95. {
  96. cout << "That built-in command does not exist." << endl;
  97. }
  98. }
  99.  
  100. wasNotBuiltIn = false;
  101. }
  102.  
  103. if(strcmp(result[0],cd.c_str()) == 0)
  104. {
  105.  
  106. if(result[1] == nullptr)
  107. {
  108. chdir(getenv("HOME"));
  109. }
  110. else
  111. {
  112. chdir(result[1]);
  113. }
  114. homeDir = getenv("HOME");
  115. presentDir = get_current_dir_name();
  116. if(strcmp(homeDir.c_str(), presentDir.substr(0, homeDir.length()).c_str()) == 0)
  117. {
  118. toPrint = "~" + presentDir.substr(homeDir.length(), presentDir.length());
  119. }
  120. else
  121. {
  122. toPrint = presentDir;
  123. }
  124.  
  125. wasNotBuiltIn = false;
  126. }
  127.  
  128. if(wasNotBuiltIn)
  129. {
  130. execute(result);
  131. }
  132. wasNotBuiltIn = true;
  133.  
  134. for(int i = 0; i < 100; i++)
  135. {
  136. result[i] = nullptr;
  137. }
  138. }
  139.  
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement