Subject: lib/6381: freopen(3) ignores "a" (append) mode
To: None <gnats-bugs@gnats.netbsd.org>
From: None <mason@primenet.com.au>
List: netbsd-bugs
Date: 10/31/1998 06:47:21
>Number:         6381
>Category:       lib
>Synopsis:       freopen(3) ignores "a" (append) mode
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    lib-bug-people (Library Bug People)
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Fri Oct 30 22:50:01 1998
>Last-Modified:
>Originator:     Geoff C. Wing
>Organization:
Geoff Wing   <gcw@pobox.com>            Mobile : 0412 162 441
Work URL: http://www.primenet.com.au/   Ego URL: http://pobox.com/~gcw/
>Release:        19981030
>Environment:
System: NetBSD coral.primenet.com.au 1.3H NetBSD 1.3H (CORAL) #1: Sat Oct 31 17:24:32 EST 1998 mason@coral.primenet.com.au:/usr/netbsd/src/sys/arch/i386/compile/CORAL i386


>Description:
	As reported on the inn-workers mailing list by 
	Arjan de Vet <Arjan.deVet@nl.origin-it.com> , freopen(3) ignores 
	append mode.  Looking through the source of fopen(3) and freopen(3)
	shows that freopen(3) is missing code.

>How-To-Repeat:
	Quoting Arjan:

    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


>Fix:
    I'd guess, copy into /usr/src/lib/libc/stdio/freopen.c from the bottom
    of /usr/src/lib/libc/stdio/fopen.c which is:

        /*
         * 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);
>Audit-Trail:
>Unformatted: