Call cd command in c++ using system()

Asked

Viewed 1,785 times

1

I’m trying to "program a shell" inside C++ but I’m not able to use the cd command (path) it just won’t go, searched and saw that when I use the system() a new shell is created outside the directory in which we are being so can not use it, what would be the solution?

PS: The other commands (I have tested) usually take.

#include <stdio.h>
#include <stdlib.h>

#define LIMITE 256

int main (void){
char comando[LIMITE];


//Laço
while(1){
    printf("teste@teste-VirtualBox:~$ ");
    if(fgets(comando, sizeof(comando), stdin) != NULL){
        system(comando);
    }
}


return 0;

}

2 answers

3


If you are making a shell, rather than calling 'system' is calling 'Fork' and 'execvp' in Linux or 'Createprocess' in Windows. Some examples for Linux are given in English by others who are also making a shell:

https://stackoverflow.com/questions/16675094/minishell-problems-with-cd-c

https://stackoverflow.com/questions/39049898/implement-linux-shell-in-c

Calling 'system' will create a new process that uses a shell to process the given command. If you give 'cd' as command, the new process will change its own path and end, leaving the path of your program the same as before.

The cd command is not a program that is called, so you have to make your shell recognize and process the cd command for itself. To implement this command, you can call chdir to change the path.

  • You also have an example of how to use 'Fork' and 'exec' in English here: https://answall.com/questions/142384/h%C3%A1-some-way-to-run-a-java-jar-a-from-a-program-c/142509#142509

  • 1

    Lacobus' answers are great =]

3

Voce can use chdir to force this directory change to the system on linux

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void){
   //apontamos o terminal para /
   chdir("/");
   system("pwd");

   //apontamos o terminal para /home/kodonokami
   chdir("/home/kodonokami");
   system("pwd");
}

inserir a descrição da imagem aqui

in windows we may have the same result with Setcurrentdirectory

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

int main(void){
   //apontamos o terminal para c:\
   SetCurrentDiretory("c:\\");
   system("dir");

   //apontamos o terminal para c:\windows
   SetCurrentDiretory("c:\\windows");
   system("dir");
}

Placing commands on the same system separated by a period and comma also works (in windows & to separate is used), Voce can always use the cd when calling the system pass to it a variable that has the current directory and modify this variable when you need to change the directory

#include <stdio.h>
#include <stdlib.h>

int main(void){
   //isso é uma gambiarra '-'
   system("cd / ; pwd ; cd /home/kodonokami ; pwd");
}

another way can be done by running the shell by the program as already quoted by Kyale A

#include <stdio.h>
#include <unistd.h>

int main(void){
    execv("/bin/sh",NULL);
}

has other forms besides those cited ^^

Browser other questions tagged

You are not signed in. Login or sign up in order to post.