Date: Sun, 1 Nov 1998 22:04:02 +0000 (GMT) From: Terry Lambert <tlambert@primenet.com> To: Arjan.deVet@adv.iae.nl (Arjan de Vet) Cc: hackers@FreeBSD.ORG Subject: Re: Possible bug in freopen()? Message-ID: <199811012204.PAA27908@usr05.primenet.com> In-Reply-To: <199810302139.WAA13889@adv.iae.nl> from "Arjan de Vet" at Oct 30, 98 10:39:02 pm
next in thread | previous in thread | raw e-mail | index | archive | help
> I may have found a bug in freopen() while testing INN 2.2-stable. > > Consider the following program: > > #include <stdio.h> > #include <unistd.h> > > main () { > FILE *f, *g; > long i; > > g = fopen("/tmp/test", "a"); > f = freopen("/tmp/test", "a", g); > i = ftell(f); > printf("%d\n", i); > fprintf(f, "test"); > i = ftell(f); > printf("%d\n", i); > close(f); > } > > Start with an empty /tmp/test file and run the program three times > consecutively. The results on BSD/OS 3.0, FreeBSD 2.2.7-stable and Solaris > 2.6 are: > > BSDI 3.0 FreeBSD 2.2.7 Solaris > ----------------------------------------------------------------------------- > 0 0 0 > 4 4 4 > > 0 0 4 > 8 4 8 > > 0 0 8 > 12 4 12 > > Hmm... Quite different. I think Solaris shows the correct behaviour. In each > case /tmp/test contains "testtesttest" after the running the program three > times. Realize that stdio buffers are not required to be flushed to disk unless you explicitly call fclose on the FILE's. Notice, also, that you are calling "close" on "f", which does nothing. Also, you should not expect that an fopen and a freopen on the same file will use the same user space stdio buffer space, merely because the are connected to the same file. The buffer space is set associative with the FILE, not with the underlying fd. That said, if you correct your program to do the right fclose, it looks as if: ``a'' Open for writing. The file is created if it does not exist. The stream is positioned at the end of the file. Is not being obeyed by the freopen() in either BSDI or FreeBSD, and that the seek is delayed until after the write attempt, so the first ftell fails on both FreeBSD and BSDI. So it looks like a missing set of the offset to the file size. It's interesting to note that both Solaris and BSDI do the write, even though you don't properly close the file. This is probably bogus, since it implies at best that they are resource tracking the stdio buffers in an implementation dependent way, and at worst that their stdio isn't very efficient. Terry Lambert terry@lambert.org --- Any opinions in this posting are my own and not those of my present or previous employers. To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199811012204.PAA27908>