linux - Programming pipes in c. Trying to execute piped commands cd and pwd, but it does not work out for me -


so programming pipes in c (to support multiple pipes). testing hardcoded commands. tried execute cd command change current working directory , pwd command display new directory. not work me: cd not change working directory , pwd display old directory of debug folder in project root. thankful if clarify cause of problem me. thank you!

here code

int main(int argc, const char * argv[]) {         char *cd[] = {"cd", "/users/user01/desktop", null};     char *pwd[] = {"pwd", null};      char **cmd [] = {cd, pwd, null};      loop_pipe(cmd);     return 0; }  void loop_pipe(char ***cmd){      int   p[2];     pid_t pid;     int   fd_in = 0;      while (*cmd != null)     {         pipe(p);         if ((pid = fork()) == -1){             exit(exit_failure);         }else if (pid == 0){             dup2(fd_in, 0); //change input according old 1             if (*(cmd + 1) != null)                 dup2(p[1], 1);             close(p[0]);             execvp((*cmd)[0], *cmd);             exit(exit_failure);         }else{             wait(null);             close(p[1]);             fd_in = p[0]; //save input next command             cmd++;         }     } } 

cd not program. in shell, implemented built-in.

think happen if not case: new child process change directory , promptly exit, effecting no persistent change. parent , child processes not share working directories.

i doubt goal change directories per se, if wanted, have "manually" in parent process, using chdir() call.


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -