in fdopen, avoid setting O_APPEND flag if it's already set
authorRich Felker <dalias@aerifal.cx>
Fri, 7 Feb 2014 06:16:53 +0000 (01:16 -0500)
committerRich Felker <dalias@aerifal.cx>
Fri, 7 Feb 2014 06:16:53 +0000 (01:16 -0500)
this saves a syscall in the case where the underlying open already
took place with O_APPEND, which is common because fopen with append
modes sets O_APPEND at the time of open before passing the file
descriptor to __fdopen.

src/stdio/__fdopen.c

index a2ca62b..a6ae73a 100644 (file)
@@ -32,7 +32,8 @@ FILE *__fdopen(int fd, const char *mode)
        /* Set append mode on fd if opened for append */
        if (*mode == 'a') {
                int flags = __syscall(SYS_fcntl, fd, F_GETFL);
-               __syscall(SYS_fcntl, fd, F_SETFL, flags | O_APPEND);
+               if (!(flags & O_APPEND))
+                       __syscall(SYS_fcntl, fd, F_SETFL, flags | O_APPEND);
                f->flags |= F_APP;
        }