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))
	while((line[posIni] != ' ') && (posIni < 80))
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)
return i;

void main(){
	int session = 1, i = 0;
	char inputLine[80], command[60], arg1[30];
	string path;
	char dir = system("cd");
	while(session == 1){
		printf("SHELL> ");
	if((i = buscaCommand(inputLine)) == numCommands)
		printf("Unknown Command\n");
		case 0:
		case 1:
		case 2:
			//dir + path.c_str();
		case 3:

Edited by doomfrawen: n/a

6 Years
Discussion Span
Last Post by doomfrawen

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;

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:
         printf( "Unable to locate the directory: %s\n","\Debug");
      system("dir *.exe");

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.