Subject: Re: rewind(3) question
To: Jaromir Dolecek <dolecek@ics.muni.cz>
From: None <seebs@plethora.net>
List: current-users
Date: 07/05/1998 21:33:16
In message <199807060226.EAA27486@saruman.ics.muni.cz>, Jaromir Dolecek writes:
>ANSI guru wanted ;)
Hi!
>Well, I have a stdio stream (created by tmpfile()). Data to the
>underlying descriptor are written directly by write(2), then rewind(3)
>is called and data are read via fread(). Until now I thought
>rewind(3) does implicit lseek(2) every time. But it's definitely not true
>under Linux.
There is generally no guarantee at all of what happens if you mix stdio and
lower-level I/O.
>What ANSI spec says ? Should rewind(3) work even when
>stdio functions are bypassed and data are written to descriptor directly ?
ANSI has no spec that includes write(2). I'm not sure what POSIX says, but
every instinct in my aging bones says you can't reliably mix the "syscall"
interface and the "stdio" interface.
>Anyway, I praise BSDs for taking safe approach. And I'd like to know
>if Linux's approach is broken or the behaviour is unspecified.
I'd say you're getting burned by your own cleverness; use fwrite(3). If
stdio thinks you're at the right position, there's no reason for it to
generate a seek. If you use write(2) to write data, there's no reason for
stdio to think anything happened. Typically, you would just use fwrite().
Note also that you are required to use some stdio seek call (I think rewind
counts) between reads and writes, or writes and reads.
-s