1

I'm creating a program which is like an "shell interpreter" but in C, in the main program called "Bash" if I write in command line "change NAME" it creates a fork and executes an execlp to call a program "cd" who should change my current directory

if(strcmp(argv[0], "change"){
     pid = fork();
     if (pid == 0){
         execlp("./cd", "./cd", argv[1], NULL);
     }

It works perfectly, it runs my program cd and in theory changes but after the program end it turns back to elder directory

#include <unistd.h>
#include <stdio.h>
#include <limits.h> 
#include <string.h>
#include <stdlib.h>
//PWD
int main(int argc, char *argv[]) {
    char diratual[90], dir[90], barra[90] = "/";
    char *local[argc];
    local[0] = argv[1];
    getcwd(diratual, sizeof(diratual));
    
    strncat(diratual, barra, sizeof(barra));
    strncat(diratual,local[0],sizeof(local[0]));
    
    chdir(diratual);
    system("pwd");
    
    }

I use string to create the full path destination, I test the program after whole execution and it gives me back the path I've entered, but when it ends if I type "pwd" in my main program it shows the older. example:

I'm in /home/user/Documents

And want to enter in /home/user/Documents/Test

So I type 'change Test'

it receives 'Test' as arg[1] and align with program internal strings to create a string called:

string = "/home/user/Documents/Test"

After that I use - chdir(string);

So in the end the system("pwd"); returns the expected "/home/user/Documents/Test'

but when the program ends it returns to the main program

and when I type pwd in my main program it shows '/home/user/Documents

How can I solve this? to enter and stay in the directory typed?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • 1
    Was this some sort of recitation ? I read this twice and didn't find a question. – WhozCraig Nov 18 '19 at 15:23
  • `strcmp` return 0 when successful, you might wanna use `if(!strcmp(...` – Roy Avidan Nov 18 '19 at 15:24
  • 5
    The current directory is a property of the current process. So if you change the directory in a child process, it can never affect the current directory of the parent. This is just a fundamental property of the way processes and the "current directory" concept work in Unix-like operating systems, including Linux. – Steve Summit Nov 18 '19 at 15:39
  • Sorry i forgot the answer, now its under the text. Theres other way to change the directory in my "bash program"? Like, can i return some value to main program ? or run a program that works like an pointer to some directory? – Nicolas Bispo Nov 18 '19 at 16:16
  • You can do everything you want inside your "bash program". But the best thing would be to implement internal commands; otherwise you can call external programs designed to interact in a known way with your main program. I would call them "plugins". – linuxfan says Reinstate Monica Nov 18 '19 at 16:36
  • You have just discovered the reason why `cd` is a "built in" command to `bash` and other shells. It is one of a handful of commands that simply can't work via the normal mechanism of forking a subprocess and exec'ing some other program in it. – Steve Summit Nov 18 '19 at 17:01
  • Ow, now i'm a litlle sad, it just complicates my job a lot. But thank you very much to all of you, it clarified a lot to me. – Nicolas Bispo Nov 18 '19 at 17:24
  • Does this answer your question? [cd command not working with execvp](https://stackoverflow.com/questions/18686114/cd-command-not-working-with-execvp) – user253751 Nov 18 '19 at 17:46
  • See also: https://stackoverflow.com/questions/255414/why-doesnt-cd-work-in-a-shell-script?rq=1 – user253751 Nov 18 '19 at 17:46

0 Answers0