| 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 | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 49 | #include <android/fdsan.h> |
| 50 | |
| Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 51 | #include <async_safe/log.h> |
| 52 | |
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 53 | #include "glue.h" |
| Elliott Hughes | f9cfecf | 2021-02-04 16:58:13 -0800 | [diff] [blame] | 54 | #include "local.h" |
| 55 | #include "private/ErrnoRestorer.h" |
| 56 | #include "private/FdPath.h" |
| Elliott Hughes | 886370c | 2019-03-21 21:11:41 -0700 | [diff] [blame] | 57 | #include "private/__bionic_get_shell_path.h" |
| Elliott Hughes | fb3873d | 2016-08-10 11:07:54 -0700 | [diff] [blame] | 58 | #include "private/bionic_fortify.h" |
| Elliott Hughes | 6a03abc | 2014-11-03 12:32:17 -0800 | [diff] [blame] | 59 | |
| Elliott Hughes | a07d9da | 2025-09-09 07:43:44 -0700 | [diff] [blame] | 60 | // Check a FILE* isn't nullptr, so we can emit a clear diagnostic message |
| 61 | // instead of just crashing with SIGSEGV. |
| 62 | #define CHECK_FP(fp) \ |
| 63 | if (fp == nullptr) __fortify_fatal("%s: null FILE*", __FUNCTION__) |
| 64 | |
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 65 | #define NDYNAMIC 10 /* add ten more whenever necessary */ |
| 66 | |
| Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 67 | #define PRINTF_IMPL(expr) \ |
| 68 | va_list ap; \ |
| 69 | va_start(ap, fmt); \ |
| 70 | int result = (expr); \ |
| 71 | va_end(ap); \ |
| 72 | return result; |
| 73 | |
| Elliott Hughes | 468efc8 | 2018-07-10 14:39:49 -0700 | [diff] [blame] | 74 | #define MAKE_STD_STREAM(flags, fd) \ |
| 75 | { \ |
| 76 | ._flags = flags, ._file = fd, ._cookie = __sF + fd, ._close = __sclose, \ |
| 77 | ._read = __sread, ._write = __swrite, ._ext = { \ |
| 78 | ._base = reinterpret_cast<uint8_t*>(__sFext + fd) \ |
| 79 | } \ |
| 80 | } |
| Elliott Hughes | 29ee639 | 2015-12-07 11:07:15 -0800 | [diff] [blame] | 81 | |
| Elliott Hughes | bb46afd | 2015-12-04 18:03:12 -0800 | [diff] [blame] | 82 | static struct __sfileext __sFext[3] = { |
| Elliott Hughes | 468efc8 | 2018-07-10 14:39:49 -0700 | [diff] [blame] | 83 | {._lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP, |
| 84 | ._caller_handles_locking = false, |
| 85 | ._seek64 = __sseek64, |
| 86 | ._popen_pid = 0}, |
| 87 | {._lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP, |
| 88 | ._caller_handles_locking = false, |
| 89 | ._seek64 = __sseek64, |
| 90 | ._popen_pid = 0}, |
| 91 | {._lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP, |
| 92 | ._caller_handles_locking = false, |
| 93 | ._seek64 = __sseek64, |
| 94 | ._popen_pid = 0}, |
| Elliott Hughes | bb46afd | 2015-12-04 18:03:12 -0800 | [diff] [blame] | 95 | }; |
| Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 96 | |
| 97 | // __sF is exported for backwards compatibility. Until M, we didn't have symbols |
| 98 | // for stdin/stdout/stderr; they were macros accessing __sF. |
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 99 | FILE __sF[3] = { |
| Elliott Hughes | 468efc8 | 2018-07-10 14:39:49 -0700 | [diff] [blame] | 100 | MAKE_STD_STREAM(__SRD, STDIN_FILENO), |
| 101 | MAKE_STD_STREAM(__SWR, STDOUT_FILENO), |
| 102 | MAKE_STD_STREAM(__SWR|__SNBF, STDERR_FILENO), |
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 103 | }; |
| Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 104 | |
| Elliott Hughes | 168667c | 2014-11-14 14:42:59 -0800 | [diff] [blame] | 105 | FILE* stdin = &__sF[0]; |
| 106 | FILE* stdout = &__sF[1]; |
| 107 | FILE* stderr = &__sF[2]; |
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 108 | |
| Ryan Prichard | c485cdb | 2019-04-30 14:47:34 -0700 | [diff] [blame] | 109 | static pthread_mutex_t __stdio_mutex = PTHREAD_MUTEX_INITIALIZER; |
| Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 110 | |
| 111 | static uint64_t __get_file_tag(FILE* fp) { |
| 112 | // Don't use a tag for the standard streams. |
| 113 | // They don't really own their file descriptors, because the values are well-known, and you're |
| 114 | // allowed to do things like `close(STDIN_FILENO); open("foo", O_RDONLY)` when single-threaded. |
| 115 | if (fp == stdin || fp == stderr || fp == stdout) { |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | return android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_FILE, |
| 120 | reinterpret_cast<uint64_t>(fp)); |
| 121 | } |
| 122 | |
| Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame] | 123 | struct glue __sglue = { nullptr, 3, __sF }; |
| Elliott Hughes | bb46afd | 2015-12-04 18:03:12 -0800 | [diff] [blame] | 124 | static struct glue* lastglue = &__sglue; |
| 125 | |
| Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 126 | class ScopedFileLock { |
| 127 | public: |
| Chih-Hung Hsieh | 62e3a07 | 2016-05-03 12:08:05 -0700 | [diff] [blame] | 128 | explicit ScopedFileLock(FILE* fp) : fp_(fp) { |
| Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 129 | FLOCKFILE(fp_); |
| 130 | } |
| 131 | ~ScopedFileLock() { |
| 132 | FUNLOCKFILE(fp_); |
| 133 | } |
| 134 | |
| 135 | private: |
| 136 | FILE* fp_; |
| 137 | }; |
| 138 | |
| Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 139 | static glue* moreglue(int n) { |
| Elliott Hughes | e118778 | 2024-10-16 17:50:45 +0000 | [diff] [blame] | 140 | char* data = new char[sizeof(glue) + |
| 141 | alignof(FILE) + n * sizeof(FILE) + |
| 142 | alignof(__sfileext) + n * sizeof(__sfileext)]; |
| Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 143 | if (data == nullptr) return nullptr; |
| Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 144 | |
| Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 145 | glue* g = reinterpret_cast<glue*>(data); |
| Elliott Hughes | e118778 | 2024-10-16 17:50:45 +0000 | [diff] [blame] | 146 | FILE* p = reinterpret_cast<FILE*>(__builtin_align_up(g + 1, alignof(FILE))); |
| 147 | __sfileext* pext = reinterpret_cast<__sfileext*>(__builtin_align_up(p + n, alignof(__sfileext))); |
| Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame] | 148 | g->next = nullptr; |
| Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 149 | g->niobs = n; |
| 150 | g->iobs = p; |
| 151 | while (--n >= 0) { |
| Elliott Hughes | 468efc8 | 2018-07-10 14:39:49 -0700 | [diff] [blame] | 152 | *p = {}; |
| Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 153 | _FILEEXT_SETUP(p, pext); |
| 154 | p++; |
| 155 | pext++; |
| 156 | } |
| 157 | return g; |
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| Elliott Hughes | 80e4c15 | 2017-07-21 13:57:55 -0700 | [diff] [blame] | 160 | static inline void free_fgetln_buffer(FILE* fp) { |
| 161 | if (__predict_false(fp->_lb._base != nullptr)) { |
| 162 | free(fp->_lb._base); |
| 163 | fp->_lb._base = nullptr; |
| 164 | } |
| 165 | } |
| 166 | |
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 167 | /* |
| 168 | * Find a free FILE for fopen et al. |
| 169 | */ |
| Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 170 | FILE* __sfp(void) { |
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 171 | FILE *fp; |
| 172 | int n; |
| 173 | struct glue *g; |
| 174 | |
| Elliott Hughes | 468efc8 | 2018-07-10 14:39:49 -0700 | [diff] [blame] | 175 | pthread_mutex_lock(&__stdio_mutex); |
| Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame] | 176 | for (g = &__sglue; g != nullptr; g = g->next) { |
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 177 | for (fp = g->iobs, n = g->niobs; --n >= 0; fp++) |
| 178 | if (fp->_flags == 0) |
| 179 | goto found; |
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 180 | } |
| Kenny Root | f582340 | 2011-02-12 07:13:44 -0800 | [diff] [blame] | 181 | |
| 182 | /* release lock while mallocing */ |
| Elliott Hughes | 468efc8 | 2018-07-10 14:39:49 -0700 | [diff] [blame] | 183 | pthread_mutex_unlock(&__stdio_mutex); |
| Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame] | 184 | if ((g = moreglue(NDYNAMIC)) == nullptr) return nullptr; |
| Elliott Hughes | 468efc8 | 2018-07-10 14:39:49 -0700 | [diff] [blame] | 185 | pthread_mutex_lock(&__stdio_mutex); |
| Kenny Root | f582340 | 2011-02-12 07:13:44 -0800 | [diff] [blame] | 186 | lastglue->next = g; |
| 187 | lastglue = g; |
| 188 | fp = g->iobs; |
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 189 | found: |
| 190 | fp->_flags = 1; /* reserve this slot; caller sets real flags */ |
| Elliott Hughes | 468efc8 | 2018-07-10 14:39:49 -0700 | [diff] [blame] | 191 | pthread_mutex_unlock(&__stdio_mutex); |
| Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame] | 192 | fp->_p = nullptr; /* no current pointer */ |
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 193 | fp->_w = 0; /* nothing to read or write */ |
| 194 | fp->_r = 0; |
| Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame] | 195 | fp->_bf._base = nullptr; /* no buffer */ |
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 196 | fp->_bf._size = 0; |
| 197 | fp->_lbfsize = 0; /* not line buffered */ |
| 198 | fp->_file = -1; /* no file */ |
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 199 | |
| Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame] | 200 | fp->_lb._base = nullptr; /* no line buffer */ |
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 201 | fp->_lb._size = 0; |
| Elliott Hughes | 1a56a26 | 2017-12-20 08:53:49 -0800 | [diff] [blame] | 202 | |
| 203 | memset(_EXT(fp), 0, sizeof(struct __sfileext)); |
| Elliott Hughes | 20dd3fe | 2023-03-02 01:26:29 +0000 | [diff] [blame] | 204 | _EXT(fp)->_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; |
| Elliott Hughes | 1a56a26 | 2017-12-20 08:53:49 -0800 | [diff] [blame] | 205 | _EXT(fp)->_caller_handles_locking = false; |
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 206 | |
| 207 | // Caller sets cookie, _read/_write etc. |
| 208 | // We explicitly clear _seek and _seek64 to prevent subtle bugs. |
| 209 | fp->_seek = nullptr; |
| 210 | _EXT(fp)->_seek64 = nullptr; |
| 211 | |
| 212 | return fp; |
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| Elliott Hughes | 468efc8 | 2018-07-10 14:39:49 -0700 | [diff] [blame] | 215 | int _fwalk(int (*callback)(FILE*)) { |
| Elliott Hughes | 468efc8 | 2018-07-10 14:39:49 -0700 | [diff] [blame] | 216 | int result = 0; |
| 217 | for (glue* g = &__sglue; g != nullptr; g = g->next) { |
| 218 | FILE* fp = g->iobs; |
| 219 | for (int n = g->niobs; --n >= 0; ++fp) { |
| Elliott Hughes | 468efc8 | 2018-07-10 14:39:49 -0700 | [diff] [blame] | 220 | if (fp->_flags != 0 && (fp->_flags & __SIGN) == 0) { |
| 221 | result |= (*callback)(fp); |
| 222 | } |
| 223 | } |
| 224 | } |
| Elliott Hughes | 468efc8 | 2018-07-10 14:39:49 -0700 | [diff] [blame] | 225 | return result; |
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 226 | } |
| Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 227 | |
| Ryan Prichard | c485cdb | 2019-04-30 14:47:34 -0700 | [diff] [blame] | 228 | extern "C" __LIBC_HIDDEN__ void __libc_stdio_cleanup(void) { |
| 229 | // Equivalent to fflush(nullptr), but without all the locking since we're shutting down anyway. |
| 230 | _fwalk(__sflush); |
| 231 | } |
| 232 | |
| Elliott Hughes | dc0836d | 2025-09-09 12:05:33 -0700 | [diff] [blame^] | 233 | /* |
| 234 | * Allocate a file buffer, or switch to unbuffered I/O. |
| 235 | * Per the ANSI C standard, ALL tty devices default to line buffered. |
| 236 | * |
| 237 | * As a side effect, we set __SOPT or __SNPT (en/dis-able fseek |
| 238 | * optimisation) right after the fstat() that finds the buffer size. |
| 239 | */ |
| 240 | void |
| 241 | __smakebuf(FILE *fp) |
| 242 | { |
| 243 | unsigned char *p; |
| 244 | int flags; |
| 245 | size_t size; |
| 246 | int couldbetty; |
| 247 | |
| 248 | if (fp->_flags & __SNBF) { |
| 249 | fp->_bf._base = fp->_p = fp->_nbuf; |
| 250 | fp->_bf._size = 1; |
| 251 | return; |
| 252 | } |
| 253 | flags = __swhatbuf(fp, &size, &couldbetty); |
| 254 | if ((p = static_cast<unsigned char*>(malloc(size))) == NULL) { |
| 255 | fp->_flags |= __SNBF; |
| 256 | fp->_bf._base = fp->_p = fp->_nbuf; |
| 257 | fp->_bf._size = 1; |
| 258 | return; |
| 259 | } |
| 260 | flags |= __SMBF; |
| 261 | fp->_bf._base = fp->_p = p; |
| 262 | fp->_bf._size = size; |
| 263 | if (couldbetty && isatty(fp->_file)) |
| 264 | flags |= __SLBF; |
| 265 | fp->_flags |= flags; |
| 266 | } |
| 267 | |
| 268 | /* |
| 269 | * Internal routine to determine `proper' buffering for a file. |
| 270 | */ |
| 271 | int |
| 272 | __swhatbuf(FILE *fp, size_t *bufsize, int *couldbetty) |
| 273 | { |
| 274 | struct stat st; |
| 275 | |
| 276 | if (fp->_file < 0 || fstat(fp->_file, &st) == -1) { |
| 277 | *couldbetty = 0; |
| 278 | *bufsize = BUFSIZ; |
| 279 | return (__SNPT); |
| 280 | } |
| 281 | |
| 282 | /* could be a tty iff it is a character device */ |
| 283 | *couldbetty = S_ISCHR(st.st_mode); |
| 284 | if (st.st_blksize == 0) { |
| 285 | *bufsize = BUFSIZ; |
| 286 | return (__SNPT); |
| 287 | } |
| 288 | |
| 289 | /* |
| 290 | * Optimise fseek() only if it is a regular file. (The test for |
| 291 | * __sseek is mainly paranoia.) It is safe to set _blksize |
| 292 | * unconditionally; it will only be used if __SOPT is also set. |
| 293 | */ |
| 294 | *bufsize = st.st_blksize; |
| 295 | fp->_blksize = st.st_blksize; |
| 296 | return ((st.st_mode & S_IFMT) == S_IFREG && fp->_seek == __sseek ? |
| 297 | __SOPT : __SNPT); |
| 298 | } |
| 299 | |
| Elliott Hughes | d3915c7 | 2021-02-08 16:24:46 -0800 | [diff] [blame] | 300 | static FILE* __FILE_init(FILE* fp, int fd, int flags) { |
| Elliott Hughes | f9cfecf | 2021-02-04 16:58:13 -0800 | [diff] [blame] | 301 | if (fp == nullptr) return nullptr; |
| 302 | |
| Elliott Hughes | d3915c7 | 2021-02-08 16:24:46 -0800 | [diff] [blame] | 303 | #if !defined(__LP64__) |
| 304 | if (fd > SHRT_MAX) __fortify_fatal("stdio: fd %d > SHRT_MAX", fd); |
| 305 | #endif |
| 306 | |
| Elliott Hughes | f9cfecf | 2021-02-04 16:58:13 -0800 | [diff] [blame] | 307 | fp->_file = fd; |
| 308 | android_fdsan_exchange_owner_tag(fd, 0, __get_file_tag(fp)); |
| 309 | fp->_flags = flags; |
| 310 | fp->_cookie = fp; |
| 311 | fp->_read = __sread; |
| 312 | fp->_write = __swrite; |
| 313 | fp->_close = __sclose; |
| 314 | _EXT(fp)->_seek64 = __sseek64; |
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 315 | return fp; |
| 316 | } |
| 317 | |
| 318 | FILE* fopen(const char* file, const char* mode) { |
| Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 319 | int mode_flags; |
| 320 | int flags = __sflags(mode, &mode_flags); |
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 321 | if (flags == 0) return nullptr; |
| 322 | |
| Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 323 | int fd = open(file, mode_flags, DEFFILEMODE); |
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 324 | if (fd == -1) { |
| 325 | return nullptr; |
| 326 | } |
| 327 | |
| Elliott Hughes | d3915c7 | 2021-02-08 16:24:46 -0800 | [diff] [blame] | 328 | FILE* fp = __FILE_init(__sfp(), fd, flags); |
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 329 | if (fp == nullptr) { |
| 330 | ErrnoRestorer errno_restorer; |
| 331 | close(fd); |
| 332 | return nullptr; |
| 333 | } |
| 334 | |
| Elliott Hughes | f9cfecf | 2021-02-04 16:58:13 -0800 | [diff] [blame] | 335 | // For append mode, O_APPEND sets the write position for free, but we need to |
| 336 | // set the read position manually. |
| Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 337 | if ((mode_flags & O_APPEND) != 0) __sseek64(fp, 0, SEEK_END); |
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 338 | return fp; |
| 339 | } |
| Elliott Hughes | f226ee5 | 2016-02-03 11:24:28 -0800 | [diff] [blame] | 340 | __strong_alias(fopen64, fopen); |
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 341 | |
| 342 | FILE* fdopen(int fd, const char* mode) { |
| Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 343 | int mode_flags; |
| 344 | int flags = __sflags(mode, &mode_flags); |
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 345 | if (flags == 0) return nullptr; |
| 346 | |
| 347 | // 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] | 348 | int fd_flags = fcntl(fd, F_GETFL, 0); |
| 349 | if (fd_flags == -1) return nullptr; |
| 350 | int tmp = fd_flags & O_ACCMODE; |
| 351 | if (tmp != O_RDWR && (tmp != (mode_flags & O_ACCMODE))) { |
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 352 | errno = EINVAL; |
| 353 | return nullptr; |
| 354 | } |
| 355 | |
| Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 356 | // Make sure O_APPEND is set on the underlying fd if our mode has 'a'. |
| 357 | // POSIX says we just take the current offset of the underlying fd. |
| 358 | if ((mode_flags & O_APPEND) && !(fd_flags & O_APPEND)) { |
| 359 | if (fcntl(fd, F_SETFL, fd_flags | O_APPEND) == -1) return nullptr; |
| 360 | } |
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 361 | |
| Dan Albert | ba1151c | 2019-03-26 13:01:22 -0700 | [diff] [blame] | 362 | // Make sure O_CLOEXEC is set on the underlying fd if our mode has 'e'. |
| Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 363 | if ((mode_flags & O_CLOEXEC) && !((tmp = fcntl(fd, F_GETFD)) & FD_CLOEXEC)) { |
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 364 | fcntl(fd, F_SETFD, tmp | FD_CLOEXEC); |
| 365 | } |
| 366 | |
| Elliott Hughes | d3915c7 | 2021-02-08 16:24:46 -0800 | [diff] [blame] | 367 | return __FILE_init(__sfp(), fd, flags); |
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 368 | } |
| 369 | |
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 370 | FILE* freopen(const char* file, const char* mode, FILE* fp) { |
| Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 371 | CHECK_FP(fp); |
| Elliott Hughes | f9cfecf | 2021-02-04 16:58:13 -0800 | [diff] [blame] | 372 | |
| 373 | // POSIX says: "If pathname is a null pointer, the freopen() function shall |
| 374 | // attempt to change the mode of the stream to that specified by mode, as if |
| 375 | // the name of the file currently associated with the stream had been used. In |
| 376 | // this case, the file descriptor associated with the stream need not be |
| 377 | // closed if the call to freopen() succeeds. It is implementation-defined |
| 378 | // which changes of mode are permitted (if any), and under what |
| 379 | // circumstances." |
| 380 | // |
| 381 | // Linux is quite restrictive about what changes you can make with F_SETFL, |
| 382 | // and in particular won't let you touch the access bits. It's easiest and |
| 383 | // most effective to just rely on /proc/self/fd/... |
| 384 | FdPath fd_path(fp->_file); |
| 385 | if (file == nullptr) file = fd_path.c_str(); |
| 386 | |
| Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 387 | int mode_flags; |
| 388 | int flags = __sflags(mode, &mode_flags); |
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 389 | if (flags == 0) { |
| 390 | fclose(fp); |
| 391 | return nullptr; |
| 392 | } |
| 393 | |
| 394 | ScopedFileLock sfl(fp); |
| 395 | |
| Elliott Hughes | f9cfecf | 2021-02-04 16:58:13 -0800 | [diff] [blame] | 396 | // TODO: rewrite this mess completely. |
| 397 | |
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 398 | // There are actually programs that depend on being able to "freopen" |
| 399 | // descriptors that weren't originally open. Keep this from breaking. |
| 400 | // Remember whether the stream was open to begin with, and which file |
| 401 | // descriptor (if any) was associated with it. If it was attached to |
| 402 | // a descriptor, defer closing it; freopen("/dev/stdin", "r", stdin) |
| 403 | // should work. This is unnecessary if it was not a Unix file. |
| 404 | int isopen, wantfd; |
| 405 | if (fp->_flags == 0) { |
| 406 | fp->_flags = __SEOF; // Hold on to it. |
| 407 | isopen = 0; |
| 408 | wantfd = -1; |
| 409 | } else { |
| 410 | // Flush the stream; ANSI doesn't require this. |
| 411 | if (fp->_flags & __SWR) __sflush(fp); |
| 412 | |
| Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame] | 413 | // If close is null, closing is a no-op, hence pointless. |
| 414 | isopen = (fp->_close != nullptr); |
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 415 | if ((wantfd = fp->_file) < 0 && isopen) { |
| 416 | (*fp->_close)(fp->_cookie); |
| 417 | isopen = 0; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | // Get a new descriptor to refer to the new file. |
| Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 422 | int fd = open(file, mode_flags, DEFFILEMODE); |
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 423 | if (fd < 0 && isopen) { |
| 424 | // If out of fd's close the old one and try again. |
| 425 | if (errno == ENFILE || errno == EMFILE) { |
| 426 | (*fp->_close)(fp->_cookie); |
| 427 | isopen = 0; |
| Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 428 | fd = open(file, mode_flags, DEFFILEMODE); |
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | |
| 432 | int sverrno = errno; |
| 433 | |
| 434 | // Finish closing fp. Even if the open succeeded above, we cannot |
| 435 | // keep fp->_base: it may be the wrong size. This loses the effect |
| 436 | // of any setbuffer calls, but stdio has always done this before. |
| 437 | if (isopen && fd != wantfd) (*fp->_close)(fp->_cookie); |
| 438 | if (fp->_flags & __SMBF) free(fp->_bf._base); |
| 439 | fp->_w = 0; |
| 440 | fp->_r = 0; |
| Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame] | 441 | fp->_p = nullptr; |
| 442 | fp->_bf._base = nullptr; |
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 443 | fp->_bf._size = 0; |
| 444 | fp->_lbfsize = 0; |
| 445 | if (HASUB(fp)) FREEUB(fp); |
| 446 | _UB(fp)._size = 0; |
| 447 | WCIO_FREE(fp); |
| Elliott Hughes | 80e4c15 | 2017-07-21 13:57:55 -0700 | [diff] [blame] | 448 | free_fgetln_buffer(fp); |
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 449 | fp->_lb._size = 0; |
| 450 | |
| 451 | if (fd < 0) { // Did not get it after all. |
| 452 | fp->_flags = 0; // Release. |
| 453 | errno = sverrno; // Restore errno in case _close clobbered it. |
| 454 | return nullptr; |
| 455 | } |
| 456 | |
| 457 | // If reopening something that was open before on a real file, try |
| 458 | // to maintain the descriptor. Various C library routines (perror) |
| 459 | // assume stderr is always fd STDERR_FILENO, even if being freopen'd. |
| 460 | if (wantfd >= 0 && fd != wantfd) { |
| Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 461 | if (dup3(fd, wantfd, mode_flags & O_CLOEXEC) >= 0) { |
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 462 | close(fd); |
| 463 | fd = wantfd; |
| 464 | } |
| 465 | } |
| 466 | |
| Elliott Hughes | d3915c7 | 2021-02-08 16:24:46 -0800 | [diff] [blame] | 467 | __FILE_init(fp, fd, flags); |
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 468 | |
| Elliott Hughes | f9cfecf | 2021-02-04 16:58:13 -0800 | [diff] [blame] | 469 | // For append mode, O_APPEND sets the write position for free, but we need to |
| 470 | // set the read position manually. |
| Elliott Hughes | d3915c7 | 2021-02-08 16:24:46 -0800 | [diff] [blame] | 471 | if ((mode_flags & O_APPEND) != 0) __sseek64(fp, 0, SEEK_END); |
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 472 | |
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 473 | return fp; |
| 474 | } |
| Elliott Hughes | f226ee5 | 2016-02-03 11:24:28 -0800 | [diff] [blame] | 475 | __strong_alias(freopen64, freopen); |
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 476 | |
| Elliott Hughes | f2d7d01 | 2025-09-08 12:20:38 -0700 | [diff] [blame] | 477 | int fclose(FILE* fp) { |
| 478 | CHECK_FP(fp); |
| 479 | |
| Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 480 | if (fp->_flags == 0) { |
| 481 | // Already freed! |
| 482 | errno = EBADF; |
| 483 | return EOF; |
| 484 | } |
| Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 485 | |
| Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 486 | ScopedFileLock sfl(fp); |
| 487 | WCIO_FREE(fp); |
| 488 | int r = fp->_flags & __SWR ? __sflush(fp) : 0; |
| Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame] | 489 | if (fp->_close != nullptr && (*fp->_close)(fp->_cookie) < 0) { |
| Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 490 | r = EOF; |
| 491 | } |
| 492 | if (fp->_flags & __SMBF) free(fp->_bf._base); |
| 493 | if (HASUB(fp)) FREEUB(fp); |
| Elliott Hughes | 80e4c15 | 2017-07-21 13:57:55 -0700 | [diff] [blame] | 494 | free_fgetln_buffer(fp); |
| Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 495 | |
| Elliott Hughes | 468efc8 | 2018-07-10 14:39:49 -0700 | [diff] [blame] | 496 | // If we were created by popen(3), wait for the child. |
| 497 | pid_t pid = _EXT(fp)->_popen_pid; |
| 498 | if (pid > 0) { |
| 499 | int status; |
| 500 | if (TEMP_FAILURE_RETRY(wait4(pid, &status, 0, nullptr)) != -1) { |
| 501 | r = status; |
| 502 | } |
| 503 | } |
| 504 | _EXT(fp)->_popen_pid = 0; |
| 505 | |
| Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 506 | // Poison this FILE so accesses after fclose will be obvious. |
| 507 | fp->_file = -1; |
| 508 | fp->_r = fp->_w = 0; |
| Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 509 | |
| Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 510 | // Release this FILE for reuse. |
| 511 | fp->_flags = 0; |
| 512 | return r; |
| Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 513 | } |
| Elliott Hughes | f2d7d01 | 2025-09-08 12:20:38 -0700 | [diff] [blame] | 514 | __strong_alias(pclose, fclose); |
| Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 515 | |
| Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 516 | int fileno_unlocked(FILE* fp) { |
| Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 517 | CHECK_FP(fp); |
| Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 518 | int fd = fp->_file; |
| 519 | if (fd == -1) { |
| 520 | errno = EBADF; |
| 521 | return -1; |
| 522 | } |
| 523 | return fd; |
| 524 | } |
| 525 | |
| Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 526 | int fileno(FILE* fp) { |
| Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 527 | CHECK_FP(fp); |
| Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 528 | ScopedFileLock sfl(fp); |
| 529 | return fileno_unlocked(fp); |
| Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 530 | } |
| Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 531 | |
| Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 532 | void clearerr_unlocked(FILE* fp) { |
| Elliott Hughes | 22917f6 | 2018-10-01 14:21:07 -0700 | [diff] [blame] | 533 | CHECK_FP(fp); |
| Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 534 | return __sclearerr(fp); |
| 535 | } |
| 536 | |
| 537 | void clearerr(FILE* fp) { |
| Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 538 | CHECK_FP(fp); |
| Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 539 | ScopedFileLock sfl(fp); |
| 540 | clearerr_unlocked(fp); |
| 541 | } |
| 542 | |
| 543 | int feof_unlocked(FILE* fp) { |
| Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 544 | CHECK_FP(fp); |
| Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 545 | return ((fp->_flags & __SEOF) != 0); |
| Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | int feof(FILE* fp) { |
| Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 549 | CHECK_FP(fp); |
| Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 550 | ScopedFileLock sfl(fp); |
| 551 | return feof_unlocked(fp); |
| 552 | } |
| 553 | |
| 554 | int ferror_unlocked(FILE* fp) { |
| Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 555 | CHECK_FP(fp); |
| Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 556 | return __sferror(fp); |
| 557 | } |
| 558 | |
| 559 | int ferror(FILE* fp) { |
| Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 560 | CHECK_FP(fp); |
| Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 561 | ScopedFileLock sfl(fp); |
| 562 | return ferror_unlocked(fp); |
| 563 | } |
| 564 | |
| Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame] | 565 | int __sflush(FILE* fp) { |
| 566 | // Flushing a read-only file is a no-op. |
| 567 | if ((fp->_flags & __SWR) == 0) return 0; |
| 568 | |
| 569 | // Flushing a file without a buffer is a no-op. |
| 570 | unsigned char* p = fp->_bf._base; |
| 571 | if (p == nullptr) return 0; |
| 572 | |
| 573 | // Set these immediately to avoid problems with longjmp and to allow |
| 574 | // exchange buffering (via setvbuf) in user write function. |
| 575 | int n = fp->_p - p; |
| 576 | fp->_p = p; |
| 577 | fp->_w = (fp->_flags & (__SLBF|__SNBF)) ? 0 : fp->_bf._size; |
| 578 | |
| 579 | while (n > 0) { |
| 580 | int written = (*fp->_write)(fp->_cookie, reinterpret_cast<char*>(p), n); |
| 581 | if (written <= 0) { |
| 582 | fp->_flags |= __SERR; |
| 583 | return EOF; |
| 584 | } |
| 585 | n -= written, p += written; |
| 586 | } |
| 587 | return 0; |
| 588 | } |
| 589 | |
| Ryan Prichard | c485cdb | 2019-04-30 14:47:34 -0700 | [diff] [blame] | 590 | int __sflush_locked(FILE* fp) { |
| 591 | ScopedFileLock sfl(fp); |
| 592 | return __sflush(fp); |
| 593 | } |
| 594 | |
| Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 595 | int __sread(void* cookie, char* buf, int n) { |
| Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 596 | FILE* fp = reinterpret_cast<FILE*>(cookie); |
| 597 | return TEMP_FAILURE_RETRY(read(fp->_file, buf, n)); |
| Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | int __swrite(void* cookie, const char* buf, int n) { |
| Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 601 | FILE* fp = reinterpret_cast<FILE*>(cookie); |
| Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 602 | return TEMP_FAILURE_RETRY(write(fp->_file, buf, n)); |
| Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 603 | } |
| 604 | |
| Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 605 | fpos_t __sseek(void* cookie, fpos_t offset, int whence) { |
| Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 606 | FILE* fp = reinterpret_cast<FILE*>(cookie); |
| 607 | return TEMP_FAILURE_RETRY(lseek(fp->_file, offset, whence)); |
| Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 608 | } |
| 609 | |
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 610 | off64_t __sseek64(void* cookie, off64_t offset, int whence) { |
| 611 | FILE* fp = reinterpret_cast<FILE*>(cookie); |
| 612 | return TEMP_FAILURE_RETRY(lseek64(fp->_file, offset, whence)); |
| 613 | } |
| 614 | |
| Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 615 | int __sclose(void* cookie) { |
| Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 616 | FILE* fp = reinterpret_cast<FILE*>(cookie); |
| Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 617 | return android_fdsan_close_with_tag(fp->_file, __get_file_tag(fp)); |
| Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 618 | } |
| 619 | |
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 620 | static off64_t __seek_unlocked(FILE* fp, off64_t offset, int whence) { |
| 621 | // Use `_seek64` if set, but fall back to `_seek`. |
| 622 | if (_EXT(fp)->_seek64 != nullptr) { |
| 623 | return (*_EXT(fp)->_seek64)(fp->_cookie, offset, whence); |
| 624 | } else if (fp->_seek != nullptr) { |
| Elliott Hughes | 955426e | 2016-01-26 18:25:52 -0800 | [diff] [blame] | 625 | off64_t result = (*fp->_seek)(fp->_cookie, offset, whence); |
| 626 | #if !defined(__LP64__) |
| 627 | // Avoid sign extension if off64_t is larger than off_t. |
| 628 | if (result != -1) result &= 0xffffffff; |
| 629 | #endif |
| 630 | return result; |
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 631 | } else { |
| 632 | errno = ESPIPE; |
| 633 | return -1; |
| Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 634 | } |
| Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 635 | } |
| 636 | |
| Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 637 | static off64_t __ftello64_unlocked(FILE* fp) { |
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 638 | // Find offset of underlying I/O object, then adjust for buffered bytes. |
| Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 639 | __sflush(fp); // May adjust seek offset on append stream. |
| Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 640 | |
| Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 641 | off64_t result = __seek_unlocked(fp, 0, SEEK_CUR); |
| Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 642 | if (result == -1) { |
| 643 | return -1; |
| 644 | } |
| 645 | |
| 646 | if (fp->_flags & __SRD) { |
| 647 | // Reading. Any unread characters (including |
| 648 | // those from ungetc) cause the position to be |
| 649 | // smaller than that in the underlying object. |
| 650 | result -= fp->_r; |
| 651 | if (HASUB(fp)) result -= fp->_ur; |
| Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame] | 652 | } else if (fp->_flags & __SWR && fp->_p != nullptr) { |
| Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 653 | // Writing. Any buffered characters cause the |
| 654 | // position to be greater than that in the |
| 655 | // underlying object. |
| 656 | result += fp->_p - fp->_bf._base; |
| 657 | } |
| 658 | return result; |
| 659 | } |
| 660 | |
| Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 661 | 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] | 662 | ScopedFileLock sfl(fp); |
| 663 | |
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 664 | // Change any SEEK_CUR to SEEK_SET, and check `whence` argument. |
| Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 665 | // After this, whence is either SEEK_SET or SEEK_END. |
| 666 | if (whence == SEEK_CUR) { |
| Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 667 | fpos64_t current_offset = __ftello64_unlocked(fp); |
| Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 668 | if (current_offset == -1) { |
| Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 669 | return -1; |
| Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 670 | } |
| 671 | offset += current_offset; |
| 672 | whence = SEEK_SET; |
| 673 | } else if (whence != SEEK_SET && whence != SEEK_END) { |
| 674 | errno = EINVAL; |
| Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 675 | return -1; |
| 676 | } |
| 677 | |
| 678 | // If our caller has a 32-bit interface, refuse to go past a 32-bit file offset. |
| 679 | if (off_t_bits == 32 && offset > LONG_MAX) { |
| 680 | errno = EOVERFLOW; |
| 681 | return -1; |
| Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 682 | } |
| 683 | |
| Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame] | 684 | if (fp->_bf._base == nullptr) __smakebuf(fp); |
| Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 685 | |
| 686 | // Flush unwritten data and attempt the seek. |
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 687 | if (__sflush(fp) || __seek_unlocked(fp, offset, whence) == -1) { |
| Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 688 | return -1; |
| Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | // Success: clear EOF indicator and discard ungetc() data. |
| 692 | if (HASUB(fp)) FREEUB(fp); |
| 693 | fp->_p = fp->_bf._base; |
| 694 | fp->_r = 0; |
| 695 | /* fp->_w = 0; */ /* unnecessary (I think...) */ |
| 696 | fp->_flags &= ~__SEOF; |
| 697 | return 0; |
| 698 | } |
| 699 | |
| Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 700 | int fseeko(FILE* fp, off_t offset, int whence) { |
| Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 701 | CHECK_FP(fp); |
| Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 702 | static_assert(sizeof(off_t) == sizeof(long), "sizeof(off_t) != sizeof(long)"); |
| 703 | return __fseeko64(fp, offset, whence, 8*sizeof(off_t)); |
| 704 | } |
| 705 | __strong_alias(fseek, fseeko); |
| 706 | |
| 707 | int fseeko64(FILE* fp, off64_t offset, int whence) { |
| Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 708 | CHECK_FP(fp); |
| Ryan Prichard | bf54986 | 2017-11-07 15:30:32 -0800 | [diff] [blame] | 709 | return __fseeko64(fp, offset, whence, 8*sizeof(off64_t)); |
| Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 710 | } |
| 711 | |
| Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 712 | int fsetpos(FILE* fp, const fpos_t* pos) { |
| Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 713 | CHECK_FP(fp); |
| Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 714 | return fseeko(fp, *pos, SEEK_SET); |
| 715 | } |
| 716 | |
| 717 | int fsetpos64(FILE* fp, const fpos64_t* pos) { |
| Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 718 | CHECK_FP(fp); |
| Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 719 | return fseeko64(fp, *pos, SEEK_SET); |
| 720 | } |
| 721 | |
| Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 722 | off_t ftello(FILE* fp) { |
| Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 723 | CHECK_FP(fp); |
| Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 724 | static_assert(sizeof(off_t) == sizeof(long), "sizeof(off_t) != sizeof(long)"); |
| 725 | off64_t result = ftello64(fp); |
| 726 | if (result > LONG_MAX) { |
| Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 727 | errno = EOVERFLOW; |
| 728 | return -1; |
| 729 | } |
| Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 730 | return result; |
| 731 | } |
| 732 | __strong_alias(ftell, ftello); |
| 733 | |
| 734 | off64_t ftello64(FILE* fp) { |
| Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 735 | CHECK_FP(fp); |
| Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 736 | ScopedFileLock sfl(fp); |
| 737 | return __ftello64_unlocked(fp); |
| Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 738 | } |
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 739 | |
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 740 | int fgetpos(FILE* fp, fpos_t* pos) { |
| Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 741 | CHECK_FP(fp); |
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 742 | *pos = ftello(fp); |
| Elliott Hughes | 955426e | 2016-01-26 18:25:52 -0800 | [diff] [blame] | 743 | return (*pos == -1) ? -1 : 0; |
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 744 | } |
| 745 | |
| Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 746 | int fgetpos64(FILE* fp, fpos64_t* pos) { |
| Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 747 | CHECK_FP(fp); |
| Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 748 | *pos = ftello64(fp); |
| Elliott Hughes | 955426e | 2016-01-26 18:25:52 -0800 | [diff] [blame] | 749 | return (*pos == -1) ? -1 : 0; |
| Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 750 | } |
| Elliott Hughes | 03e65eb | 2016-01-26 14:13:04 -0800 | [diff] [blame] | 751 | |
| 752 | static FILE* __funopen(const void* cookie, |
| 753 | int (*read_fn)(void*, char*, int), |
| 754 | int (*write_fn)(void*, const char*, int), |
| 755 | int (*close_fn)(void*)) { |
| 756 | if (read_fn == nullptr && write_fn == nullptr) { |
| 757 | errno = EINVAL; |
| 758 | return nullptr; |
| 759 | } |
| 760 | |
| 761 | FILE* fp = __sfp(); |
| 762 | if (fp == nullptr) return nullptr; |
| 763 | |
| 764 | if (read_fn != nullptr && write_fn != nullptr) { |
| 765 | fp->_flags = __SRW; |
| 766 | } else if (read_fn != nullptr) { |
| 767 | fp->_flags = __SRD; |
| 768 | } else if (write_fn != nullptr) { |
| 769 | fp->_flags = __SWR; |
| 770 | } |
| 771 | |
| 772 | fp->_file = -1; |
| 773 | fp->_cookie = const_cast<void*>(cookie); // The funopen(3) API is incoherent. |
| 774 | fp->_read = read_fn; |
| 775 | fp->_write = write_fn; |
| 776 | fp->_close = close_fn; |
| 777 | |
| 778 | return fp; |
| 779 | } |
| 780 | |
| 781 | FILE* funopen(const void* cookie, |
| 782 | int (*read_fn)(void*, char*, int), |
| 783 | int (*write_fn)(void*, const char*, int), |
| 784 | fpos_t (*seek_fn)(void*, fpos_t, int), |
| 785 | int (*close_fn)(void*)) { |
| 786 | FILE* fp = __funopen(cookie, read_fn, write_fn, close_fn); |
| 787 | if (fp != nullptr) { |
| 788 | fp->_seek = seek_fn; |
| 789 | } |
| 790 | return fp; |
| 791 | } |
| 792 | |
| 793 | FILE* funopen64(const void* cookie, |
| 794 | int (*read_fn)(void*, char*, int), |
| 795 | int (*write_fn)(void*, const char*, int), |
| 796 | fpos64_t (*seek_fn)(void*, fpos64_t, int), |
| 797 | int (*close_fn)(void*)) { |
| 798 | FILE* fp = __funopen(cookie, read_fn, write_fn, close_fn); |
| 799 | if (fp != nullptr) { |
| 800 | _EXT(fp)->_seek64 = seek_fn; |
| 801 | } |
| 802 | return fp; |
| 803 | } |
| Elliott Hughes | 20788ae | 2016-06-09 15:16:32 -0700 | [diff] [blame] | 804 | |
| Elliott Hughes | 53cf348 | 2016-08-09 13:06:41 -0700 | [diff] [blame] | 805 | int asprintf(char** s, const char* fmt, ...) { |
| 806 | PRINTF_IMPL(vasprintf(s, fmt, ap)); |
| 807 | } |
| 808 | |
| Elliott Hughes | 20788ae | 2016-06-09 15:16:32 -0700 | [diff] [blame] | 809 | char* ctermid(char* s) { |
| 810 | return s ? strcpy(s, _PATH_TTY) : const_cast<char*>(_PATH_TTY); |
| 811 | } |
| Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 812 | |
| Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 813 | int dprintf(int fd, const char* fmt, ...) { |
| 814 | PRINTF_IMPL(vdprintf(fd, fmt, ap)); |
| 815 | } |
| 816 | |
| 817 | int fprintf(FILE* fp, const char* fmt, ...) { |
| Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 818 | CHECK_FP(fp); |
| Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 819 | PRINTF_IMPL(vfprintf(fp, fmt, ap)); |
| 820 | } |
| 821 | |
| Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 822 | int fgetc(FILE* fp) { |
| Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 823 | CHECK_FP(fp); |
| Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 824 | return getc(fp); |
| 825 | } |
| 826 | |
| Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame] | 827 | int fgetc_unlocked(FILE* fp) { |
| 828 | CHECK_FP(fp); |
| 829 | return getc_unlocked(fp); |
| 830 | } |
| 831 | |
| George Burgess IV | 9024235 | 2018-02-06 12:51:31 -0800 | [diff] [blame] | 832 | char* fgets(char* buf, int n, FILE* fp) { |
| Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame] | 833 | CHECK_FP(fp); |
| 834 | ScopedFileLock sfl(fp); |
| 835 | return fgets_unlocked(buf, n, fp); |
| 836 | } |
| 837 | |
| Elliott Hughes | 468efc8 | 2018-07-10 14:39:49 -0700 | [diff] [blame] | 838 | // Reads at most n-1 characters from the given file. |
| 839 | // Stops when a newline has been read, or the count runs out. |
| 840 | // Returns first argument, or nullptr if no characters were read. |
| 841 | // Does not return nullptr if n == 1. |
| Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame] | 842 | char* fgets_unlocked(char* buf, int n, FILE* fp) { |
| Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 843 | if (n <= 0) __fortify_fatal("fgets: buffer size %d <= 0", n); |
| Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame] | 844 | |
| Elliott Hughes | 531199c | 2023-05-09 16:11:49 -0700 | [diff] [blame] | 845 | _SET_ORIENTATION(fp, ORIENT_BYTES); |
| Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame] | 846 | |
| 847 | char* s = buf; |
| 848 | n--; // Leave space for NUL. |
| 849 | while (n != 0) { |
| 850 | // If the buffer is empty, refill it. |
| 851 | if (fp->_r <= 0) { |
| 852 | if (__srefill(fp)) { |
| 853 | // EOF/error: stop with partial or no line. |
| 854 | if (s == buf) return nullptr; |
| 855 | break; |
| 856 | } |
| 857 | } |
| 858 | size_t len = fp->_r; |
| 859 | unsigned char* p = fp->_p; |
| 860 | |
| 861 | // Scan through at most n bytes of the current buffer, |
| 862 | // looking for '\n'. If found, copy up to and including |
| 863 | // newline, and stop. Otherwise, copy entire chunk and loop. |
| 864 | if (len > static_cast<size_t>(n)) len = n; |
| 865 | unsigned char* t = static_cast<unsigned char*>(memchr(p, '\n', len)); |
| 866 | if (t != nullptr) { |
| 867 | len = ++t - p; |
| 868 | fp->_r -= len; |
| 869 | fp->_p = t; |
| 870 | memcpy(s, p, len); |
| 871 | s[len] = '\0'; |
| 872 | return buf; |
| 873 | } |
| 874 | fp->_r -= len; |
| 875 | fp->_p += len; |
| 876 | memcpy(s, p, len); |
| 877 | s += len; |
| 878 | n -= len; |
| 879 | } |
| 880 | *s = '\0'; |
| 881 | return buf; |
| 882 | } |
| 883 | |
| Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 884 | int fputc(int c, FILE* fp) { |
| Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 885 | CHECK_FP(fp); |
| Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 886 | return putc(c, fp); |
| 887 | } |
| 888 | |
| Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame] | 889 | int fputc_unlocked(int c, FILE* fp) { |
| 890 | CHECK_FP(fp); |
| 891 | return putc_unlocked(c, fp); |
| 892 | } |
| 893 | |
| 894 | int fputs(const char* s, FILE* fp) { |
| 895 | CHECK_FP(fp); |
| 896 | ScopedFileLock sfl(fp); |
| 897 | return fputs_unlocked(s, fp); |
| 898 | } |
| 899 | |
| 900 | int fputs_unlocked(const char* s, FILE* fp) { |
| 901 | CHECK_FP(fp); |
| 902 | size_t length = strlen(s); |
| 903 | return (fwrite_unlocked(s, 1, length, fp) == length) ? 0 : EOF; |
| 904 | } |
| 905 | |
| Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 906 | int fscanf(FILE* fp, const char* fmt, ...) { |
| Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 907 | CHECK_FP(fp); |
| Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 908 | PRINTF_IMPL(vfscanf(fp, fmt, ap)); |
| 909 | } |
| 910 | |
| 911 | int fwprintf(FILE* fp, const wchar_t* fmt, ...) { |
| Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 912 | CHECK_FP(fp); |
| Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 913 | PRINTF_IMPL(vfwprintf(fp, fmt, ap)); |
| 914 | } |
| 915 | |
| 916 | int fwscanf(FILE* fp, const wchar_t* fmt, ...) { |
| Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 917 | CHECK_FP(fp); |
| Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 918 | PRINTF_IMPL(vfwscanf(fp, fmt, ap)); |
| 919 | } |
| 920 | |
| 921 | int getc(FILE* fp) { |
| Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 922 | CHECK_FP(fp); |
| Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 923 | ScopedFileLock sfl(fp); |
| 924 | return getc_unlocked(fp); |
| 925 | } |
| 926 | |
| 927 | int getc_unlocked(FILE* fp) { |
| Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 928 | CHECK_FP(fp); |
| Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 929 | return __sgetc(fp); |
| 930 | } |
| 931 | |
| 932 | int getchar_unlocked() { |
| 933 | return getc_unlocked(stdin); |
| 934 | } |
| 935 | |
| 936 | int getchar() { |
| 937 | return getc(stdin); |
| 938 | } |
| 939 | |
| Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 940 | ssize_t getline(char** buf, size_t* len, 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 getdelim(buf, len, '\n', fp); |
| 943 | } |
| 944 | |
| 945 | wint_t getwc(FILE* fp) { |
| Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 946 | CHECK_FP(fp); |
| Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 947 | return fgetwc(fp); |
| 948 | } |
| 949 | |
| 950 | wint_t getwchar() { |
| 951 | return fgetwc(stdin); |
| 952 | } |
| 953 | |
| Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame] | 954 | void perror(const char* msg) { |
| 955 | if (msg == nullptr) msg = ""; |
| Elliott Hughes | ae1c64a | 2023-03-03 23:46:34 +0000 | [diff] [blame] | 956 | fprintf(stderr, "%s%s%m\n", msg, (*msg == '\0') ? "" : ": "); |
| Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame] | 957 | } |
| 958 | |
| Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 959 | int printf(const char* fmt, ...) { |
| 960 | PRINTF_IMPL(vfprintf(stdout, fmt, ap)); |
| 961 | } |
| 962 | |
| 963 | int putc(int c, FILE* fp) { |
| Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 964 | CHECK_FP(fp); |
| Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 965 | ScopedFileLock sfl(fp); |
| 966 | return putc_unlocked(c, fp); |
| 967 | } |
| 968 | |
| 969 | int putc_unlocked(int c, FILE* fp) { |
| Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 970 | CHECK_FP(fp); |
| Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 971 | if (cantwrite(fp)) { |
| 972 | errno = EBADF; |
| 973 | return EOF; |
| 974 | } |
| Elliott Hughes | 531199c | 2023-05-09 16:11:49 -0700 | [diff] [blame] | 975 | _SET_ORIENTATION(fp, ORIENT_BYTES); |
| Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 976 | if (--fp->_w >= 0 || (fp->_w >= fp->_lbfsize && c != '\n')) { |
| 977 | return (*fp->_p++ = c); |
| 978 | } |
| 979 | return (__swbuf(c, fp)); |
| 980 | } |
| 981 | |
| 982 | int putchar(int c) { |
| 983 | return putc(c, stdout); |
| 984 | } |
| 985 | |
| 986 | int putchar_unlocked(int c) { |
| 987 | return putc_unlocked(c, stdout); |
| 988 | } |
| 989 | |
| Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame] | 990 | int puts(const char* s) { |
| 991 | size_t length = strlen(s); |
| 992 | ScopedFileLock sfl(stdout); |
| 993 | return (fwrite_unlocked(s, 1, length, stdout) == length && |
| 994 | putc_unlocked('\n', stdout) != EOF) ? 0 : EOF; |
| 995 | } |
| 996 | |
| Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 997 | wint_t putwc(wchar_t wc, FILE* fp) { |
| Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 998 | CHECK_FP(fp); |
| Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 999 | return fputwc(wc, fp); |
| 1000 | } |
| 1001 | |
| 1002 | wint_t putwchar(wchar_t wc) { |
| 1003 | return fputwc(wc, stdout); |
| 1004 | } |
| 1005 | |
| Elliott Hughes | d1f25a7 | 2016-08-05 15:53:03 -0700 | [diff] [blame] | 1006 | int remove(const char* path) { |
| 1007 | if (unlink(path) != -1) return 0; |
| 1008 | if (errno != EISDIR) return -1; |
| 1009 | return rmdir(path); |
| 1010 | } |
| 1011 | |
| Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 1012 | void rewind(FILE* fp) { |
| Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 1013 | CHECK_FP(fp); |
| Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 1014 | ScopedFileLock sfl(fp); |
| 1015 | fseek(fp, 0, SEEK_SET); |
| 1016 | clearerr_unlocked(fp); |
| 1017 | } |
| 1018 | |
| Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 1019 | int scanf(const char* fmt, ...) { |
| 1020 | PRINTF_IMPL(vfscanf(stdin, fmt, ap)); |
| 1021 | } |
| 1022 | |
| Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 1023 | void setbuf(FILE* fp, char* buf) { |
| Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 1024 | CHECK_FP(fp); |
| Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 1025 | setbuffer(fp, buf, BUFSIZ); |
| 1026 | } |
| 1027 | |
| 1028 | void setbuffer(FILE* fp, char* buf, int size) { |
| Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 1029 | CHECK_FP(fp); |
| Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 1030 | setvbuf(fp, buf, buf ? _IOFBF : _IONBF, size); |
| 1031 | } |
| 1032 | |
| 1033 | int setlinebuf(FILE* fp) { |
| Josh Gao | d162060 | 2017-10-05 13:48:08 -0700 | [diff] [blame] | 1034 | CHECK_FP(fp); |
| Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 1035 | return setvbuf(fp, nullptr, _IOLBF, 0); |
| 1036 | } |
| 1037 | |
| Elliott Hughes | dc0836d | 2025-09-09 12:05:33 -0700 | [diff] [blame^] | 1038 | /* |
| 1039 | * Set one of the three kinds of buffering, optionally including |
| 1040 | * a buffer. |
| 1041 | */ |
| 1042 | int |
| 1043 | setvbuf(FILE *fp, char *buf, int mode, size_t size) |
| 1044 | { |
| 1045 | int ret, flags; |
| 1046 | size_t iosize; |
| 1047 | int ttyflag; |
| 1048 | |
| 1049 | /* |
| 1050 | * Verify arguments. The `int' limit on `size' is due to this |
| 1051 | * particular implementation. Note, buf and size are ignored |
| 1052 | * when setting _IONBF. |
| 1053 | */ |
| 1054 | if (mode != _IONBF) |
| 1055 | if ((mode != _IOFBF && mode != _IOLBF) || size > INT_MAX) |
| 1056 | return (EOF); |
| 1057 | |
| 1058 | /* |
| 1059 | * Write current buffer, if any. Discard unread input (including |
| 1060 | * ungetc data), cancel line buffering, and free old buffer if |
| 1061 | * malloc()ed. We also clear any eof condition, as if this were |
| 1062 | * a seek. |
| 1063 | */ |
| 1064 | FLOCKFILE(fp); |
| 1065 | ret = 0; |
| 1066 | (void)__sflush(fp); |
| 1067 | if (HASUB(fp)) |
| 1068 | FREEUB(fp); |
| 1069 | WCIO_FREE(fp); |
| 1070 | fp->_r = fp->_lbfsize = 0; |
| 1071 | flags = fp->_flags; |
| 1072 | if (flags & __SMBF) |
| 1073 | free(fp->_bf._base); |
| 1074 | flags &= ~(__SLBF | __SNBF | __SMBF | __SOPT | __SNPT | __SEOF); |
| 1075 | |
| 1076 | /* If setting unbuffered mode, skip all the hard work. */ |
| 1077 | if (mode == _IONBF) |
| 1078 | goto nbf; |
| 1079 | |
| 1080 | /* |
| 1081 | * Find optimal I/O size for seek optimization. This also returns |
| 1082 | * a `tty flag' to suggest that we check isatty(fd), but we do not |
| 1083 | * care since our caller told us how to buffer. |
| 1084 | */ |
| 1085 | flags |= __swhatbuf(fp, &iosize, &ttyflag); |
| 1086 | if (size == 0) { |
| 1087 | buf = NULL; /* force local allocation */ |
| 1088 | size = iosize; |
| 1089 | } |
| 1090 | |
| 1091 | /* Allocate buffer if needed. */ |
| 1092 | if (buf == NULL) { |
| 1093 | if ((buf = static_cast<char*>(malloc(size))) == NULL) { |
| 1094 | /* |
| 1095 | * Unable to honor user's request. We will return |
| 1096 | * failure, but try again with file system size. |
| 1097 | */ |
| 1098 | ret = EOF; |
| 1099 | if (size != iosize) { |
| 1100 | size = iosize; |
| 1101 | buf = static_cast<char*>(malloc(size)); |
| 1102 | } |
| 1103 | } |
| 1104 | if (buf == NULL) { |
| 1105 | /* No luck; switch to unbuffered I/O. */ |
| 1106 | nbf: |
| 1107 | fp->_flags = flags | __SNBF; |
| 1108 | fp->_w = 0; |
| 1109 | fp->_bf._base = fp->_p = fp->_nbuf; |
| 1110 | fp->_bf._size = 1; |
| 1111 | FUNLOCKFILE(fp); |
| 1112 | return (ret); |
| 1113 | } |
| 1114 | flags |= __SMBF; |
| 1115 | } |
| 1116 | |
| 1117 | /* |
| 1118 | * We're committed to buffering from here, so make sure we've |
| 1119 | * registered to flush buffers on exit. |
| 1120 | */ |
| 1121 | if (!__sdidinit) |
| 1122 | __sinit(); |
| 1123 | |
| 1124 | /* |
| 1125 | * Kill any seek optimization if the buffer is not the |
| 1126 | * right size. |
| 1127 | * |
| 1128 | * SHOULD WE ALLOW MULTIPLES HERE (i.e., ok iff (size % iosize) == 0)? |
| 1129 | */ |
| 1130 | if (size != iosize) |
| 1131 | flags |= __SNPT; |
| 1132 | |
| 1133 | /* |
| 1134 | * Fix up the FILE fields, and set __cleanup for output flush on |
| 1135 | * exit (since we are buffered in some way). |
| 1136 | */ |
| 1137 | if (mode == _IOLBF) |
| 1138 | flags |= __SLBF; |
| 1139 | fp->_flags = flags; |
| 1140 | fp->_bf._base = fp->_p = reinterpret_cast<unsigned char*>(buf); |
| 1141 | fp->_bf._size = size; |
| 1142 | /* fp->_lbfsize is still 0 */ |
| 1143 | if (flags & __SWR) { |
| 1144 | /* |
| 1145 | * Begin or continue writing: see __swsetup(). Note |
| 1146 | * that __SNBF is impossible (it was handled earlier). |
| 1147 | */ |
| 1148 | if (flags & __SLBF) { |
| 1149 | fp->_w = 0; |
| 1150 | fp->_lbfsize = -fp->_bf._size; |
| 1151 | } else |
| 1152 | fp->_w = size; |
| 1153 | } else { |
| 1154 | /* begin/continue reading, or stay in intermediate state */ |
| 1155 | fp->_w = 0; |
| 1156 | } |
| 1157 | FUNLOCKFILE(fp); |
| 1158 | |
| 1159 | return (ret); |
| 1160 | } |
| 1161 | |
| Elliott Hughes | 53cf348 | 2016-08-09 13:06:41 -0700 | [diff] [blame] | 1162 | int snprintf(char* s, size_t n, const char* fmt, ...) { |
| 1163 | PRINTF_IMPL(vsnprintf(s, n, fmt, ap)); |
| 1164 | } |
| 1165 | |
| 1166 | int sprintf(char* s, const char* fmt, ...) { |
| Elliott Hughes | fb3873d | 2016-08-10 11:07:54 -0700 | [diff] [blame] | 1167 | PRINTF_IMPL(vsprintf(s, fmt, ap)); |
| Elliott Hughes | 53cf348 | 2016-08-09 13:06:41 -0700 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | int sscanf(const char* s, const char* fmt, ...) { |
| 1171 | PRINTF_IMPL(vsscanf(s, fmt, ap)); |
| 1172 | } |
| 1173 | |
| Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 1174 | int swprintf(wchar_t* s, size_t n, const wchar_t* fmt, ...) { |
| 1175 | PRINTF_IMPL(vswprintf(s, n, fmt, ap)); |
| 1176 | } |
| 1177 | |
| 1178 | int swscanf(const wchar_t* s, const wchar_t* fmt, ...) { |
| 1179 | PRINTF_IMPL(vswscanf(s, fmt, ap)); |
| 1180 | } |
| 1181 | |
| Elliott Hughes | 618303c | 2017-11-02 16:58:44 -0700 | [diff] [blame] | 1182 | int vfprintf(FILE* fp, const char* fmt, va_list ap) { |
| 1183 | ScopedFileLock sfl(fp); |
| 1184 | return __vfprintf(fp, fmt, ap); |
| 1185 | } |
| 1186 | |
| Elliott Hughes | 345b727 | 2017-11-10 16:20:43 -0800 | [diff] [blame] | 1187 | int vfscanf(FILE* fp, const char* fmt, va_list ap) { |
| 1188 | ScopedFileLock sfl(fp); |
| 1189 | return __svfscanf(fp, fmt, ap); |
| 1190 | } |
| 1191 | |
| Elliott Hughes | 618303c | 2017-11-02 16:58:44 -0700 | [diff] [blame] | 1192 | int vfwprintf(FILE* fp, const wchar_t* fmt, va_list ap) { |
| 1193 | ScopedFileLock sfl(fp); |
| 1194 | return __vfwprintf(fp, fmt, ap); |
| 1195 | } |
| 1196 | |
| Elliott Hughes | 345b727 | 2017-11-10 16:20:43 -0800 | [diff] [blame] | 1197 | int vfwscanf(FILE* fp, const wchar_t* fmt, va_list ap) { |
| 1198 | ScopedFileLock sfl(fp); |
| 1199 | return __vfwscanf(fp, fmt, ap); |
| 1200 | } |
| 1201 | |
| Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 1202 | int vprintf(const char* fmt, va_list ap) { |
| 1203 | return vfprintf(stdout, fmt, ap); |
| 1204 | } |
| 1205 | |
| 1206 | int vscanf(const char* fmt, va_list ap) { |
| 1207 | return vfscanf(stdin, fmt, ap); |
| 1208 | } |
| 1209 | |
| Elliott Hughes | fb3873d | 2016-08-10 11:07:54 -0700 | [diff] [blame] | 1210 | int vsnprintf(char* s, size_t n, const char* fmt, va_list ap) { |
| 1211 | // stdio internals use int rather than size_t. |
| 1212 | static_assert(INT_MAX <= SSIZE_MAX, "SSIZE_MAX too large to fit in int"); |
| 1213 | |
| 1214 | __check_count("vsnprintf", "size", n); |
| 1215 | |
| 1216 | // Stdio internals do not deal correctly with zero length buffer. |
| Elliott Hughes | 68ae6ad | 2020-07-21 16:11:30 -0700 | [diff] [blame] | 1217 | char one_byte_buffer[1]; |
| Elliott Hughes | fb3873d | 2016-08-10 11:07:54 -0700 | [diff] [blame] | 1218 | if (n == 0) { |
| Elliott Hughes | 68ae6ad | 2020-07-21 16:11:30 -0700 | [diff] [blame] | 1219 | s = one_byte_buffer; |
| Elliott Hughes | fb3873d | 2016-08-10 11:07:54 -0700 | [diff] [blame] | 1220 | n = 1; |
| 1221 | } |
| 1222 | |
| 1223 | FILE f; |
| 1224 | __sfileext fext; |
| 1225 | _FILEEXT_SETUP(&f, &fext); |
| 1226 | f._file = -1; |
| 1227 | f._flags = __SWR | __SSTR; |
| 1228 | f._bf._base = f._p = reinterpret_cast<unsigned char*>(s); |
| 1229 | f._bf._size = f._w = n - 1; |
| 1230 | |
| 1231 | int result = __vfprintf(&f, fmt, ap); |
| 1232 | *f._p = '\0'; |
| 1233 | return result; |
| 1234 | } |
| 1235 | |
| Elliott Hughes | 53cf348 | 2016-08-09 13:06:41 -0700 | [diff] [blame] | 1236 | int vsprintf(char* s, const char* fmt, va_list ap) { |
| Elliott Hughes | fb3873d | 2016-08-10 11:07:54 -0700 | [diff] [blame] | 1237 | return vsnprintf(s, SSIZE_MAX, fmt, ap); |
| Elliott Hughes | 53cf348 | 2016-08-09 13:06:41 -0700 | [diff] [blame] | 1238 | } |
| 1239 | |
| Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 1240 | int vwprintf(const wchar_t* fmt, va_list ap) { |
| 1241 | return vfwprintf(stdout, fmt, ap); |
| 1242 | } |
| 1243 | |
| 1244 | int vwscanf(const wchar_t* fmt, va_list ap) { |
| 1245 | return vfwscanf(stdin, fmt, ap); |
| 1246 | } |
| Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 1247 | |
| 1248 | int wprintf(const wchar_t* fmt, ...) { |
| 1249 | PRINTF_IMPL(vfwprintf(stdout, fmt, ap)); |
| 1250 | } |
| 1251 | |
| 1252 | int wscanf(const wchar_t* fmt, ...) { |
| 1253 | PRINTF_IMPL(vfwscanf(stdin, fmt, ap)); |
| 1254 | } |
| Dan Albert | 3037ea4 | 2016-10-06 15:46:45 -0700 | [diff] [blame] | 1255 | |
| Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame] | 1256 | static int fflush_all() { |
| Ryan Prichard | c485cdb | 2019-04-30 14:47:34 -0700 | [diff] [blame] | 1257 | return _fwalk(__sflush_locked); |
| Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame] | 1258 | } |
| 1259 | |
| 1260 | int fflush(FILE* fp) { |
| 1261 | if (fp == nullptr) return fflush_all(); |
| 1262 | ScopedFileLock sfl(fp); |
| 1263 | return fflush_unlocked(fp); |
| 1264 | } |
| 1265 | |
| 1266 | int fflush_unlocked(FILE* fp) { |
| 1267 | if (fp == nullptr) return fflush_all(); |
| 1268 | if ((fp->_flags & (__SWR | __SRW)) == 0) { |
| 1269 | errno = EBADF; |
| 1270 | return EOF; |
| 1271 | } |
| 1272 | return __sflush(fp); |
| 1273 | } |
| 1274 | |
| Elliott Hughes | 00a2a6f | 2024-08-12 21:32:07 +0000 | [diff] [blame] | 1275 | int fpurge(FILE* fp) { |
| 1276 | CHECK_FP(fp); |
| 1277 | |
| 1278 | ScopedFileLock sfl(fp); |
| 1279 | |
| 1280 | if (fp->_flags == 0) { |
| 1281 | // Already freed! |
| 1282 | errno = EBADF; |
| 1283 | return EOF; |
| 1284 | } |
| 1285 | |
| 1286 | if (HASUB(fp)) FREEUB(fp); |
| 1287 | WCIO_FREE(fp); |
| 1288 | fp->_p = fp->_bf._base; |
| 1289 | fp->_r = 0; |
| 1290 | fp->_w = fp->_flags & (__SLBF | __SNBF) ? 0 : fp->_bf._size; |
| 1291 | return 0; |
| 1292 | } |
| 1293 | __strong_alias(__fpurge, fpurge); |
| 1294 | |
| George Burgess IV | 9024235 | 2018-02-06 12:51:31 -0800 | [diff] [blame] | 1295 | 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] | 1296 | CHECK_FP(fp); |
| 1297 | ScopedFileLock sfl(fp); |
| 1298 | return fread_unlocked(buf, size, count, fp); |
| 1299 | } |
| 1300 | |
| 1301 | size_t fread_unlocked(void* buf, size_t size, size_t count, FILE* fp) { |
| 1302 | CHECK_FP(fp); |
| 1303 | |
| 1304 | size_t desired_total; |
| 1305 | if (__builtin_mul_overflow(size, count, &desired_total)) { |
| 1306 | errno = EOVERFLOW; |
| 1307 | fp->_flags |= __SERR; |
| 1308 | return 0; |
| 1309 | } |
| 1310 | |
| 1311 | size_t total = desired_total; |
| 1312 | if (total == 0) return 0; |
| 1313 | |
| Elliott Hughes | 531199c | 2023-05-09 16:11:49 -0700 | [diff] [blame] | 1314 | _SET_ORIENTATION(fp, ORIENT_BYTES); |
| Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame] | 1315 | |
| 1316 | // TODO: how can this ever happen?! |
| 1317 | if (fp->_r < 0) fp->_r = 0; |
| 1318 | |
| 1319 | // Ensure _bf._size is valid. |
| 1320 | if (fp->_bf._base == nullptr) __smakebuf(fp); |
| 1321 | |
| 1322 | char* dst = static_cast<char*>(buf); |
| 1323 | |
| 1324 | while (total > 0) { |
| 1325 | // Copy data out of the buffer. |
| 1326 | size_t buffered_bytes = MIN(static_cast<size_t>(fp->_r), total); |
| 1327 | memcpy(dst, fp->_p, buffered_bytes); |
| 1328 | fp->_p += buffered_bytes; |
| 1329 | fp->_r -= buffered_bytes; |
| 1330 | dst += buffered_bytes; |
| 1331 | total -= buffered_bytes; |
| 1332 | |
| 1333 | // Are we done? |
| 1334 | if (total == 0) goto out; |
| 1335 | |
| 1336 | // Do we have so much more to read that we should avoid copying it through the buffer? |
| 1337 | if (total > static_cast<size_t>(fp->_bf._size)) break; |
| 1338 | |
| 1339 | // Less than a buffer to go, so refill the buffer and go around the loop again. |
| 1340 | if (__srefill(fp)) goto out; |
| 1341 | } |
| 1342 | |
| 1343 | // Read directly into the caller's buffer. |
| 1344 | while (total > 0) { |
| Elliott Hughes | be78fc9 | 2022-07-28 20:58:45 +0000 | [diff] [blame] | 1345 | // The _read function pointer takes an int instead of a size_t. |
| 1346 | int chunk_size = MIN(total, INT_MAX); |
| 1347 | ssize_t bytes_read = (*fp->_read)(fp->_cookie, dst, chunk_size); |
| Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame] | 1348 | if (bytes_read <= 0) { |
| 1349 | fp->_flags |= (bytes_read == 0) ? __SEOF : __SERR; |
| 1350 | break; |
| 1351 | } |
| 1352 | dst += bytes_read; |
| 1353 | total -= bytes_read; |
| 1354 | } |
| 1355 | |
| 1356 | out: |
| 1357 | return ((desired_total - total) / size); |
| 1358 | } |
| 1359 | |
| 1360 | size_t fwrite(const void* buf, size_t size, size_t count, FILE* fp) { |
| 1361 | CHECK_FP(fp); |
| 1362 | ScopedFileLock sfl(fp); |
| 1363 | return fwrite_unlocked(buf, size, count, fp); |
| 1364 | } |
| 1365 | |
| 1366 | size_t fwrite_unlocked(const void* buf, size_t size, size_t count, FILE* fp) { |
| 1367 | CHECK_FP(fp); |
| 1368 | |
| 1369 | size_t n; |
| 1370 | if (__builtin_mul_overflow(size, count, &n)) { |
| 1371 | errno = EOVERFLOW; |
| 1372 | fp->_flags |= __SERR; |
| 1373 | return 0; |
| 1374 | } |
| 1375 | |
| 1376 | if (n == 0) return 0; |
| 1377 | |
| 1378 | __siov iov = { .iov_base = const_cast<void*>(buf), .iov_len = n }; |
| 1379 | __suio uio = { .uio_iov = &iov, .uio_iovcnt = 1, .uio_resid = n }; |
| 1380 | |
| Elliott Hughes | 531199c | 2023-05-09 16:11:49 -0700 | [diff] [blame] | 1381 | _SET_ORIENTATION(fp, ORIENT_BYTES); |
| Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame] | 1382 | |
| 1383 | // The usual case is success (__sfvwrite returns 0); skip the divide if this happens, |
| 1384 | // since divides are generally slow. |
| 1385 | return (__sfvwrite(fp, &uio) == 0) ? count : ((n - uio.uio_resid) / size); |
| 1386 | } |
| 1387 | |
| Elliott Hughes | 468efc8 | 2018-07-10 14:39:49 -0700 | [diff] [blame] | 1388 | static FILE* __popen_fail(int fds[2]) { |
| 1389 | ErrnoRestorer errno_restorer; |
| 1390 | close(fds[0]); |
| 1391 | close(fds[1]); |
| 1392 | return nullptr; |
| 1393 | } |
| 1394 | |
| 1395 | FILE* popen(const char* cmd, const char* mode) { |
| Elliott Hughes | 468efc8 | 2018-07-10 14:39:49 -0700 | [diff] [blame] | 1396 | // Was the request for a socketpair or just a pipe? |
| 1397 | int fds[2]; |
| 1398 | bool bidirectional = false; |
| 1399 | if (strchr(mode, '+') != nullptr) { |
| 1400 | if (socketpair(AF_LOCAL, SOCK_CLOEXEC | SOCK_STREAM, 0, fds) == -1) return nullptr; |
| 1401 | bidirectional = true; |
| 1402 | mode = "r+"; |
| 1403 | } else { |
| 1404 | if (pipe2(fds, O_CLOEXEC) == -1) return nullptr; |
| 1405 | mode = strrchr(mode, 'r') ? "r" : "w"; |
| 1406 | } |
| 1407 | |
| 1408 | // If the parent wants to read, the child's fd needs to be stdout. |
| 1409 | int parent, child, desired_child_fd; |
| 1410 | if (*mode == 'r') { |
| 1411 | parent = 0; |
| 1412 | child = 1; |
| 1413 | desired_child_fd = STDOUT_FILENO; |
| 1414 | } else { |
| 1415 | parent = 1; |
| 1416 | child = 0; |
| 1417 | desired_child_fd = STDIN_FILENO; |
| 1418 | } |
| 1419 | |
| 1420 | // Ensure that the child fd isn't the desired child fd. |
| 1421 | if (fds[child] == desired_child_fd) { |
| 1422 | int new_fd = fcntl(fds[child], F_DUPFD_CLOEXEC, 0); |
| 1423 | if (new_fd == -1) return __popen_fail(fds); |
| 1424 | close(fds[child]); |
| 1425 | fds[child] = new_fd; |
| 1426 | } |
| 1427 | |
| 1428 | pid_t pid = vfork(); |
| 1429 | if (pid == -1) return __popen_fail(fds); |
| 1430 | |
| 1431 | if (pid == 0) { |
| 1432 | close(fds[parent]); |
| Elliott Hughes | 468efc8 | 2018-07-10 14:39:49 -0700 | [diff] [blame] | 1433 | // dup2 so that the child fd isn't closed on exec. |
| 1434 | if (dup2(fds[child], desired_child_fd) == -1) _exit(127); |
| 1435 | close(fds[child]); |
| 1436 | if (bidirectional) dup2(STDOUT_FILENO, STDIN_FILENO); |
| Elliott Hughes | b6b7e2e | 2021-11-04 17:18:58 -0700 | [diff] [blame] | 1437 | execl(__bionic_get_shell_path(), "sh", "-c", "--", cmd, nullptr); |
| Elliott Hughes | 468efc8 | 2018-07-10 14:39:49 -0700 | [diff] [blame] | 1438 | _exit(127); |
| 1439 | } |
| 1440 | |
| 1441 | FILE* fp = fdopen(fds[parent], mode); |
| 1442 | if (fp == nullptr) return __popen_fail(fds); |
| 1443 | |
| Elliott Hughes | 468efc8 | 2018-07-10 14:39:49 -0700 | [diff] [blame] | 1444 | close(fds[child]); |
| 1445 | |
| 1446 | _EXT(fp)->_popen_pid = pid; |
| 1447 | return fp; |
| 1448 | } |
| 1449 | |
| Elliott Hughes | 20dd3fe | 2023-03-02 01:26:29 +0000 | [diff] [blame] | 1450 | void flockfile(FILE* fp) { |
| 1451 | CHECK_FP(fp); |
| 1452 | pthread_mutex_lock(&_EXT(fp)->_lock); |
| 1453 | } |
| 1454 | |
| 1455 | int ftrylockfile(FILE* fp) { |
| 1456 | CHECK_FP(fp); |
| 1457 | // The specification for ftrylockfile() says it returns 0 on success, |
| 1458 | // or non-zero on error. We don't bother canonicalizing to 0/-1... |
| 1459 | return pthread_mutex_trylock(&_EXT(fp)->_lock); |
| 1460 | } |
| 1461 | |
| 1462 | void funlockfile(FILE* fp) { |
| 1463 | CHECK_FP(fp); |
| 1464 | pthread_mutex_unlock(&_EXT(fp)->_lock); |
| 1465 | } |
| 1466 | |
| Dan Albert | 3037ea4 | 2016-10-06 15:46:45 -0700 | [diff] [blame] | 1467 | namespace { |
| 1468 | |
| 1469 | namespace phony { |
| 1470 | #include <bits/struct_file.h> |
| 1471 | } |
| 1472 | |
| 1473 | static_assert(sizeof(::__sFILE) == sizeof(phony::__sFILE), |
| 1474 | "size mismatch between `struct __sFILE` implementation and public stub"); |
| 1475 | static_assert(alignof(::__sFILE) == alignof(phony::__sFILE), |
| 1476 | "alignment mismatch between `struct __sFILE` implementation and public stub"); |
| 1477 | |
| 1478 | } |