Index: head/lib/libc/stdio/fdopen.c =================================================================== --- head/lib/libc/stdio/fdopen.c (revision 289862) +++ head/lib/libc/stdio/fdopen.c (revision 289863) @@ -1,103 +1,106 @@ /*- * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Chris Torek. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #if defined(LIBC_SCCS) && !defined(lint) static char sccsid[] = "@(#)fdopen.c 8.1 (Berkeley) 6/4/93"; #endif /* LIBC_SCCS and not lint */ #include __FBSDID("$FreeBSD$"); #include "namespace.h" #include #include #include #include #include #include #include "un-namespace.h" #include "local.h" FILE * fdopen(int fd, const char *mode) { FILE *fp; int flags, oflags, fdflags, tmp; /* * File descriptors are a full int, but _file is only a short. * If we get a valid file descriptor that is greater than * SHRT_MAX, then the fd will get sign-extended into an * invalid file descriptor. Handle this case by failing the * open. */ if (fd > SHRT_MAX) { errno = EMFILE; return (NULL); } if ((flags = __sflags(mode, &oflags)) == 0) return (NULL); /* Make sure the mode the user wants is a subset of the actual mode. */ if ((fdflags = _fcntl(fd, F_GETFL, 0)) < 0) return (NULL); /* Work around incorrect O_ACCMODE. */ tmp = fdflags & (O_ACCMODE | O_EXEC); if (tmp != O_RDWR && (tmp != (oflags & O_ACCMODE))) { errno = EINVAL; return (NULL); } if ((fp = __sfp()) == NULL) return (NULL); if ((oflags & O_CLOEXEC) && _fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) { fp->_flags = 0; return (NULL); } fp->_flags = flags; /* * If opened for appending, but underlying descriptor does not have * O_APPEND bit set, assert __SAPP so that __swrite() caller * will _sseek() to the end before write. */ - if ((oflags & O_APPEND) && !(fdflags & O_APPEND)) + /* XXX: Reuse __SALC for O_APPEND. */ + if (fdflags & O_APPEND) + fp->_flags |= __SALC; + else if (oflags & O_APPEND) fp->_flags |= __SAPP; fp->_file = fd; fp->_cookie = fp; fp->_read = __sread; fp->_write = __swrite; fp->_seek = __sseek; fp->_close = __sclose; return (fp); } Index: head/lib/libc/stdio/fopen.c =================================================================== --- head/lib/libc/stdio/fopen.c (revision 289862) +++ head/lib/libc/stdio/fopen.c (revision 289863) @@ -1,97 +1,100 @@ /*- * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Chris Torek. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #if defined(LIBC_SCCS) && !defined(lint) static char sccsid[] = "@(#)fopen.c 8.1 (Berkeley) 6/4/93"; #endif /* LIBC_SCCS and not lint */ #include __FBSDID("$FreeBSD$"); #include "namespace.h" #include #include #include #include #include #include #include #include "un-namespace.h" #include "local.h" FILE * fopen(const char * __restrict file, const char * __restrict mode) { FILE *fp; int f; int flags, oflags; if ((flags = __sflags(mode, &oflags)) == 0) return (NULL); if ((fp = __sfp()) == NULL) return (NULL); if ((f = _open(file, oflags, DEFFILEMODE)) < 0) { fp->_flags = 0; /* release */ return (NULL); } /* * File descriptors are a full int, but _file is only a short. * If we get a valid file descriptor that is greater than * SHRT_MAX, then the fd will get sign-extended into an * invalid file descriptor. Handle this case by failing the * open. */ if (f > SHRT_MAX) { fp->_flags = 0; /* release */ _close(f); errno = EMFILE; return (NULL); } fp->_file = f; fp->_flags = flags; fp->_cookie = fp; fp->_read = __sread; 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) + if (oflags & O_APPEND) { + /* XXX: Reuse __SALC for O_APPEND. */ + fp->_flags |= __SALC; (void)_sseek(fp, (fpos_t)0, SEEK_END); + } return (fp); } Index: head/lib/libc/stdio/freopen.c =================================================================== --- head/lib/libc/stdio/freopen.c (revision 289862) +++ head/lib/libc/stdio/freopen.c (revision 289863) @@ -1,247 +1,250 @@ /*- * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Chris Torek. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #if defined(LIBC_SCCS) && !defined(lint) static char sccsid[] = "@(#)freopen.c 8.1 (Berkeley) 6/4/93"; #endif /* LIBC_SCCS and not lint */ #include __FBSDID("$FreeBSD$"); #include "namespace.h" #include #include #include #include #include #include #include #include #include "un-namespace.h" #include "libc_private.h" #include "local.h" /* * Re-direct an existing, open (probably) file to some other file. * ANSI is written such that the original file gets closed if at * all possible, no matter what. */ FILE * freopen(const char * __restrict file, const char * __restrict mode, FILE * __restrict fp) { int f; int dflags, flags, isopen, oflags, sverrno, wantfd; if ((flags = __sflags(mode, &oflags)) == 0) { sverrno = errno; (void) fclose(fp); errno = sverrno; return (NULL); } FLOCKFILE(fp); if (!__sdidinit) __sinit(); /* * If the filename is a NULL pointer, the caller is asking us to * re-open the same file with a different mode. We allow this only * if the modes are compatible. */ if (file == NULL) { /* See comment below regarding freopen() of closed files. */ if (fp->_flags == 0) { FUNLOCKFILE(fp); errno = EINVAL; return (NULL); } if ((dflags = _fcntl(fp->_file, F_GETFL)) < 0) { sverrno = errno; fclose(fp); FUNLOCKFILE(fp); errno = sverrno; return (NULL); } /* Work around incorrect O_ACCMODE. */ if ((dflags & O_ACCMODE) != O_RDWR && (dflags & (O_ACCMODE | O_EXEC)) != (oflags & O_ACCMODE)) { fclose(fp); FUNLOCKFILE(fp); errno = EBADF; return (NULL); } if (fp->_flags & __SWR) (void) __sflush(fp); if ((oflags ^ dflags) & O_APPEND) { dflags &= ~O_APPEND; dflags |= oflags & O_APPEND; if (_fcntl(fp->_file, F_SETFL, dflags) < 0) { sverrno = errno; fclose(fp); FUNLOCKFILE(fp); errno = sverrno; return (NULL); } } if (oflags & O_TRUNC) (void) ftruncate(fp->_file, (off_t)0); if (!(oflags & O_APPEND)) (void) _sseek(fp, (fpos_t)0, SEEK_SET); if (oflags & O_CLOEXEC) (void) _fcntl(fp->_file, F_SETFD, FD_CLOEXEC); f = fp->_file; isopen = 0; wantfd = -1; goto finish; } /* * There are actually programs that depend on being able to "freopen" * descriptors that weren't originally open. Keep this from breaking. * Remember whether the stream was open to begin with, and which file * descriptor (if any) was associated with it. If it was attached to * a descriptor, defer closing it; freopen("/dev/stdin", "r", stdin) * should work. This is unnecessary if it was not a Unix file. */ if (fp->_flags == 0) { fp->_flags = __SEOF; /* hold on to it */ isopen = 0; wantfd = -1; } else { /* flush the stream; ANSI doesn't require this. */ if (fp->_flags & __SWR) (void) __sflush(fp); /* if close is NULL, closing is a no-op, hence pointless */ isopen = fp->_close != NULL; if ((wantfd = fp->_file) < 0 && isopen) { (void) (*fp->_close)(fp->_cookie); isopen = 0; } } /* Get a new descriptor to refer to the new file. */ f = _open(file, oflags, DEFFILEMODE); /* If out of fd's close the old one and try again. */ if (f < 0 && isopen && wantfd > STDERR_FILENO && (errno == ENFILE || errno == EMFILE)) { (void) (*fp->_close)(fp->_cookie); isopen = 0; wantfd = -1; f = _open(file, oflags, DEFFILEMODE); } sverrno = errno; finish: /* * Finish closing fp. Even if the open succeeded above, we cannot * keep fp->_base: it may be the wrong size. This loses the effect * of any setbuffer calls, but stdio has always done this before. * * Leave the existing file descriptor open until dup2() is called * below to avoid races where a concurrent open() in another thread * could claim the existing descriptor. */ if (fp->_flags & __SMBF) free((char *)fp->_bf._base); fp->_w = 0; fp->_r = 0; fp->_p = NULL; fp->_bf._base = NULL; fp->_bf._size = 0; fp->_lbfsize = 0; if (HASUB(fp)) FREEUB(fp); fp->_ub._size = 0; if (HASLB(fp)) FREELB(fp); fp->_lb._size = 0; fp->_orientation = 0; memset(&fp->_mbstate, 0, sizeof(mbstate_t)); if (f < 0) { /* did not get it after all */ if (isopen) (void) (*fp->_close)(fp->_cookie); fp->_flags = 0; /* set it free */ FUNLOCKFILE(fp); errno = sverrno; /* restore in case _close clobbered */ return (NULL); } /* * If reopening something that was open before on a real file, try * to maintain the descriptor. Various C library routines (perror) * assume stderr is always fd STDERR_FILENO, even if being freopen'd. */ if (wantfd >= 0) { if ((oflags & O_CLOEXEC ? _fcntl(f, F_DUP2FD_CLOEXEC, wantfd) : _dup2(f, wantfd)) >= 0) { (void)_close(f); f = wantfd; } else (void)_close(fp->_file); } /* * File descriptors are a full int, but _file is only a short. * If we get a valid file descriptor that is greater than * SHRT_MAX, then the fd will get sign-extended into an * invalid file descriptor. Handle this case by failing the * open. */ if (f > SHRT_MAX) { fp->_flags = 0; /* set it free */ FUNLOCKFILE(fp); errno = EMFILE; return (NULL); } fp->_flags = flags; fp->_file = f; fp->_cookie = fp; fp->_read = __sread; 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) + if (oflags & O_APPEND) { + /* XXX: Reuse __SALC for O_APPEND. */ + fp->_flags |= __SALC; (void) _sseek(fp, (fpos_t)0, SEEK_END); + } FUNLOCKFILE(fp); return (fp); } Index: head/lib/libc/stdio/ftell.c =================================================================== --- head/lib/libc/stdio/ftell.c (revision 289862) +++ head/lib/libc/stdio/ftell.c (revision 289863) @@ -1,153 +1,155 @@ /*- * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Chris Torek. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #if defined(LIBC_SCCS) && !defined(lint) static char sccsid[] = "@(#)ftell.c 8.2 (Berkeley) 5/4/95"; #endif /* LIBC_SCCS and not lint */ #include __FBSDID("$FreeBSD$"); #include "namespace.h" #include #include #include #include #include #include "un-namespace.h" #include "local.h" #include "libc_private.h" /* * standard ftell function. */ long ftell(FILE *fp) { off_t rv; rv = ftello(fp); if (rv > LONG_MAX) { errno = EOVERFLOW; return (-1); } return (rv); } /* * ftello: return current offset. */ off_t ftello(FILE *fp) { fpos_t rv; int ret; FLOCKFILE(fp); ret = _ftello(fp, &rv); FUNLOCKFILE(fp); if (ret) return (-1); if (rv < 0) { /* Unspecified value because of ungetc() at 0 */ errno = ESPIPE; return (-1); } return (rv); } int _ftello(FILE *fp, fpos_t *offset) { fpos_t pos; size_t n; - int dflags; if (fp->_seek == NULL) { errno = ESPIPE; /* historic practice */ return (1); } /* * Find offset of underlying I/O object, then * adjust for buffered bytes. */ if (fp->_flags & __SOFF) pos = fp->_offset; else { pos = _sseek(fp, (fpos_t)0, SEEK_CUR); if (pos == -1) return (1); } if (fp->_flags & __SRD) { /* * Reading. Any unread characters (including * those from ungetc) cause the position to be * smaller than that in the underlying object. */ if ((pos -= (HASUB(fp) ? fp->_ur : fp->_r)) < 0) { fp->_flags |= __SERR; errno = EIO; return (1); } if (HASUB(fp)) pos -= fp->_r; /* Can be negative at this point. */ } else if ((fp->_flags & __SWR) && fp->_p != NULL) { - dflags = 0; - if (fp->_flags & __SAPP) - dflags = O_APPEND; - else if (fp->_file != -1 && - (dflags = _fcntl(fp->_file, F_GETFL)) < 0) - return (1); - if ((dflags & O_APPEND) && - (pos = _sseek(fp, (fpos_t)0, SEEK_END)) == -1) { - if ((fp->_flags & __SOPT) || __sflush(fp) || - (pos = _sseek(fp, (fpos_t)0, SEEK_CUR)) == -1) - return (1); - else { - *offset = pos; - return (0); + /* XXX: Reuse __SALC for O_APPEND. */ + if (fp->_flags & (__SAPP|__SALC)) { + int serrno = errno; + + errno = 0; + if ((pos = _sseek(fp, (fpos_t)0, SEEK_END)) == -1) { + if (errno == ESPIPE || + (fp->_flags & __SOPT) || __sflush(fp) || + (pos = + _sseek(fp, (fpos_t)0, SEEK_CUR)) == -1) + return (1); + else { + errno = serrno; + *offset = pos; + return (0); + } } + errno = serrno; } /* * Writing. Any buffered characters cause the * position to be greater than that in the * underlying object. */ n = fp->_p - fp->_bf._base; if (pos > OFF_MAX - n) { errno = EOVERFLOW; return (1); } pos += n; } *offset = pos; return (0); } Index: head/lib/libc/stdio/stdio.c =================================================================== --- head/lib/libc/stdio/stdio.c (revision 289862) +++ head/lib/libc/stdio/stdio.c (revision 289863) @@ -1,168 +1,169 @@ /*- * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Chris Torek. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #if defined(LIBC_SCCS) && !defined(lint) static char sccsid[] = "@(#)stdio.c 8.1 (Berkeley) 6/4/93"; #endif /* LIBC_SCCS and not lint */ #include __FBSDID("$FreeBSD$"); #include "namespace.h" #include #include #include #include #include #include #include "un-namespace.h" #include "local.h" /* * Small standard I/O/seek/close functions. */ int __sread(void *cookie, char *buf, int n) { FILE *fp = cookie; return(_read(fp->_file, buf, (size_t)n)); } int __swrite(void *cookie, char const *buf, int n) { FILE *fp = cookie; return (_write(fp->_file, buf, (size_t)n)); } fpos_t __sseek(void *cookie, fpos_t offset, int whence) { FILE *fp = cookie; return (lseek(fp->_file, (off_t)offset, whence)); } int __sclose(void *cookie) { return (_close(((FILE *)cookie)->_file)); } /* * Higher level wrappers. */ int _sread(FILE *fp, char *buf, int n) { int ret; ret = (*fp->_read)(fp->_cookie, buf, n); if (ret > 0) { if (fp->_flags & __SOFF) { if (fp->_offset <= OFF_MAX - ret) fp->_offset += ret; else fp->_flags &= ~__SOFF; } } else if (ret < 0) fp->_flags &= ~__SOFF; return (ret); } int _swrite(FILE *fp, char const *buf, int n) { int ret; int serrno; if (fp->_flags & __SAPP) { serrno = errno; if (_sseek(fp, (fpos_t)0, SEEK_END) == -1 && (fp->_flags & __SOPT)) return (-1); errno = serrno; } ret = (*fp->_write)(fp->_cookie, buf, n); /* __SOFF removed even on success in case O_APPEND mode is set. */ if (ret >= 0) { - if ((fp->_flags & (__SAPP|__SOFF)) == (__SAPP|__SOFF) && + /* XXX: Reuse __SALC for O_APPEND. */ + if ((fp->_flags & __SOFF) && !(fp->_flags & __SALC) && fp->_offset <= OFF_MAX - ret) fp->_offset += ret; else fp->_flags &= ~__SOFF; } else if (ret < 0) fp->_flags &= ~__SOFF; return (ret); } fpos_t _sseek(FILE *fp, fpos_t offset, int whence) { fpos_t ret; int serrno, errret; serrno = errno; errno = 0; ret = (*fp->_seek)(fp->_cookie, offset, whence); errret = errno; if (errno == 0) errno = serrno; /* * Disallow negative seeks per POSIX. * It is needed here to help upper level caller * in the cases it can't detect. */ if (ret < 0) { if (errret == 0) { if (offset != 0 || whence != SEEK_CUR) { if (HASUB(fp)) FREEUB(fp); fp->_p = fp->_bf._base; fp->_r = 0; fp->_flags &= ~__SEOF; } fp->_flags |= __SERR; errno = EINVAL; } else if (errret == ESPIPE) fp->_flags &= ~__SAPP; fp->_flags &= ~__SOFF; ret = -1; } else if (fp->_flags & __SOPT) { fp->_flags |= __SOFF; fp->_offset = ret; } return (ret); }