Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <iostream>
- #include <list>
- #include <cstring>
- #include <string>
- #include <thread>
- using namespace std;
- void initialize() {
- int MAX_LENGTH = 100;
- bool isExecuted = false;
- char commandStr[MAX_LENGTH];
- list <string> commandsArray {
- "dir",
- "ls",
- "help",
- "vol",
- "path",
- "tasklist",
- "notepad",
- "echo",
- "color",
- "ping",
- };
- // new line indicator
- cout << "\n⇒ ";
- // get input from user
- fgets(commandStr, MAX_LENGTH, stdin);
- // Stores a copy of original string
- string inputString = commandStr;
- // splits strings
- char *argsTok = strtok(commandStr, " ");
- while (argsTok != NULL) {
- // make sure it's not running
- if (!isExecuted) {
- // find command in command list
- for (list<string>::iterator i = commandsArray.begin(); i != commandsArray.end(); i++) {
- if ( *argsTok == *i-> c_str()) {
- // can execute
- // create new thread and run command
- thread CreateThread {[&](){
- system(inputString.c_str());
- isExecuted = true;
- initialize();
- }};
- // wait for thread
- CreateThread.join();
- // stop searching if executed
- break;
- }
- }
- }
- argsTok = strtok(NULL, " ");
- }
- // if user types invalid command
- if(!isExecuted) {
- cout << "Unsupported command " << inputString.c_str() << "\n";
- initialize();
- }
- }
- int main() {
- cout << "Welcome to myShell\n";
- initialize();
- return (0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement