Ich schreibe eine Shell und ich scheine irgendwann die Stdin zu schließen, aber ich kann nicht herausfinden, wo. Ich habe diesen Code ausgegossen und übergossen, kann aber nicht herausfinden, wo ich ihn schließen könnte.Eine Shell in C schreiben, die stdin irgendwo schließen
es nur dann geschlossen wird, wenn ich ein Rohr verwenden, etwa so:
cat f2.txt | cat
oder
cat < f2.txt | cat
oder
cat | cat | cat
die Linien, wo dieser Code behandelt wird, ist 232 bis 268 und dann 270 bis 288
Ich kann nicht scheinen, um die Formatierung richtig zu bekommen, so ist hier der formatierte Code: http://pastebin.com/pe8BkVPV
Ich werde auch den Abschnitt in Frage unten einfügen.
Irgendwelche Ideen?
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 */
Der Stand in geschlossen, wenn das Rohr endet. Die fgetchar() sollte an diesem Punkt -1 zurückgeben. – LawfulHacker