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