构建 shell - IO 问题
我正在为系统编程课程开发 shell。我在文件重定向方面遇到了一些问题。我刚刚将输出重定向到工作,例如“ls > a”,但是当我在 shell 中键入“cat < a”之类的命令时,它会删除文件中的所有内容。我觉得问题源于第二个 if 语句 - “fdin = open(_inputFile,777)”
如果是这种情况,将非常感谢推荐教程/其他示例的链接。
顺便说一句,我包含了整个函数,但是在它创建管道时,我还没有测试任何东西。我也不相信它能正常工作,但这可能是由于另一个文件中的错误造成的。
<代码> void Command::execute(){
if(_numberOfSimpleCommands == 0){
prompt();
return;
}
//save input/output
int defaultin = dup(0);
int defaultout = dup(1);
//initial input
int fdin;
if(_inputFile){
fdin = open(_inputFile,0777);
}else{
//use default input
fdin = dup(defaultin);
}
//execution
int pid;
int fdout;
for(int i = 0; i < _numberOfSimpleCommands; i++){
dup2(fdin,0);
close(fdin);
//setoutput
if(i == _numberOfSimpleCommands -1){
if(_outFile){
fdout = creat(_outFile,0666);
}else{
fdout = dup(defaultout);
}
}else{
int fdpipe[2];
pipe(fdpipe);
fdout = fdpipe[0];
fdin = fdpipe[1];
}
dup2(fdout,1);
close(fdout);
//create child
pid = fork();
if(pid == 0){
execvp(_simpleCommands[0]->_arguments[0],_simpleCommands[0]->_arguments);
perror("-myshell");
_exit(1);
}
}
//restore IO defaults
dup2(defaultin,0);
dup2(defaultout,1);
close(defaultin);
close(defaultout);
if(!_background){
waitpid(pid,0,0);
}
}
I am working on a shell for a systems programming class. I have been having some trouble with the file redirection. I just got redirecting the output to work, e.x. "ls > a" however when I type a command like "cat < a" into my shell it deletes everything in the file. I feel like the problem stems from the second if statement- "fdin = open(_inputFile,777)"
If that is the case a link to a recommended tutorial / other examples would be much appreciated.
On a side note, I included the entire function, however at the point which it creates the pipe, I have not tested anything there yet. I don't believe it works properly either though, but that may be from a mistake in another file.
void Command:: execute(){
if(_numberOfSimpleCommands == 0){
prompt();
return;
}
//save input/output
int defaultin = dup(0);
int defaultout = dup(1);
//initial input
int fdin;
if(_inputFile){
fdin = open(_inputFile,0777);
}else{
//use default input
fdin = dup(defaultin);
}
//execution
int pid;
int fdout;
for(int i = 0; i < _numberOfSimpleCommands; i++){
dup2(fdin,0);
close(fdin);
//setoutput
if(i == _numberOfSimpleCommands -1){
if(_outFile){
fdout = creat(_outFile,0666);
}else{
fdout = dup(defaultout);
}
}else{
int fdpipe[2];
pipe(fdpipe);
fdout = fdpipe[0];
fdin = fdpipe[1];
}
dup2(fdout,1);
close(fdout);
//create child
pid = fork();
if(pid == 0){
execvp(_simpleCommands[0]->_arguments[0],_simpleCommands[0]->_arguments);
perror("-myshell");
_exit(1);
}
}
//restore IO defaults
dup2(defaultin,0);
dup2(defaultout,1);
close(defaultin);
close(defaultout);
if(!_background){
waitpid(pid,0,0);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的调用
open(_inputFile, 0777)
不正确。open
的第二个参数应该包含值的按位或组合,这些值指定访问模式和文件创建标志等(O_RDONLY
、O_WRONLY
等)。由于您要传递0777
,因此最终可能会同时包含O_CREAT
和O_TRUNC
,这会导致_inputFile
删除了。您可能需要open(_inputFile, O_RDONLY)
。Your call
open(_inputFile, 0777)
is incorrect. The second argument toopen
is supposed to contain a bitwise or'd combination of values that specify access mode and file creation flags, among other things (O_RDONLY
,O_WRONLY
, etc). Since you're passing0777
, that probably ends up containing bothO_CREAT
andO_TRUNC
, which causes_inputFile
to be erased. You probably wantopen(_inputFile, O_RDONLY)
.