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