I am working on a unix-like file system and I need to take in commands
from the command line. Some commands are one argument like mkfs
, ls
, etc. But some are two commands like mkdir foo
, rmfile hello
. My question is, how can I parse the string to take in a command (cmd), but also take in a second argument (next)? Here is my code so far: (I have not included the functions for the sake of space)
int main(int argc, char *argv[]){
string cmd;
string next;
while (1){
cout << "Russ_John_Shell> ";
cin >> cmd;
//Checks to see whether the string has a file/directory name after the command
if (next == ""){
//mkfs command
if (cmd == "mkfs"){
makeFS();
}
//exit command
else if (cmd == "exit"){
exitShell();
}
//ls command
else if (cmd == "ls"){
listDir();
}
//command not recognized at all
else {
printf("Command not recognized.\n");
}
}
else{
//mkdir command
if (cmd == "mkdir"){
makeDir(next);
}
//rmdir command
else if (cmd == "rmdie"){
remDir(cmd);
}
//cd command
else if (cmd == "cd"){
changeDir(next);
}
//stat command
else if (cmd == "stat"){
status(next);
}
//mkfile command
else if (cmd == "mkfile"){
makeFile(next);
}
//rmfile command
else if (cmd == "rmfile"){
remFile(next);
}
//command not recognized at all
else {
printf("Command not recognized.\n");
}
}
}
}
Thanks in advance for any help!