Hello friends, I am writing a program that behaves like a (Linux)shell
(but it's not really a shell because it does not interpret commands itself).
It will read some commands like in Linux shell and then run the command in the same window of course. The standard MSDOS shell does not do reverse incremental history
search, tab completion, color coding (but BASH has all these cool features) and is something that i want to implement later, now my problem is that i am using the system() function to emulate the commands, but i can not make work the cd command all it does is display the current path, i have tried many ways but without success :(, hope you guys could enlighten me since i do not program in c++ very often.
Thank you again and here is my code ATM:
#include <stdio.h>
#include <string>
#include <stdlib.h>
#include <Windows.h>
#include <direct.h>
using namespace std;
char commands[11][6] = {"exit","ls","cd","cls"};
int numCommands = 4;
int buscaToken(int posIni, int type, char line[]){
if(type == 0)
while((line[posIni] == ' ') && (posIni < 80))
posIni++;
else
while((line[posIni] != ' ') && (posIni < 80))
posIni++;
return posIni;
}
int buscaCommand(char line[])
{int ini,fin,i = 0,j;
ini = buscaToken(0,0,line);
fin = buscaToken(ini,1,line);
while((i < numCommands) && (j = strncmp(&line[ini],commands[i],fin-ini)) != 0)
i++;
return i;
}
void main(){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),0X0002);
int session = 1, i = 0;
char inputLine[80], command[60], arg1[30];
string path;
char dir = system("cd");
while(session == 1){
printf("SHELL> ");
gets_s(inputLine);
if((i = buscaCommand(inputLine)) == numCommands)
printf("Unknown Command\n");
else
switch(i){
case 0:
exit(0);
break;
case 1:
system("dir");
break;
case 2:
//dir + path.c_str();
//system(cd)
break;
case 3:
system("cls");
break;
}
}
}