I'm looking for a way to mimic the behavior of tail -f my_file
within a C/C++ program, that is:
- read all file (or not - that part is not hard to figure out)
- wait for characters to be appended at the end of the file, then read them
I've tried going through tail source code but it's really not transparent (maybe because it has a lot of different behaviors depending on situations).
I can think of a way, using seekg
and comparing end-of-file position to an old value after reopening. Is reopening mandatory in this case?
I know select
ing the FD will not work as intended since disk files are always selectable.
Another way would be to pipe the output of tail
to the program but that would mean no interruption catching (since IO is always blocking) and be quite ugly (I don't want to use pipes but files directly).
There should be a proper way to do this, but I can't figure out which system utility/man page would describe the "canonical" implementation for this (fopen
/fread
or open
/read
...).