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;
		}
	}
}

First, you can use the "system" command", but it's generally frowned upon. Normally you use "execvp", execlp", or something similar, after forking, piping, or whatever. Use the Microsoft equivalent. The system command is doable, but keep in mind the lifetime of the process and where stdout goes, etc. The system command starts a new process, executes it then closes it. Any changes of directory stops when this process ends. So if you change the directory with a system command, the working directory of your emulator will NOT change. This is in addition to any syntax problems you may have in the program.

I don't know what direct.h or gets_s is. I'm not using Visual Studio, so if it's Visual Studio-specific, I can't compile the program using Code Blocks.

If you want to use the system command, I would suggest keeping track of the current directory in the program itself. When the user enters "cd", change the variable to the argument specified after "cd". Don't invoke the system command when the user enters "cd".

When the user enters "dir", do something like this.

string working_directory;  // contains the working directory
string command = "dir " + working_directory;
system(command.c_str());

Now, I don't know if this will display or not. I can't remember how the system command works there. You may need to pipe it. But if "dir" already is working for you now, perhaps not.

But the gist is this. You need to be aware that the process created by the system command will not be the process of your emulator, so a change in the environmental variables (i.e. working directory) in the "system" process will not cause a change in the other process. So I think you need to keep a variable in the emulator process.

Thank you so much for your answer, well now about direct.h is a C++ header file that is used to manipulate directories( i included because i was thinking in implementing the chdir() function) and gets_s is the safe function of get() according to visual studio, i think eclipse use it too but maybe i am wrong, but it should compile with Code Blocks.
I was thinking in using something like this in the code instead of the system("cd"):

case 2:
			//_chdir(path.c_str());
			if(_chdir("\Debug")){
         printf( "Unable to locate the directory: %s\n","\Debug");
      }
   else
      system("dir *.exe");
		break;

Of course replacing the "Debug" directory with the path specified by the user, now that is the problem here, i do not know how to give the path to the chdir() function i think i should use a tokenizer or something and delete the "cd" command from the prompt and just use the path, but i can not figure it out :S :$ hehe.

Now about the System process you are right, i think i will try to find another methods to emulate the unix Shell without using the "system" command.

Oh btw dir works pretty well, thank you again ;) :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.