Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <cstring>
- #include <cerrno>
- #include <unistd.h>
- #include <fcntl.h>
- #include <string>
- #include <iostream>
- using namespace std;
- extern char ** environ;
- char * result[100];
- bool wasNotBuiltIn = true;
- char ** convert(string s)
- {
- char * cstr = new char [s.length()+1];
- strcpy (cstr, s.c_str());
- // cstr now contains a c-string copy of str
- int counter = 0;
- char * p = strtok (cstr," ");
- while (p!=0)
- {
- result[counter] = p;
- p = std::strtok(NULL," ");
- counter++;
- }
- return result;
- }
- void execute(char ** c)
- {
- if (execvp(c[0], c + 1) == -1) perror(c[0]);
- }
- int main()
- {
- string line;
- string homeDir = getenv("HOME");
- string presentDir = getenv("PWD");
- string toPrint;
- if(strcmp(homeDir.c_str(), presentDir.substr(0, homeDir.length()).c_str()) == 0)
- {
- toPrint = "~" + presentDir.substr(homeDir.length(), presentDir.length());
- }
- else
- {
- toPrint = presentDir;
- }
- while(printf("1730sh:%s$ ", toPrint.c_str()) && getline(cin, line))
- {
- convert(line);
- string exit = "exit";
- if(strcmp(result[0],exit.c_str()) == 0)
- {
- return EXIT_SUCCESS;
- }
- string help = "help";
- string cd = "cd";
- if(strcmp(result[0],help.c_str()) == 0)
- {
- if(result[1] == nullptr)
- {
- cout << "Built-In Commands" << endl;
- cout << "cd [PATH] - Change the current directory to PATH. The environmental variable HOME is the default PATH." << endl;
- 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;
- cout << "help - Display helpful information about builtin commands." << endl;
- }
- else
- {
- if(strcmp(result[1],cd.c_str()) == 0)
- {
- cout << "Built-In Command " << result[1] << endl;
- cout << "cd [PATH] - Change the current directory to PATH. The environmental variable HOME is the default PATH." << endl;
- }
- else if(strcmp(result[1],exit.c_str()) == 0)
- {
- cout << "Built-In Command " << result[1] << endl;
- 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;
- }
- else if(strcmp(result[1],help.c_str()) == 0)
- {
- cout << "Built-In Command " << result[1] << endl;
- cout << "help - Display helpful information about builtin commands." << endl;
- }
- else
- {
- cout << "That built-in command does not exist." << endl;
- }
- }
- wasNotBuiltIn = false;
- }
- if(strcmp(result[0],cd.c_str()) == 0)
- {
- if(result[1] == nullptr)
- {
- chdir(getenv("HOME"));
- }
- else
- {
- chdir(result[1]);
- }
- homeDir = getenv("HOME");
- presentDir = get_current_dir_name();
- if(strcmp(homeDir.c_str(), presentDir.substr(0, homeDir.length()).c_str()) == 0)
- {
- toPrint = "~" + presentDir.substr(homeDir.length(), presentDir.length());
- }
- else
- {
- toPrint = presentDir;
- }
- wasNotBuiltIn = false;
- }
- if(wasNotBuiltIn)
- {
- execute(result);
- }
- wasNotBuiltIn = true;
- for(int i = 0; i < 100; i++)
- {
- result[i] = nullptr;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement