簡體   English   中英

在 C 中寫入 shell,在某處關閉標准輸入

[英]Writing a shell in C, closing stdin somewhere

我正在寫一個 shell 並且我似乎在某個時候關閉標准輸入,但我不知道在哪里。 我已經傾注了一遍又一遍這段代碼,但找不到我可能關閉它的地方。

只有當我像這樣使用 pipe 時它才會關閉:

cat f2.txt | cat

要么

cat < f2.txt | cat

要么

cat | cat | cat

處理此代碼的行是 232 到 268,然后是 270 到 288

我似乎無法正確設置格式,所以這里是格式化代碼: http://pastebin.com/pe8BkVPV

我還將在下面粘貼有問題的部分。

有任何想法嗎?

if (ct->recieve_input == 1 && ct->redirect_output == 0) { 

ptr = dll_prev(tmp) ; 
    ctmp = jval_v(ptr->val) ; 
//fprintf(stderr, "Previous->command = %s\n", ctmp->command) ; 
fflush(stdout) ; 
            fs = fork() ; 
            if (fs == 0) { 

                if (ct->tdw == 1) { /* If we are redirecting output */
                    fprintf(stderr, "ct->tdw = 1\n") ; 
                    if (dup2(ct->fd1, 1) != 1) { perror("dup2 tdw A") ; exit(1) ; } 
                    if (close(ct->fd1) < 0) { perror("c1"); exit(1); }
                } /* tdw == 1 */ 

                if (ct->tdr == 1) { /* If we are recieving input */
                    fprintf(stderr, "ct->tdr = 1\n") ; 
                    if (dup2(ct->fd0, 0) != 0) { perror("dup2 tdr A") ; exit(1) ; } 
                    if (close(ct->fd0) < 0) { perror("c0"); exit(1); }
                }

                if (dup2(ctmp->pipefd[0], 0) != 0) { 
                    perror("dup2 : 0, 0") ; 
                    exit(1) ; 
                }
                //close(ct->pipefd[1]) ;
                //close(ct->pipefd[0]) ; 
                close(ctmp->pipefd[1]) ; 
                close(ct->pipefd[1]) ; 



                status = execvp(ct->command, ct->args) ; 
                fprintf(stderr, "execvp command failed\n") ; 
                exit(1) ; 
            } 
        }


        if (ct->redirect_output == 1 && ct->recieve_input == 0) { 
            ptr = (to_exec)->blink ; 
            ctmp = jval_v(ptr->val) ; 

            ctmp->recieve_input = 1 ; 
            fflush(stdout) ; 
            fs = fork() ; 

            if (fs == 0) { 

                if (dup2(ct->pipefd[1], 1) == -1) { 
                    perror("dup2 : RD== 1:1, 1") ;
                    exit(1) ; 
                } 

                //close(ct->pipefd[0]) ; // TODO
                status = execvp(ct->command, ct->args) ; 
                fprintf(stderr, "exevp command failed\n") ; 
                exit(1) ; 
            } 
        } /* End redirect output */ 

據我所知,你缺少的是另一個

if (cc > 0) c->recieve_input = 1 ;

行處理循環結束后。 你需要練習你的 DRY 紀律(不要重復自己)。

PS 你拼錯了 receive

暫無
暫無

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

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