2012-04-07 9 views
0

私はシェルを書いていますが、ある時点でstdinを閉じているようですが、どこにあるのか分かりません。私はこのコードを注いで注ぎましたが、どこでそれを閉じるかも知れません。 268を通してどこかにstdinを閉じてCでシェルを書く

cat f2.txt | cat 

又は

cat | cat | cat 

このコードが処理される行

cat < f2.txt | cat 

又は

は232:私はそうのような管を使用するときにのみ閉じられます

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 */ 
+0

パイプが終わるとスタンダードが閉じます。 fgetchar()は、この時点で-1を返す必要があります。 – LawfulHacker

答えて

0

はライン処理ループの終了後に別の

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

です。 DRY規律を練習する必要があります(繰り返しません)。

P.S.あなたはスペルミスを受け取りました

+0

ありがとう、私はそれが遅れていることを知っていますが、何らかの理由で私はいつもその言葉のスペルを間違えています。特に私が疲れているとき。 私はdup2()を正しく使っていました。私が正しく覚えていれば、フォークに電話する前に私はそれを呼んでいましたが、これはうまくいきません。 – FatAdama

関連する問題