--------------153D7D566EE92E2982905088
Content-Type: text/plain; charset=koi8-r
Content-Transfer-Encoding: 7bit
Hello!
Please test this program.
After executing this program file 'test' is 0 bytes lenght.
On Solaris 2.5 x86 this works fine.
--
Alex Riedel
--------------153D7D566EE92E2982905088
Content-Type: text/html; charset=koi8-r
Content-Transfer-Encoding: 7bit
<HTML>
Hello!
<PRE>Please test this program.</PRE>
<PRE>After executing this program file 'test' is 0 bytes lenght.</PRE>
<PRE>On Solaris 2.5 x86 this works fine.</PRE>
<PRE>--
Alex Riedel</PRE>
</HTML>
--------------153D7D566EE92E2982905088--
--------------D697EE4F6C1ED519EEEBA61F
Content-Type: text/plain; charset=koi8-r; name="testcopy.c"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="testcopy.c"
#include <stdio.h>
int main (void)
{
char buf[512];
FILE *bank;
FILE *bank1;
bank1 = fopen("test","w");
fprintf(bank1,"\n BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG");
fprintf(bank1,"\n BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG");
fprintf(bank1,"\n BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG");
fprintf(bank1,"\n BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG");
fprintf(bank1,"\n BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG");
fprintf(bank1,"\n BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG");
fprintf(bank1,"\n BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG");
fprintf(bank1,"\n BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG");
fprintf(bank1,"\n BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG");
fprintf(bank1,"\n BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG");
fprintf(bank1,"\n BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG");
fprintf(bank1,"\n BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG");
fclose(bank1);
bank = fopen("test","r+");
fseek(bank,0L,0);
fread(buf,512,1,bank);
fseek(bank,0L,0);
//fflush(bank); //uncoment this for bugfix
ftruncate( fileno(bank), 0 );
fseek(bank,0L,0);
fwrite(buf,512,1,bank);
//fflush(bank); //uncoment this for bugfix
lseek(fileno(bank),0L,2);
}
--------------D697EE4F6C1ED519EEEBA61F--
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majo...@vger.rutgers.edu
Please read the FAQ at http://www.altern.org/andrebalsa/doc/lkml-faq.html
[...]
bank = fopen("test","r+");
You open a file using stdio.
fseek(bank,0L,0);
fread(buf,512,1,bank);
fseek(bank,0L,0);
You read the data from it then to back to the start.
//fflush(bank); //uncoment this for bugfix
ftruncate( fileno(bank), 0 );
You do something bad by improperly using stdio; before you can do anything
with fileno() you need to fflush the stream. The Solaris man page for
stdio has a reasonable discussion of this.
Any behaviour now is undefined because you are mixing stdio and non-stdio
on the same descriptor without proper caution.
Marc Slemko wrote:
> Why do you think there is any bug?
>
> [...]
> bank = fopen("test","r+");
>
> You open a file using stdio.
>
> fseek(bank,0L,0);
> fread(buf,512,1,bank);
> fseek(bank,0L,0);
>
> You read the data from it then to back to the start.
>
> //fflush(bank); //uncoment this for bugfix
>
> ftruncate( fileno(bank), 0 );
fseek(bank,0L,0); //back to the startfwrite(buf,512,1,bank); //i write data
to start
//hier file test is 512 bytes len.
lseek(fileno(bank),0L,2); //go to the end
//after lseek file test truncated.
On Solaris without "fflush" this works fine.
This is not my program. I just ported this prog. from Solaris to Linux.
>
>
> You do something bad by improperly using stdio; before you can do anything
> with fileno() you need to fflush the stream. The Solaris man page for
> stdio has a reasonable discussion of this.
This is feature? or bug?
>
>
> Any behaviour now is undefined because you are mixing stdio and non-stdio
> on the same descriptor without proper caution.
>
> -
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to majo...@vger.rutgers.edu
> Please read the FAQ at http://www.altern.org/andrebalsa/doc/lkml-faq.html
--
Alex Riedel
--------------C0E1DEFC3CAA3EE0A83DC3F8
Content-Type: text/html; charset=koi8-r
Content-Transfer-Encoding: 7bit
<HTML>
Marc Slemko wrote:
<BLOCKQUOTE TYPE=CITE>Why do you think there is any bug?
<P>[...]
<BR> bank = fopen("test","r+");
<P>You open a file using stdio.
<P> fseek(bank,0L,0);
<BR> fread(buf,512,1,bank);
<BR> fseek(bank,0L,0);
<P>You read the data from it then to back to the start.
<P> //fflush(bank); //uncoment this for bugfix
<P> ftruncate( fileno(bank), 0 );</BLOCKQUOTE>
fseek(bank,0L,0); //back to the startfwrite(buf,512,1,bank); //i write
data to start
<BR>//hier file test is 512 bytes len.
<BR>lseek(fileno(bank),0L,2); //go to the end
<BR>//after lseek file test truncated.
<P>On Solaris without "fflush" this works fine.
<BR>This is not my program. I just ported this prog. from Solaris to Linux.
<BLOCKQUOTE TYPE=CITE>
<P>You do something bad by improperly using stdio; before you can do anything
<BR>with fileno() you need to fflush the stream. The Solaris man
page for
<BR>stdio has a reasonable discussion of this.</BLOCKQUOTE>
This is feature? or bug?
<BLOCKQUOTE TYPE=CITE>
<P>Any behaviour now is undefined because you are mixing stdio and non-stdio
<BR>on the same descriptor without proper caution.
<P>-
<BR>To unsubscribe from this list: send the line "unsubscribe linux-kernel"
in
<BR>the body of a message to majo...@vger.rutgers.edu
<BR>Please read the FAQ at <A HREF="http://www.altern.org/andrebalsa/doc/lkml-faq.html">http://www.altern.org/andrebalsa/doc/lkml-faq.html</A></BLOCKQUOTE>
<PRE>--
Alex Riedel</PRE>
</HTML>
--------------C0E1DEFC3CAA3EE0A83DC3F8--
In message <35E0EE92...@taraz.kz>, Alex Riedel writes:
+-----
| fseek(bank,0L,0); //back to the startfwrite(buf,512,1,bank); //i write data
| to start
| //hier file test is 512 bytes len.
| lseek(fileno(bank),0L,2); //go to the end
| //after lseek file test truncated.
|
| On Solaris without "fflush" this works fine.
| This is not my program. I just ported this prog. from Solaris to Linux.
+--->8
The author clearly ignored the *Solaris* documentation. That it happens to
work on Solaris is an artifact of the stdio implementation it uses; but the
documentation for that implementation tells you that that is not guaranteed
to work in any particular way. So it does not work "fine", it
*accidentally* does what you want. Sun could replace stdio in the next
release of Solaris and cause this buggy program to not accidentally work any
more.
| > You do something bad by improperly using stdio; before you can do anything
| > with fileno() you need to fflush the stream. The Solaris man page for
| > stdio has a reasonable discussion of this.
|
| This is feature? or bug?
+--->8
Neither.
Buffered differs from unbuffered (to state the obvious). Unbuffered
operations *cannot be mixed* with buffered options reliably unless you flush
the buffers (one would think this also would be obvious).
Also note that read and write are not the only things that can be buffered:
one can buffer a seek until it's needed. This is a big win
performance-wise, but causes buggy programs like yours to not accidentally
do what was naively expected of them.
This is the nature of mixing buffered and unbuffered I/O. You *should not*
do this without explicitly flushing buffers before performing unbuffered
operations.
BTW, please go into your web browser's properties and turn off the HTML
trash. This is a mailing list, not a web page, and it's hard enough for me
to wade through the 400 messages in my inbox without having to wait for
messages to load twice, one oversized because of tags and unnecessary
formatting.
--
brandon s. allbery [os/2][linux][solaris][japh] all...@kf8nh.apk.net
system administrator [WAY too many hats] all...@ece.cmu.edu
electrical and computer engineering KF8NH
carnegie mellon university
libc only guarantees the effect of mixed raw and buffered I/O after an
fflush(). It's the program that is broken, not libc and not the kernel;
without the fflush() the behavior of the ftruncate() and lseek() is
*undefined*.
Please turn off the HTML formatting. It is redundant; sending two copies
of your mail to everyone. Many of us are not using a m$ inflicted product
nor do we use netscape for email. Thus we do not see your post because it
is an "attachment". It's quite perturbing to go dig it out of a mailfile
and cut/paste. Many of us simply filter these kinds of mail to /dev/null.
To the linux-kernel FAQ maintainer, if this is not on the FAQ, please add
a blurb.
thankyou,
-d
--
Look, look, see Windows 98. Buy, lemmings, buy!
(c) 1998 David Ford. Redistribution via the Microsoft Network is prohibited.
for linux-kernel: please read linux/Documentation/* before posting problems