【问题标题】:Redirecting Input/Output in Simple Shell C在简单的 Shell C 中重定向输入/输出
【发布时间】:2017-05-27 00:36:02
【问题描述】:

我正在尝试在我构建的 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);

【问题讨论】:

  • 一开始所有那些硬编码的数组索引都让我担心。您应该只循环遍历数组,而不是在特定位置查找 &gt; 和 &lt;。
  • 移动execArray 元素的循环是错误的。您需要从execArray[i+2] 复制,以便删除&gt; 和文件名。完成后,您还需要将 execArraySize 减少 2。
  • execArraySize 是否包含指示参数结束的空指针?如果没有,您需要在调用 execvp 之前将其添加到数组中。
  • @Barmar 如何添加 NULL 指针?
  • @Barmar 我有这段代码在填充数组之前清空数组memset(execArray, '\0', sizeof(execArray));。你指的是这个吗?

标签: c shell


【解决方案1】:

除了@Barmar 提出的问题之外,您的代码实际上没有将输出从ls 重定向到test 的原因是您实际上在exec 之前关闭了文件描述符。

您应该在fork 之后的子进程中进行重定向和exec。不要忘记在 fork 之后关闭父级中的输出/输入文件描述符和 STDOUT 和 STDIN。

【讨论】:

  • 我不确定我是否理解。那么,close(fileDescriptor) 应该移到父级吗?上面的代码是在代码的子部分。
  • 是的,我想是的。它要么修复它,要么什么都不改变,所以值得一试。另外,为了便于阅读,我会使用常量 STDOUT_FILENO、STDERR_FILENO 和 STDIN_FILENO 而不是它们的整数形式。
猜你喜欢
  • 1970-01-01
  • 2016-03-10
  • 1970-01-01
  • 2012-03-06
  • 1970-01-01
  • 2011-04-26
  • 2023-03-10
  • 2016-01-15
  • 2013-10-20
相关资源
最近更新 更多