c - Stdin not flushed after normal dummy program -
c - Stdin not flushed after normal dummy program -
here piece of c code wrote after testing stuffs.
i know not vulnerability concern, don't understand why stdin not flushed after normal homecoming of program, @ point prompt stdin,stdout,stderr. mean why remaining chars on stdin redirected stdout after end of normal execution of programme , not flushed?
$cat dummy.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <errno.h> int main(){ char rbuf[100]; if (read(0, rbuf,5) == -1){ perror("learn count"); printf("errno = %d.\n", errno); exit(1); } //printf("rbuf : %s\n",rbuf); homecoming 1; } here execution:
$ gcc -o dummy dummy.c $ ./dummy aaaaa /bin/sh $ /bin/sh sh-3.2$ exit exit $ i guess remaining string of stdin printed on mew stdout prompt. plus line feed @ end, somehow emulates come in pressed user execute command. what's going on? i'm curious know more that.
yes, guess right, these characters in stdin:
do this:
void flush_stdin() { while(getchar() != '\n'); } note: not utilize fflush() on stdin because undefined behavior
edit
the stdin wired terminal starts program(which bash). starts new programme dummy , stdin of dummy wired stdin of bash.
from there on, dummy process reads 5 characters, neglects others(leaving them in stdin buffer). when command returns bash waits until there atleast 1 character read in buffer. low , behold, there characters in stdin buffer, hence programme - instead of waiting, starts read stdin , since stdin @ end, contains \n process executed. starts /bin/sh. rest /bin/sh worry about!
c stdin flush
Comments
Post a Comment