reverse of getchar() read() open() fopen() ?

Dan Nelson dnelson at allantgroup.com
Fri Feb 11 22:04:37 UTC 2011


In the last episode (Feb 11), Julian H. Stacey said:
> Hi hackers@, 
> Do we have C libraries with reverse of getchar() [ & maybe read() ] &
> fopen() [ & maybe open() ] etc, to read from end of file toward beginning
> ?  I dont see anything in the See Also sections.  I'm not looking to
> write, just read.  I'm looking for something that returns last char in
> file as first etc, I'm not interested in wchars etc, I could write some C
> functions, with seek etc & probably will, if none exist, but no point if
> they already exist ?

tail -r does this on a line-by-line basis.  Tail does it for regular files
by mmaping and then reading backwards, but you could also read the block of
data at (filesize MOD buffersize) and return its bytes in reverse order,
then seek back buffersize bytes, read that block and print it reversed, etc.

You might even be able to write functions that could be passed to funopen(). 
Then you'd have a regular FILE* that you could call with regular stdio
functions.  Getting the buffering right for good performance might get
tricky, though.

-- 
	Dan Nelson
	dnelson at allantgroup.com


More information about the freebsd-hackers mailing list