Subject: lib/6381: freopen(3) ignores "a" (append) mode
To: None <>
From: None <>
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
>Originator:     Geoff C. Wing
Geoff Wing   <>            Mobile : 0412 162 441
Work URL:   Ego URL:
>Release:        19981030
System: NetBSD 1.3H NetBSD 1.3H (CORAL) #1: Sat Oct 31 17:24:32 EST 1998 i386

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

	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);

    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

    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);