Dmitriy Ivanov | 623b0d0 | 2014-05-14 23:11:05 -0700 | [diff] [blame] | 1 | /* $OpenBSD: findfp.c,v 1.15 2013/12/17 16:33:27 deraadt Exp $ */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2 | /*- |
| 3 | * Copyright (c) 1990, 1993 |
| 4 | * The Regents of the University of California. All rights reserved. |
| 5 | * |
| 6 | * This code is derived from software contributed to Berkeley by |
| 7 | * Chris Torek. |
| 8 | * |
| 9 | * Redistribution and use in source and binary forms, with or without |
| 10 | * modification, are permitted provided that the following conditions |
| 11 | * are met: |
| 12 | * 1. Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in the |
| 16 | * documentation and/or other materials provided with the distribution. |
| 17 | * 3. Neither the name of the University nor the names of its contributors |
| 18 | * may be used to endorse or promote products derived from this software |
| 19 | * without specific prior written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 31 | * SUCH DAMAGE. |
| 32 | */ |
| 33 | |
Elliott Hughes | 53cf348 | 2016-08-09 13:06:41 -0700 | [diff] [blame] | 34 | #define __BIONIC_NO_STDIO_FORTIFY |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 35 | #include <stdio.h> |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 36 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 37 | #include <errno.h> |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 38 | #include <fcntl.h> |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 39 | #include <limits.h> |
Elliott Hughes | 20788ae | 2016-06-09 15:16:32 -0700 | [diff] [blame] | 40 | #include <paths.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 41 | #include <stdlib.h> |
| 42 | #include <string.h> |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 43 | #include <sys/param.h> |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 44 | #include <sys/stat.h> |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 45 | #include <unistd.h> |
| 46 | |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 47 | #include <async_safe/log.h> |
| 48 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 49 | #include "local.h" |
| 50 | #include "glue.h" |
Elliott Hughes | fb3873d | 2016-08-10 11:07:54 -0700 | [diff] [blame] | 51 | #include "private/bionic_fortify.h" |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 52 | #include "private/ErrnoRestorer.h" |
Elliott Hughes | 6a03abc | 2014-11-03 12:32:17 -0800 | [diff] [blame] | 53 | #include "private/thread_private.h" |
| 54 | |
| 55 | #define ALIGNBYTES (sizeof(uintptr_t) - 1) |
| 56 | #define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) &~ ALIGNBYTES) |
Calin Juravle | c20de90 | 2014-03-20 15:21:32 +0000 | [diff] [blame] | 57 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 58 | #define NDYNAMIC 10 /* add ten more whenever necessary */ |
| 59 | |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 60 | #define PRINTF_IMPL(expr) \ |
| 61 | va_list ap; \ |
| 62 | va_start(ap, fmt); \ |
| 63 | int result = (expr); \ |
| 64 | va_end(ap); \ |
| 65 | return result; |
| 66 | |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 67 | #define std(flags, file) \ |
| 68 | {0,0,0,flags,file,{0,0},0,__sF+file,__sclose,__sread,nullptr,__swrite, \ |
| 69 | {(unsigned char *)(__sFext+file), 0},nullptr,0,{0},{0},{0,0},0,0} |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 70 | |
Kenny Root | f582340 | 2011-02-12 07:13:44 -0800 | [diff] [blame] | 71 | _THREAD_PRIVATE_MUTEX(__sfp_mutex); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 72 | |
Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 73 | #define SBUF_INIT {} |
| 74 | #define WCHAR_IO_DATA_INIT {} |
Elliott Hughes | 29ee639 | 2015-12-07 11:07:15 -0800 | [diff] [blame] | 75 | |
Elliott Hughes | bb46afd | 2015-12-04 18:03:12 -0800 | [diff] [blame] | 76 | static struct __sfileext __sFext[3] = { |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 77 | { SBUF_INIT, WCHAR_IO_DATA_INIT, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP, false, __sseek64 }, |
| 78 | { SBUF_INIT, WCHAR_IO_DATA_INIT, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP, false, __sseek64 }, |
| 79 | { SBUF_INIT, WCHAR_IO_DATA_INIT, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP, false, __sseek64 }, |
Elliott Hughes | bb46afd | 2015-12-04 18:03:12 -0800 | [diff] [blame] | 80 | }; |
Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 81 | |
| 82 | // __sF is exported for backwards compatibility. Until M, we didn't have symbols |
| 83 | // for stdin/stdout/stderr; they were macros accessing __sF. |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 84 | FILE __sF[3] = { |
Elliott Hughes | bb46afd | 2015-12-04 18:03:12 -0800 | [diff] [blame] | 85 | std(__SRD, STDIN_FILENO), |
| 86 | std(__SWR, STDOUT_FILENO), |
| 87 | std(__SWR|__SNBF, STDERR_FILENO), |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 88 | }; |
Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 89 | |
Elliott Hughes | 168667c | 2014-11-14 14:42:59 -0800 | [diff] [blame] | 90 | FILE* stdin = &__sF[0]; |
| 91 | FILE* stdout = &__sF[1]; |
| 92 | FILE* stderr = &__sF[2]; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 93 | |
Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame^] | 94 | struct glue __sglue = { nullptr, 3, __sF }; |
Elliott Hughes | bb46afd | 2015-12-04 18:03:12 -0800 | [diff] [blame] | 95 | static struct glue* lastglue = &__sglue; |
| 96 | |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 97 | class ScopedFileLock { |
| 98 | public: |
Chih-Hung Hsieh | 62e3a07 | 2016-05-03 12:08:05 -0700 | [diff] [blame] | 99 | explicit ScopedFileLock(FILE* fp) : fp_(fp) { |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 100 | FLOCKFILE(fp_); |
| 101 | } |
| 102 | ~ScopedFileLock() { |
| 103 | FUNLOCKFILE(fp_); |
| 104 | } |
| 105 | |
| 106 | private: |
| 107 | FILE* fp_; |
| 108 | }; |
| 109 | |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 110 | static glue* moreglue(int n) { |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 111 | static FILE empty; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 112 | |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 113 | char* data = new char[sizeof(glue) + ALIGNBYTES + n * sizeof(FILE) + n * sizeof(__sfileext)]; |
| 114 | if (data == nullptr) return nullptr; |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 115 | |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 116 | glue* g = reinterpret_cast<glue*>(data); |
| 117 | FILE* p = reinterpret_cast<FILE*>(ALIGN(data + sizeof(*g))); |
| 118 | __sfileext* pext = reinterpret_cast<__sfileext*>(ALIGN(data + sizeof(*g)) + n * sizeof(FILE)); |
Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame^] | 119 | g->next = nullptr; |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 120 | g->niobs = n; |
| 121 | g->iobs = p; |
| 122 | while (--n >= 0) { |
| 123 | *p = empty; |
| 124 | _FILEEXT_SETUP(p, pext); |
| 125 | p++; |
| 126 | pext++; |
| 127 | } |
| 128 | return g; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Elliott Hughes | 80e4c15 | 2017-07-21 13:57:55 -0700 | [diff] [blame] | 131 | static inline void free_fgetln_buffer(FILE* fp) { |
| 132 | if (__predict_false(fp->_lb._base != nullptr)) { |
| 133 | free(fp->_lb._base); |
| 134 | fp->_lb._base = nullptr; |
| 135 | } |
| 136 | } |
| 137 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 138 | /* |
| 139 | * Find a free FILE for fopen et al. |
| 140 | */ |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 141 | FILE* __sfp(void) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 142 | FILE *fp; |
| 143 | int n; |
| 144 | struct glue *g; |
| 145 | |
Kenny Root | f582340 | 2011-02-12 07:13:44 -0800 | [diff] [blame] | 146 | _THREAD_PRIVATE_MUTEX_LOCK(__sfp_mutex); |
Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame^] | 147 | for (g = &__sglue; g != nullptr; g = g->next) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 148 | for (fp = g->iobs, n = g->niobs; --n >= 0; fp++) |
| 149 | if (fp->_flags == 0) |
| 150 | goto found; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 151 | } |
Kenny Root | f582340 | 2011-02-12 07:13:44 -0800 | [diff] [blame] | 152 | |
| 153 | /* release lock while mallocing */ |
| 154 | _THREAD_PRIVATE_MUTEX_UNLOCK(__sfp_mutex); |
Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame^] | 155 | if ((g = moreglue(NDYNAMIC)) == nullptr) return nullptr; |
Kenny Root | f582340 | 2011-02-12 07:13:44 -0800 | [diff] [blame] | 156 | _THREAD_PRIVATE_MUTEX_LOCK(__sfp_mutex); |
| 157 | lastglue->next = g; |
| 158 | lastglue = g; |
| 159 | fp = g->iobs; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 160 | found: |
| 161 | fp->_flags = 1; /* reserve this slot; caller sets real flags */ |
Kenny Root | f582340 | 2011-02-12 07:13:44 -0800 | [diff] [blame] | 162 | _THREAD_PRIVATE_MUTEX_UNLOCK(__sfp_mutex); |
Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame^] | 163 | fp->_p = nullptr; /* no current pointer */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 164 | fp->_w = 0; /* nothing to read or write */ |
| 165 | fp->_r = 0; |
Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame^] | 166 | fp->_bf._base = nullptr; /* no buffer */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 167 | fp->_bf._size = 0; |
| 168 | fp->_lbfsize = 0; /* not line buffered */ |
| 169 | fp->_file = -1; /* no file */ |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 170 | |
Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame^] | 171 | fp->_lb._base = nullptr; /* no line buffer */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 172 | fp->_lb._size = 0; |
| 173 | _FILEEXT_INIT(fp); |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 174 | |
| 175 | // Caller sets cookie, _read/_write etc. |
| 176 | // We explicitly clear _seek and _seek64 to prevent subtle bugs. |
| 177 | fp->_seek = nullptr; |
| 178 | _EXT(fp)->_seek64 = nullptr; |
| 179 | |
| 180 | return fp; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 181 | } |
| 182 | |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 183 | extern "C" __LIBC_HIDDEN__ void __libc_stdio_cleanup(void) { |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 184 | // Equivalent to fflush(nullptr), but without all the locking since we're shutting down anyway. |
| 185 | _fwalk(__sflush); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 186 | } |
Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 187 | |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 188 | static FILE* __fopen(int fd, int flags) { |
| 189 | #if !defined(__LP64__) |
| 190 | if (fd > SHRT_MAX) { |
| 191 | errno = EMFILE; |
| 192 | return nullptr; |
| 193 | } |
| 194 | #endif |
| 195 | |
| 196 | FILE* fp = __sfp(); |
| 197 | if (fp != nullptr) { |
| 198 | fp->_file = fd; |
| 199 | fp->_flags = flags; |
| 200 | fp->_cookie = fp; |
| 201 | fp->_read = __sread; |
| 202 | fp->_write = __swrite; |
| 203 | fp->_close = __sclose; |
| 204 | _EXT(fp)->_seek64 = __sseek64; |
| 205 | } |
| 206 | return fp; |
| 207 | } |
| 208 | |
| 209 | FILE* fopen(const char* file, const char* mode) { |
Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 210 | int mode_flags; |
| 211 | int flags = __sflags(mode, &mode_flags); |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 212 | if (flags == 0) return nullptr; |
| 213 | |
Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 214 | int fd = open(file, mode_flags, DEFFILEMODE); |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 215 | if (fd == -1) { |
| 216 | return nullptr; |
| 217 | } |
| 218 | |
| 219 | FILE* fp = __fopen(fd, flags); |
| 220 | if (fp == nullptr) { |
| 221 | ErrnoRestorer errno_restorer; |
| 222 | close(fd); |
| 223 | return nullptr; |
| 224 | } |
| 225 | |
Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 226 | // For append mode, even though we use O_APPEND, we need to seek to the end now. |
| 227 | if ((mode_flags & O_APPEND) != 0) __sseek64(fp, 0, SEEK_END); |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 228 | return fp; |
| 229 | } |
Elliott Hughes | f226ee5 | 2016-02-03 11:24:28 -0800 | [diff] [blame] | 230 | __strong_alias(fopen64, fopen); |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 231 | |
| 232 | FILE* fdopen(int fd, const char* mode) { |
Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 233 | int mode_flags; |
| 234 | int flags = __sflags(mode, &mode_flags); |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 235 | if (flags == 0) return nullptr; |
| 236 | |
| 237 | // Make sure the mode the user wants is a subset of the actual mode. |
Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 238 | int fd_flags = fcntl(fd, F_GETFL, 0); |
| 239 | if (fd_flags == -1) return nullptr; |
| 240 | int tmp = fd_flags & O_ACCMODE; |
| 241 | if (tmp != O_RDWR && (tmp != (mode_flags & O_ACCMODE))) { |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 242 | errno = EINVAL; |
| 243 | return nullptr; |
| 244 | } |
| 245 | |
Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 246 | // Make sure O_APPEND is set on the underlying fd if our mode has 'a'. |
| 247 | // POSIX says we just take the current offset of the underlying fd. |
| 248 | if ((mode_flags & O_APPEND) && !(fd_flags & O_APPEND)) { |
| 249 | if (fcntl(fd, F_SETFL, fd_flags | O_APPEND) == -1) return nullptr; |
| 250 | } |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 251 | |
Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 252 | // Make sure O_CLOEXEC is set on the underlying fd if our mode has 'x'. |
| 253 | if ((mode_flags & O_CLOEXEC) && !((tmp = fcntl(fd, F_GETFD)) & FD_CLOEXEC)) { |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 254 | fcntl(fd, F_SETFD, tmp | FD_CLOEXEC); |
| 255 | } |
| 256 | |
| 257 | return __fopen(fd, flags); |
| 258 | } |
| 259 | |
| 260 | // Re-direct an existing, open (probably) file to some other file. |
| 261 | // ANSI is written such that the original file gets closed if at |
| 262 | // all possible, no matter what. |
| 263 | // TODO: rewrite this mess completely. |
| 264 | FILE* freopen(const char* file, const char* mode, FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 265 | CHECK_FP(fp); |
Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 266 | int mode_flags; |
| 267 | int flags = __sflags(mode, &mode_flags); |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 268 | if (flags == 0) { |
| 269 | fclose(fp); |
| 270 | return nullptr; |
| 271 | } |
| 272 | |
| 273 | ScopedFileLock sfl(fp); |
| 274 | |
| 275 | // There are actually programs that depend on being able to "freopen" |
| 276 | // descriptors that weren't originally open. Keep this from breaking. |
| 277 | // Remember whether the stream was open to begin with, and which file |
| 278 | // descriptor (if any) was associated with it. If it was attached to |
| 279 | // a descriptor, defer closing it; freopen("/dev/stdin", "r", stdin) |
| 280 | // should work. This is unnecessary if it was not a Unix file. |
| 281 | int isopen, wantfd; |
| 282 | if (fp->_flags == 0) { |
| 283 | fp->_flags = __SEOF; // Hold on to it. |
| 284 | isopen = 0; |
| 285 | wantfd = -1; |
| 286 | } else { |
| 287 | // Flush the stream; ANSI doesn't require this. |
| 288 | if (fp->_flags & __SWR) __sflush(fp); |
| 289 | |
Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame^] | 290 | // If close is null, closing is a no-op, hence pointless. |
| 291 | isopen = (fp->_close != nullptr); |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 292 | if ((wantfd = fp->_file) < 0 && isopen) { |
| 293 | (*fp->_close)(fp->_cookie); |
| 294 | isopen = 0; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | // Get a new descriptor to refer to the new file. |
Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 299 | int fd = open(file, mode_flags, DEFFILEMODE); |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 300 | if (fd < 0 && isopen) { |
| 301 | // If out of fd's close the old one and try again. |
| 302 | if (errno == ENFILE || errno == EMFILE) { |
| 303 | (*fp->_close)(fp->_cookie); |
| 304 | isopen = 0; |
Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 305 | fd = open(file, mode_flags, DEFFILEMODE); |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
| 309 | int sverrno = errno; |
| 310 | |
| 311 | // Finish closing fp. Even if the open succeeded above, we cannot |
| 312 | // keep fp->_base: it may be the wrong size. This loses the effect |
| 313 | // of any setbuffer calls, but stdio has always done this before. |
| 314 | if (isopen && fd != wantfd) (*fp->_close)(fp->_cookie); |
| 315 | if (fp->_flags & __SMBF) free(fp->_bf._base); |
| 316 | fp->_w = 0; |
| 317 | fp->_r = 0; |
Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame^] | 318 | fp->_p = nullptr; |
| 319 | fp->_bf._base = nullptr; |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 320 | fp->_bf._size = 0; |
| 321 | fp->_lbfsize = 0; |
| 322 | if (HASUB(fp)) FREEUB(fp); |
| 323 | _UB(fp)._size = 0; |
| 324 | WCIO_FREE(fp); |
Elliott Hughes | 80e4c15 | 2017-07-21 13:57:55 -0700 | [diff] [blame] | 325 | free_fgetln_buffer(fp); |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 326 | fp->_lb._size = 0; |
| 327 | |
| 328 | if (fd < 0) { // Did not get it after all. |
| 329 | fp->_flags = 0; // Release. |
| 330 | errno = sverrno; // Restore errno in case _close clobbered it. |
| 331 | return nullptr; |
| 332 | } |
| 333 | |
| 334 | // If reopening something that was open before on a real file, try |
| 335 | // to maintain the descriptor. Various C library routines (perror) |
| 336 | // assume stderr is always fd STDERR_FILENO, even if being freopen'd. |
| 337 | if (wantfd >= 0 && fd != wantfd) { |
Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 338 | if (dup3(fd, wantfd, mode_flags & O_CLOEXEC) >= 0) { |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 339 | close(fd); |
| 340 | fd = wantfd; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | // _file is only a short. |
| 345 | if (fd > SHRT_MAX) { |
| 346 | fp->_flags = 0; // Release. |
| 347 | errno = EMFILE; |
| 348 | return nullptr; |
| 349 | } |
| 350 | |
| 351 | fp->_flags = flags; |
| 352 | fp->_file = fd; |
| 353 | fp->_cookie = fp; |
| 354 | fp->_read = __sread; |
| 355 | fp->_write = __swrite; |
| 356 | fp->_close = __sclose; |
| 357 | _EXT(fp)->_seek64 = __sseek64; |
| 358 | |
Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 359 | // For append mode, even though we use O_APPEND, we need to seek to the end now. |
| 360 | if ((mode_flags & O_APPEND) != 0) __sseek64(fp, 0, SEEK_END); |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 361 | return fp; |
| 362 | } |
Elliott Hughes | f226ee5 | 2016-02-03 11:24:28 -0800 | [diff] [blame] | 363 | __strong_alias(freopen64, freopen); |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 364 | |
Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 365 | int fclose(FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 366 | CHECK_FP(fp); |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 367 | if (fp->_flags == 0) { |
| 368 | // Already freed! |
| 369 | errno = EBADF; |
| 370 | return EOF; |
| 371 | } |
Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 372 | |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 373 | ScopedFileLock sfl(fp); |
| 374 | WCIO_FREE(fp); |
| 375 | int r = fp->_flags & __SWR ? __sflush(fp) : 0; |
Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame^] | 376 | if (fp->_close != nullptr && (*fp->_close)(fp->_cookie) < 0) { |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 377 | r = EOF; |
| 378 | } |
| 379 | if (fp->_flags & __SMBF) free(fp->_bf._base); |
| 380 | if (HASUB(fp)) FREEUB(fp); |
Elliott Hughes | 80e4c15 | 2017-07-21 13:57:55 -0700 | [diff] [blame] | 381 | free_fgetln_buffer(fp); |
Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 382 | |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 383 | // Poison this FILE so accesses after fclose will be obvious. |
| 384 | fp->_file = -1; |
| 385 | fp->_r = fp->_w = 0; |
Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 386 | |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 387 | // Release this FILE for reuse. |
| 388 | fp->_flags = 0; |
| 389 | return r; |
Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 390 | } |
| 391 | |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 392 | int fileno_unlocked(FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 393 | CHECK_FP(fp); |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 394 | int fd = fp->_file; |
| 395 | if (fd == -1) { |
| 396 | errno = EBADF; |
| 397 | return -1; |
| 398 | } |
| 399 | return fd; |
| 400 | } |
| 401 | |
Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 402 | int fileno(FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 403 | CHECK_FP(fp); |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 404 | ScopedFileLock sfl(fp); |
| 405 | return fileno_unlocked(fp); |
Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 406 | } |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 407 | |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 408 | void clearerr_unlocked(FILE* fp) { |
| 409 | return __sclearerr(fp); |
| 410 | } |
| 411 | |
| 412 | void clearerr(FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 413 | CHECK_FP(fp); |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 414 | ScopedFileLock sfl(fp); |
| 415 | clearerr_unlocked(fp); |
| 416 | } |
| 417 | |
| 418 | int feof_unlocked(FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 419 | CHECK_FP(fp); |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 420 | return ((fp->_flags & __SEOF) != 0); |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | int feof(FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 424 | CHECK_FP(fp); |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 425 | ScopedFileLock sfl(fp); |
| 426 | return feof_unlocked(fp); |
| 427 | } |
| 428 | |
| 429 | int ferror_unlocked(FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 430 | CHECK_FP(fp); |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 431 | return __sferror(fp); |
| 432 | } |
| 433 | |
| 434 | int ferror(FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 435 | CHECK_FP(fp); |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 436 | ScopedFileLock sfl(fp); |
| 437 | return ferror_unlocked(fp); |
| 438 | } |
| 439 | |
Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame^] | 440 | int __sflush(FILE* fp) { |
| 441 | // Flushing a read-only file is a no-op. |
| 442 | if ((fp->_flags & __SWR) == 0) return 0; |
| 443 | |
| 444 | // Flushing a file without a buffer is a no-op. |
| 445 | unsigned char* p = fp->_bf._base; |
| 446 | if (p == nullptr) return 0; |
| 447 | |
| 448 | // Set these immediately to avoid problems with longjmp and to allow |
| 449 | // exchange buffering (via setvbuf) in user write function. |
| 450 | int n = fp->_p - p; |
| 451 | fp->_p = p; |
| 452 | fp->_w = (fp->_flags & (__SLBF|__SNBF)) ? 0 : fp->_bf._size; |
| 453 | |
| 454 | while (n > 0) { |
| 455 | int written = (*fp->_write)(fp->_cookie, reinterpret_cast<char*>(p), n); |
| 456 | if (written <= 0) { |
| 457 | fp->_flags |= __SERR; |
| 458 | return EOF; |
| 459 | } |
| 460 | n -= written, p += written; |
| 461 | } |
| 462 | return 0; |
| 463 | } |
| 464 | |
| 465 | int __sflush_locked(FILE* fp) { |
| 466 | ScopedFileLock sfl(fp); |
| 467 | return __sflush(fp); |
| 468 | } |
| 469 | |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 470 | int __sread(void* cookie, char* buf, int n) { |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 471 | FILE* fp = reinterpret_cast<FILE*>(cookie); |
| 472 | return TEMP_FAILURE_RETRY(read(fp->_file, buf, n)); |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | int __swrite(void* cookie, const char* buf, int n) { |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 476 | FILE* fp = reinterpret_cast<FILE*>(cookie); |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 477 | return TEMP_FAILURE_RETRY(write(fp->_file, buf, n)); |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 478 | } |
| 479 | |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 480 | fpos_t __sseek(void* cookie, fpos_t offset, int whence) { |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 481 | FILE* fp = reinterpret_cast<FILE*>(cookie); |
| 482 | return TEMP_FAILURE_RETRY(lseek(fp->_file, offset, whence)); |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 483 | } |
| 484 | |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 485 | off64_t __sseek64(void* cookie, off64_t offset, int whence) { |
| 486 | FILE* fp = reinterpret_cast<FILE*>(cookie); |
| 487 | return TEMP_FAILURE_RETRY(lseek64(fp->_file, offset, whence)); |
| 488 | } |
| 489 | |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 490 | int __sclose(void* cookie) { |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 491 | FILE* fp = reinterpret_cast<FILE*>(cookie); |
| 492 | return close(fp->_file); |
| 493 | } |
| 494 | |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 495 | static off64_t __seek_unlocked(FILE* fp, off64_t offset, int whence) { |
| 496 | // Use `_seek64` if set, but fall back to `_seek`. |
| 497 | if (_EXT(fp)->_seek64 != nullptr) { |
| 498 | return (*_EXT(fp)->_seek64)(fp->_cookie, offset, whence); |
| 499 | } else if (fp->_seek != nullptr) { |
Elliott Hughes | 955426e | 2016-01-26 18:25:52 -0800 | [diff] [blame] | 500 | off64_t result = (*fp->_seek)(fp->_cookie, offset, whence); |
| 501 | #if !defined(__LP64__) |
| 502 | // Avoid sign extension if off64_t is larger than off_t. |
| 503 | if (result != -1) result &= 0xffffffff; |
| 504 | #endif |
| 505 | return result; |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 506 | } else { |
| 507 | errno = ESPIPE; |
| 508 | return -1; |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 509 | } |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 510 | } |
| 511 | |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 512 | static off64_t __ftello64_unlocked(FILE* fp) { |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 513 | // Find offset of underlying I/O object, then adjust for buffered bytes. |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 514 | __sflush(fp); // May adjust seek offset on append stream. |
Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 515 | |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 516 | off64_t result = __seek_unlocked(fp, 0, SEEK_CUR); |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 517 | if (result == -1) { |
| 518 | return -1; |
| 519 | } |
| 520 | |
| 521 | if (fp->_flags & __SRD) { |
| 522 | // Reading. Any unread characters (including |
| 523 | // those from ungetc) cause the position to be |
| 524 | // smaller than that in the underlying object. |
| 525 | result -= fp->_r; |
| 526 | if (HASUB(fp)) result -= fp->_ur; |
Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame^] | 527 | } else if (fp->_flags & __SWR && fp->_p != nullptr) { |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 528 | // Writing. Any buffered characters cause the |
| 529 | // position to be greater than that in the |
| 530 | // underlying object. |
| 531 | result += fp->_p - fp->_bf._base; |
| 532 | } |
| 533 | return result; |
| 534 | } |
| 535 | |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 536 | int __fseeko64(FILE* fp, off64_t offset, int whence, int off_t_bits) { |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 537 | ScopedFileLock sfl(fp); |
| 538 | |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 539 | // Change any SEEK_CUR to SEEK_SET, and check `whence` argument. |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 540 | // After this, whence is either SEEK_SET or SEEK_END. |
| 541 | if (whence == SEEK_CUR) { |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 542 | fpos64_t current_offset = __ftello64_unlocked(fp); |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 543 | if (current_offset == -1) { |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 544 | return -1; |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 545 | } |
| 546 | offset += current_offset; |
| 547 | whence = SEEK_SET; |
| 548 | } else if (whence != SEEK_SET && whence != SEEK_END) { |
| 549 | errno = EINVAL; |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 550 | return -1; |
| 551 | } |
| 552 | |
| 553 | // If our caller has a 32-bit interface, refuse to go past a 32-bit file offset. |
| 554 | if (off_t_bits == 32 && offset > LONG_MAX) { |
| 555 | errno = EOVERFLOW; |
| 556 | return -1; |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 557 | } |
| 558 | |
Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame^] | 559 | if (fp->_bf._base == nullptr) __smakebuf(fp); |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 560 | |
| 561 | // Flush unwritten data and attempt the seek. |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 562 | if (__sflush(fp) || __seek_unlocked(fp, offset, whence) == -1) { |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 563 | return -1; |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | // Success: clear EOF indicator and discard ungetc() data. |
| 567 | if (HASUB(fp)) FREEUB(fp); |
| 568 | fp->_p = fp->_bf._base; |
| 569 | fp->_r = 0; |
| 570 | /* fp->_w = 0; */ /* unnecessary (I think...) */ |
| 571 | fp->_flags &= ~__SEOF; |
| 572 | return 0; |
| 573 | } |
| 574 | |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 575 | int fseeko(FILE* fp, off_t offset, int whence) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 576 | CHECK_FP(fp); |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 577 | static_assert(sizeof(off_t) == sizeof(long), "sizeof(off_t) != sizeof(long)"); |
| 578 | return __fseeko64(fp, offset, whence, 8*sizeof(off_t)); |
| 579 | } |
| 580 | __strong_alias(fseek, fseeko); |
| 581 | |
| 582 | int fseeko64(FILE* fp, off64_t offset, int whence) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 583 | CHECK_FP(fp); |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 584 | return __fseeko64(fp, offset, whence, 8*sizeof(off_t)); |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 585 | } |
| 586 | |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 587 | int fsetpos(FILE* fp, const fpos_t* pos) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 588 | CHECK_FP(fp); |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 589 | return fseeko(fp, *pos, SEEK_SET); |
| 590 | } |
| 591 | |
| 592 | int fsetpos64(FILE* fp, const fpos64_t* pos) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 593 | CHECK_FP(fp); |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 594 | return fseeko64(fp, *pos, SEEK_SET); |
| 595 | } |
| 596 | |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 597 | off_t ftello(FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 598 | CHECK_FP(fp); |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 599 | static_assert(sizeof(off_t) == sizeof(long), "sizeof(off_t) != sizeof(long)"); |
| 600 | off64_t result = ftello64(fp); |
| 601 | if (result > LONG_MAX) { |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 602 | errno = EOVERFLOW; |
| 603 | return -1; |
| 604 | } |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 605 | return result; |
| 606 | } |
| 607 | __strong_alias(ftell, ftello); |
| 608 | |
| 609 | off64_t ftello64(FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 610 | CHECK_FP(fp); |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 611 | ScopedFileLock sfl(fp); |
| 612 | return __ftello64_unlocked(fp); |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 613 | } |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 614 | |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 615 | int fgetpos(FILE* fp, fpos_t* pos) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 616 | CHECK_FP(fp); |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 617 | *pos = ftello(fp); |
Elliott Hughes | 955426e | 2016-01-26 18:25:52 -0800 | [diff] [blame] | 618 | return (*pos == -1) ? -1 : 0; |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 619 | } |
| 620 | |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 621 | int fgetpos64(FILE* fp, fpos64_t* pos) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 622 | CHECK_FP(fp); |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 623 | *pos = ftello64(fp); |
Elliott Hughes | 955426e | 2016-01-26 18:25:52 -0800 | [diff] [blame] | 624 | return (*pos == -1) ? -1 : 0; |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 625 | } |
Elliott Hughes | 03e65eb | 2016-01-26 14:13:04 -0800 | [diff] [blame] | 626 | |
| 627 | static FILE* __funopen(const void* cookie, |
| 628 | int (*read_fn)(void*, char*, int), |
| 629 | int (*write_fn)(void*, const char*, int), |
| 630 | int (*close_fn)(void*)) { |
| 631 | if (read_fn == nullptr && write_fn == nullptr) { |
| 632 | errno = EINVAL; |
| 633 | return nullptr; |
| 634 | } |
| 635 | |
| 636 | FILE* fp = __sfp(); |
| 637 | if (fp == nullptr) return nullptr; |
| 638 | |
| 639 | if (read_fn != nullptr && write_fn != nullptr) { |
| 640 | fp->_flags = __SRW; |
| 641 | } else if (read_fn != nullptr) { |
| 642 | fp->_flags = __SRD; |
| 643 | } else if (write_fn != nullptr) { |
| 644 | fp->_flags = __SWR; |
| 645 | } |
| 646 | |
| 647 | fp->_file = -1; |
| 648 | fp->_cookie = const_cast<void*>(cookie); // The funopen(3) API is incoherent. |
| 649 | fp->_read = read_fn; |
| 650 | fp->_write = write_fn; |
| 651 | fp->_close = close_fn; |
| 652 | |
| 653 | return fp; |
| 654 | } |
| 655 | |
| 656 | FILE* funopen(const void* cookie, |
| 657 | int (*read_fn)(void*, char*, int), |
| 658 | int (*write_fn)(void*, const char*, int), |
| 659 | fpos_t (*seek_fn)(void*, fpos_t, int), |
| 660 | int (*close_fn)(void*)) { |
| 661 | FILE* fp = __funopen(cookie, read_fn, write_fn, close_fn); |
| 662 | if (fp != nullptr) { |
| 663 | fp->_seek = seek_fn; |
| 664 | } |
| 665 | return fp; |
| 666 | } |
| 667 | |
| 668 | FILE* funopen64(const void* cookie, |
| 669 | int (*read_fn)(void*, char*, int), |
| 670 | int (*write_fn)(void*, const char*, int), |
| 671 | fpos64_t (*seek_fn)(void*, fpos64_t, int), |
| 672 | int (*close_fn)(void*)) { |
| 673 | FILE* fp = __funopen(cookie, read_fn, write_fn, close_fn); |
| 674 | if (fp != nullptr) { |
| 675 | _EXT(fp)->_seek64 = seek_fn; |
| 676 | } |
| 677 | return fp; |
| 678 | } |
Elliott Hughes | 20788ae | 2016-06-09 15:16:32 -0700 | [diff] [blame] | 679 | |
Elliott Hughes | 53cf348 | 2016-08-09 13:06:41 -0700 | [diff] [blame] | 680 | int asprintf(char** s, const char* fmt, ...) { |
| 681 | PRINTF_IMPL(vasprintf(s, fmt, ap)); |
| 682 | } |
| 683 | |
Elliott Hughes | 20788ae | 2016-06-09 15:16:32 -0700 | [diff] [blame] | 684 | char* ctermid(char* s) { |
| 685 | return s ? strcpy(s, _PATH_TTY) : const_cast<char*>(_PATH_TTY); |
| 686 | } |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 687 | |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 688 | int dprintf(int fd, const char* fmt, ...) { |
| 689 | PRINTF_IMPL(vdprintf(fd, fmt, ap)); |
| 690 | } |
| 691 | |
| 692 | int fprintf(FILE* fp, const char* fmt, ...) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 693 | CHECK_FP(fp); |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 694 | PRINTF_IMPL(vfprintf(fp, fmt, ap)); |
| 695 | } |
| 696 | |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 697 | int fgetc(FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 698 | CHECK_FP(fp); |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 699 | return getc(fp); |
| 700 | } |
| 701 | |
Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame^] | 702 | int fgetc_unlocked(FILE* fp) { |
| 703 | CHECK_FP(fp); |
| 704 | return getc_unlocked(fp); |
| 705 | } |
| 706 | |
| 707 | /* |
| 708 | * Read at most n-1 characters from the given file. |
| 709 | * Stop when a newline has been read, or the count runs out. |
| 710 | * Return first argument, or NULL if no characters were read. |
| 711 | * Do not return NULL if n == 1. |
| 712 | */ |
| 713 | char* fgets(char* buf, int n, FILE* fp) __overloadable { |
| 714 | CHECK_FP(fp); |
| 715 | ScopedFileLock sfl(fp); |
| 716 | return fgets_unlocked(buf, n, fp); |
| 717 | } |
| 718 | |
| 719 | char* fgets_unlocked(char* buf, int n, FILE* fp) { |
| 720 | if (n <= 0) { |
| 721 | errno = EINVAL; |
| 722 | return nullptr; |
| 723 | } |
| 724 | |
| 725 | _SET_ORIENTATION(fp, -1); |
| 726 | |
| 727 | char* s = buf; |
| 728 | n--; // Leave space for NUL. |
| 729 | while (n != 0) { |
| 730 | // If the buffer is empty, refill it. |
| 731 | if (fp->_r <= 0) { |
| 732 | if (__srefill(fp)) { |
| 733 | // EOF/error: stop with partial or no line. |
| 734 | if (s == buf) return nullptr; |
| 735 | break; |
| 736 | } |
| 737 | } |
| 738 | size_t len = fp->_r; |
| 739 | unsigned char* p = fp->_p; |
| 740 | |
| 741 | // Scan through at most n bytes of the current buffer, |
| 742 | // looking for '\n'. If found, copy up to and including |
| 743 | // newline, and stop. Otherwise, copy entire chunk and loop. |
| 744 | if (len > static_cast<size_t>(n)) len = n; |
| 745 | unsigned char* t = static_cast<unsigned char*>(memchr(p, '\n', len)); |
| 746 | if (t != nullptr) { |
| 747 | len = ++t - p; |
| 748 | fp->_r -= len; |
| 749 | fp->_p = t; |
| 750 | memcpy(s, p, len); |
| 751 | s[len] = '\0'; |
| 752 | return buf; |
| 753 | } |
| 754 | fp->_r -= len; |
| 755 | fp->_p += len; |
| 756 | memcpy(s, p, len); |
| 757 | s += len; |
| 758 | n -= len; |
| 759 | } |
| 760 | *s = '\0'; |
| 761 | return buf; |
| 762 | } |
| 763 | |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 764 | int fputc(int c, FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 765 | CHECK_FP(fp); |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 766 | return putc(c, fp); |
| 767 | } |
| 768 | |
Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame^] | 769 | int fputc_unlocked(int c, FILE* fp) { |
| 770 | CHECK_FP(fp); |
| 771 | return putc_unlocked(c, fp); |
| 772 | } |
| 773 | |
| 774 | int fputs(const char* s, FILE* fp) { |
| 775 | CHECK_FP(fp); |
| 776 | ScopedFileLock sfl(fp); |
| 777 | return fputs_unlocked(s, fp); |
| 778 | } |
| 779 | |
| 780 | int fputs_unlocked(const char* s, FILE* fp) { |
| 781 | CHECK_FP(fp); |
| 782 | size_t length = strlen(s); |
| 783 | return (fwrite_unlocked(s, 1, length, fp) == length) ? 0 : EOF; |
| 784 | } |
| 785 | |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 786 | int fscanf(FILE* fp, const char* fmt, ...) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 787 | CHECK_FP(fp); |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 788 | PRINTF_IMPL(vfscanf(fp, fmt, ap)); |
| 789 | } |
| 790 | |
| 791 | int fwprintf(FILE* fp, const wchar_t* fmt, ...) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 792 | CHECK_FP(fp); |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 793 | PRINTF_IMPL(vfwprintf(fp, fmt, ap)); |
| 794 | } |
| 795 | |
| 796 | int fwscanf(FILE* fp, const wchar_t* fmt, ...) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 797 | CHECK_FP(fp); |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 798 | PRINTF_IMPL(vfwscanf(fp, fmt, ap)); |
| 799 | } |
| 800 | |
| 801 | int getc(FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 802 | CHECK_FP(fp); |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 803 | ScopedFileLock sfl(fp); |
| 804 | return getc_unlocked(fp); |
| 805 | } |
| 806 | |
| 807 | int getc_unlocked(FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 808 | CHECK_FP(fp); |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 809 | return __sgetc(fp); |
| 810 | } |
| 811 | |
| 812 | int getchar_unlocked() { |
| 813 | return getc_unlocked(stdin); |
| 814 | } |
| 815 | |
| 816 | int getchar() { |
| 817 | return getc(stdin); |
| 818 | } |
| 819 | |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 820 | ssize_t getline(char** buf, size_t* len, FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 821 | CHECK_FP(fp); |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 822 | return getdelim(buf, len, '\n', fp); |
| 823 | } |
| 824 | |
| 825 | wint_t getwc(FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 826 | CHECK_FP(fp); |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 827 | return fgetwc(fp); |
| 828 | } |
| 829 | |
| 830 | wint_t getwchar() { |
| 831 | return fgetwc(stdin); |
| 832 | } |
| 833 | |
Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame^] | 834 | void perror(const char* msg) { |
| 835 | if (msg == nullptr) msg = ""; |
| 836 | fprintf(stderr, "%s%s%s\n", msg, (*msg == '\0') ? "" : ": ", strerror(errno)); |
| 837 | } |
| 838 | |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 839 | int printf(const char* fmt, ...) { |
| 840 | PRINTF_IMPL(vfprintf(stdout, fmt, ap)); |
| 841 | } |
| 842 | |
| 843 | int putc(int c, FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 844 | CHECK_FP(fp); |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 845 | ScopedFileLock sfl(fp); |
| 846 | return putc_unlocked(c, fp); |
| 847 | } |
| 848 | |
| 849 | int putc_unlocked(int c, FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 850 | CHECK_FP(fp); |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 851 | if (cantwrite(fp)) { |
| 852 | errno = EBADF; |
| 853 | return EOF; |
| 854 | } |
| 855 | _SET_ORIENTATION(fp, -1); |
| 856 | if (--fp->_w >= 0 || (fp->_w >= fp->_lbfsize && c != '\n')) { |
| 857 | return (*fp->_p++ = c); |
| 858 | } |
| 859 | return (__swbuf(c, fp)); |
| 860 | } |
| 861 | |
| 862 | int putchar(int c) { |
| 863 | return putc(c, stdout); |
| 864 | } |
| 865 | |
| 866 | int putchar_unlocked(int c) { |
| 867 | return putc_unlocked(c, stdout); |
| 868 | } |
| 869 | |
Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame^] | 870 | int puts(const char* s) { |
| 871 | size_t length = strlen(s); |
| 872 | ScopedFileLock sfl(stdout); |
| 873 | return (fwrite_unlocked(s, 1, length, stdout) == length && |
| 874 | putc_unlocked('\n', stdout) != EOF) ? 0 : EOF; |
| 875 | } |
| 876 | |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 877 | wint_t putwc(wchar_t wc, FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 878 | CHECK_FP(fp); |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 879 | return fputwc(wc, fp); |
| 880 | } |
| 881 | |
| 882 | wint_t putwchar(wchar_t wc) { |
| 883 | return fputwc(wc, stdout); |
| 884 | } |
| 885 | |
Elliott Hughes | d1f25a7 | 2016-08-05 15:53:03 -0700 | [diff] [blame] | 886 | int remove(const char* path) { |
| 887 | if (unlink(path) != -1) return 0; |
| 888 | if (errno != EISDIR) return -1; |
| 889 | return rmdir(path); |
| 890 | } |
| 891 | |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 892 | void rewind(FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 893 | CHECK_FP(fp); |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 894 | ScopedFileLock sfl(fp); |
| 895 | fseek(fp, 0, SEEK_SET); |
| 896 | clearerr_unlocked(fp); |
| 897 | } |
| 898 | |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 899 | int scanf(const char* fmt, ...) { |
| 900 | PRINTF_IMPL(vfscanf(stdin, fmt, ap)); |
| 901 | } |
| 902 | |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 903 | void setbuf(FILE* fp, char* buf) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 904 | CHECK_FP(fp); |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 905 | setbuffer(fp, buf, BUFSIZ); |
| 906 | } |
| 907 | |
| 908 | void setbuffer(FILE* fp, char* buf, int size) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 909 | CHECK_FP(fp); |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 910 | setvbuf(fp, buf, buf ? _IOFBF : _IONBF, size); |
| 911 | } |
| 912 | |
| 913 | int setlinebuf(FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 914 | CHECK_FP(fp); |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 915 | return setvbuf(fp, nullptr, _IOLBF, 0); |
| 916 | } |
| 917 | |
Elliott Hughes | 53cf348 | 2016-08-09 13:06:41 -0700 | [diff] [blame] | 918 | int snprintf(char* s, size_t n, const char* fmt, ...) { |
| 919 | PRINTF_IMPL(vsnprintf(s, n, fmt, ap)); |
| 920 | } |
| 921 | |
| 922 | int sprintf(char* s, const char* fmt, ...) { |
Elliott Hughes | fb3873d | 2016-08-10 11:07:54 -0700 | [diff] [blame] | 923 | PRINTF_IMPL(vsprintf(s, fmt, ap)); |
Elliott Hughes | 53cf348 | 2016-08-09 13:06:41 -0700 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | int sscanf(const char* s, const char* fmt, ...) { |
| 927 | PRINTF_IMPL(vsscanf(s, fmt, ap)); |
| 928 | } |
| 929 | |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 930 | int swprintf(wchar_t* s, size_t n, const wchar_t* fmt, ...) { |
| 931 | PRINTF_IMPL(vswprintf(s, n, fmt, ap)); |
| 932 | } |
| 933 | |
| 934 | int swscanf(const wchar_t* s, const wchar_t* fmt, ...) { |
| 935 | PRINTF_IMPL(vswscanf(s, fmt, ap)); |
| 936 | } |
| 937 | |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 938 | int vprintf(const char* fmt, va_list ap) { |
| 939 | return vfprintf(stdout, fmt, ap); |
| 940 | } |
| 941 | |
| 942 | int vscanf(const char* fmt, va_list ap) { |
| 943 | return vfscanf(stdin, fmt, ap); |
| 944 | } |
| 945 | |
Elliott Hughes | fb3873d | 2016-08-10 11:07:54 -0700 | [diff] [blame] | 946 | int vsnprintf(char* s, size_t n, const char* fmt, va_list ap) { |
| 947 | // stdio internals use int rather than size_t. |
| 948 | static_assert(INT_MAX <= SSIZE_MAX, "SSIZE_MAX too large to fit in int"); |
| 949 | |
| 950 | __check_count("vsnprintf", "size", n); |
| 951 | |
| 952 | // Stdio internals do not deal correctly with zero length buffer. |
| 953 | char dummy; |
| 954 | if (n == 0) { |
| 955 | s = &dummy; |
| 956 | n = 1; |
| 957 | } |
| 958 | |
| 959 | FILE f; |
| 960 | __sfileext fext; |
| 961 | _FILEEXT_SETUP(&f, &fext); |
| 962 | f._file = -1; |
| 963 | f._flags = __SWR | __SSTR; |
| 964 | f._bf._base = f._p = reinterpret_cast<unsigned char*>(s); |
| 965 | f._bf._size = f._w = n - 1; |
| 966 | |
| 967 | int result = __vfprintf(&f, fmt, ap); |
| 968 | *f._p = '\0'; |
| 969 | return result; |
| 970 | } |
| 971 | |
Elliott Hughes | 53cf348 | 2016-08-09 13:06:41 -0700 | [diff] [blame] | 972 | int vsprintf(char* s, const char* fmt, va_list ap) { |
Elliott Hughes | fb3873d | 2016-08-10 11:07:54 -0700 | [diff] [blame] | 973 | return vsnprintf(s, SSIZE_MAX, fmt, ap); |
Elliott Hughes | 53cf348 | 2016-08-09 13:06:41 -0700 | [diff] [blame] | 974 | } |
| 975 | |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 976 | int vwprintf(const wchar_t* fmt, va_list ap) { |
| 977 | return vfwprintf(stdout, fmt, ap); |
| 978 | } |
| 979 | |
| 980 | int vwscanf(const wchar_t* fmt, va_list ap) { |
| 981 | return vfwscanf(stdin, fmt, ap); |
| 982 | } |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 983 | |
| 984 | int wprintf(const wchar_t* fmt, ...) { |
| 985 | PRINTF_IMPL(vfwprintf(stdout, fmt, ap)); |
| 986 | } |
| 987 | |
| 988 | int wscanf(const wchar_t* fmt, ...) { |
| 989 | PRINTF_IMPL(vfwscanf(stdin, fmt, ap)); |
| 990 | } |
Dan Albert | 3037ea4 | 2016-10-06 15:46:45 -0700 | [diff] [blame] | 991 | |
Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame^] | 992 | static int fflush_all() { |
| 993 | return _fwalk(__sflush_locked); |
| 994 | } |
| 995 | |
| 996 | int fflush(FILE* fp) { |
| 997 | if (fp == nullptr) return fflush_all(); |
| 998 | ScopedFileLock sfl(fp); |
| 999 | return fflush_unlocked(fp); |
| 1000 | } |
| 1001 | |
| 1002 | int fflush_unlocked(FILE* fp) { |
| 1003 | if (fp == nullptr) return fflush_all(); |
| 1004 | if ((fp->_flags & (__SWR | __SRW)) == 0) { |
| 1005 | errno = EBADF; |
| 1006 | return EOF; |
| 1007 | } |
| 1008 | return __sflush(fp); |
| 1009 | } |
| 1010 | |
| 1011 | size_t fread(void* buf, size_t size, size_t count, FILE* fp) __overloadable { |
| 1012 | CHECK_FP(fp); |
| 1013 | ScopedFileLock sfl(fp); |
| 1014 | return fread_unlocked(buf, size, count, fp); |
| 1015 | } |
| 1016 | |
| 1017 | size_t fread_unlocked(void* buf, size_t size, size_t count, FILE* fp) { |
| 1018 | CHECK_FP(fp); |
| 1019 | |
| 1020 | size_t desired_total; |
| 1021 | if (__builtin_mul_overflow(size, count, &desired_total)) { |
| 1022 | errno = EOVERFLOW; |
| 1023 | fp->_flags |= __SERR; |
| 1024 | return 0; |
| 1025 | } |
| 1026 | |
| 1027 | size_t total = desired_total; |
| 1028 | if (total == 0) return 0; |
| 1029 | |
| 1030 | _SET_ORIENTATION(fp, -1); |
| 1031 | |
| 1032 | // TODO: how can this ever happen?! |
| 1033 | if (fp->_r < 0) fp->_r = 0; |
| 1034 | |
| 1035 | // Ensure _bf._size is valid. |
| 1036 | if (fp->_bf._base == nullptr) __smakebuf(fp); |
| 1037 | |
| 1038 | char* dst = static_cast<char*>(buf); |
| 1039 | |
| 1040 | while (total > 0) { |
| 1041 | // Copy data out of the buffer. |
| 1042 | size_t buffered_bytes = MIN(static_cast<size_t>(fp->_r), total); |
| 1043 | memcpy(dst, fp->_p, buffered_bytes); |
| 1044 | fp->_p += buffered_bytes; |
| 1045 | fp->_r -= buffered_bytes; |
| 1046 | dst += buffered_bytes; |
| 1047 | total -= buffered_bytes; |
| 1048 | |
| 1049 | // Are we done? |
| 1050 | if (total == 0) goto out; |
| 1051 | |
| 1052 | // Do we have so much more to read that we should avoid copying it through the buffer? |
| 1053 | if (total > static_cast<size_t>(fp->_bf._size)) break; |
| 1054 | |
| 1055 | // Less than a buffer to go, so refill the buffer and go around the loop again. |
| 1056 | if (__srefill(fp)) goto out; |
| 1057 | } |
| 1058 | |
| 1059 | // Read directly into the caller's buffer. |
| 1060 | while (total > 0) { |
| 1061 | ssize_t bytes_read = (*fp->_read)(fp->_cookie, dst, total); |
| 1062 | if (bytes_read <= 0) { |
| 1063 | fp->_flags |= (bytes_read == 0) ? __SEOF : __SERR; |
| 1064 | break; |
| 1065 | } |
| 1066 | dst += bytes_read; |
| 1067 | total -= bytes_read; |
| 1068 | } |
| 1069 | |
| 1070 | out: |
| 1071 | return ((desired_total - total) / size); |
| 1072 | } |
| 1073 | |
| 1074 | size_t fwrite(const void* buf, size_t size, size_t count, FILE* fp) { |
| 1075 | CHECK_FP(fp); |
| 1076 | ScopedFileLock sfl(fp); |
| 1077 | return fwrite_unlocked(buf, size, count, fp); |
| 1078 | } |
| 1079 | |
| 1080 | size_t fwrite_unlocked(const void* buf, size_t size, size_t count, FILE* fp) { |
| 1081 | CHECK_FP(fp); |
| 1082 | |
| 1083 | size_t n; |
| 1084 | if (__builtin_mul_overflow(size, count, &n)) { |
| 1085 | errno = EOVERFLOW; |
| 1086 | fp->_flags |= __SERR; |
| 1087 | return 0; |
| 1088 | } |
| 1089 | |
| 1090 | if (n == 0) return 0; |
| 1091 | |
| 1092 | __siov iov = { .iov_base = const_cast<void*>(buf), .iov_len = n }; |
| 1093 | __suio uio = { .uio_iov = &iov, .uio_iovcnt = 1, .uio_resid = n }; |
| 1094 | |
| 1095 | _SET_ORIENTATION(fp, -1); |
| 1096 | |
| 1097 | // The usual case is success (__sfvwrite returns 0); skip the divide if this happens, |
| 1098 | // since divides are generally slow. |
| 1099 | return (__sfvwrite(fp, &uio) == 0) ? count : ((n - uio.uio_resid) / size); |
| 1100 | } |
| 1101 | |
Dan Albert | 3037ea4 | 2016-10-06 15:46:45 -0700 | [diff] [blame] | 1102 | namespace { |
| 1103 | |
| 1104 | namespace phony { |
| 1105 | #include <bits/struct_file.h> |
| 1106 | } |
| 1107 | |
| 1108 | static_assert(sizeof(::__sFILE) == sizeof(phony::__sFILE), |
| 1109 | "size mismatch between `struct __sFILE` implementation and public stub"); |
| 1110 | static_assert(alignof(::__sFILE) == alignof(phony::__sFILE), |
| 1111 | "alignment mismatch between `struct __sFILE` implementation and public stub"); |
| 1112 | |
| 1113 | } |