lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Mon, 21 Feb 2011, Sam Roberts wrote:
> On Fri, Feb 18, 2011 at 3:04 AM, Tony Finch <dot@dotat.at> wrote:
> > I note that if the reader function is a simple wrapper around the read(2)
> > syscall then it is possible for it to return EOF then on a subsequent call
> > return some data, e.g. if the input is a terminal.
>
> read() should block on read from a terminal -- or if the fd is set
> non-blocking, return -1.
>
> read() will return zero on EOF, and then return some positive value if
> more is written to the end of file, but that's pretty pathological.

Try running the following program. Type stuff at it and see what happens
when you press CTRL-D before or after typing a newline.

#include <unistd.h>
#include <stdio.h>

int main(void) {
        char buf[256];
        for(;;) {
                int i = read(0, buf, sizeof(buf));
                printf("\ngot %d bytes ==>%.*s<==\n", i, i, buf);
        }
}

Tony.
-- 
f.anthony.n.finch  <dot@dotat.at>  http://dotat.at/
Forth, Tyne, Dogger: Southeasterly 4 or 5, occasionally 6 in Dogger. Moderate
or rough. Occasional rain. Moderate or good, occasionally poor.