Date: Sat, 31 Oct 1998 05:14:38 -0800 (PST) From: Arjan.deVet@adv.iae.nl To: freebsd-gnats-submit@FreeBSD.ORG Subject: bin/8518: freopen() in append mode followed by ftell() gives strange results Message-ID: <199810311314.FAA22690@hub.freebsd.org>
next in thread | raw e-mail | index | archive | help
>Number: 8518
>Category: bin
>Synopsis: freopen() in append mode followed by ftell() gives strange results
>Confidential: no
>Severity: serious
>Priority: medium
>Responsible: freebsd-bugs
>State: open
>Quarter:
>Keywords:
>Date-Required:
>Class: sw-bug
>Submitter-Id: current-users
>Arrival-Date: Sat Oct 31 05:20:00 PST 1998
>Last-Modified:
>Originator: Arjan de Vet
>Organization:
>Release: FreeBSD 2.2.7
>Environment:
FreeBSD adv.iae.nl 2.2.7-RELEASE FreeBSD 2.2.7-RELEASE #0: Mon Jul 27 20:40:41 CEST 1998 root@adv.iae.nl:/usr/src/sys/compile/ADV i386
>Description:
During testing INN 2.2beta I discovered a possible 'bug' in the
freopen() call when used with "a" (append mode). The first ftell()
call always give 0 (irrespective of the current file length) and
subsequent writes to the file ftell() does not tell the current
write position in the file but the number of bytes written.
>How-To-Repeat:
#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);
fclose(f);
}
Create empty /tmp/test file and run the program three times. Each
time it will print 0 and 4. BSD/OS prints 0 4, 0 8 and 0 12. Solaris
and Linux print 0 4, 4 8 and 8 12.
>Fix:
Fix provided by Steinar Haugh, sthaug@nethelp.no:
*** lib/libc/stdio/freopen.c.orig Tue May 30 07:41:43 1995
--- lib/libc/stdio/freopen.c Sat Oct 31 10:06:08 1998
***************
*** 151,155 ****
--- 151,166 ----
fp->_write = __swrite;
fp->_seek = __sseek;
fp->_close = __sclose;
+
+ /*
+ * When opening in append mode, even though we use O_APPEND,
+ * we need to seek to the end so that ftell() gets the right
+ * answer. If the user then alters the seek pointer, or
+ * the file extends, this will fail, but there is not much
+ * we can do about this. (We could set __SAPP and check in
+ * fseek and ftell.)
+ */
+ if (oflags & O_APPEND)
+ (void) __sseek((void *)fp, (fpos_t)0, SEEK_END);
return (fp);
}
See further "Possible bug in freopen()?" thread on hackers@freebsd.org.
>Audit-Trail:
>Unformatted:
To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-bugs" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199810311314.FAA22690>
