unbuffered streams.

#include "../ourhdr.h"

int main(void)
{
int c;
char *buf;
setvbuf(stdin,buf,_IONBF,10);
setvbuf(stdout,buf,_IONBF,10);
while((c=getc(stdin)) != EOF)
{
if(putc(c,stdout) == EOF)
err_sys("output error");
}
if (ferror(stdin))
err_sys("input error");
exit(0);
}

for the above program i expected following output.
$> ./a.out
hh
(user input is in black colour and output is indicated in blue colour)
but the output is coming after pressing return key

h<return-key>
h

see in the above code i made stdin and stdout as unbuffered streams..

so can anyone explain why the output is coming after pressing return key..

You have to set the tty to canonical ---

This reads one key at a time without requiring the <return>

int getch(void) {
      int c=0;

      struct termios org_opts, new_opts;
      int res=0;
          //-----  store old settings -----------
      res=tcgetattr(STDIN_FILENO, &org_opts);
      assert(res==0);
          //---- set new terminal parms --------
      memcpy(&new_opts, &org_opts, sizeof(new_opts));
      new_opts.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOKE | ICRNL);
      tcsetattr(STDIN_FILENO, TCSANOW, &new_opts);
      c=getchar();
          //------  restore old settings ---------
      res=tcsetattr(STDIN_FILENO, TCSANOW, &org_opts);
      assert(res==0);
      return(c);
}

I too was suffering from this problem, and I hit another snag. When I tried this sample code it still wouldn't work form me, until I entered a total of 4 characters or hit return 3 times after having entered my first desired character.

After a lot of digging, I found that there is a minimum value setting in the termios structure that dictates the minimum number of characters that must be read before it passes the data to you.

The default is suppose to be 1, but in my case it was set to 4. How? I don't know, but it is.

I resolved this problem by adding the following line:

    new_opts.c_cc[VMIN]=1;

before the line:

    tcsetattr(STDIN_FILENO, TCSANOW, &new_opts);

I found this information from the termio man page, look for MIN. There are also other values such as TIME in the event that you to set a timeout -- but I leave that for additional reading.

I just wanted to point out that this was 99% perfect for me, and this 1% was all that I lacked.

This may help someone else too.

-=John