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 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 34 | #include <stdio.h> |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 35 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 36 | #include <errno.h> |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 37 | #include <fcntl.h> |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 38 | #include <limits.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 39 | #include <stdlib.h> |
| 40 | #include <string.h> |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 41 | #include <sys/param.h> |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame^] | 42 | #include <sys/stat.h> |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 43 | #include <unistd.h> |
| 44 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 45 | #include "local.h" |
| 46 | #include "glue.h" |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame^] | 47 | #include "private/ErrnoRestorer.h" |
Elliott Hughes | 6a03abc | 2014-11-03 12:32:17 -0800 | [diff] [blame] | 48 | #include "private/thread_private.h" |
| 49 | |
| 50 | #define ALIGNBYTES (sizeof(uintptr_t) - 1) |
| 51 | #define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) &~ ALIGNBYTES) |
Calin Juravle | c20de90 | 2014-03-20 15:21:32 +0000 | [diff] [blame] | 52 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 53 | #define NDYNAMIC 10 /* add ten more whenever necessary */ |
| 54 | |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame^] | 55 | #define std(flags, file) \ |
| 56 | {0,0,0,flags,file,{0,0},0,__sF+file,__sclose,__sread,nullptr,__swrite, \ |
| 57 | {(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] | 58 | |
Kenny Root | f582340 | 2011-02-12 07:13:44 -0800 | [diff] [blame] | 59 | _THREAD_PRIVATE_MUTEX(__sfp_mutex); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 60 | |
Elliott Hughes | 29ee639 | 2015-12-07 11:07:15 -0800 | [diff] [blame] | 61 | // TODO: when we no longer have to support both clang and GCC, we can simplify all this. |
| 62 | #define SBUF_INIT {0,0} |
| 63 | #if defined(__LP64__) |
| 64 | #define MBSTATE_T_INIT {{0},{0}} |
| 65 | #else |
| 66 | #define MBSTATE_T_INIT {{0}} |
| 67 | #endif |
| 68 | #define WCHAR_IO_DATA_INIT {MBSTATE_T_INIT,MBSTATE_T_INIT,{0},0,0} |
| 69 | |
Elliott Hughes | bb46afd | 2015-12-04 18:03:12 -0800 | [diff] [blame] | 70 | static struct __sfileext __sFext[3] = { |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame^] | 71 | { SBUF_INIT, WCHAR_IO_DATA_INIT, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP, false, __sseek64 }, |
| 72 | { SBUF_INIT, WCHAR_IO_DATA_INIT, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP, false, __sseek64 }, |
| 73 | { 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] | 74 | }; |
Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 75 | |
| 76 | // __sF is exported for backwards compatibility. Until M, we didn't have symbols |
| 77 | // for stdin/stdout/stderr; they were macros accessing __sF. |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 78 | FILE __sF[3] = { |
Elliott Hughes | bb46afd | 2015-12-04 18:03:12 -0800 | [diff] [blame] | 79 | std(__SRD, STDIN_FILENO), |
| 80 | std(__SWR, STDOUT_FILENO), |
| 81 | std(__SWR|__SNBF, STDERR_FILENO), |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 82 | }; |
Elliott Hughes | f0141df | 2015-10-12 12:44:23 -0700 | [diff] [blame] | 83 | |
Elliott Hughes | 168667c | 2014-11-14 14:42:59 -0800 | [diff] [blame] | 84 | FILE* stdin = &__sF[0]; |
| 85 | FILE* stdout = &__sF[1]; |
| 86 | FILE* stderr = &__sF[2]; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 87 | |
Elliott Hughes | bb46afd | 2015-12-04 18:03:12 -0800 | [diff] [blame] | 88 | struct glue __sglue = { NULL, 3, __sF }; |
| 89 | static struct glue* lastglue = &__sglue; |
| 90 | |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 91 | class ScopedFileLock { |
| 92 | public: |
| 93 | ScopedFileLock(FILE* fp) : fp_(fp) { |
| 94 | FLOCKFILE(fp_); |
| 95 | } |
| 96 | ~ScopedFileLock() { |
| 97 | FUNLOCKFILE(fp_); |
| 98 | } |
| 99 | |
| 100 | private: |
| 101 | FILE* fp_; |
| 102 | }; |
| 103 | |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 104 | static glue* moreglue(int n) { |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 105 | static FILE empty; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 106 | |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 107 | char* data = new char[sizeof(glue) + ALIGNBYTES + n * sizeof(FILE) + n * sizeof(__sfileext)]; |
| 108 | if (data == nullptr) return nullptr; |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 109 | |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 110 | glue* g = reinterpret_cast<glue*>(data); |
| 111 | FILE* p = reinterpret_cast<FILE*>(ALIGN(data + sizeof(*g))); |
| 112 | __sfileext* pext = reinterpret_cast<__sfileext*>(ALIGN(data + sizeof(*g)) + n * sizeof(FILE)); |
| 113 | g->next = NULL; |
| 114 | g->niobs = n; |
| 115 | g->iobs = p; |
| 116 | while (--n >= 0) { |
| 117 | *p = empty; |
| 118 | _FILEEXT_SETUP(p, pext); |
| 119 | p++; |
| 120 | pext++; |
| 121 | } |
| 122 | return g; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /* |
| 126 | * Find a free FILE for fopen et al. |
| 127 | */ |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 128 | FILE* __sfp(void) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 129 | FILE *fp; |
| 130 | int n; |
| 131 | struct glue *g; |
| 132 | |
Kenny Root | f582340 | 2011-02-12 07:13:44 -0800 | [diff] [blame] | 133 | _THREAD_PRIVATE_MUTEX_LOCK(__sfp_mutex); |
| 134 | for (g = &__sglue; g != NULL; g = g->next) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 135 | for (fp = g->iobs, n = g->niobs; --n >= 0; fp++) |
| 136 | if (fp->_flags == 0) |
| 137 | goto found; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 138 | } |
Kenny Root | f582340 | 2011-02-12 07:13:44 -0800 | [diff] [blame] | 139 | |
| 140 | /* release lock while mallocing */ |
| 141 | _THREAD_PRIVATE_MUTEX_UNLOCK(__sfp_mutex); |
| 142 | if ((g = moreglue(NDYNAMIC)) == NULL) |
| 143 | return (NULL); |
| 144 | _THREAD_PRIVATE_MUTEX_LOCK(__sfp_mutex); |
| 145 | lastglue->next = g; |
| 146 | lastglue = g; |
| 147 | fp = g->iobs; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 148 | found: |
| 149 | fp->_flags = 1; /* reserve this slot; caller sets real flags */ |
Kenny Root | f582340 | 2011-02-12 07:13:44 -0800 | [diff] [blame] | 150 | _THREAD_PRIVATE_MUTEX_UNLOCK(__sfp_mutex); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 151 | fp->_p = NULL; /* no current pointer */ |
| 152 | fp->_w = 0; /* nothing to read or write */ |
| 153 | fp->_r = 0; |
| 154 | fp->_bf._base = NULL; /* no buffer */ |
| 155 | fp->_bf._size = 0; |
| 156 | fp->_lbfsize = 0; /* not line buffered */ |
| 157 | fp->_file = -1; /* no file */ |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame^] | 158 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 159 | fp->_lb._base = NULL; /* no line buffer */ |
| 160 | fp->_lb._size = 0; |
| 161 | _FILEEXT_INIT(fp); |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame^] | 162 | |
| 163 | // Caller sets cookie, _read/_write etc. |
| 164 | // We explicitly clear _seek and _seek64 to prevent subtle bugs. |
| 165 | fp->_seek = nullptr; |
| 166 | _EXT(fp)->_seek64 = nullptr; |
| 167 | |
| 168 | return fp; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 171 | extern "C" __LIBC_HIDDEN__ void __libc_stdio_cleanup(void) { |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 172 | // Equivalent to fflush(nullptr), but without all the locking since we're shutting down anyway. |
| 173 | _fwalk(__sflush); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 174 | } |
Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 175 | |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame^] | 176 | static FILE* __fopen(int fd, int flags) { |
| 177 | #if !defined(__LP64__) |
| 178 | if (fd > SHRT_MAX) { |
| 179 | errno = EMFILE; |
| 180 | return nullptr; |
| 181 | } |
| 182 | #endif |
| 183 | |
| 184 | FILE* fp = __sfp(); |
| 185 | if (fp != nullptr) { |
| 186 | fp->_file = fd; |
| 187 | fp->_flags = flags; |
| 188 | fp->_cookie = fp; |
| 189 | fp->_read = __sread; |
| 190 | fp->_write = __swrite; |
| 191 | fp->_close = __sclose; |
| 192 | _EXT(fp)->_seek64 = __sseek64; |
| 193 | } |
| 194 | return fp; |
| 195 | } |
| 196 | |
| 197 | FILE* fopen(const char* file, const char* mode) { |
| 198 | int oflags; |
| 199 | int flags = __sflags(mode, &oflags); |
| 200 | if (flags == 0) return nullptr; |
| 201 | |
| 202 | int fd = open(file, oflags, DEFFILEMODE); |
| 203 | if (fd == -1) { |
| 204 | return nullptr; |
| 205 | } |
| 206 | |
| 207 | FILE* fp = __fopen(fd, flags); |
| 208 | if (fp == nullptr) { |
| 209 | ErrnoRestorer errno_restorer; |
| 210 | close(fd); |
| 211 | return nullptr; |
| 212 | } |
| 213 | |
| 214 | // When opening in append mode, even though we use O_APPEND, |
| 215 | // we need to seek to the end so that ftell() gets the right |
| 216 | // answer. If the user then alters the seek pointer, or |
| 217 | // the file extends, this will fail, but there is not much |
| 218 | // we can do about this. (We could set __SAPP and check in |
| 219 | // fseek and ftell.) |
| 220 | // TODO: check in __sseek instead. |
| 221 | if (oflags & O_APPEND) __sseek64(fp, 0, SEEK_END); |
| 222 | |
| 223 | return fp; |
| 224 | } |
| 225 | |
| 226 | FILE* fdopen(int fd, const char* mode) { |
| 227 | int oflags; |
| 228 | int flags = __sflags(mode, &oflags); |
| 229 | if (flags == 0) return nullptr; |
| 230 | |
| 231 | // Make sure the mode the user wants is a subset of the actual mode. |
| 232 | int fdflags = fcntl(fd, F_GETFL, 0); |
| 233 | if (fdflags < 0) return nullptr; |
| 234 | int tmp = fdflags & O_ACCMODE; |
| 235 | if (tmp != O_RDWR && (tmp != (oflags & O_ACCMODE))) { |
| 236 | errno = EINVAL; |
| 237 | return nullptr; |
| 238 | } |
| 239 | |
| 240 | // If opened for appending, but underlying descriptor does not have |
| 241 | // O_APPEND bit set, assert __SAPP so that __swrite() will lseek to |
| 242 | // end before each write. |
| 243 | // TODO: use fcntl(2) to set O_APPEND instead. |
| 244 | if ((oflags & O_APPEND) && !(fdflags & O_APPEND)) flags |= __SAPP; |
| 245 | |
| 246 | // If close-on-exec was requested, then turn it on if not already. |
| 247 | if ((oflags & O_CLOEXEC) && !((tmp = fcntl(fd, F_GETFD)) & FD_CLOEXEC)) { |
| 248 | fcntl(fd, F_SETFD, tmp | FD_CLOEXEC); |
| 249 | } |
| 250 | |
| 251 | return __fopen(fd, flags); |
| 252 | } |
| 253 | |
| 254 | // Re-direct an existing, open (probably) file to some other file. |
| 255 | // ANSI is written such that the original file gets closed if at |
| 256 | // all possible, no matter what. |
| 257 | // TODO: rewrite this mess completely. |
| 258 | FILE* freopen(const char* file, const char* mode, FILE* fp) { |
| 259 | int oflags; |
| 260 | int flags = __sflags(mode, &oflags); |
| 261 | if (flags == 0) { |
| 262 | fclose(fp); |
| 263 | return nullptr; |
| 264 | } |
| 265 | |
| 266 | ScopedFileLock sfl(fp); |
| 267 | |
| 268 | // There are actually programs that depend on being able to "freopen" |
| 269 | // descriptors that weren't originally open. Keep this from breaking. |
| 270 | // Remember whether the stream was open to begin with, and which file |
| 271 | // descriptor (if any) was associated with it. If it was attached to |
| 272 | // a descriptor, defer closing it; freopen("/dev/stdin", "r", stdin) |
| 273 | // should work. This is unnecessary if it was not a Unix file. |
| 274 | int isopen, wantfd; |
| 275 | if (fp->_flags == 0) { |
| 276 | fp->_flags = __SEOF; // Hold on to it. |
| 277 | isopen = 0; |
| 278 | wantfd = -1; |
| 279 | } else { |
| 280 | // Flush the stream; ANSI doesn't require this. |
| 281 | if (fp->_flags & __SWR) __sflush(fp); |
| 282 | |
| 283 | // If close is NULL, closing is a no-op, hence pointless. |
| 284 | isopen = fp->_close != NULL; |
| 285 | if ((wantfd = fp->_file) < 0 && isopen) { |
| 286 | (*fp->_close)(fp->_cookie); |
| 287 | isopen = 0; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | // Get a new descriptor to refer to the new file. |
| 292 | int fd = open(file, oflags, DEFFILEMODE); |
| 293 | if (fd < 0 && isopen) { |
| 294 | // If out of fd's close the old one and try again. |
| 295 | if (errno == ENFILE || errno == EMFILE) { |
| 296 | (*fp->_close)(fp->_cookie); |
| 297 | isopen = 0; |
| 298 | fd = open(file, oflags, DEFFILEMODE); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | int sverrno = errno; |
| 303 | |
| 304 | // Finish closing fp. Even if the open succeeded above, we cannot |
| 305 | // keep fp->_base: it may be the wrong size. This loses the effect |
| 306 | // of any setbuffer calls, but stdio has always done this before. |
| 307 | if (isopen && fd != wantfd) (*fp->_close)(fp->_cookie); |
| 308 | if (fp->_flags & __SMBF) free(fp->_bf._base); |
| 309 | fp->_w = 0; |
| 310 | fp->_r = 0; |
| 311 | fp->_p = NULL; |
| 312 | fp->_bf._base = NULL; |
| 313 | fp->_bf._size = 0; |
| 314 | fp->_lbfsize = 0; |
| 315 | if (HASUB(fp)) FREEUB(fp); |
| 316 | _UB(fp)._size = 0; |
| 317 | WCIO_FREE(fp); |
| 318 | if (HASLB(fp)) FREELB(fp); |
| 319 | fp->_lb._size = 0; |
| 320 | |
| 321 | if (fd < 0) { // Did not get it after all. |
| 322 | fp->_flags = 0; // Release. |
| 323 | errno = sverrno; // Restore errno in case _close clobbered it. |
| 324 | return nullptr; |
| 325 | } |
| 326 | |
| 327 | // If reopening something that was open before on a real file, try |
| 328 | // to maintain the descriptor. Various C library routines (perror) |
| 329 | // assume stderr is always fd STDERR_FILENO, even if being freopen'd. |
| 330 | if (wantfd >= 0 && fd != wantfd) { |
| 331 | if (dup3(fd, wantfd, oflags & O_CLOEXEC) >= 0) { |
| 332 | close(fd); |
| 333 | fd = wantfd; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | // _file is only a short. |
| 338 | if (fd > SHRT_MAX) { |
| 339 | fp->_flags = 0; // Release. |
| 340 | errno = EMFILE; |
| 341 | return nullptr; |
| 342 | } |
| 343 | |
| 344 | fp->_flags = flags; |
| 345 | fp->_file = fd; |
| 346 | fp->_cookie = fp; |
| 347 | fp->_read = __sread; |
| 348 | fp->_write = __swrite; |
| 349 | fp->_close = __sclose; |
| 350 | _EXT(fp)->_seek64 = __sseek64; |
| 351 | |
| 352 | // When opening in append mode, even though we use O_APPEND, |
| 353 | // we need to seek to the end so that ftell() gets the right |
| 354 | // answer. If the user then alters the seek pointer, or |
| 355 | // the file extends, this will fail, but there is not much |
| 356 | // we can do about this. (We could set __SAPP and check in |
| 357 | // fseek and ftell.) |
| 358 | if (oflags & O_APPEND) __sseek64(fp, 0, SEEK_END); |
| 359 | return fp; |
| 360 | } |
| 361 | |
Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 362 | int fclose(FILE* fp) { |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 363 | if (fp->_flags == 0) { |
| 364 | // Already freed! |
| 365 | errno = EBADF; |
| 366 | return EOF; |
| 367 | } |
Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 368 | |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 369 | ScopedFileLock sfl(fp); |
| 370 | WCIO_FREE(fp); |
| 371 | int r = fp->_flags & __SWR ? __sflush(fp) : 0; |
| 372 | if (fp->_close != NULL && (*fp->_close)(fp->_cookie) < 0) { |
| 373 | r = EOF; |
| 374 | } |
| 375 | if (fp->_flags & __SMBF) free(fp->_bf._base); |
| 376 | if (HASUB(fp)) FREEUB(fp); |
| 377 | if (HASLB(fp)) FREELB(fp); |
Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 378 | |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 379 | // Poison this FILE so accesses after fclose will be obvious. |
| 380 | fp->_file = -1; |
| 381 | fp->_r = fp->_w = 0; |
Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 382 | |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 383 | // Release this FILE for reuse. |
| 384 | fp->_flags = 0; |
| 385 | return r; |
Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | int fileno(FILE* fp) { |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 389 | ScopedFileLock sfl(fp); |
| 390 | return fileno_unlocked(fp); |
Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 391 | } |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 392 | |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 393 | int __sread(void* cookie, char* buf, int n) { |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 394 | FILE* fp = reinterpret_cast<FILE*>(cookie); |
| 395 | return TEMP_FAILURE_RETRY(read(fp->_file, buf, n)); |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | int __swrite(void* cookie, const char* buf, int n) { |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 399 | FILE* fp = reinterpret_cast<FILE*>(cookie); |
| 400 | if (fp->_flags & __SAPP) { |
| 401 | // The FILE* is in append mode, but the underlying fd doesn't have O_APPEND set. |
| 402 | // We need to seek manually. |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame^] | 403 | // TODO: use fcntl(2) to set O_APPEND in fdopen(3) instead? |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 404 | TEMP_FAILURE_RETRY(lseek64(fp->_file, 0, SEEK_END)); |
| 405 | } |
| 406 | return TEMP_FAILURE_RETRY(write(fp->_file, buf, n)); |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 407 | } |
| 408 | |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 409 | fpos_t __sseek(void* cookie, fpos_t offset, int whence) { |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 410 | FILE* fp = reinterpret_cast<FILE*>(cookie); |
| 411 | return TEMP_FAILURE_RETRY(lseek(fp->_file, offset, whence)); |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 412 | } |
| 413 | |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame^] | 414 | off64_t __sseek64(void* cookie, off64_t offset, int whence) { |
| 415 | FILE* fp = reinterpret_cast<FILE*>(cookie); |
| 416 | return TEMP_FAILURE_RETRY(lseek64(fp->_file, offset, whence)); |
| 417 | } |
| 418 | |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 419 | int __sclose(void* cookie) { |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 420 | FILE* fp = reinterpret_cast<FILE*>(cookie); |
| 421 | return close(fp->_file); |
| 422 | } |
| 423 | |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame^] | 424 | static off64_t __seek_unlocked(FILE* fp, off64_t offset, int whence) { |
| 425 | // Use `_seek64` if set, but fall back to `_seek`. |
| 426 | if (_EXT(fp)->_seek64 != nullptr) { |
| 427 | return (*_EXT(fp)->_seek64)(fp->_cookie, offset, whence); |
| 428 | } else if (fp->_seek != nullptr) { |
| 429 | return (*fp->_seek)(fp->_cookie, offset, whence); |
| 430 | } else { |
| 431 | errno = ESPIPE; |
| 432 | return -1; |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 433 | } |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | // TODO: _FILE_OFFSET_BITS=64. |
| 437 | static off_t __ftello_unlocked(FILE* fp) { |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame^] | 438 | // Find offset of underlying I/O object, then adjust for buffered bytes. |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 439 | __sflush(fp); // May adjust seek offset on append stream. |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame^] | 440 | fpos_t result = __seek_unlocked(fp, 0, SEEK_CUR); |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 441 | if (result == -1) { |
| 442 | return -1; |
| 443 | } |
| 444 | |
| 445 | if (fp->_flags & __SRD) { |
| 446 | // Reading. Any unread characters (including |
| 447 | // those from ungetc) cause the position to be |
| 448 | // smaller than that in the underlying object. |
| 449 | result -= fp->_r; |
| 450 | if (HASUB(fp)) result -= fp->_ur; |
| 451 | } else if (fp->_flags & __SWR && fp->_p != NULL) { |
| 452 | // Writing. Any buffered characters cause the |
| 453 | // position to be greater than that in the |
| 454 | // underlying object. |
| 455 | result += fp->_p - fp->_bf._base; |
| 456 | } |
| 457 | return result; |
| 458 | } |
| 459 | |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame^] | 460 | // TODO: _FILE_OFFSET_BITS=64. |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 461 | int fseeko(FILE* fp, off_t offset, int whence) { |
| 462 | ScopedFileLock sfl(fp); |
| 463 | |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame^] | 464 | // Change any SEEK_CUR to SEEK_SET, and check `whence` argument. |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 465 | // After this, whence is either SEEK_SET or SEEK_END. |
| 466 | if (whence == SEEK_CUR) { |
| 467 | fpos_t current_offset = __ftello_unlocked(fp); |
| 468 | if (current_offset == -1) { |
| 469 | return EOF; |
| 470 | } |
| 471 | offset += current_offset; |
| 472 | whence = SEEK_SET; |
| 473 | } else if (whence != SEEK_SET && whence != SEEK_END) { |
| 474 | errno = EINVAL; |
| 475 | return EOF; |
| 476 | } |
| 477 | |
| 478 | if (fp->_bf._base == NULL) __smakebuf(fp); |
| 479 | |
| 480 | // Flush unwritten data and attempt the seek. |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame^] | 481 | if (__sflush(fp) || __seek_unlocked(fp, offset, whence) == -1) { |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 482 | return EOF; |
| 483 | } |
| 484 | |
| 485 | // Success: clear EOF indicator and discard ungetc() data. |
| 486 | if (HASUB(fp)) FREEUB(fp); |
| 487 | fp->_p = fp->_bf._base; |
| 488 | fp->_r = 0; |
| 489 | /* fp->_w = 0; */ /* unnecessary (I think...) */ |
| 490 | fp->_flags &= ~__SEOF; |
| 491 | return 0; |
| 492 | } |
| 493 | |
| 494 | // TODO: _FILE_OFFSET_BITS=64. |
| 495 | int fseek(FILE* fp, long offset, int whence) { |
| 496 | return fseeko(fp, offset, whence); |
| 497 | } |
| 498 | |
| 499 | // TODO: _FILE_OFFSET_BITS=64. |
| 500 | off_t ftello(FILE* fp) { |
| 501 | ScopedFileLock sfl(fp); |
| 502 | return __ftello_unlocked(fp); |
| 503 | } |
| 504 | |
| 505 | // TODO: _FILE_OFFSET_BITS=64 |
| 506 | long ftell(FILE* fp) { |
| 507 | off_t offset = ftello(fp); |
| 508 | if (offset > LONG_MAX) { |
| 509 | errno = EOVERFLOW; |
| 510 | return -1; |
| 511 | } |
| 512 | return offset; |
Elliott Hughes | 021335e | 2016-01-19 16:28:15 -0800 | [diff] [blame] | 513 | } |
Elliott Hughes | 023c307 | 2016-01-22 15:04:51 -0800 | [diff] [blame^] | 514 | |
| 515 | // TODO: _FILE_OFFSET_BITS=64 |
| 516 | int fgetpos(FILE* fp, fpos_t* pos) { |
| 517 | *pos = ftello(fp); |
| 518 | return (*pos == -1); |
| 519 | } |
| 520 | |
| 521 | // TODO: _FILE_OFFSET_BITS=64 |
| 522 | int fsetpos(FILE* fp, const fpos_t* pos) { |
| 523 | return fseeko(fp, *pos, SEEK_SET); |
| 524 | } |