在简单Shell C中重定向输入/输出

时间:2017-05-27 00:36:03

标签: c shell

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

1 个答案:

答案 0 :(得分:0)

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

您应该在exec后的子进程中进行重定向和exec。别忘了关闭输出/输入文件描述符和STDOUT&amp;叉子后父母的STDIN。