简体   繁体   中英

Writing a shell in C, closing stdin somewhere

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.

It only gets closed when I use a pipe like so:

cat f2.txt | cat

or

cat < f2.txt | cat

or

cat | cat | cat

The lines where this code is handled is 232 through 268 and then 270 through 288

I can't seem to get the formatting right so here is the formatted code: http://pastebin.com/pe8BkVPV

I will also paste the section in question below.

Any ideas?

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

As far as I can tell what you're missing is another

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

after the end of the line processing loop. You need to practice your DRY discipline (don't repeat yourself).

PS you've misspelled receive

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM