Quantcast
Channel: Active questions tagged linux-kernel - Stack Overflow
Viewing all articles
Browse latest Browse all 12332

Use chdir to change current working directory

$
0
0

I'm creating a program wich 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

Soo i type 'change Test'

it receives 'Test' as arg[1] and allign 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?


Viewing all articles
Browse latest Browse all 12332

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>