1

我正在为家庭作业编写自己的外壳,并且遇到了这个问题:

每当我输入将输出重定向到文件的命令时(即 ls -al > output.txt),我的 shell 应该处理该命令并重定向输出。但是,我不断收到此消息:ls: write error: Bad file descriptor

我在其他一些论坛上看到它可能与超出内存量有关,但我不明白这可能是什么问题。这是我的一些代码(如果您需要更多代码以了解清楚,请随时询问):

loc[0] = argv[0];                 //used for execution

while(argv[count] != 0){                    //loop through commands
    if(strcmp(argv[count], "<") == 0)    //and test for certain flags
        inFlag = 1;
    else if(strcmp(argv[count], ">") == 0){
        argv[count] = argv[count+1] = 0;
        outFlag = 1;
    }
    else if(strcmp(argv[count], "&") == 0)
        bgFlag == 1;
    else if(strcmp(argv[count], "|") == 0){
        argv[count] = 0;
        loc[pipes+1] = argv[count+1];
        pipes++;
    }
        count++;
}

for(k = 0; k <= pipes; k++){
    if(j < pipes){
        pipe(r_tube);
        j++;
    }

    pid = fork();

    if(pid > 0){
        if(j > 0){
            close(l_tube[0]);
            close(l_tube[1]);
        }
        l_tube[0] = r_tube[0];
        l_tube[1] = r_tube[1];
    }
    else if(pid == 0){
        if((k == 0) && (inFlag == 1)){
            int n = open("input.txt", "r");
            close (0);
            dup (n);
            close (n);
        }
        else if((k > 0) && (k < pipes)){

        }
        else if((k == pipes) && (outFlag == 1)){   //<-----issue
            int out = open("output.txt", 0666);
            close (1);
            dup (out);
            close (out);
        }
        else if(k == pipes){

        }
        execvp(argv[loc[k]], &argv[loc[k]]);
4

2 回答 2

0

int open(const char pathname, int flags, ... / mode_t mode */);

您需要一个访问模式以确定您要对文件执行的操作。例如(O_WRONLY - 只写或 O_RDONLY - 只读)...顺便说一句,这比这 2 个要多得多。

于 2012-10-22T03:32:20.717 回答
-1

试试这个:

count = 0;

loc[0] = argv[0];                 //used for execution

count++;
pipes++;

char* outFile = NULL;
char* inFile = NULL;

while(argv[count] != 0)
{ 
    if(strcmp(argv[count], "<") == 0)
    { 
        inFile = strdup(argv[count+1]);
        inFlag = 1;
        count++;
    }
    else if(strcmp(argv[count], ">") == 0)
    {
        outFile = strdup(argv[count+1]);
        outFlag = 1;
        count++;
    }
    else if(strcmp(argv[count], "&") == 0)
    {
        bgFlag == 1;
    }
    else if(strcmp(argv[count], "|") == 0)
    {
        argv[count] = 0;
        loc[pipes] = argv[count+1];
        pipes++;
    }

    count++;
}

int current_out = STDOUT_FILENO;
int current_in = STDIN_FILENO;
int next_in = STDIN_FILENO;

for(k = 0; k <= pipes; k++)
{
    int cur_pipes[2];

    int last_out = current_out;
    int last_in = current_in;

    if(k < pipes)
    {
        pipe(cur_pipes);
        current_out = cur_pipes[1];
        current_in = next_in;
        next_in = cur_pipes[0];
    }

    else if (k == pipes)
    {
        current_out = STDOUT_FILENO;
        current_in = next_in;
    }

    pid = fork();

    if(pid > 0)
    {
        if(k > 0)
        {
            close(last_out);
            close(last_in);
        }
    }
    else if(pid == 0)
    {
        if((k == 0) && (inFlag == 1))
        {
            int n = open(inFile, O_RDONLY);
            if (n == -1) 
            {
                printf("Could not open input file!\n");
                exit(1);
            }
            current_in = n;
        }

        if((k == pipes) && (outFlag == 1))
        {
            int n = open(outFile, O_WRONLY | O_CREAT, 0666);
            if (n == -1) {
                printf("Could not open output file!\n");
                exit(1);
            }

            current_out = n;
        }       

        if (current_in != STDIN_FILENO)
        {
            close (STDIN_FILENO);
            dup (current_in);
            close (current_in);
        }

        if(current_out != STDOUT_FILENO)
        {
            close(STDOUT_FILENO);
            dup(current_out);
            close(current_out);
        }

        execvp(argv[loc[k]], &argv[loc[k]]);
    }
}
于 2012-10-22T01:00:27.723 回答