must seek to end when appending, to make ftell work
authorChris Torek <torek@ucbvax.Berkeley.EDU>
Wed, 6 Feb 1991 03:40:44 +0000 (19:40 -0800)
committerChris Torek <torek@ucbvax.Berkeley.EDU>
Wed, 6 Feb 1991 03:40:44 +0000 (19:40 -0800)
SCCS-vsn: lib/libc/stdio/fopen.c 5.5

usr/src/lib/libc/stdio/fopen.c

index ca2edd5..82bed43 100644 (file)
@@ -9,11 +9,12 @@
  */
 
 #if defined(LIBC_SCCS) && !defined(lint)
  */
 
 #if defined(LIBC_SCCS) && !defined(lint)
-static char sccsid[] = "@(#)fopen.c    5.4 (Berkeley) %G%";
+static char sccsid[] = "@(#)fopen.c    5.5 (Berkeley) %G%";
 #endif /* LIBC_SCCS and not lint */
 
 #include <sys/types.h>
 #include <sys/stat.h>
 #endif /* LIBC_SCCS and not lint */
 
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <fcntl.h>
 #include <stdio.h>
 #include <errno.h>
 #include "local.h"
 #include <stdio.h>
 #include <errno.h>
 #include "local.h"
@@ -42,5 +43,16 @@ fopen(file, mode)
        fp->_write = __swrite;
        fp->_seek = __sseek;
        fp->_close = __sclose;
        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);
 }
        return (fp);
 }