linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* line buffered stdin
@ 2010-01-11 16:27 Bastian Ruppert
  2010-01-11 16:36 ` Manish Katiyar
  2010-01-11 20:51 ` Glynn Clements
  0 siblings, 2 replies; 4+ messages in thread
From: Bastian Ruppert @ 2010-01-11 16:27 UTC (permalink / raw)
  To: linux-c-programming

Hello,

under Linux stdin is line buffered, that means you can read one byte with
following statement:

read(0,&myvar,1);

You get the byte after typing one or more chars AND return.

But i need this function to return after every char entered in stdin.

Is there a function that reads stdin without line buffering?

Is it possible to disable the linebuffer or to reduce the buffersize to one?
Does this kind of manupilation have consequences for the hole system or 
just for
the app
doing this?

Any Ideas are very welcome,
thank you.

Regards
Bastian.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: line buffered stdin
  2010-01-11 16:27 line buffered stdin Bastian Ruppert
@ 2010-01-11 16:36 ` Manish Katiyar
  2010-01-11 19:09   ` ern0
  2010-01-11 20:51 ` Glynn Clements
  1 sibling, 1 reply; 4+ messages in thread
From: Manish Katiyar @ 2010-01-11 16:36 UTC (permalink / raw)
  To: Bastian Ruppert; +Cc: linux-c-programming

On Mon, Jan 11, 2010 at 9:57 PM, Bastian Ruppert <ng@max01.eu> wrote:
> Hello,
>
> under Linux stdin is line buffered, that means you can read one byte with
> following statement:
>
> read(0,&myvar,1);
>
> You get the byte after typing one or more chars AND return.
>
> But i need this function to return after every char entered in stdin.
>
> Is there a function that reads stdin without line buffering?
>
> Is it possible to disable the linebuffer or to reduce the buffersize to one?
First two links in google search.

http://www.linux.org/docs/ldp/howto/NCURSES-Programming-HOWTO/init.html
http://www.flipcode.com/archives/_kbhit_for_Linux.shtml
> Does this kind of manupilation have consequences for the hole system or just
> for
> the app
> doing this?
>
> Any Ideas are very welcome,
> thank you.
>
> Regards
> Bastian.
> --
> To unsubscribe from this list: send the line "unsubscribe
> linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>



-- 
Thanks -
Manish
==================================
[$\*.^ -- I miss being one of them
==================================
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: line buffered stdin
  2010-01-11 16:36 ` Manish Katiyar
@ 2010-01-11 19:09   ` ern0
  0 siblings, 0 replies; 4+ messages in thread
From: ern0 @ 2010-01-11 19:09 UTC (permalink / raw)
  To: linux-c-programming

> Is there a function that reads stdin without line buffering?

You should set some terminal attr:

   termios oldt;
   [...]
   tcgetattr(STDIN_FILENO,&oldt);
   termios newt = oldt;
   newt.c_lflag &= (~ICANON & ~ECHO);
   tcsetattr(STDIN_FILENO,TCSANOW,&newt);

and restore the old state when you're done:

   tcsetattr(STDIN_FILENO,TCSANOW,&oldt);

If you interrupt your program, you will see no echo, which is annoying. 
Type - blind mode - at the shell prompt: "reset" (don't worry, it only 
resets the terminal).

Gnome-terminal also clears the scrollback buffer upon "reset".
-- 
ern0.scene.plus4.amiga.code.muzak
Haben Sie Fragen?


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: line buffered stdin
  2010-01-11 16:27 line buffered stdin Bastian Ruppert
  2010-01-11 16:36 ` Manish Katiyar
@ 2010-01-11 20:51 ` Glynn Clements
  1 sibling, 0 replies; 4+ messages in thread
From: Glynn Clements @ 2010-01-11 20:51 UTC (permalink / raw)
  To: Bastian Ruppert; +Cc: linux-c-programming


Bastian Ruppert wrote:

> under Linux stdin is line buffered,

More precisely, it's line buffered if stdin is a tty, fully-buffered
otherwise.

> that means you can read one byte with
> following statement:
> 
> read(0,&myvar,1);
> 
> You get the byte after typing one or more chars AND return.
> 
> But i need this function to return after every char entered in stdin.
> 
> Is there a function that reads stdin without line buffering?

User-space buffering of a stream (i.e. a "FILE *") can be controlled
with setvbuf() (or the other functions described in the setvbuf(3)
manpage).

However, the terminal driver also performs line-buffering by default
(so that you can edit the current line with Backspace, Ctrl-W,
Ctrl-U). This can be disabled with tcsetattr() (see ern0's reply for
details), although this also disables the use of Ctrl-D to generate
EOF.

> Is it possible to disable the linebuffer or to reduce the buffersize to one?
> Does this kind of manupilation have consequences for the hole system or 
> just for the app doing this?

setvbuf() only applies to a specific process. tcsetattr() affects the
terminal device, so you must restore the state upon termination (both
normal termination via exit() or return from main(), and abnormal
termination via a signal), upon suspension (i.e. SIGTSTP; you can't
catch SIGSTOP), or if you invoke a child process (via e.g. system())
which might need to use the terminal.

-- 
Glynn Clements <glynn@gclements.plus.com>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2010-01-11 20:51 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-01-11 16:27 line buffered stdin Bastian Ruppert
2010-01-11 16:36 ` Manish Katiyar
2010-01-11 19:09   ` ern0
2010-01-11 20:51 ` Glynn Clements

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).