Dmitriy Ivanov | 623b0d0 | 2014-05-14 23:11:05 -0700 | [diff] [blame] | 1 | /* $OpenBSD: findfp.c,v 1.15 2013/12/17 16:33:27 deraadt Exp $ */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2 | /*- |
| 3 | * Copyright (c) 1990, 1993 |
| 4 | * The Regents of the University of California. All rights reserved. |
| 5 | * |
| 6 | * This code is derived from software contributed to Berkeley by |
| 7 | * Chris Torek. |
| 8 | * |
| 9 | * Redistribution and use in source and binary forms, with or without |
| 10 | * modification, are permitted provided that the following conditions |
| 11 | * are met: |
| 12 | * 1. Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in the |
| 16 | * documentation and/or other materials provided with the distribution. |
| 17 | * 3. Neither the name of the University nor the names of its contributors |
| 18 | * may be used to endorse or promote products derived from this software |
| 19 | * without specific prior written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 31 | * SUCH DAMAGE. |
| 32 | */ |
| 33 | |
Elliott Hughes | 53cf348 | 2016-08-09 13:06:41 -0700 | [diff] [blame] | 34 | #define __BIONIC_NO_STDIO_FORTIFY |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 35 | #include <stdio.h> |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 36 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 37 | #include <errno.h> |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 38 | #include <fcntl.h> |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 39 | #include <limits.h> |
Elliott Hughes | 20788ae | 2016-06-09 15:16:32 -0700 | [diff] [blame] | 40 | #include <paths.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 41 | #include <stdlib.h> |
| 42 | #include <string.h> |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 43 | #include <sys/param.h> |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 44 | #include <sys/stat.h> |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 45 | #include <unistd.h> |
| 46 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 47 | #include "local.h" |
| 48 | #include "glue.h" |
Elliott Hughes | fb3873d | 2016-08-10 11:07:54 -0700 | [diff] [blame] | 49 | #include "private/bionic_fortify.h" |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 50 | #include "private/ErrnoRestorer.h" |
Elliott Hughes | 6a03abc | 2014-11-03 12:32:17 -0800 | [diff] [blame] | 51 | #include "private/thread_private.h" |
| 52 | |
| 53 | #define ALIGNBYTES (sizeof(uintptr_t) - 1) |
| 54 | #define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) &~ ALIGNBYTES) |
Calin Juravle | c20de90 | 2014-03-20 15:21:32 +0000 | [diff] [blame] | 55 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 56 | #define NDYNAMIC 10 /* add ten more whenever necessary */ |
| 57 | |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 58 | #define PRINTF_IMPL(expr) \ |
| 59 | va_list ap; \ |
| 60 | va_start(ap, fmt); \ |
| 61 | int result = (expr); \ |
| 62 | va_end(ap); \ |
| 63 | return result; |
| 64 | |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 65 | #define std(flags, file) \ |
| 66 | {0,0,0,flags,file,{0,0},0,__sF+file,__sclose,__sread,nullptr,__swrite, \ |
| 67 | {(unsigned char *)(__sFext+file), 0},nullptr,0,{0},{0},{0,0},0,0} |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 68 | |
Kenny Root | f582340 | 2011-02-12 07:13:44 -0800 | [diff] [blame] | 69 | _THREAD_PRIVATE_MUTEX(__sfp_mutex); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 70 | |
Elliott Hughes | 29ee639 | 2015-12-07 11:07:15 -0800 | [diff] [blame] | 71 | // TODO: when we no longer have to support both clang and GCC, we can simplify all this. |
| 72 | #define SBUF_INIT {0,0} |
| 73 | #if defined(__LP64__) |
| 74 | #define MBSTATE_T_INIT {{0},{0}} |
| 75 | #else |
| 76 | #define MBSTATE_T_INIT {{0}} |
| 77 | #endif |
| 78 | #define WCHAR_IO_DATA_INIT {MBSTATE_T_INIT,MBSTATE_T_INIT,{0},0,0} |
| 79 | |
Elliott Hughes | bb46afd | 2015-12-04 18:03:12 -0800 | [diff] [blame] | 80 | static struct __sfileext __sFext[3] = { |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 81 | { SBUF_INIT, WCHAR_IO_DATA_INIT, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP, false, __sseek64 }, |
| 82 | { SBUF_INIT, WCHAR_IO_DATA_INIT, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP, false, __sseek64 }, |
| 83 | { SBUF_INIT, WCHAR_IO_DATA_INIT, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP, false, __sseek64 }, |
Elliott Hughes | bb46afd | 2015-12-04 18:03:12 -0800 | [diff] [blame] | 84 | }; |
Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 85 | |
| 86 | // __sF is exported for backwards compatibility. Until M, we didn't have symbols |
| 87 | // for stdin/stdout/stderr; they were macros accessing __sF. |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 88 | FILE __sF[3] = { |
Elliott Hughes | bb46afd | 2015-12-04 18:03:12 -0800 | [diff] [blame] | 89 | std(__SRD, STDIN_FILENO), |
| 90 | std(__SWR, STDOUT_FILENO), |
| 91 | std(__SWR|__SNBF, STDERR_FILENO), |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 92 | }; |
Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 93 | |
Elliott Hughes | 168667c | 2014-11-14 14:42:59 -0800 | [diff] [blame] | 94 | FILE* stdin = &__sF[0]; |
| 95 | FILE* stdout = &__sF[1]; |
| 96 | FILE* stderr = &__sF[2]; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 97 | |
Elliott Hughes | bb46afd | 2015-12-04 18:03:12 -0800 | [diff] [blame] | 98 | struct glue __sglue = { NULL, 3, __sF }; |
| 99 | static struct glue* lastglue = &__sglue; |
| 100 | |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 101 | class ScopedFileLock { |
| 102 | public: |
Chih-Hung Hsieh | 62e3a07 | 2016-05-03 12:08:05 -0700 | [diff] [blame] | 103 | explicit ScopedFileLock(FILE* fp) : fp_(fp) { |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 104 | FLOCKFILE(fp_); |
| 105 | } |
| 106 | ~ScopedFileLock() { |
| 107 | FUNLOCKFILE(fp_); |
| 108 | } |
| 109 | |
| 110 | private: |
| 111 | FILE* fp_; |
| 112 | }; |
| 113 | |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 114 | static glue* moreglue(int n) { |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 115 | static FILE empty; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 116 | |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 117 | char* data = new char[sizeof(glue) + ALIGNBYTES + n * sizeof(FILE) + n * sizeof(__sfileext)]; |
| 118 | if (data == nullptr) return nullptr; |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 119 | |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 120 | glue* g = reinterpret_cast<glue*>(data); |
| 121 | FILE* p = reinterpret_cast<FILE*>(ALIGN(data + sizeof(*g))); |
| 122 | __sfileext* pext = reinterpret_cast<__sfileext*>(ALIGN(data + sizeof(*g)) + n * sizeof(FILE)); |
| 123 | g->next = NULL; |
| 124 | g->niobs = n; |
| 125 | g->iobs = p; |
| 126 | while (--n >= 0) { |
| 127 | *p = empty; |
| 128 | _FILEEXT_SETUP(p, pext); |
| 129 | p++; |
| 130 | pext++; |
| 131 | } |
| 132 | return g; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Elliott Hughes | 80e4c15 | 2017-07-21 13:57:55 -0700 | [diff] [blame^] | 135 | static inline void free_fgetln_buffer(FILE* fp) { |
| 136 | if (__predict_false(fp->_lb._base != nullptr)) { |
| 137 | free(fp->_lb._base); |
| 138 | fp->_lb._base = nullptr; |
| 139 | } |
| 140 | } |
| 141 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 142 | /* |
| 143 | * Find a free FILE for fopen et al. |
| 144 | */ |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 145 | FILE* __sfp(void) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 146 | FILE *fp; |
| 147 | int n; |
| 148 | struct glue *g; |
| 149 | |
Kenny Root | f582340 | 2011-02-12 07:13:44 -0800 | [diff] [blame] | 150 | _THREAD_PRIVATE_MUTEX_LOCK(__sfp_mutex); |
| 151 | for (g = &__sglue; g != NULL; g = g->next) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 152 | for (fp = g->iobs, n = g->niobs; --n >= 0; fp++) |
| 153 | if (fp->_flags == 0) |
| 154 | goto found; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 155 | } |
Kenny Root | f582340 | 2011-02-12 07:13:44 -0800 | [diff] [blame] | 156 | |
| 157 | /* release lock while mallocing */ |
| 158 | _THREAD_PRIVATE_MUTEX_UNLOCK(__sfp_mutex); |
| 159 | if ((g = moreglue(NDYNAMIC)) == NULL) |
| 160 | return (NULL); |
| 161 | _THREAD_PRIVATE_MUTEX_LOCK(__sfp_mutex); |
| 162 | lastglue->next = g; |
| 163 | lastglue = g; |
| 164 | fp = g->iobs; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 165 | found: |
| 166 | fp->_flags = 1; /* reserve this slot; caller sets real flags */ |
Kenny Root | f582340 | 2011-02-12 07:13:44 -0800 | [diff] [blame] | 167 | _THREAD_PRIVATE_MUTEX_UNLOCK(__sfp_mutex); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 168 | fp->_p = NULL; /* no current pointer */ |
| 169 | fp->_w = 0; /* nothing to read or write */ |
| 170 | fp->_r = 0; |
| 171 | fp->_bf._base = NULL; /* no buffer */ |
| 172 | fp->_bf._size = 0; |
| 173 | fp->_lbfsize = 0; /* not line buffered */ |
| 174 | fp->_file = -1; /* no file */ |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 175 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 176 | fp->_lb._base = NULL; /* no line buffer */ |
| 177 | fp->_lb._size = 0; |
| 178 | _FILEEXT_INIT(fp); |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 179 | |
| 180 | // Caller sets cookie, _read/_write etc. |
| 181 | // We explicitly clear _seek and _seek64 to prevent subtle bugs. |
| 182 | fp->_seek = nullptr; |
| 183 | _EXT(fp)->_seek64 = nullptr; |
| 184 | |
| 185 | return fp; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 188 | extern "C" __LIBC_HIDDEN__ void __libc_stdio_cleanup(void) { |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 189 | // Equivalent to fflush(nullptr), but without all the locking since we're shutting down anyway. |
| 190 | _fwalk(__sflush); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 191 | } |
Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 192 | |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 193 | static FILE* __fopen(int fd, int flags) { |
| 194 | #if !defined(__LP64__) |
| 195 | if (fd > SHRT_MAX) { |
| 196 | errno = EMFILE; |
| 197 | return nullptr; |
| 198 | } |
| 199 | #endif |
| 200 | |
| 201 | FILE* fp = __sfp(); |
| 202 | if (fp != nullptr) { |
| 203 | fp->_file = fd; |
| 204 | fp->_flags = flags; |
| 205 | fp->_cookie = fp; |
| 206 | fp->_read = __sread; |
| 207 | fp->_write = __swrite; |
| 208 | fp->_close = __sclose; |
| 209 | _EXT(fp)->_seek64 = __sseek64; |
| 210 | } |
| 211 | return fp; |
| 212 | } |
| 213 | |
| 214 | FILE* fopen(const char* file, const char* mode) { |
| 215 | int oflags; |
| 216 | int flags = __sflags(mode, &oflags); |
| 217 | if (flags == 0) return nullptr; |
| 218 | |
| 219 | int fd = open(file, oflags, DEFFILEMODE); |
| 220 | if (fd == -1) { |
| 221 | return nullptr; |
| 222 | } |
| 223 | |
| 224 | FILE* fp = __fopen(fd, flags); |
| 225 | if (fp == nullptr) { |
| 226 | ErrnoRestorer errno_restorer; |
| 227 | close(fd); |
| 228 | return nullptr; |
| 229 | } |
| 230 | |
| 231 | // When opening in append mode, even though we use O_APPEND, |
| 232 | // we need to seek to the end so that ftell() gets the right |
| 233 | // answer. If the user then alters the seek pointer, or |
| 234 | // the file extends, this will fail, but there is not much |
| 235 | // we can do about this. (We could set __SAPP and check in |
| 236 | // fseek and ftell.) |
| 237 | // TODO: check in __sseek instead. |
| 238 | if (oflags & O_APPEND) __sseek64(fp, 0, SEEK_END); |
| 239 | |
| 240 | return fp; |
| 241 | } |
Elliott Hughes | f226ee5 | 2016-02-03 11:24:28 -0800 | [diff] [blame] | 242 | __strong_alias(fopen64, fopen); |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 243 | |
| 244 | FILE* fdopen(int fd, const char* mode) { |
| 245 | int oflags; |
| 246 | int flags = __sflags(mode, &oflags); |
| 247 | if (flags == 0) return nullptr; |
| 248 | |
| 249 | // Make sure the mode the user wants is a subset of the actual mode. |
| 250 | int fdflags = fcntl(fd, F_GETFL, 0); |
| 251 | if (fdflags < 0) return nullptr; |
| 252 | int tmp = fdflags & O_ACCMODE; |
| 253 | if (tmp != O_RDWR && (tmp != (oflags & O_ACCMODE))) { |
| 254 | errno = EINVAL; |
| 255 | return nullptr; |
| 256 | } |
| 257 | |
| 258 | // If opened for appending, but underlying descriptor does not have |
| 259 | // O_APPEND bit set, assert __SAPP so that __swrite() will lseek to |
| 260 | // end before each write. |
| 261 | // TODO: use fcntl(2) to set O_APPEND instead. |
| 262 | if ((oflags & O_APPEND) && !(fdflags & O_APPEND)) flags |= __SAPP; |
| 263 | |
| 264 | // If close-on-exec was requested, then turn it on if not already. |
| 265 | if ((oflags & O_CLOEXEC) && !((tmp = fcntl(fd, F_GETFD)) & FD_CLOEXEC)) { |
| 266 | fcntl(fd, F_SETFD, tmp | FD_CLOEXEC); |
| 267 | } |
| 268 | |
| 269 | return __fopen(fd, flags); |
| 270 | } |
| 271 | |
| 272 | // Re-direct an existing, open (probably) file to some other file. |
| 273 | // ANSI is written such that the original file gets closed if at |
| 274 | // all possible, no matter what. |
| 275 | // TODO: rewrite this mess completely. |
| 276 | FILE* freopen(const char* file, const char* mode, FILE* fp) { |
| 277 | int oflags; |
| 278 | int flags = __sflags(mode, &oflags); |
| 279 | if (flags == 0) { |
| 280 | fclose(fp); |
| 281 | return nullptr; |
| 282 | } |
| 283 | |
| 284 | ScopedFileLock sfl(fp); |
| 285 | |
| 286 | // There are actually programs that depend on being able to "freopen" |
| 287 | // descriptors that weren't originally open. Keep this from breaking. |
| 288 | // Remember whether the stream was open to begin with, and which file |
| 289 | // descriptor (if any) was associated with it. If it was attached to |
| 290 | // a descriptor, defer closing it; freopen("/dev/stdin", "r", stdin) |
| 291 | // should work. This is unnecessary if it was not a Unix file. |
| 292 | int isopen, wantfd; |
| 293 | if (fp->_flags == 0) { |
| 294 | fp->_flags = __SEOF; // Hold on to it. |
| 295 | isopen = 0; |
| 296 | wantfd = -1; |
| 297 | } else { |
| 298 | // Flush the stream; ANSI doesn't require this. |
| 299 | if (fp->_flags & __SWR) __sflush(fp); |
| 300 | |
| 301 | // If close is NULL, closing is a no-op, hence pointless. |
| 302 | isopen = fp->_close != NULL; |
| 303 | if ((wantfd = fp->_file) < 0 && isopen) { |
| 304 | (*fp->_close)(fp->_cookie); |
| 305 | isopen = 0; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | // Get a new descriptor to refer to the new file. |
| 310 | int fd = open(file, oflags, DEFFILEMODE); |
| 311 | if (fd < 0 && isopen) { |
| 312 | // If out of fd's close the old one and try again. |
| 313 | if (errno == ENFILE || errno == EMFILE) { |
| 314 | (*fp->_close)(fp->_cookie); |
| 315 | isopen = 0; |
| 316 | fd = open(file, oflags, DEFFILEMODE); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | int sverrno = errno; |
| 321 | |
| 322 | // Finish closing fp. Even if the open succeeded above, we cannot |
| 323 | // keep fp->_base: it may be the wrong size. This loses the effect |
| 324 | // of any setbuffer calls, but stdio has always done this before. |
| 325 | if (isopen && fd != wantfd) (*fp->_close)(fp->_cookie); |
| 326 | if (fp->_flags & __SMBF) free(fp->_bf._base); |
| 327 | fp->_w = 0; |
| 328 | fp->_r = 0; |
| 329 | fp->_p = NULL; |
| 330 | fp->_bf._base = NULL; |
| 331 | fp->_bf._size = 0; |
| 332 | fp->_lbfsize = 0; |
| 333 | if (HASUB(fp)) FREEUB(fp); |
| 334 | _UB(fp)._size = 0; |
| 335 | WCIO_FREE(fp); |
Elliott Hughes | 80e4c15 | 2017-07-21 13:57:55 -0700 | [diff] [blame^] | 336 | free_fgetln_buffer(fp); |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 337 | fp->_lb._size = 0; |
| 338 | |
| 339 | if (fd < 0) { // Did not get it after all. |
| 340 | fp->_flags = 0; // Release. |
| 341 | errno = sverrno; // Restore errno in case _close clobbered it. |
| 342 | return nullptr; |
| 343 | } |
| 344 | |
| 345 | // If reopening something that was open before on a real file, try |
| 346 | // to maintain the descriptor. Various C library routines (perror) |
| 347 | // assume stderr is always fd STDERR_FILENO, even if being freopen'd. |
| 348 | if (wantfd >= 0 && fd != wantfd) { |
| 349 | if (dup3(fd, wantfd, oflags & O_CLOEXEC) >= 0) { |
| 350 | close(fd); |
| 351 | fd = wantfd; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | // _file is only a short. |
| 356 | if (fd > SHRT_MAX) { |
| 357 | fp->_flags = 0; // Release. |
| 358 | errno = EMFILE; |
| 359 | return nullptr; |
| 360 | } |
| 361 | |
| 362 | fp->_flags = flags; |
| 363 | fp->_file = fd; |
| 364 | fp->_cookie = fp; |
| 365 | fp->_read = __sread; |
| 366 | fp->_write = __swrite; |
| 367 | fp->_close = __sclose; |
| 368 | _EXT(fp)->_seek64 = __sseek64; |
| 369 | |
| 370 | // When opening in append mode, even though we use O_APPEND, |
| 371 | // we need to seek to the end so that ftell() gets the right |
| 372 | // answer. If the user then alters the seek pointer, or |
| 373 | // the file extends, this will fail, but there is not much |
| 374 | // we can do about this. (We could set __SAPP and check in |
| 375 | // fseek and ftell.) |
| 376 | if (oflags & O_APPEND) __sseek64(fp, 0, SEEK_END); |
| 377 | return fp; |
| 378 | } |
Elliott Hughes | f226ee5 | 2016-02-03 11:24:28 -0800 | [diff] [blame] | 379 | __strong_alias(freopen64, freopen); |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 380 | |
Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 381 | int fclose(FILE* fp) { |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 382 | if (fp->_flags == 0) { |
| 383 | // Already freed! |
| 384 | errno = EBADF; |
| 385 | return EOF; |
| 386 | } |
Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 387 | |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 388 | ScopedFileLock sfl(fp); |
| 389 | WCIO_FREE(fp); |
| 390 | int r = fp->_flags & __SWR ? __sflush(fp) : 0; |
| 391 | if (fp->_close != NULL && (*fp->_close)(fp->_cookie) < 0) { |
| 392 | r = EOF; |
| 393 | } |
| 394 | if (fp->_flags & __SMBF) free(fp->_bf._base); |
| 395 | if (HASUB(fp)) FREEUB(fp); |
Elliott Hughes | 80e4c15 | 2017-07-21 13:57:55 -0700 | [diff] [blame^] | 396 | free_fgetln_buffer(fp); |
Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 397 | |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 398 | // Poison this FILE so accesses after fclose will be obvious. |
| 399 | fp->_file = -1; |
| 400 | fp->_r = fp->_w = 0; |
Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 401 | |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 402 | // Release this FILE for reuse. |
| 403 | fp->_flags = 0; |
| 404 | return r; |
Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 405 | } |
| 406 | |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 407 | int fileno_unlocked(FILE* fp) { |
| 408 | int fd = fp->_file; |
| 409 | if (fd == -1) { |
| 410 | errno = EBADF; |
| 411 | return -1; |
| 412 | } |
| 413 | return fd; |
| 414 | } |
| 415 | |
Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 416 | int fileno(FILE* fp) { |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 417 | ScopedFileLock sfl(fp); |
| 418 | return fileno_unlocked(fp); |
Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 419 | } |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 420 | |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 421 | void clearerr_unlocked(FILE* fp) { |
| 422 | return __sclearerr(fp); |
| 423 | } |
| 424 | |
| 425 | void clearerr(FILE* fp) { |
| 426 | ScopedFileLock sfl(fp); |
| 427 | clearerr_unlocked(fp); |
| 428 | } |
| 429 | |
| 430 | int feof_unlocked(FILE* fp) { |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 431 | return ((fp->_flags & __SEOF) != 0); |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | int feof(FILE* fp) { |
| 435 | ScopedFileLock sfl(fp); |
| 436 | return feof_unlocked(fp); |
| 437 | } |
| 438 | |
| 439 | int ferror_unlocked(FILE* fp) { |
| 440 | return __sferror(fp); |
| 441 | } |
| 442 | |
| 443 | int ferror(FILE* fp) { |
| 444 | ScopedFileLock sfl(fp); |
| 445 | return ferror_unlocked(fp); |
| 446 | } |
| 447 | |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 448 | int __sread(void* cookie, char* buf, int n) { |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 449 | FILE* fp = reinterpret_cast<FILE*>(cookie); |
| 450 | return TEMP_FAILURE_RETRY(read(fp->_file, buf, n)); |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | int __swrite(void* cookie, const char* buf, int n) { |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 454 | FILE* fp = reinterpret_cast<FILE*>(cookie); |
| 455 | if (fp->_flags & __SAPP) { |
| 456 | // The FILE* is in append mode, but the underlying fd doesn't have O_APPEND set. |
| 457 | // We need to seek manually. |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 458 | // TODO: use fcntl(2) to set O_APPEND in fdopen(3) instead? |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 459 | TEMP_FAILURE_RETRY(lseek64(fp->_file, 0, SEEK_END)); |
| 460 | } |
| 461 | return TEMP_FAILURE_RETRY(write(fp->_file, buf, n)); |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 462 | } |
| 463 | |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 464 | fpos_t __sseek(void* cookie, fpos_t offset, int whence) { |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 465 | FILE* fp = reinterpret_cast<FILE*>(cookie); |
| 466 | return TEMP_FAILURE_RETRY(lseek(fp->_file, offset, whence)); |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 467 | } |
| 468 | |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 469 | off64_t __sseek64(void* cookie, off64_t offset, int whence) { |
| 470 | FILE* fp = reinterpret_cast<FILE*>(cookie); |
| 471 | return TEMP_FAILURE_RETRY(lseek64(fp->_file, offset, whence)); |
| 472 | } |
| 473 | |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 474 | int __sclose(void* cookie) { |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 475 | FILE* fp = reinterpret_cast<FILE*>(cookie); |
| 476 | return close(fp->_file); |
| 477 | } |
| 478 | |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 479 | static off64_t __seek_unlocked(FILE* fp, off64_t offset, int whence) { |
| 480 | // Use `_seek64` if set, but fall back to `_seek`. |
| 481 | if (_EXT(fp)->_seek64 != nullptr) { |
| 482 | return (*_EXT(fp)->_seek64)(fp->_cookie, offset, whence); |
| 483 | } else if (fp->_seek != nullptr) { |
Elliott Hughes | 955426e | 2016-01-26 18:25:52 -0800 | [diff] [blame] | 484 | off64_t result = (*fp->_seek)(fp->_cookie, offset, whence); |
| 485 | #if !defined(__LP64__) |
| 486 | // Avoid sign extension if off64_t is larger than off_t. |
| 487 | if (result != -1) result &= 0xffffffff; |
| 488 | #endif |
| 489 | return result; |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 490 | } else { |
| 491 | errno = ESPIPE; |
| 492 | return -1; |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 493 | } |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 494 | } |
| 495 | |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 496 | static off64_t __ftello64_unlocked(FILE* fp) { |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 497 | // Find offset of underlying I/O object, then adjust for buffered bytes. |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 498 | __sflush(fp); // May adjust seek offset on append stream. |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 499 | off64_t result = __seek_unlocked(fp, 0, SEEK_CUR); |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 500 | if (result == -1) { |
| 501 | return -1; |
| 502 | } |
| 503 | |
| 504 | if (fp->_flags & __SRD) { |
| 505 | // Reading. Any unread characters (including |
| 506 | // those from ungetc) cause the position to be |
| 507 | // smaller than that in the underlying object. |
| 508 | result -= fp->_r; |
| 509 | if (HASUB(fp)) result -= fp->_ur; |
| 510 | } else if (fp->_flags & __SWR && fp->_p != NULL) { |
| 511 | // Writing. Any buffered characters cause the |
| 512 | // position to be greater than that in the |
| 513 | // underlying object. |
| 514 | result += fp->_p - fp->_bf._base; |
| 515 | } |
| 516 | return result; |
| 517 | } |
| 518 | |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 519 | 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] | 520 | ScopedFileLock sfl(fp); |
| 521 | |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 522 | // Change any SEEK_CUR to SEEK_SET, and check `whence` argument. |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 523 | // After this, whence is either SEEK_SET or SEEK_END. |
| 524 | if (whence == SEEK_CUR) { |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 525 | fpos64_t current_offset = __ftello64_unlocked(fp); |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 526 | if (current_offset == -1) { |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 527 | return -1; |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 528 | } |
| 529 | offset += current_offset; |
| 530 | whence = SEEK_SET; |
| 531 | } else if (whence != SEEK_SET && whence != SEEK_END) { |
| 532 | errno = EINVAL; |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 533 | return -1; |
| 534 | } |
| 535 | |
| 536 | // If our caller has a 32-bit interface, refuse to go past a 32-bit file offset. |
| 537 | if (off_t_bits == 32 && offset > LONG_MAX) { |
| 538 | errno = EOVERFLOW; |
| 539 | return -1; |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | if (fp->_bf._base == NULL) __smakebuf(fp); |
| 543 | |
| 544 | // Flush unwritten data and attempt the seek. |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 545 | if (__sflush(fp) || __seek_unlocked(fp, offset, whence) == -1) { |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 546 | return -1; |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | // Success: clear EOF indicator and discard ungetc() data. |
| 550 | if (HASUB(fp)) FREEUB(fp); |
| 551 | fp->_p = fp->_bf._base; |
| 552 | fp->_r = 0; |
| 553 | /* fp->_w = 0; */ /* unnecessary (I think...) */ |
| 554 | fp->_flags &= ~__SEOF; |
| 555 | return 0; |
| 556 | } |
| 557 | |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 558 | int fseeko(FILE* fp, off_t offset, int whence) { |
| 559 | static_assert(sizeof(off_t) == sizeof(long), "sizeof(off_t) != sizeof(long)"); |
| 560 | return __fseeko64(fp, offset, whence, 8*sizeof(off_t)); |
| 561 | } |
| 562 | __strong_alias(fseek, fseeko); |
| 563 | |
| 564 | int fseeko64(FILE* fp, off64_t offset, int whence) { |
| 565 | return __fseeko64(fp, offset, whence, 8*sizeof(off_t)); |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 566 | } |
| 567 | |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 568 | int fsetpos(FILE* fp, const fpos_t* pos) { |
| 569 | return fseeko(fp, *pos, SEEK_SET); |
| 570 | } |
| 571 | |
| 572 | int fsetpos64(FILE* fp, const fpos64_t* pos) { |
| 573 | return fseeko64(fp, *pos, SEEK_SET); |
| 574 | } |
| 575 | |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 576 | off_t ftello(FILE* fp) { |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 577 | static_assert(sizeof(off_t) == sizeof(long), "sizeof(off_t) != sizeof(long)"); |
| 578 | off64_t result = ftello64(fp); |
| 579 | if (result > LONG_MAX) { |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 580 | errno = EOVERFLOW; |
| 581 | return -1; |
| 582 | } |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 583 | return result; |
| 584 | } |
| 585 | __strong_alias(ftell, ftello); |
| 586 | |
| 587 | off64_t ftello64(FILE* fp) { |
| 588 | ScopedFileLock sfl(fp); |
| 589 | return __ftello64_unlocked(fp); |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 590 | } |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 591 | |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 592 | int fgetpos(FILE* fp, fpos_t* pos) { |
| 593 | *pos = ftello(fp); |
Elliott Hughes | 955426e | 2016-01-26 18:25:52 -0800 | [diff] [blame] | 594 | return (*pos == -1) ? -1 : 0; |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 595 | } |
| 596 | |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 597 | int fgetpos64(FILE* fp, fpos64_t* pos) { |
| 598 | *pos = ftello64(fp); |
Elliott Hughes | 955426e | 2016-01-26 18:25:52 -0800 | [diff] [blame] | 599 | return (*pos == -1) ? -1 : 0; |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame] | 600 | } |
Elliott Hughes | 03e65eb | 2016-01-26 14:13:04 -0800 | [diff] [blame] | 601 | |
| 602 | static FILE* __funopen(const void* cookie, |
| 603 | int (*read_fn)(void*, char*, int), |
| 604 | int (*write_fn)(void*, const char*, int), |
| 605 | int (*close_fn)(void*)) { |
| 606 | if (read_fn == nullptr && write_fn == nullptr) { |
| 607 | errno = EINVAL; |
| 608 | return nullptr; |
| 609 | } |
| 610 | |
| 611 | FILE* fp = __sfp(); |
| 612 | if (fp == nullptr) return nullptr; |
| 613 | |
| 614 | if (read_fn != nullptr && write_fn != nullptr) { |
| 615 | fp->_flags = __SRW; |
| 616 | } else if (read_fn != nullptr) { |
| 617 | fp->_flags = __SRD; |
| 618 | } else if (write_fn != nullptr) { |
| 619 | fp->_flags = __SWR; |
| 620 | } |
| 621 | |
| 622 | fp->_file = -1; |
| 623 | fp->_cookie = const_cast<void*>(cookie); // The funopen(3) API is incoherent. |
| 624 | fp->_read = read_fn; |
| 625 | fp->_write = write_fn; |
| 626 | fp->_close = close_fn; |
| 627 | |
| 628 | return fp; |
| 629 | } |
| 630 | |
| 631 | FILE* funopen(const void* cookie, |
| 632 | int (*read_fn)(void*, char*, int), |
| 633 | int (*write_fn)(void*, const char*, int), |
| 634 | fpos_t (*seek_fn)(void*, fpos_t, int), |
| 635 | int (*close_fn)(void*)) { |
| 636 | FILE* fp = __funopen(cookie, read_fn, write_fn, close_fn); |
| 637 | if (fp != nullptr) { |
| 638 | fp->_seek = seek_fn; |
| 639 | } |
| 640 | return fp; |
| 641 | } |
| 642 | |
| 643 | FILE* funopen64(const void* cookie, |
| 644 | int (*read_fn)(void*, char*, int), |
| 645 | int (*write_fn)(void*, const char*, int), |
| 646 | fpos64_t (*seek_fn)(void*, fpos64_t, int), |
| 647 | int (*close_fn)(void*)) { |
| 648 | FILE* fp = __funopen(cookie, read_fn, write_fn, close_fn); |
| 649 | if (fp != nullptr) { |
| 650 | _EXT(fp)->_seek64 = seek_fn; |
| 651 | } |
| 652 | return fp; |
| 653 | } |
Elliott Hughes | 20788ae | 2016-06-09 15:16:32 -0700 | [diff] [blame] | 654 | |
Elliott Hughes | 53cf348 | 2016-08-09 13:06:41 -0700 | [diff] [blame] | 655 | int asprintf(char** s, const char* fmt, ...) { |
| 656 | PRINTF_IMPL(vasprintf(s, fmt, ap)); |
| 657 | } |
| 658 | |
Elliott Hughes | 20788ae | 2016-06-09 15:16:32 -0700 | [diff] [blame] | 659 | char* ctermid(char* s) { |
| 660 | return s ? strcpy(s, _PATH_TTY) : const_cast<char*>(_PATH_TTY); |
| 661 | } |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 662 | |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 663 | int dprintf(int fd, const char* fmt, ...) { |
| 664 | PRINTF_IMPL(vdprintf(fd, fmt, ap)); |
| 665 | } |
| 666 | |
| 667 | int fprintf(FILE* fp, const char* fmt, ...) { |
| 668 | PRINTF_IMPL(vfprintf(fp, fmt, ap)); |
| 669 | } |
| 670 | |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 671 | int fgetc(FILE* fp) { |
| 672 | return getc(fp); |
| 673 | } |
| 674 | |
| 675 | int fputc(int c, FILE* fp) { |
| 676 | return putc(c, fp); |
| 677 | } |
| 678 | |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 679 | int fscanf(FILE* fp, const char* fmt, ...) { |
| 680 | PRINTF_IMPL(vfscanf(fp, fmt, ap)); |
| 681 | } |
| 682 | |
| 683 | int fwprintf(FILE* fp, const wchar_t* fmt, ...) { |
| 684 | PRINTF_IMPL(vfwprintf(fp, fmt, ap)); |
| 685 | } |
| 686 | |
| 687 | int fwscanf(FILE* fp, const wchar_t* fmt, ...) { |
| 688 | PRINTF_IMPL(vfwscanf(fp, fmt, ap)); |
| 689 | } |
| 690 | |
| 691 | int getc(FILE* fp) { |
| 692 | ScopedFileLock sfl(fp); |
| 693 | return getc_unlocked(fp); |
| 694 | } |
| 695 | |
| 696 | int getc_unlocked(FILE* fp) { |
| 697 | return __sgetc(fp); |
| 698 | } |
| 699 | |
| 700 | int getchar_unlocked() { |
| 701 | return getc_unlocked(stdin); |
| 702 | } |
| 703 | |
| 704 | int getchar() { |
| 705 | return getc(stdin); |
| 706 | } |
| 707 | |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 708 | ssize_t getline(char** buf, size_t* len, FILE* fp) { |
| 709 | return getdelim(buf, len, '\n', fp); |
| 710 | } |
| 711 | |
| 712 | wint_t getwc(FILE* fp) { |
| 713 | return fgetwc(fp); |
| 714 | } |
| 715 | |
| 716 | wint_t getwchar() { |
| 717 | return fgetwc(stdin); |
| 718 | } |
| 719 | |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 720 | int printf(const char* fmt, ...) { |
| 721 | PRINTF_IMPL(vfprintf(stdout, fmt, ap)); |
| 722 | } |
| 723 | |
| 724 | int putc(int c, FILE* fp) { |
| 725 | ScopedFileLock sfl(fp); |
| 726 | return putc_unlocked(c, fp); |
| 727 | } |
| 728 | |
| 729 | int putc_unlocked(int c, FILE* fp) { |
| 730 | if (cantwrite(fp)) { |
| 731 | errno = EBADF; |
| 732 | return EOF; |
| 733 | } |
| 734 | _SET_ORIENTATION(fp, -1); |
| 735 | if (--fp->_w >= 0 || (fp->_w >= fp->_lbfsize && c != '\n')) { |
| 736 | return (*fp->_p++ = c); |
| 737 | } |
| 738 | return (__swbuf(c, fp)); |
| 739 | } |
| 740 | |
| 741 | int putchar(int c) { |
| 742 | return putc(c, stdout); |
| 743 | } |
| 744 | |
| 745 | int putchar_unlocked(int c) { |
| 746 | return putc_unlocked(c, stdout); |
| 747 | } |
| 748 | |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 749 | wint_t putwc(wchar_t wc, FILE* fp) { |
| 750 | return fputwc(wc, fp); |
| 751 | } |
| 752 | |
| 753 | wint_t putwchar(wchar_t wc) { |
| 754 | return fputwc(wc, stdout); |
| 755 | } |
| 756 | |
Elliott Hughes | d1f25a7 | 2016-08-05 15:53:03 -0700 | [diff] [blame] | 757 | int remove(const char* path) { |
| 758 | if (unlink(path) != -1) return 0; |
| 759 | if (errno != EISDIR) return -1; |
| 760 | return rmdir(path); |
| 761 | } |
| 762 | |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 763 | void rewind(FILE* fp) { |
| 764 | ScopedFileLock sfl(fp); |
| 765 | fseek(fp, 0, SEEK_SET); |
| 766 | clearerr_unlocked(fp); |
| 767 | } |
| 768 | |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 769 | int scanf(const char* fmt, ...) { |
| 770 | PRINTF_IMPL(vfscanf(stdin, fmt, ap)); |
| 771 | } |
| 772 | |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 773 | void setbuf(FILE* fp, char* buf) { |
| 774 | setbuffer(fp, buf, BUFSIZ); |
| 775 | } |
| 776 | |
| 777 | void setbuffer(FILE* fp, char* buf, int size) { |
| 778 | setvbuf(fp, buf, buf ? _IOFBF : _IONBF, size); |
| 779 | } |
| 780 | |
| 781 | int setlinebuf(FILE* fp) { |
| 782 | return setvbuf(fp, nullptr, _IOLBF, 0); |
| 783 | } |
| 784 | |
Elliott Hughes | 53cf348 | 2016-08-09 13:06:41 -0700 | [diff] [blame] | 785 | int snprintf(char* s, size_t n, const char* fmt, ...) { |
| 786 | PRINTF_IMPL(vsnprintf(s, n, fmt, ap)); |
| 787 | } |
| 788 | |
| 789 | int sprintf(char* s, const char* fmt, ...) { |
Elliott Hughes | fb3873d | 2016-08-10 11:07:54 -0700 | [diff] [blame] | 790 | PRINTF_IMPL(vsprintf(s, fmt, ap)); |
Elliott Hughes | 53cf348 | 2016-08-09 13:06:41 -0700 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | int sscanf(const char* s, const char* fmt, ...) { |
| 794 | PRINTF_IMPL(vsscanf(s, fmt, ap)); |
| 795 | } |
| 796 | |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 797 | int swprintf(wchar_t* s, size_t n, const wchar_t* fmt, ...) { |
| 798 | PRINTF_IMPL(vswprintf(s, n, fmt, ap)); |
| 799 | } |
| 800 | |
| 801 | int swscanf(const wchar_t* s, const wchar_t* fmt, ...) { |
| 802 | PRINTF_IMPL(vswscanf(s, fmt, ap)); |
| 803 | } |
| 804 | |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 805 | int vprintf(const char* fmt, va_list ap) { |
| 806 | return vfprintf(stdout, fmt, ap); |
| 807 | } |
| 808 | |
| 809 | int vscanf(const char* fmt, va_list ap) { |
| 810 | return vfscanf(stdin, fmt, ap); |
| 811 | } |
| 812 | |
Elliott Hughes | fb3873d | 2016-08-10 11:07:54 -0700 | [diff] [blame] | 813 | int vsnprintf(char* s, size_t n, const char* fmt, va_list ap) { |
| 814 | // stdio internals use int rather than size_t. |
| 815 | static_assert(INT_MAX <= SSIZE_MAX, "SSIZE_MAX too large to fit in int"); |
| 816 | |
| 817 | __check_count("vsnprintf", "size", n); |
| 818 | |
| 819 | // Stdio internals do not deal correctly with zero length buffer. |
| 820 | char dummy; |
| 821 | if (n == 0) { |
| 822 | s = &dummy; |
| 823 | n = 1; |
| 824 | } |
| 825 | |
| 826 | FILE f; |
| 827 | __sfileext fext; |
| 828 | _FILEEXT_SETUP(&f, &fext); |
| 829 | f._file = -1; |
| 830 | f._flags = __SWR | __SSTR; |
| 831 | f._bf._base = f._p = reinterpret_cast<unsigned char*>(s); |
| 832 | f._bf._size = f._w = n - 1; |
| 833 | |
| 834 | int result = __vfprintf(&f, fmt, ap); |
| 835 | *f._p = '\0'; |
| 836 | return result; |
| 837 | } |
| 838 | |
Elliott Hughes | 53cf348 | 2016-08-09 13:06:41 -0700 | [diff] [blame] | 839 | int vsprintf(char* s, const char* fmt, va_list ap) { |
Elliott Hughes | fb3873d | 2016-08-10 11:07:54 -0700 | [diff] [blame] | 840 | return vsnprintf(s, SSIZE_MAX, fmt, ap); |
Elliott Hughes | 53cf348 | 2016-08-09 13:06:41 -0700 | [diff] [blame] | 841 | } |
| 842 | |
Elliott Hughes | cceaf06 | 2016-07-29 16:31:52 -0700 | [diff] [blame] | 843 | int vwprintf(const wchar_t* fmt, va_list ap) { |
| 844 | return vfwprintf(stdout, fmt, ap); |
| 845 | } |
| 846 | |
| 847 | int vwscanf(const wchar_t* fmt, va_list ap) { |
| 848 | return vfwscanf(stdin, fmt, ap); |
| 849 | } |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 850 | |
| 851 | int wprintf(const wchar_t* fmt, ...) { |
| 852 | PRINTF_IMPL(vfwprintf(stdout, fmt, ap)); |
| 853 | } |
| 854 | |
| 855 | int wscanf(const wchar_t* fmt, ...) { |
| 856 | PRINTF_IMPL(vfwscanf(stdin, fmt, ap)); |
| 857 | } |
Dan Albert | 3037ea4 | 2016-10-06 15:46:45 -0700 | [diff] [blame] | 858 | |
| 859 | namespace { |
| 860 | |
| 861 | namespace phony { |
| 862 | #include <bits/struct_file.h> |
| 863 | } |
| 864 | |
| 865 | static_assert(sizeof(::__sFILE) == sizeof(phony::__sFILE), |
| 866 | "size mismatch between `struct __sFILE` implementation and public stub"); |
| 867 | static_assert(alignof(::__sFILE) == alignof(phony::__sFILE), |
| 868 | "alignment mismatch between `struct __sFILE` implementation and public stub"); |
| 869 | |
| 870 | } |