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

2011-02-11 Thread Andrew Duane
I've never seen any such thing, but I've done similar things a lot. I'd say 
malloc/read the whole file in and use a decrementing pointer to return the 
previous character.

--
Andrew Duane Juniper Networks
978-589-0551  10 Technology Park Dr
adu...@juniper.net  Westford, MA  01886-3418


From: owner-freebsd-hack...@freebsd.org [owner-freebsd-hack...@freebsd.org] On 
Behalf Of Julian H. Stacey [j...@berklix.com]
Sent: Friday, February 11, 2011 4:32 PM
To: hack...@freebsd.org
Subject: reverse of  getchar() read() open() fopen() ?

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 ?

Cheers,
Julian
--
Julian Stacey, BSD Unix Linux C Sys Eng Consultants Munich http://berklix.com
 Mail plain text;  Not quoted-printable, Not HTML, Not base 64.
 Reply below text sections not at top, to avoid breaking cumulative context.
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to freebsd-hackers-unsubscr...@freebsd.org
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to freebsd-hackers-unsubscr...@freebsd.org


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

2011-02-11 Thread Dan Nelson
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
dnel...@allantgroup.com
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to freebsd-hackers-unsubscr...@freebsd.org


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

2011-02-11 Thread Devin Teske
On Fri, 2011-02-11 at 22:32 +0100, Julian H. Stacey wrote:

 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 ?


`tail -r' will spit out lines of a file in reverse-order.

Maybe the source to tail(1) can offer some insights:

http://www.freebsd.org/cgi/cvsweb.cgi/src/usr.bin/tail/
-- 
Devin

P.S. Sorry for the non-answer.



   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 ?
 
 Cheers,
 Julian


___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to freebsd-hackers-unsubscr...@freebsd.org


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

2011-02-11 Thread Julian H. Stacey
 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 ?

PS I'm not looking for a program that reverses chars in lines ( eg like my
http://berklix.com/~jhs/src/bsd/jhs/bin/local/rev/ 
), instead I'm wondering if we have functions that read binary backwards,
\0  \n not special.

Cheers,
Julian
-- 
Julian Stacey, BSD Unix Linux C Sys Eng Consultants Munich http://berklix.com
 Mail plain text;  Not quoted-printable, Not HTML, Not base 64.
 Reply below text sections not at top, to avoid breaking cumulative context.
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to freebsd-hackers-unsubscr...@freebsd.org


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

2011-02-11 Thread Brian Reichert
On Fri, Feb 11, 2011 at 10:32:46PM +0100, Julian H. Stacey wrote:
 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 ?

Use lseek() to position yourself, iterate over the file, copying into a
small buffer, then iterate over your buffer in reverse?

Maybe I misunderstood you...

 Cheers,
 Julian

-- 
Brian Reichert  reich...@numachi.com
55 Crystal Ave. #286
Derry NH 03038-1725 USA BSD admin/developer at large
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to freebsd-hackers-unsubscr...@freebsd.org


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

2011-02-11 Thread Julian H. Stacey
Andrew Duane wrote:
 I've never seen any such thing, but I've done similar things a
 lot. I'd say malloc/read the whole file in and use a decrementing
 pointer to return the previous character.

Thanks, but I'll need a loop too, as malloc(filesize()) would be
too big, as I omitted to say I'll be running it from find, reading
all files on system, including DVD images @ 4.7G. Some of my machines
dont have that much swap, let alone RAM :-)

Aside:
  I was doing inefficient things here with getchar() (while searching
  forward, for a one off run, (CPU cycles were cheaper than brain
  cycles, so it was easier to hack a pre-existing prog that used
  getchar() ).
  I was doing a search for (trailing) nulls blocks, from BSD tar silently
  zeroing data from bad media, per
http://lists.freebsd.org/pipermail/freebsd-hackers/2011-January/034254.html
http://www.freebsd.org/cgi/query-pr.cgi?pr=154407
http://berklix.com/~jhs/src/bsd/fixes/FreeBSD/src/gen/usr.bin/tar/
  But my search logs from find with
http://berklix.com/~jhs/src/bsd/jhs/bin/public/8f
  were too big, so I'll reduce by searching backward  only indexing
  on trailing nulls.


Dan Nelson wrote:

 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.

Thanks, I didnt know funopen, I'll read it again tomorrow morning :-)

Thanks to Devin Teske re src/usr.bin/tail/,
sorry in my first post I forgot to say binary.


Brian Reichert wrote:

 Use lseek() to position yourself, iterate over the file, copying into a
 small buffer, then iterate over your buffer in reverse?

Yes, thanks, better for efficiency, being lazy I had first
wondered if there was some fopen_starting_from_tail()
 getchar_starting_from_tail() I could just call for aone off run :-)

Thanks all.

Cheers,
Julian
-- 
Julian Stacey, BSD Unix Linux C Sys Eng Consultants Munich http://berklix.com
 Mail plain text;  Not quoted-printable, Not HTML, Not base 64.
 Reply below text sections not at top, to avoid breaking cumulative context.
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to freebsd-hackers-unsubscr...@freebsd.org