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