Implementing 'tail -f' in C code

I would like to use low level read()s to read a file that is
constantly growing. It appear that when I hit what was the end of
the file at the time that I opened it, I get an error 22 “Invalid
Argument” regardless of how big the file is when I get to that point.

Is there a way to continuously read the file like the ‘tail -f’
utility without having to:
int where;
where = tell();
close()
open()
lseek( where );
/* some operands have been omited to protect the innocent */

Bill Caroselli <qtps@earthlink.net> wrote:

I would like to use low level read()s to read a file that is
constantly growing. It appear that when I hit what was the end of
the file at the time that I opened it, I get an error 22 “Invalid
Argument” regardless of how big the file is when I get to that point.

Just as a guess, when you hit the EINVAL, try:

lseek(fd, 0, SEEK_CUR);
then try read() again.

(I looked at tail -f, it’s implemented using stdio functions.)

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

Bill Caroselli <qtps@earthlink.net> wrote:

I would like to use low level read()s to read a file that is
constantly growing. It appear that when I hit what was the end of
the file at the time that I opened it, I get an error 22 “Invalid
Argument” regardless of how big the file is when I get to that point.

A disk filesystem will not behave that way. You should get a final
read() which returns 0 at EOF; after more data has been written to
that file the next read will return it (without you having to do
anything or close/open the file again). What filesystem are you using
that misbehaves like this (EINVAL)? Or are you sure you don’t have
a bug in your calling of lseek() - it would return EINVAL if you have
mixed up the argument order?

Is there a way to continuously read the file like the ‘tail -f’

You can sleep() after a read() returns 0 and loop and retry …
… works for me in a quick test (no seeking needed either).