Minishell problems with cd (C)

后端 未结 1 1499
醉酒成梦
醉酒成梦 2021-01-28 08:05

I made a simple minishell in C and it works, except for the cd command. When I try to run it nothing happens except it creates a child process that never actually e

相关标签:
1条回答
  • 2021-01-28 08:09

    cd is necessarily a shell built-in, not an external utility. You want to change the current working directory of the current process (the shell itself), not of a child process. Call chdir instead of forking a child process.

    Separately, check execvp for errors and defensively terminate the child after a failed exec. You'd have seen an informative error if you had done so:

    ... (child) ...
    execvp(Array_arg[0], Array_arg);
    perror("Error - exec failed"); // If we are here, we did *not* replace the process image
    exit(0);
    
    0 讨论(0)
提交回复
热议问题