Note that there are some explanatory texts on larger screens.

plurals
  1. POWriting a shell in C, closing stdin somewhere
    primarykey
    data
    text
    <p>I'm writing a shell and I seem to be closing stdin at some point, but I can't figure out where. I have poured and poured over this code, but can't find where I might be closing it. </p> <p>It only gets closed when I use a pipe like so:</p> <pre><code>cat f2.txt | cat </code></pre> <p>or </p> <pre><code>cat &lt; f2.txt | cat </code></pre> <p>or </p> <pre><code>cat | cat | cat </code></pre> <p>The lines where this code is handled is 232 through 268 and then 270 through 288</p> <p>I can't seem to get the formatting right so here is the formatted code: <a href="http://pastebin.com/pe8BkVPV" rel="nofollow">http://pastebin.com/pe8BkVPV</a></p> <p>I will also paste the section in question below. </p> <p>Any ideas?</p> <pre><code>if (ct-&gt;recieve_input == 1 &amp;&amp; ct-&gt;redirect_output == 0) { ptr = dll_prev(tmp) ; ctmp = jval_v(ptr-&gt;val) ; //fprintf(stderr, "Previous-&gt;command = %s\n", ctmp-&gt;command) ; fflush(stdout) ; fs = fork() ; if (fs == 0) { if (ct-&gt;tdw == 1) { /* If we are redirecting output */ fprintf(stderr, "ct-&gt;tdw = 1\n") ; if (dup2(ct-&gt;fd1, 1) != 1) { perror("dup2 tdw A") ; exit(1) ; } if (close(ct-&gt;fd1) &lt; 0) { perror("c1"); exit(1); } } /* tdw == 1 */ if (ct-&gt;tdr == 1) { /* If we are recieving input */ fprintf(stderr, "ct-&gt;tdr = 1\n") ; if (dup2(ct-&gt;fd0, 0) != 0) { perror("dup2 tdr A") ; exit(1) ; } if (close(ct-&gt;fd0) &lt; 0) { perror("c0"); exit(1); } } if (dup2(ctmp-&gt;pipefd[0], 0) != 0) { perror("dup2 : 0, 0") ; exit(1) ; } //close(ct-&gt;pipefd[1]) ; //close(ct-&gt;pipefd[0]) ; close(ctmp-&gt;pipefd[1]) ; close(ct-&gt;pipefd[1]) ; status = execvp(ct-&gt;command, ct-&gt;args) ; fprintf(stderr, "execvp command failed\n") ; exit(1) ; } } if (ct-&gt;redirect_output == 1 &amp;&amp; ct-&gt;recieve_input == 0) { ptr = (to_exec)-&gt;blink ; ctmp = jval_v(ptr-&gt;val) ; ctmp-&gt;recieve_input = 1 ; fflush(stdout) ; fs = fork() ; if (fs == 0) { if (dup2(ct-&gt;pipefd[1], 1) == -1) { perror("dup2 : RD== 1:1, 1") ; exit(1) ; } //close(ct-&gt;pipefd[0]) ; // TODO status = execvp(ct-&gt;command, ct-&gt;args) ; fprintf(stderr, "exevp command failed\n") ; exit(1) ; } } /* End redirect output */ </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload