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