From owner-freebsd-hackers Sun Nov 1 14:04:22 1998 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id OAA13881 for freebsd-hackers-outgoing; Sun, 1 Nov 1998 14:04:22 -0800 (PST) (envelope-from owner-freebsd-hackers@FreeBSD.ORG) Received: from smtp01.primenet.com (smtp01.primenet.com [206.165.6.131]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id OAA13876 for ; Sun, 1 Nov 1998 14:04:21 -0800 (PST) (envelope-from tlambert@usr05.primenet.com) Received: (from daemon@localhost) by smtp01.primenet.com (8.8.8/8.8.8) id PAA17948; Sun, 1 Nov 1998 15:04:17 -0700 (MST) Received: from usr05.primenet.com(206.165.6.205) via SMTP by smtp01.primenet.com, id smtpd017908; Sun Nov 1 15:04:07 1998 Received: (from tlambert@localhost) by usr05.primenet.com (8.8.5/8.8.5) id PAA27908; Sun, 1 Nov 1998 15:04:02 -0700 (MST) From: Terry Lambert Message-Id: <199811012204.PAA27908@usr05.primenet.com> Subject: Re: Possible bug in freopen()? To: Arjan.deVet@adv.iae.nl (Arjan de Vet) Date: Sun, 1 Nov 1998 22:04:02 +0000 (GMT) Cc: hackers@FreeBSD.ORG In-Reply-To: <199810302139.WAA13889@adv.iae.nl> from "Arjan de Vet" at Oct 30, 98 10:39:02 pm X-Mailer: ELM [version 2.4 PL25] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG > I may have found a bug in freopen() while testing INN 2.2-stable. > > Consider the following program: > > #include > #include > > 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