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 | bb46afd | 2015-12-04 18:03:12 -0800 | [diff] [blame] | 94 | struct glue __sglue = { NULL, 3, __sF }; |
| 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)); |
| 119 | g->next = NULL; |
| 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); |
| 147 | for (g = &__sglue; g != NULL; 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); |
| 155 | if ((g = moreglue(NDYNAMIC)) == NULL) |
| 156 | return (NULL); |
| 157 | _THREAD_PRIVATE_MUTEX_LOCK(__sfp_mutex); |
| 158 | lastglue->next = g; |
| 159 | lastglue = g; |
| 160 | fp = g->iobs; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 161 | found: |
| 162 | fp->_flags = 1; /* reserve this slot; caller sets real flags */ |
Kenny Root | f582340 | 2011-02-12 07:13:44 -0800 | [diff] [blame] | 163 | _THREAD_PRIVATE_MUTEX_UNLOCK(__sfp_mutex); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 164 | fp->_p = NULL; /* no current pointer */ |
| 165 | fp->_w = 0; /* nothing to read or write */ |
| 166 | fp->_r = 0; |
| 167 | fp->_bf._base = NULL; /* no buffer */ |
| 168 | fp->_bf._size = 0; |
| 169 | fp->_lbfsize = 0; /* not line buffered */ |
| 170 | fp->_file = -1; /* no file */ |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 171 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 172 | fp->_lb._base = NULL; /* no line buffer */ |
| 173 | fp->_lb._size = 0; |
| 174 | _FILEEXT_INIT(fp); |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 175 | |
| 176 | // Caller sets cookie, _read/_write etc. |
| 177 | // We explicitly clear _seek and _seek64 to prevent subtle bugs. |
| 178 | fp->_seek = nullptr; |
| 179 | _EXT(fp)->_seek64 = nullptr; |
| 180 | |
| 181 | return fp; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 182 | } |
| 183 | |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 184 | extern "C" __LIBC_HIDDEN__ void __libc_stdio_cleanup(void) { |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 185 | // Equivalent to fflush(nullptr), but without all the locking since we're shutting down anyway. |
| 186 | _fwalk(__sflush); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 187 | } |
Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 188 | |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 189 | static FILE* __fopen(int fd, int flags) { |
| 190 | #if !defined(__LP64__) |
| 191 | if (fd > SHRT_MAX) { |
| 192 | errno = EMFILE; |
| 193 | return nullptr; |
| 194 | } |
| 195 | #endif |
| 196 | |
| 197 | FILE* fp = __sfp(); |
| 198 | if (fp != nullptr) { |
| 199 | fp->_file = fd; |
| 200 | fp->_flags = flags; |
| 201 | fp->_cookie = fp; |
| 202 | fp->_read = __sread; |
| 203 | fp->_write = __swrite; |
| 204 | fp->_close = __sclose; |
| 205 | _EXT(fp)->_seek64 = __sseek64; |
| 206 | } |
| 207 | return fp; |
| 208 | } |
| 209 | |
| 210 | FILE* fopen(const char* file, const char* mode) { |
Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 211 | int mode_flags; |
| 212 | int flags = __sflags(mode, &mode_flags); |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 213 | if (flags == 0) return nullptr; |
| 214 | |
Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 215 | int fd = open(file, mode_flags, DEFFILEMODE); |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 216 | if (fd == -1) { |
| 217 | return nullptr; |
| 218 | } |
| 219 | |
| 220 | FILE* fp = __fopen(fd, flags); |
| 221 | if (fp == nullptr) { |
| 222 | ErrnoRestorer errno_restorer; |
| 223 | close(fd); |
| 224 | return nullptr; |
| 225 | } |
| 226 | |
Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 227 | // For append mode, even though we use O_APPEND, we need to seek to the end now. |
| 228 | if ((mode_flags & O_APPEND) != 0) __sseek64(fp, 0, SEEK_END); |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 229 | return fp; |
| 230 | } |
Elliott Hughes | f226ee5 | 2016-02-03 11:24:28 -0800 | [diff] [blame] | 231 | __strong_alias(fopen64, fopen); |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 232 | |
| 233 | FILE* fdopen(int fd, const char* mode) { |
Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 234 | int mode_flags; |
| 235 | int flags = __sflags(mode, &mode_flags); |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 236 | if (flags == 0) return nullptr; |
| 237 | |
| 238 | // 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] | 239 | int fd_flags = fcntl(fd, F_GETFL, 0); |
| 240 | if (fd_flags == -1) return nullptr; |
| 241 | int tmp = fd_flags & O_ACCMODE; |
| 242 | if (tmp != O_RDWR && (tmp != (mode_flags & O_ACCMODE))) { |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 243 | errno = EINVAL; |
| 244 | return nullptr; |
| 245 | } |
| 246 | |
Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 247 | // Make sure O_APPEND is set on the underlying fd if our mode has 'a'. |
| 248 | // POSIX says we just take the current offset of the underlying fd. |
| 249 | if ((mode_flags & O_APPEND) && !(fd_flags & O_APPEND)) { |
| 250 | if (fcntl(fd, F_SETFL, fd_flags | O_APPEND) == -1) return nullptr; |
| 251 | } |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 252 | |
Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 253 | // Make sure O_CLOEXEC is set on the underlying fd if our mode has 'x'. |
| 254 | if ((mode_flags & O_CLOEXEC) && !((tmp = fcntl(fd, F_GETFD)) & FD_CLOEXEC)) { |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 255 | fcntl(fd, F_SETFD, tmp | FD_CLOEXEC); |
| 256 | } |
| 257 | |
| 258 | return __fopen(fd, flags); |
| 259 | } |
| 260 | |
| 261 | // Re-direct an existing, open (probably) file to some other file. |
| 262 | // ANSI is written such that the original file gets closed if at |
| 263 | // all possible, no matter what. |
| 264 | // TODO: rewrite this mess completely. |
| 265 | FILE* freopen(const char* file, const char* mode, FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame^] | 266 | CHECK_FP(fp); |
Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 267 | int mode_flags; |
| 268 | int flags = __sflags(mode, &mode_flags); |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 269 | if (flags == 0) { |
| 270 | fclose(fp); |
| 271 | return nullptr; |
| 272 | } |
| 273 | |
| 274 | ScopedFileLock sfl(fp); |
| 275 | |
| 276 | // There are actually programs that depend on being able to "freopen" |
| 277 | // descriptors that weren't originally open. Keep this from breaking. |
| 278 | // Remember whether the stream was open to begin with, and which file |
| 279 | // descriptor (if any) was associated with it. If it was attached to |
| 280 | // a descriptor, defer closing it; freopen("/dev/stdin", "r", stdin) |
| 281 | // should work. This is unnecessary if it was not a Unix file. |
| 282 | int isopen, wantfd; |
| 283 | if (fp->_flags == 0) { |
| 284 | fp->_flags = __SEOF; // Hold on to it. |
| 285 | isopen = 0; |
| 286 | wantfd = -1; |
| 287 | } else { |
| 288 | // Flush the stream; ANSI doesn't require this. |
| 289 | if (fp->_flags & __SWR) __sflush(fp); |
| 290 | |
| 291 | // If close is NULL, closing is a no-op, hence pointless. |
| 292 | isopen = fp->_close != NULL; |
| 293 | if ((wantfd = fp->_file) < 0 && isopen) { |
| 294 | (*fp->_close)(fp->_cookie); |
| 295 | isopen = 0; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | // Get a new descriptor to refer to the new file. |
Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 300 | int fd = open(file, mode_flags, DEFFILEMODE); |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 301 | if (fd < 0 && isopen) { |
| 302 | // If out of fd's close the old one and try again. |
| 303 | if (errno == ENFILE || errno == EMFILE) { |
| 304 | (*fp->_close)(fp->_cookie); |
| 305 | isopen = 0; |
Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 306 | fd = open(file, mode_flags, DEFFILEMODE); |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
| 310 | int sverrno = errno; |
| 311 | |
| 312 | // Finish closing fp. Even if the open succeeded above, we cannot |
| 313 | // keep fp->_base: it may be the wrong size. This loses the effect |
| 314 | // of any setbuffer calls, but stdio has always done this before. |
| 315 | if (isopen && fd != wantfd) (*fp->_close)(fp->_cookie); |
| 316 | if (fp->_flags & __SMBF) free(fp->_bf._base); |
| 317 | fp->_w = 0; |
| 318 | fp->_r = 0; |
| 319 | fp->_p = NULL; |
| 320 | fp->_bf._base = NULL; |
| 321 | fp->_bf._size = 0; |
| 322 | fp->_lbfsize = 0; |
| 323 | if (HASUB(fp)) FREEUB(fp); |
| 324 | _UB(fp)._size = 0; |
| 325 | WCIO_FREE(fp); |
Elliott Hughes | 80e4c15 | 2017-07-21 13:57:55 -0700 | [diff] [blame] | 326 | free_fgetln_buffer(fp); |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 327 | fp->_lb._size = 0; |
| 328 | |
| 329 | if (fd < 0) { // Did not get it after all. |
| 330 | fp->_flags = 0; // Release. |
| 331 | errno = sverrno; // Restore errno in case _close clobbered it. |
| 332 | return nullptr; |
| 333 | } |
| 334 | |
| 335 | // If reopening something that was open before on a real file, try |
| 336 | // to maintain the descriptor. Various C library routines (perror) |
| 337 | // assume stderr is always fd STDERR_FILENO, even if being freopen'd. |
| 338 | if (wantfd >= 0 && fd != wantfd) { |
Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 339 | if (dup3(fd, wantfd, mode_flags & O_CLOEXEC) >= 0) { |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 340 | close(fd); |
| 341 | fd = wantfd; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | // _file is only a short. |
| 346 | if (fd > SHRT_MAX) { |
| 347 | fp->_flags = 0; // Release. |
| 348 | errno = EMFILE; |
| 349 | return nullptr; |
| 350 | } |
| 351 | |
| 352 | fp->_flags = flags; |
| 353 | fp->_file = fd; |
| 354 | fp->_cookie = fp; |
| 355 | fp->_read = __sread; |
| 356 | fp->_write = __swrite; |
| 357 | fp->_close = __sclose; |
| 358 | _EXT(fp)->_seek64 = __sseek64; |
| 359 | |
Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 360 | // For append mode, even though we use O_APPEND, we need to seek to the end now. |
| 361 | if ((mode_flags & O_APPEND) != 0) __sseek64(fp, 0, SEEK_END); |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 362 | return fp; |
| 363 | } |
Elliott Hughes | f226ee5 | 2016-02-03 11:24:28 -0800 | [diff] [blame] | 364 | __strong_alias(freopen64, freopen); |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 365 | |
Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 366 | int fclose(FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame^] | 367 | CHECK_FP(fp); |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 368 | if (fp->_flags == 0) { |
| 369 | // Already freed! |
| 370 | errno = EBADF; |
| 371 | return EOF; |
| 372 | } |
Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 373 | |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 374 | ScopedFileLock sfl(fp); |
| 375 | WCIO_FREE(fp); |
| 376 | int r = fp->_flags & __SWR ? __sflush(fp) : 0; |
| 377 | if (fp->_close != NULL && (*fp->_close)(fp->_cookie) < 0) { |
| 378 | r = EOF; |
| 379 | } |
| 380 | if (fp->_flags & __SMBF) free(fp->_bf._base); |
| 381 | if (HASUB(fp)) FREEUB(fp); |
Elliott Hughes | 80e4c15 | 2017-07-21 13:57:55 -0700 | [diff] [blame] | 382 | free_fgetln_buffer(fp); |
Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 383 | |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 384 | // Poison this FILE so accesses after fclose will be obvious. |
| 385 | fp->_file = -1; |
| 386 | fp->_r = fp->_w = 0; |
Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 387 | |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 388 | // Release this FILE for reuse. |
| 389 | fp->_flags = 0; |
| 390 | return r; |
Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 391 | } |
| 392 | |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 393 | int fileno_unlocked(FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame^] | 394 | CHECK_FP(fp); |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 395 | int fd = fp->_file; |
| 396 | if (fd == -1) { |
| 397 | errno = EBADF; |
| 398 | return -1; |
| 399 | } |
| 400 | return fd; |
| 401 | } |
| 402 | |
Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 403 | int fileno(FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame^] | 404 | CHECK_FP(fp); |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 405 | ScopedFileLock sfl(fp); |
| 406 | return fileno_unlocked(fp); |
Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 407 | } |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 408 | |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 409 | void clearerr_unlocked(FILE* fp) { |
| 410 | return __sclearerr(fp); |
| 411 | } |
| 412 | |
| 413 | void clearerr(FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame^] | 414 | CHECK_FP(fp); |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 415 | ScopedFileLock sfl(fp); |
| 416 | clearerr_unlocked(fp); |
| 417 | } |
| 418 | |
| 419 | int feof_unlocked(FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame^] | 420 | CHECK_FP(fp); |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 421 | return ((fp->_flags & __SEOF) != 0); |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | int feof(FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame^] | 425 | CHECK_FP(fp); |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 426 | ScopedFileLock sfl(fp); |
| 427 | return feof_unlocked(fp); |
| 428 | } |
| 429 | |
| 430 | int ferror_unlocked(FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame^] | 431 | CHECK_FP(fp); |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 432 | return __sferror(fp); |
| 433 | } |
| 434 | |
| 435 | int ferror(FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame^] | 436 | CHECK_FP(fp); |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 437 | ScopedFileLock sfl(fp); |
| 438 | return ferror_unlocked(fp); |
| 439 | } |
| 440 | |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 441 | int __sread(void* cookie, char* buf, int n) { |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 442 | FILE* fp = reinterpret_cast<FILE*>(cookie); |
| 443 | return TEMP_FAILURE_RETRY(read(fp->_file, buf, n)); |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | int __swrite(void* cookie, const char* buf, int n) { |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 447 | FILE* fp = reinterpret_cast<FILE*>(cookie); |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 448 | return TEMP_FAILURE_RETRY(write(fp->_file, buf, n)); |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 449 | } |
| 450 | |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 451 | fpos_t __sseek(void* cookie, fpos_t offset, int whence) { |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 452 | FILE* fp = reinterpret_cast<FILE*>(cookie); |
| 453 | return TEMP_FAILURE_RETRY(lseek(fp->_file, offset, whence)); |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 454 | } |
| 455 | |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 456 | off64_t __sseek64(void* cookie, off64_t offset, int whence) { |
| 457 | FILE* fp = reinterpret_cast<FILE*>(cookie); |
| 458 | return TEMP_FAILURE_RETRY(lseek64(fp->_file, offset, whence)); |
| 459 | } |
| 460 | |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 461 | int __sclose(void* cookie) { |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 462 | FILE* fp = reinterpret_cast<FILE*>(cookie); |
| 463 | return close(fp->_file); |
| 464 | } |
| 465 | |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 466 | static off64_t __seek_unlocked(FILE* fp, off64_t offset, int whence) { |
| 467 | // Use `_seek64` if set, but fall back to `_seek`. |
| 468 | if (_EXT(fp)->_seek64 != nullptr) { |
| 469 | return (*_EXT(fp)->_seek64)(fp->_cookie, offset, whence); |
| 470 | } else if (fp->_seek != nullptr) { |
Elliott Hughes | 955426e | 2016-01-26 18:25:52 -0800 | [diff] [blame] | 471 | off64_t result = (*fp->_seek)(fp->_cookie, offset, whence); |
| 472 | #if !defined(__LP64__) |
| 473 | // Avoid sign extension if off64_t is larger than off_t. |
| 474 | if (result != -1) result &= 0xffffffff; |
| 475 | #endif |
| 476 | return result; |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 477 | } else { |
| 478 | errno = ESPIPE; |
| 479 | return -1; |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 480 | } |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 481 | } |
| 482 | |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 483 | static off64_t __ftello64_unlocked(FILE* fp) { |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 484 | // Find offset of underlying I/O object, then adjust for buffered bytes. |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 485 | __sflush(fp); // May adjust seek offset on append stream. |
Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 486 | |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 487 | off64_t result = __seek_unlocked(fp, 0, SEEK_CUR); |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 488 | if (result == -1) { |
| 489 | return -1; |
| 490 | } |
| 491 | |
| 492 | if (fp->_flags & __SRD) { |
| 493 | // Reading. Any unread characters (including |
| 494 | // those from ungetc) cause the position to be |
| 495 | // smaller than that in the underlying object. |
| 496 | result -= fp->_r; |
| 497 | if (HASUB(fp)) result -= fp->_ur; |
| 498 | } else if (fp->_flags & __SWR && fp->_p != NULL) { |
| 499 | // Writing. Any buffered characters cause the |
| 500 | // position to be greater than that in the |
| 501 | // underlying object. |
| 502 | result += fp->_p - fp->_bf._base; |
| 503 | } |
| 504 | return result; |
| 505 | } |
| 506 | |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 507 | 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] | 508 | ScopedFileLock sfl(fp); |
| 509 | |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 510 | // Change any SEEK_CUR to SEEK_SET, and check `whence` argument. |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 511 | // After this, whence is either SEEK_SET or SEEK_END. |
| 512 | if (whence == SEEK_CUR) { |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 513 | fpos64_t current_offset = __ftello64_unlocked(fp); |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 514 | if (current_offset == -1) { |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 515 | return -1; |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 516 | } |
| 517 | offset += current_offset; |
| 518 | whence = SEEK_SET; |
| 519 | } else if (whence != SEEK_SET && whence != SEEK_END) { |
| 520 | errno = EINVAL; |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 521 | return -1; |
| 522 | } |
| 523 | |
| 524 | // If our caller has a 32-bit interface, refuse to go past a 32-bit file offset. |
| 525 | if (off_t_bits == 32 && offset > LONG_MAX) { |
| 526 | errno = EOVERFLOW; |
| 527 | return -1; |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | if (fp->_bf._base == NULL) __smakebuf(fp); |
| 531 | |
| 532 | // Flush unwritten data and attempt the seek. |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 533 | if (__sflush(fp) || __seek_unlocked(fp, offset, whence) == -1) { |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 534 | return -1; |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | // Success: clear EOF indicator and discard ungetc() data. |
| 538 | if (HASUB(fp)) FREEUB(fp); |
| 539 | fp->_p = fp->_bf._base; |
| 540 | fp->_r = 0; |
| 541 | /* fp->_w = 0; */ /* unnecessary (I think...) */ |
| 542 | fp->_flags &= ~__SEOF; |
| 543 | return 0; |
| 544 | } |
| 545 | |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 546 | int fseeko(FILE* fp, off_t offset, int whence) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame^] | 547 | CHECK_FP(fp); |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 548 | static_assert(sizeof(off_t) == sizeof(long), "sizeof(off_t) != sizeof(long)"); |
| 549 | return __fseeko64(fp, offset, whence, 8*sizeof(off_t)); |
| 550 | } |
| 551 | __strong_alias(fseek, fseeko); |
| 552 | |
| 553 | int fseeko64(FILE* fp, off64_t offset, int whence) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame^] | 554 | CHECK_FP(fp); |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 555 | return __fseeko64(fp, offset, whence, 8*sizeof(off_t)); |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 556 | } |
| 557 | |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 558 | int fsetpos(FILE* fp, const fpos_t* pos) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame^] | 559 | CHECK_FP(fp); |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 560 | return fseeko(fp, *pos, SEEK_SET); |
| 561 | } |
| 562 | |
| 563 | int fsetpos64(FILE* fp, const fpos64_t* pos) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame^] | 564 | CHECK_FP(fp); |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 565 | return fseeko64(fp, *pos, SEEK_SET); |
| 566 | } |
| 567 | |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 568 | off_t ftello(FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame^] | 569 | CHECK_FP(fp); |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 570 | static_assert(sizeof(off_t) == sizeof(long), "sizeof(off_t) != sizeof(long)"); |
| 571 | off64_t result = ftello64(fp); |
| 572 | if (result > LONG_MAX) { |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 573 | errno = EOVERFLOW; |
| 574 | return -1; |
| 575 | } |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 576 | return result; |
| 577 | } |
| 578 | __strong_alias(ftell, ftello); |
| 579 | |
| 580 | off64_t ftello64(FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame^] | 581 | CHECK_FP(fp); |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 582 | ScopedFileLock sfl(fp); |
| 583 | return __ftello64_unlocked(fp); |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 584 | } |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 585 | |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 586 | int fgetpos(FILE* fp, fpos_t* pos) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame^] | 587 | CHECK_FP(fp); |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 588 | *pos = ftello(fp); |
Elliott Hughes | 955426e | 2016-01-26 18:25:52 -0800 | [diff] [blame] | 589 | return (*pos == -1) ? -1 : 0; |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 590 | } |
| 591 | |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 592 | int fgetpos64(FILE* fp, 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 | *pos = ftello64(fp); |
Elliott Hughes | 955426e | 2016-01-26 18:25:52 -0800 | [diff] [blame] | 595 | return (*pos == -1) ? -1 : 0; |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 596 | } |
Elliott Hughes | 03e65eb | 2016-01-26 14:13:04 -0800 | [diff] [blame] | 597 | |
| 598 | static FILE* __funopen(const void* cookie, |
| 599 | int (*read_fn)(void*, char*, int), |
| 600 | int (*write_fn)(void*, const char*, int), |
| 601 | int (*close_fn)(void*)) { |
| 602 | if (read_fn == nullptr && write_fn == nullptr) { |
| 603 | errno = EINVAL; |
| 604 | return nullptr; |
| 605 | } |
| 606 | |
| 607 | FILE* fp = __sfp(); |
| 608 | if (fp == nullptr) return nullptr; |
| 609 | |
| 610 | if (read_fn != nullptr && write_fn != nullptr) { |
| 611 | fp->_flags = __SRW; |
| 612 | } else if (read_fn != nullptr) { |
| 613 | fp->_flags = __SRD; |
| 614 | } else if (write_fn != nullptr) { |
| 615 | fp->_flags = __SWR; |
| 616 | } |
| 617 | |
| 618 | fp->_file = -1; |
| 619 | fp->_cookie = const_cast<void*>(cookie); // The funopen(3) API is incoherent. |
| 620 | fp->_read = read_fn; |
| 621 | fp->_write = write_fn; |
| 622 | fp->_close = close_fn; |
| 623 | |
| 624 | return fp; |
| 625 | } |
| 626 | |
| 627 | FILE* funopen(const void* cookie, |
| 628 | int (*read_fn)(void*, char*, int), |
| 629 | int (*write_fn)(void*, const char*, int), |
| 630 | fpos_t (*seek_fn)(void*, fpos_t, int), |
| 631 | int (*close_fn)(void*)) { |
| 632 | FILE* fp = __funopen(cookie, read_fn, write_fn, close_fn); |
| 633 | if (fp != nullptr) { |
| 634 | fp->_seek = seek_fn; |
| 635 | } |
| 636 | return fp; |
| 637 | } |
| 638 | |
| 639 | FILE* funopen64(const void* cookie, |
| 640 | int (*read_fn)(void*, char*, int), |
| 641 | int (*write_fn)(void*, const char*, int), |
| 642 | fpos64_t (*seek_fn)(void*, fpos64_t, int), |
| 643 | int (*close_fn)(void*)) { |
| 644 | FILE* fp = __funopen(cookie, read_fn, write_fn, close_fn); |
| 645 | if (fp != nullptr) { |
| 646 | _EXT(fp)->_seek64 = seek_fn; |
| 647 | } |
| 648 | return fp; |
| 649 | } |
Elliott Hughes | 20788ae | 2016-06-09 15:16:32 -0700 | [diff] [blame] | 650 | |
Elliott Hughes | 53cf348 | 2016-08-09 13:06:41 -0700 | [diff] [blame] | 651 | int asprintf(char** s, const char* fmt, ...) { |
| 652 | PRINTF_IMPL(vasprintf(s, fmt, ap)); |
| 653 | } |
| 654 | |
Elliott Hughes | 20788ae | 2016-06-09 15:16:32 -0700 | [diff] [blame] | 655 | char* ctermid(char* s) { |
| 656 | return s ? strcpy(s, _PATH_TTY) : const_cast<char*>(_PATH_TTY); |
| 657 | } |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 658 | |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 659 | int dprintf(int fd, const char* fmt, ...) { |
| 660 | PRINTF_IMPL(vdprintf(fd, fmt, ap)); |
| 661 | } |
| 662 | |
| 663 | int fprintf(FILE* fp, const char* fmt, ...) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame^] | 664 | CHECK_FP(fp); |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 665 | PRINTF_IMPL(vfprintf(fp, fmt, ap)); |
| 666 | } |
| 667 | |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 668 | int fgetc(FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame^] | 669 | CHECK_FP(fp); |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 670 | return getc(fp); |
| 671 | } |
| 672 | |
| 673 | int fputc(int c, FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame^] | 674 | CHECK_FP(fp); |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 675 | return putc(c, fp); |
| 676 | } |
| 677 | |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 678 | int fscanf(FILE* fp, const char* fmt, ...) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame^] | 679 | CHECK_FP(fp); |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 680 | PRINTF_IMPL(vfscanf(fp, fmt, ap)); |
| 681 | } |
| 682 | |
| 683 | int fwprintf(FILE* fp, const wchar_t* fmt, ...) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame^] | 684 | CHECK_FP(fp); |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 685 | PRINTF_IMPL(vfwprintf(fp, fmt, ap)); |
| 686 | } |
| 687 | |
| 688 | int fwscanf(FILE* fp, const wchar_t* fmt, ...) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame^] | 689 | CHECK_FP(fp); |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 690 | PRINTF_IMPL(vfwscanf(fp, fmt, ap)); |
| 691 | } |
| 692 | |
| 693 | int getc(FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame^] | 694 | CHECK_FP(fp); |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 695 | ScopedFileLock sfl(fp); |
| 696 | return getc_unlocked(fp); |
| 697 | } |
| 698 | |
| 699 | int getc_unlocked(FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame^] | 700 | CHECK_FP(fp); |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 701 | return __sgetc(fp); |
| 702 | } |
| 703 | |
| 704 | int getchar_unlocked() { |
| 705 | return getc_unlocked(stdin); |
| 706 | } |
| 707 | |
| 708 | int getchar() { |
| 709 | return getc(stdin); |
| 710 | } |
| 711 | |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 712 | ssize_t getline(char** buf, size_t* len, FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame^] | 713 | CHECK_FP(fp); |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 714 | return getdelim(buf, len, '\n', fp); |
| 715 | } |
| 716 | |
| 717 | wint_t getwc(FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame^] | 718 | CHECK_FP(fp); |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 719 | return fgetwc(fp); |
| 720 | } |
| 721 | |
| 722 | wint_t getwchar() { |
| 723 | return fgetwc(stdin); |
| 724 | } |
| 725 | |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 726 | int printf(const char* fmt, ...) { |
| 727 | PRINTF_IMPL(vfprintf(stdout, fmt, ap)); |
| 728 | } |
| 729 | |
| 730 | int putc(int c, FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame^] | 731 | CHECK_FP(fp); |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 732 | ScopedFileLock sfl(fp); |
| 733 | return putc_unlocked(c, fp); |
| 734 | } |
| 735 | |
| 736 | int putc_unlocked(int c, FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame^] | 737 | CHECK_FP(fp); |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 738 | if (cantwrite(fp)) { |
| 739 | errno = EBADF; |
| 740 | return EOF; |
| 741 | } |
| 742 | _SET_ORIENTATION(fp, -1); |
| 743 | if (--fp->_w >= 0 || (fp->_w >= fp->_lbfsize && c != '\n')) { |
| 744 | return (*fp->_p++ = c); |
| 745 | } |
| 746 | return (__swbuf(c, fp)); |
| 747 | } |
| 748 | |
| 749 | int putchar(int c) { |
| 750 | return putc(c, stdout); |
| 751 | } |
| 752 | |
| 753 | int putchar_unlocked(int c) { |
| 754 | return putc_unlocked(c, stdout); |
| 755 | } |
| 756 | |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 757 | wint_t putwc(wchar_t wc, FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame^] | 758 | CHECK_FP(fp); |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 759 | return fputwc(wc, fp); |
| 760 | } |
| 761 | |
| 762 | wint_t putwchar(wchar_t wc) { |
| 763 | return fputwc(wc, stdout); |
| 764 | } |
| 765 | |
Elliott Hughes | d1f25a7 | 2016-08-05 15:53:03 -0700 | [diff] [blame] | 766 | int remove(const char* path) { |
| 767 | if (unlink(path) != -1) return 0; |
| 768 | if (errno != EISDIR) return -1; |
| 769 | return rmdir(path); |
| 770 | } |
| 771 | |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 772 | void rewind(FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame^] | 773 | CHECK_FP(fp); |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 774 | ScopedFileLock sfl(fp); |
| 775 | fseek(fp, 0, SEEK_SET); |
| 776 | clearerr_unlocked(fp); |
| 777 | } |
| 778 | |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 779 | int scanf(const char* fmt, ...) { |
| 780 | PRINTF_IMPL(vfscanf(stdin, fmt, ap)); |
| 781 | } |
| 782 | |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 783 | void setbuf(FILE* fp, char* buf) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame^] | 784 | CHECK_FP(fp); |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 785 | setbuffer(fp, buf, BUFSIZ); |
| 786 | } |
| 787 | |
| 788 | void setbuffer(FILE* fp, char* buf, int size) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame^] | 789 | CHECK_FP(fp); |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 790 | setvbuf(fp, buf, buf ? _IOFBF : _IONBF, size); |
| 791 | } |
| 792 | |
| 793 | int setlinebuf(FILE* fp) { |
Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame^] | 794 | CHECK_FP(fp); |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 795 | return setvbuf(fp, nullptr, _IOLBF, 0); |
| 796 | } |
| 797 | |
Elliott Hughes | 53cf348 | 2016-08-09 13:06:41 -0700 | [diff] [blame] | 798 | int snprintf(char* s, size_t n, const char* fmt, ...) { |
| 799 | PRINTF_IMPL(vsnprintf(s, n, fmt, ap)); |
| 800 | } |
| 801 | |
| 802 | int sprintf(char* s, const char* fmt, ...) { |
Elliott Hughes | fb3873d | 2016-08-10 11:07:54 -0700 | [diff] [blame] | 803 | PRINTF_IMPL(vsprintf(s, fmt, ap)); |
Elliott Hughes | 53cf348 | 2016-08-09 13:06:41 -0700 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | int sscanf(const char* s, const char* fmt, ...) { |
| 807 | PRINTF_IMPL(vsscanf(s, fmt, ap)); |
| 808 | } |
| 809 | |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 810 | int swprintf(wchar_t* s, size_t n, const wchar_t* fmt, ...) { |
| 811 | PRINTF_IMPL(vswprintf(s, n, fmt, ap)); |
| 812 | } |
| 813 | |
| 814 | int swscanf(const wchar_t* s, const wchar_t* fmt, ...) { |
| 815 | PRINTF_IMPL(vswscanf(s, fmt, ap)); |
| 816 | } |
| 817 | |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 818 | int vprintf(const char* fmt, va_list ap) { |
| 819 | return vfprintf(stdout, fmt, ap); |
| 820 | } |
| 821 | |
| 822 | int vscanf(const char* fmt, va_list ap) { |
| 823 | return vfscanf(stdin, fmt, ap); |
| 824 | } |
| 825 | |
Elliott Hughes | fb3873d | 2016-08-10 11:07:54 -0700 | [diff] [blame] | 826 | int vsnprintf(char* s, size_t n, const char* fmt, va_list ap) { |
| 827 | // stdio internals use int rather than size_t. |
| 828 | static_assert(INT_MAX <= SSIZE_MAX, "SSIZE_MAX too large to fit in int"); |
| 829 | |
| 830 | __check_count("vsnprintf", "size", n); |
| 831 | |
| 832 | // Stdio internals do not deal correctly with zero length buffer. |
| 833 | char dummy; |
| 834 | if (n == 0) { |
| 835 | s = &dummy; |
| 836 | n = 1; |
| 837 | } |
| 838 | |
| 839 | FILE f; |
| 840 | __sfileext fext; |
| 841 | _FILEEXT_SETUP(&f, &fext); |
| 842 | f._file = -1; |
| 843 | f._flags = __SWR | __SSTR; |
| 844 | f._bf._base = f._p = reinterpret_cast<unsigned char*>(s); |
| 845 | f._bf._size = f._w = n - 1; |
| 846 | |
| 847 | int result = __vfprintf(&f, fmt, ap); |
| 848 | *f._p = '\0'; |
| 849 | return result; |
| 850 | } |
| 851 | |
Elliott Hughes | 53cf348 | 2016-08-09 13:06:41 -0700 | [diff] [blame] | 852 | int vsprintf(char* s, const char* fmt, va_list ap) { |
Elliott Hughes | fb3873d | 2016-08-10 11:07:54 -0700 | [diff] [blame] | 853 | return vsnprintf(s, SSIZE_MAX, fmt, ap); |
Elliott Hughes | 53cf348 | 2016-08-09 13:06:41 -0700 | [diff] [blame] | 854 | } |
| 855 | |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 856 | int vwprintf(const wchar_t* fmt, va_list ap) { |
| 857 | return vfwprintf(stdout, fmt, ap); |
| 858 | } |
| 859 | |
| 860 | int vwscanf(const wchar_t* fmt, va_list ap) { |
| 861 | return vfwscanf(stdin, fmt, ap); |
| 862 | } |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 863 | |
| 864 | int wprintf(const wchar_t* fmt, ...) { |
| 865 | PRINTF_IMPL(vfwprintf(stdout, fmt, ap)); |
| 866 | } |
| 867 | |
| 868 | int wscanf(const wchar_t* fmt, ...) { |
| 869 | PRINTF_IMPL(vfwscanf(stdin, fmt, ap)); |
| 870 | } |
Dan Albert | 3037ea4 | 2016-10-06 15:46:45 -0700 | [diff] [blame] | 871 | |
| 872 | namespace { |
| 873 | |
| 874 | namespace phony { |
| 875 | #include <bits/struct_file.h> |
| 876 | } |
| 877 | |
| 878 | static_assert(sizeof(::__sFILE) == sizeof(phony::__sFILE), |
| 879 | "size mismatch between `struct __sFILE` implementation and public stub"); |
| 880 | static_assert(alignof(::__sFILE) == alignof(phony::__sFILE), |
| 881 | "alignment mismatch between `struct __sFILE` implementation and public stub"); |
| 882 | |
| 883 | } |