Redirection inside call to execvp() not working

后端 未结 3 713
既然无缘
既然无缘 2020-12-09 23:55

I\'ve been implementing a small program that executes a given command using execvp(). It works fine when not using redirection, but when I run a command such as:

<         


        
3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-10 00:44

    Your "small program that executes a given command" is essentially a shell. Since you are writing a shell, it is your job to implement redirections with whatever syntax you desire. The > redirection operator is not a feature of the kernel, it is a shell feature.

    To implement sh-style redirection, you must parse the command, locate the redirection operators, open the files and assign them to input/output descriptors. This must be done before calling execvp. Look up the dup2 system call.

提交回复
热议问题