1/*- 2 * Copyright (c) 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Chris Torek. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 4. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33#if defined(LIBC_SCCS) && !defined(lint) 34static char sccsid[] = "@(#)stdio.c 8.1 (Berkeley) 6/4/93"; 35#endif /* LIBC_SCCS and not lint */ 36#include <sys/cdefs.h> 37__FBSDID("$FreeBSD: src/lib/libc/stdio/stdio.c,v 1.28 2008/05/05 16:14:02 jhb Exp $"); 38 39#include "namespace.h" 40#include <errno.h> 41#include <fcntl.h> 42#include <limits.h> 43#include <stdio.h> 44#include <stdlib.h> 45#include <unistd.h> 46#include "un-namespace.h" 47#include "local.h" 48 49/* 50 * Small standard I/O/seek/close functions. 51 */ 52int 53__sread(cookie, buf, n) 54 void *cookie; 55 char *buf; 56 int n; 57{ 58 FILE *fp = cookie; 59 60 return(_read(fp->_file, buf, (size_t)n)); 61} 62 63int 64__swrite(cookie, buf, n) 65 void *cookie; 66 char const *buf; 67 int n; 68{ 69 FILE *fp = cookie; 70 71 return (_write(fp->_file, buf, (size_t)n)); 72} 73 74fpos_t 75__sseek(cookie, offset, whence) 76 void *cookie; 77 fpos_t offset; 78 int whence; 79{ 80 FILE *fp = cookie; 81 82 return (lseek(fp->_file, (off_t)offset, whence)); 83} 84 85int 86__sclose(cookie) 87 void *cookie; 88{ 89 90 return (_close(((FILE *)cookie)->_file)); 91} 92 93/* 94 * Higher level wrappers. 95 */ 96int 97_sread(fp, buf, n) 98 FILE *fp; 99 char *buf; 100 int n; 101{ 102 int ret; 103 104 ret = (*fp->_read)(fp->_cookie, buf, n); 105 if (ret > 0) { 106 if (fp->_flags & __SOFF) { 107 if (fp->_offset <= OFF_MAX - ret) 108 fp->_offset += ret; 109 else 110 fp->_flags &= ~__SOFF; 111 } 112 } else if (ret < 0) 113 fp->_flags &= ~__SOFF; 114 return (ret); 115} 116 117int 118_swrite(fp, buf, n) 119 FILE *fp; 120 char const *buf; 121 int n; 122{ 123 int ret; 124 int serrno; 125 126 if (fp->_flags & __SAPP) { 127 serrno = errno; 128 if (_sseek(fp, (fpos_t)0, SEEK_END) == -1 && 129 (fp->_flags & __SOPT)) 130 return (-1); 131 errno = serrno; 132 } 133 ret = (*fp->_write)(fp->_cookie, buf, n); 134 /* __SOFF removed even on success in case O_APPEND mode is set. */ 135 if (ret >= 0) { 136 if ((fp->_flags & (__SAPP|__SOFF)) == (__SAPP|__SOFF) && 137 fp->_offset <= OFF_MAX - ret) 138 fp->_offset += ret; 139 else 140 fp->_flags &= ~__SOFF; 141 142 } else if (ret < 0) 143 fp->_flags &= ~__SOFF; 144 return (ret); 145} 146 147fpos_t 148_sseek(fp, offset, whence) 149 FILE *fp; 150 fpos_t offset; 151 int whence; 152{ 153 fpos_t ret; 154 int serrno, errret; 155 156 serrno = errno; 157 errno = 0; 158 ret = (*fp->_seek)(fp->_cookie, offset, whence); 159 errret = errno; 160 if (errno == 0) 161 errno = serrno; 162 /* 163 * Disallow negative seeks per POSIX. 164 * It is needed here to help upper level caller 165 * in the cases it can't detect. 166 */ 167 if (ret < 0) { 168 if (errret == 0) { 169 if (offset != 0 || whence != SEEK_CUR) { 170 if (HASUB(fp)) 171 FREEUB(fp); 172 fp->_p = fp->_bf._base; 173 fp->_r = 0; 174 fp->_flags &= ~__SEOF; 175 } 176 fp->_flags |= __SERR; 177 errno = EINVAL; 178 } else if (errret == ESPIPE) 179 fp->_flags &= ~__SAPP; 180 fp->_flags &= ~__SOFF; 181 ret = -1; 182 } else if (fp->_flags & __SOPT) { 183 fp->_flags |= __SOFF; 184 fp->_offset = ret; 185 } 186 return (ret); 187} 188