簡體   English   中英

C Shell中的錯誤文件描述符問題

[英]Bad file descriptor issue in C shell

我正在為作業編寫自己的shell,並且遇到了這個問題:

每當我輸入命令將輸出重定向到文件時(即ls -al> output.txt),我的外殼程序都應處理該命令並重定向輸出。 但是,我一直顯示此消息: 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]]);

int open(const char 路徑名,int標志,... / mode_t模式* /);

您需要一種訪問模式才能確定對文件的處理方式。 例如(O_WRONLY-只寫或O_RDONLY-只讀)...順便說一句,還有很多比這更多的2。

嘗試這個:

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]]);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM