我正在尝试重定向我构建的 C shell 中的输出。我们需要使用 fork()、dup2() 和 execvp()。如果我运行命令“ls > test”,则会创建测试文件,但它是空白的。我想知道我调用各种所需函数的顺序是否存在问题。我正在删除“>”字符并尝试传递给 execvp(),但我只是得到一个空白文件。我在删除“>”后打印了数组,它只包含“ls”和“test”。我使用这些命令的顺序或方式是否有问题?
if(execArraySize > 2 && strcmp(execArray[1], ">") == 0){
outputRedir = 1;
outputIndex = 2;
//printf("%s\n", execArray[1]);
printf("Outputing to: %s\n", execArray[outputIndex]);
//continue;
}
//Check if we are redirecting the output in a 5 argument set
//No error handling required
else if(execArraySize > 4 && strcmp(execArray[3], ">") == 0){
outputRedir = 1;
outputIndex = 4;
// printf("%s\n", execArray[3]);
// printf("Outputing to: %s\n", execArray[outputIndex]);
}
//Check if we are redirecting the input in a 3 argument set
//No error handling required
if(execArraySize > 2 && strcmp(execArray[1], "<") == 0){
inputRedir = 1;
inputIndex = 2;
printf("%s\n", execArray[1]);
printf("Input From: %s\n", execArray[inputIndex]);
}
//Check if we are redirecting the input in a 5 argument set
//No error handling required
else if(execArraySize > 4 && strcmp(execArray[3], "<") == 0){
inputRedir = 1;
inputIndex = 4;
printf("%s\n", execArray[3]);
printf("Input From: %s\n", execArray[inputIndex]);
}
//If we are in the background and are not redirectin input
if(bg == 1 && inputRedir == 0){
fileDescriptor = open("/dev/null", O_RDONLY);
if (fileDescriptor == -1)
{
printf("Error reading in input in bg %s\n", execArray[inputIndex]);
exit(1);
}
dupInt = dup2(fileDescriptor,0);
if (dupInt == -1)
{
printf("Error reading in dup2() in bg %s\n", execArray[inputIndex]);
exit(1);
}
}
//If we are redirecting input
if(inputRedir == 1){
//Open the file
fileDescriptor = open(execArray[inputIndex], O_RDONLY);
//Check the file was opened properly
if (fileDescriptor == -1)
{
printf("cannot open %s for input\n", execArray[inputIndex]);
exit(1);
}
else
{
//Duplicate file descriptor
dupInt = dup2(fileDescriptor,0);
close(fileDescriptor);
if (dupInt == -1)
{
//Error Check and exit
printf("Error in dup2() function!\n");
exit(1);
}
else
{
printf("dup2() worked as expected\n");
}
}
}
//If we are redirecting output
if (outputRedir == 1)
{
//Open the file to write to it
fileDescriptor = open(execArray[outputIndex], O_WRONLY|O_CREAT|O_TRUNC, 0644);
//Check the file was opened/created properly
if (fileDescriptor == -1)
{
printf("Error in output to: %s\n", execArray[outputIndex]);
exit(1);
}
else
{
//Duplicate file descriptor
//dupInt = dup2(fileDescriptor,1);
//close(fileDescriptor);
for(i=outputIndex-1; i < execArraySize; i++){
execArray[i] = execArray[i+1];
}
for(i=0; i < execArraySize; i++){
printf("%s\n", execArray[i]);
}
dupInt = dup2(fileDescriptor,1);
close(fileDescriptor);
if (dupInt == -1)
{
//Error handle and exit
printf("Error in dup2() function\n");
exit(1);
}
else
{
//printf("dup2() worked as expected\n");
//printf("BEFORE EXEC\n");
//execvp(execArray[0],execArray);
//perror("execvp\n");
}
//exec(execArray[outputIndex - 2]);
}
}
execvp(execArray[0], execArray);
printf("%s", execArray[0]);
fflush(NULL);
perror("");
//free(execArray);
//free(input);
exit(1);
最佳答案
除了@Barmar提出的问题之外,您的代码实际上没有将输出从ls
重定向到test
的原因是您实际上正确关闭了文件描述符在您的 exec
之前。
您应该在fork
之后在子进程中执行重定向和exec
。不要忘记在 fork 后关闭父级中的输出/输入文件描述符以及 STDOUT 和 STDIN。
关于c - 在简单 Shell C 中重定向输入/输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44211778/