• Home
  • Raw
  • Download

Lines Matching refs:fp

109 static uint64_t __get_file_tag(FILE* fp) {  in __get_file_tag()  argument
113 if (fp == stdin || fp == stderr || fp == stdout) { in __get_file_tag()
118 reinterpret_cast<uint64_t>(fp)); in __get_file_tag()
126 explicit ScopedFileLock(FILE* fp) : fp_(fp) { in ScopedFileLock() argument
156 static inline void free_fgetln_buffer(FILE* fp) { in free_fgetln_buffer() argument
157 if (__predict_false(fp->_lb._base != nullptr)) { in free_fgetln_buffer()
158 free(fp->_lb._base); in free_fgetln_buffer()
159 fp->_lb._base = nullptr; in free_fgetln_buffer()
167 FILE *fp; in __sfp() local
173 for (fp = g->iobs, n = g->niobs; --n >= 0; fp++) in __sfp()
174 if (fp->_flags == 0) in __sfp()
184 fp = g->iobs; in __sfp()
186 fp->_flags = 1; /* reserve this slot; caller sets real flags */ in __sfp()
188 fp->_p = nullptr; /* no current pointer */ in __sfp()
189 fp->_w = 0; /* nothing to read or write */ in __sfp()
190 fp->_r = 0; in __sfp()
191 fp->_bf._base = nullptr; /* no buffer */ in __sfp()
192 fp->_bf._size = 0; in __sfp()
193 fp->_lbfsize = 0; /* not line buffered */ in __sfp()
194 fp->_file = -1; /* no file */ in __sfp()
196 fp->_lb._base = nullptr; /* no line buffer */ in __sfp()
197 fp->_lb._size = 0; in __sfp()
199 memset(_EXT(fp), 0, sizeof(struct __sfileext)); in __sfp()
200 _EXT(fp)->_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; in __sfp()
201 _EXT(fp)->_caller_handles_locking = false; in __sfp()
205 fp->_seek = nullptr; in __sfp()
206 _EXT(fp)->_seek64 = nullptr; in __sfp()
208 return fp; in __sfp()
214 FILE* fp = g->iobs; in _fwalk() local
215 for (int n = g->niobs; --n >= 0; ++fp) { in _fwalk()
216 if (fp->_flags != 0 && (fp->_flags & __SIGN) == 0) { in _fwalk()
217 result |= (*callback)(fp); in _fwalk()
229 static FILE* __FILE_init(FILE* fp, int fd, int flags) { in __FILE_init() argument
230 if (fp == nullptr) return nullptr; in __FILE_init()
236 fp->_file = fd; in __FILE_init()
237 android_fdsan_exchange_owner_tag(fd, 0, __get_file_tag(fp)); in __FILE_init()
238 fp->_flags = flags; in __FILE_init()
239 fp->_cookie = fp; in __FILE_init()
240 fp->_read = __sread; in __FILE_init()
241 fp->_write = __swrite; in __FILE_init()
242 fp->_close = __sclose; in __FILE_init()
243 _EXT(fp)->_seek64 = __sseek64; in __FILE_init()
244 return fp; in __FILE_init()
257 FILE* fp = __FILE_init(__sfp(), fd, flags); in fopen() local
258 if (fp == nullptr) { in fopen()
266 if ((mode_flags & O_APPEND) != 0) __sseek64(fp, 0, SEEK_END); in fopen()
267 return fp; in fopen()
299 FILE* freopen(const char* file, const char* mode, FILE* fp) { in freopen() argument
300 CHECK_FP(fp); in freopen()
313 FdPath fd_path(fp->_file); in freopen()
319 fclose(fp); in freopen()
323 ScopedFileLock sfl(fp); in freopen()
334 if (fp->_flags == 0) { in freopen()
335 fp->_flags = __SEOF; // Hold on to it. in freopen()
340 if (fp->_flags & __SWR) __sflush(fp); in freopen()
343 isopen = (fp->_close != nullptr); in freopen()
344 if ((wantfd = fp->_file) < 0 && isopen) { in freopen()
345 (*fp->_close)(fp->_cookie); in freopen()
355 (*fp->_close)(fp->_cookie); in freopen()
366 if (isopen && fd != wantfd) (*fp->_close)(fp->_cookie); in freopen()
367 if (fp->_flags & __SMBF) free(fp->_bf._base); in freopen()
368 fp->_w = 0; in freopen()
369 fp->_r = 0; in freopen()
370 fp->_p = nullptr; in freopen()
371 fp->_bf._base = nullptr; in freopen()
372 fp->_bf._size = 0; in freopen()
373 fp->_lbfsize = 0; in freopen()
374 if (HASUB(fp)) FREEUB(fp); in freopen()
375 _UB(fp)._size = 0; in freopen()
376 WCIO_FREE(fp); in freopen()
377 free_fgetln_buffer(fp); in freopen()
378 fp->_lb._size = 0; in freopen()
381 fp->_flags = 0; // Release. in freopen()
396 __FILE_init(fp, fd, flags); in freopen()
400 if ((mode_flags & O_APPEND) != 0) __sseek64(fp, 0, SEEK_END); in freopen()
402 return fp; in freopen()
406 static int __FILE_close(FILE* fp) { in __FILE_close() argument
407 if (fp->_flags == 0) { in __FILE_close()
413 ScopedFileLock sfl(fp); in __FILE_close()
414 WCIO_FREE(fp); in __FILE_close()
415 int r = fp->_flags & __SWR ? __sflush(fp) : 0; in __FILE_close()
416 if (fp->_close != nullptr && (*fp->_close)(fp->_cookie) < 0) { in __FILE_close()
419 if (fp->_flags & __SMBF) free(fp->_bf._base); in __FILE_close()
420 if (HASUB(fp)) FREEUB(fp); in __FILE_close()
421 free_fgetln_buffer(fp); in __FILE_close()
424 pid_t pid = _EXT(fp)->_popen_pid; in __FILE_close()
431 _EXT(fp)->_popen_pid = 0; in __FILE_close()
434 fp->_file = -1; in __FILE_close()
435 fp->_r = fp->_w = 0; in __FILE_close()
438 fp->_flags = 0; in __FILE_close()
442 int fclose(FILE* fp) { in fclose() argument
443 CHECK_FP(fp); in fclose()
444 return __FILE_close(fp); in fclose()
447 int fileno_unlocked(FILE* fp) { in fileno_unlocked() argument
448 CHECK_FP(fp); in fileno_unlocked()
449 int fd = fp->_file; in fileno_unlocked()
457 int fileno(FILE* fp) { in fileno() argument
458 CHECK_FP(fp); in fileno()
459 ScopedFileLock sfl(fp); in fileno()
460 return fileno_unlocked(fp); in fileno()
463 void clearerr_unlocked(FILE* fp) { in clearerr_unlocked() argument
464 CHECK_FP(fp); in clearerr_unlocked()
465 return __sclearerr(fp); in clearerr_unlocked()
468 void clearerr(FILE* fp) { in clearerr() argument
469 CHECK_FP(fp); in clearerr()
470 ScopedFileLock sfl(fp); in clearerr()
471 clearerr_unlocked(fp); in clearerr()
474 int feof_unlocked(FILE* fp) { in feof_unlocked() argument
475 CHECK_FP(fp); in feof_unlocked()
476 return ((fp->_flags & __SEOF) != 0); in feof_unlocked()
479 int feof(FILE* fp) { in feof() argument
480 CHECK_FP(fp); in feof()
481 ScopedFileLock sfl(fp); in feof()
482 return feof_unlocked(fp); in feof()
485 int ferror_unlocked(FILE* fp) { in ferror_unlocked() argument
486 CHECK_FP(fp); in ferror_unlocked()
487 return __sferror(fp); in ferror_unlocked()
490 int ferror(FILE* fp) { in ferror() argument
491 CHECK_FP(fp); in ferror()
492 ScopedFileLock sfl(fp); in ferror()
493 return ferror_unlocked(fp); in ferror()
496 int __sflush(FILE* fp) { in __sflush() argument
498 if ((fp->_flags & __SWR) == 0) return 0; in __sflush()
501 unsigned char* p = fp->_bf._base; in __sflush()
506 int n = fp->_p - p; in __sflush()
507 fp->_p = p; in __sflush()
508 fp->_w = (fp->_flags & (__SLBF|__SNBF)) ? 0 : fp->_bf._size; in __sflush()
511 int written = (*fp->_write)(fp->_cookie, reinterpret_cast<char*>(p), n); in __sflush()
513 fp->_flags |= __SERR; in __sflush()
521 int __sflush_locked(FILE* fp) { in __sflush_locked() argument
522 ScopedFileLock sfl(fp); in __sflush_locked()
523 return __sflush(fp); in __sflush_locked()
527 FILE* fp = reinterpret_cast<FILE*>(cookie); in __sread() local
528 return TEMP_FAILURE_RETRY(read(fp->_file, buf, n)); in __sread()
532 FILE* fp = reinterpret_cast<FILE*>(cookie); in __swrite() local
533 return TEMP_FAILURE_RETRY(write(fp->_file, buf, n)); in __swrite()
537 FILE* fp = reinterpret_cast<FILE*>(cookie); in __sseek() local
538 return TEMP_FAILURE_RETRY(lseek(fp->_file, offset, whence)); in __sseek()
542 FILE* fp = reinterpret_cast<FILE*>(cookie); in __sseek64() local
543 return TEMP_FAILURE_RETRY(lseek64(fp->_file, offset, whence)); in __sseek64()
547 FILE* fp = reinterpret_cast<FILE*>(cookie); in __sclose() local
548 return android_fdsan_close_with_tag(fp->_file, __get_file_tag(fp)); in __sclose()
551 static off64_t __seek_unlocked(FILE* fp, off64_t offset, int whence) { in __seek_unlocked() argument
553 if (_EXT(fp)->_seek64 != nullptr) { in __seek_unlocked()
554 return (*_EXT(fp)->_seek64)(fp->_cookie, offset, whence); in __seek_unlocked()
555 } else if (fp->_seek != nullptr) { in __seek_unlocked()
556 off64_t result = (*fp->_seek)(fp->_cookie, offset, whence); in __seek_unlocked()
568 static off64_t __ftello64_unlocked(FILE* fp) { in __ftello64_unlocked() argument
570 __sflush(fp); // May adjust seek offset on append stream. in __ftello64_unlocked()
572 off64_t result = __seek_unlocked(fp, 0, SEEK_CUR); in __ftello64_unlocked()
577 if (fp->_flags & __SRD) { in __ftello64_unlocked()
581 result -= fp->_r; in __ftello64_unlocked()
582 if (HASUB(fp)) result -= fp->_ur; in __ftello64_unlocked()
583 } else if (fp->_flags & __SWR && fp->_p != nullptr) { in __ftello64_unlocked()
587 result += fp->_p - fp->_bf._base; in __ftello64_unlocked()
592 int __fseeko64(FILE* fp, off64_t offset, int whence, int off_t_bits) { in __fseeko64() argument
593 ScopedFileLock sfl(fp); in __fseeko64()
598 fpos64_t current_offset = __ftello64_unlocked(fp); in __fseeko64()
615 if (fp->_bf._base == nullptr) __smakebuf(fp); in __fseeko64()
618 if (__sflush(fp) || __seek_unlocked(fp, offset, whence) == -1) { in __fseeko64()
623 if (HASUB(fp)) FREEUB(fp); in __fseeko64()
624 fp->_p = fp->_bf._base; in __fseeko64()
625 fp->_r = 0; in __fseeko64()
627 fp->_flags &= ~__SEOF; in __fseeko64()
631 int fseeko(FILE* fp, off_t offset, int whence) { in fseeko() argument
632 CHECK_FP(fp); in fseeko()
634 return __fseeko64(fp, offset, whence, 8*sizeof(off_t)); in fseeko()
638 int fseeko64(FILE* fp, off64_t offset, int whence) { in fseeko64() argument
639 CHECK_FP(fp); in fseeko64()
640 return __fseeko64(fp, offset, whence, 8*sizeof(off64_t)); in fseeko64()
643 int fsetpos(FILE* fp, const fpos_t* pos) { in fsetpos() argument
644 CHECK_FP(fp); in fsetpos()
645 return fseeko(fp, *pos, SEEK_SET); in fsetpos()
648 int fsetpos64(FILE* fp, const fpos64_t* pos) { in fsetpos64() argument
649 CHECK_FP(fp); in fsetpos64()
650 return fseeko64(fp, *pos, SEEK_SET); in fsetpos64()
653 off_t ftello(FILE* fp) { in ftello() argument
654 CHECK_FP(fp); in ftello()
656 off64_t result = ftello64(fp); in ftello()
665 off64_t ftello64(FILE* fp) { in ftello64() argument
666 CHECK_FP(fp); in ftello64()
667 ScopedFileLock sfl(fp); in ftello64()
668 return __ftello64_unlocked(fp); in ftello64()
671 int fgetpos(FILE* fp, fpos_t* pos) { in fgetpos() argument
672 CHECK_FP(fp); in fgetpos()
673 *pos = ftello(fp); in fgetpos()
677 int fgetpos64(FILE* fp, fpos64_t* pos) { in fgetpos64() argument
678 CHECK_FP(fp); in fgetpos64()
679 *pos = ftello64(fp); in fgetpos64()
692 FILE* fp = __sfp(); in __funopen() local
693 if (fp == nullptr) return nullptr; in __funopen()
696 fp->_flags = __SRW; in __funopen()
698 fp->_flags = __SRD; in __funopen()
700 fp->_flags = __SWR; in __funopen()
703 fp->_file = -1; in __funopen()
704 fp->_cookie = const_cast<void*>(cookie); // The funopen(3) API is incoherent. in __funopen()
705 fp->_read = read_fn; in __funopen()
706 fp->_write = write_fn; in __funopen()
707 fp->_close = close_fn; in __funopen()
709 return fp; in __funopen()
717 FILE* fp = __funopen(cookie, read_fn, write_fn, close_fn); in funopen() local
718 if (fp != nullptr) { in funopen()
719 fp->_seek = seek_fn; in funopen()
721 return fp; in funopen()
729 FILE* fp = __funopen(cookie, read_fn, write_fn, close_fn); in funopen64() local
730 if (fp != nullptr) { in funopen64()
731 _EXT(fp)->_seek64 = seek_fn; in funopen64()
733 return fp; in funopen64()
748 int fprintf(FILE* fp, const char* fmt, ...) { in fprintf() argument
749 CHECK_FP(fp); in fprintf()
750 PRINTF_IMPL(vfprintf(fp, fmt, ap)); in fprintf()
753 int fgetc(FILE* fp) { in fgetc() argument
754 CHECK_FP(fp); in fgetc()
755 return getc(fp); in fgetc()
758 int fgetc_unlocked(FILE* fp) { in fgetc_unlocked() argument
759 CHECK_FP(fp); in fgetc_unlocked()
760 return getc_unlocked(fp); in fgetc_unlocked()
763 char* fgets(char* buf, int n, FILE* fp) { in fgets() argument
764 CHECK_FP(fp); in fgets()
765 ScopedFileLock sfl(fp); in fgets()
766 return fgets_unlocked(buf, n, fp); in fgets()
773 char* fgets_unlocked(char* buf, int n, FILE* fp) { in fgets_unlocked() argument
776 _SET_ORIENTATION(fp, ORIENT_BYTES); in fgets_unlocked()
782 if (fp->_r <= 0) { in fgets_unlocked()
783 if (__srefill(fp)) { in fgets_unlocked()
789 size_t len = fp->_r; in fgets_unlocked()
790 unsigned char* p = fp->_p; in fgets_unlocked()
799 fp->_r -= len; in fgets_unlocked()
800 fp->_p = t; in fgets_unlocked()
805 fp->_r -= len; in fgets_unlocked()
806 fp->_p += len; in fgets_unlocked()
815 int fputc(int c, FILE* fp) { in fputc() argument
816 CHECK_FP(fp); in fputc()
817 return putc(c, fp); in fputc()
820 int fputc_unlocked(int c, FILE* fp) { in fputc_unlocked() argument
821 CHECK_FP(fp); in fputc_unlocked()
822 return putc_unlocked(c, fp); in fputc_unlocked()
825 int fputs(const char* s, FILE* fp) { in fputs() argument
826 CHECK_FP(fp); in fputs()
827 ScopedFileLock sfl(fp); in fputs()
828 return fputs_unlocked(s, fp); in fputs()
831 int fputs_unlocked(const char* s, FILE* fp) { in fputs_unlocked() argument
832 CHECK_FP(fp); in fputs_unlocked()
834 return (fwrite_unlocked(s, 1, length, fp) == length) ? 0 : EOF; in fputs_unlocked()
837 int fscanf(FILE* fp, const char* fmt, ...) { in fscanf() argument
838 CHECK_FP(fp); in fscanf()
839 PRINTF_IMPL(vfscanf(fp, fmt, ap)); in fscanf()
842 int fwprintf(FILE* fp, const wchar_t* fmt, ...) { in fwprintf() argument
843 CHECK_FP(fp); in fwprintf()
844 PRINTF_IMPL(vfwprintf(fp, fmt, ap)); in fwprintf()
847 int fwscanf(FILE* fp, const wchar_t* fmt, ...) { in fwscanf() argument
848 CHECK_FP(fp); in fwscanf()
849 PRINTF_IMPL(vfwscanf(fp, fmt, ap)); in fwscanf()
852 int getc(FILE* fp) { in getc() argument
853 CHECK_FP(fp); in getc()
854 ScopedFileLock sfl(fp); in getc()
855 return getc_unlocked(fp); in getc()
858 int getc_unlocked(FILE* fp) { in getc_unlocked() argument
859 CHECK_FP(fp); in getc_unlocked()
860 return __sgetc(fp); in getc_unlocked()
871 ssize_t getline(char** buf, size_t* len, FILE* fp) { in getline() argument
872 CHECK_FP(fp); in getline()
873 return getdelim(buf, len, '\n', fp); in getline()
876 wint_t getwc(FILE* fp) { in getwc() argument
877 CHECK_FP(fp); in getwc()
878 return fgetwc(fp); in getwc()
894 int putc(int c, FILE* fp) { in putc() argument
895 CHECK_FP(fp); in putc()
896 ScopedFileLock sfl(fp); in putc()
897 return putc_unlocked(c, fp); in putc()
900 int putc_unlocked(int c, FILE* fp) { in putc_unlocked() argument
901 CHECK_FP(fp); in putc_unlocked()
902 if (cantwrite(fp)) { in putc_unlocked()
906 _SET_ORIENTATION(fp, ORIENT_BYTES); in putc_unlocked()
907 if (--fp->_w >= 0 || (fp->_w >= fp->_lbfsize && c != '\n')) { in putc_unlocked()
908 return (*fp->_p++ = c); in putc_unlocked()
910 return (__swbuf(c, fp)); in putc_unlocked()
928 wint_t putwc(wchar_t wc, FILE* fp) { in putwc() argument
929 CHECK_FP(fp); in putwc()
930 return fputwc(wc, fp); in putwc()
943 void rewind(FILE* fp) { in rewind() argument
944 CHECK_FP(fp); in rewind()
945 ScopedFileLock sfl(fp); in rewind()
946 fseek(fp, 0, SEEK_SET); in rewind()
947 clearerr_unlocked(fp); in rewind()
954 void setbuf(FILE* fp, char* buf) { in setbuf() argument
955 CHECK_FP(fp); in setbuf()
956 setbuffer(fp, buf, BUFSIZ); in setbuf()
959 void setbuffer(FILE* fp, char* buf, int size) { in setbuffer() argument
960 CHECK_FP(fp); in setbuffer()
961 setvbuf(fp, buf, buf ? _IOFBF : _IONBF, size); in setbuffer()
964 int setlinebuf(FILE* fp) { in setlinebuf() argument
965 CHECK_FP(fp); in setlinebuf()
966 return setvbuf(fp, nullptr, _IOLBF, 0); in setlinebuf()
989 int vfprintf(FILE* fp, const char* fmt, va_list ap) { in vfprintf() argument
990 ScopedFileLock sfl(fp); in vfprintf()
991 return __vfprintf(fp, fmt, ap); in vfprintf()
994 int vfscanf(FILE* fp, const char* fmt, va_list ap) { in vfscanf() argument
995 ScopedFileLock sfl(fp); in vfscanf()
996 return __svfscanf(fp, fmt, ap); in vfscanf()
999 int vfwprintf(FILE* fp, const wchar_t* fmt, va_list ap) { in vfwprintf() argument
1000 ScopedFileLock sfl(fp); in vfwprintf()
1001 return __vfwprintf(fp, fmt, ap); in vfwprintf()
1004 int vfwscanf(FILE* fp, const wchar_t* fmt, va_list ap) { in vfwscanf() argument
1005 ScopedFileLock sfl(fp); in vfwscanf()
1006 return __vfwscanf(fp, fmt, ap); in vfwscanf()
1067 int fflush(FILE* fp) { in fflush() argument
1068 if (fp == nullptr) return fflush_all(); in fflush()
1069 ScopedFileLock sfl(fp); in fflush()
1070 return fflush_unlocked(fp); in fflush()
1073 int fflush_unlocked(FILE* fp) { in fflush_unlocked() argument
1074 if (fp == nullptr) return fflush_all(); in fflush_unlocked()
1075 if ((fp->_flags & (__SWR | __SRW)) == 0) { in fflush_unlocked()
1079 return __sflush(fp); in fflush_unlocked()
1082 size_t fread(void* buf, size_t size, size_t count, FILE* fp) { in fread() argument
1083 CHECK_FP(fp); in fread()
1084 ScopedFileLock sfl(fp); in fread()
1085 return fread_unlocked(buf, size, count, fp); in fread()
1088 size_t fread_unlocked(void* buf, size_t size, size_t count, FILE* fp) { in fread_unlocked() argument
1089 CHECK_FP(fp); in fread_unlocked()
1094 fp->_flags |= __SERR; in fread_unlocked()
1101 _SET_ORIENTATION(fp, ORIENT_BYTES); in fread_unlocked()
1104 if (fp->_r < 0) fp->_r = 0; in fread_unlocked()
1107 if (fp->_bf._base == nullptr) __smakebuf(fp); in fread_unlocked()
1113 size_t buffered_bytes = MIN(static_cast<size_t>(fp->_r), total); in fread_unlocked()
1114 memcpy(dst, fp->_p, buffered_bytes); in fread_unlocked()
1115 fp->_p += buffered_bytes; in fread_unlocked()
1116 fp->_r -= buffered_bytes; in fread_unlocked()
1124 if (total > static_cast<size_t>(fp->_bf._size)) break; in fread_unlocked()
1127 if (__srefill(fp)) goto out; in fread_unlocked()
1134 ssize_t bytes_read = (*fp->_read)(fp->_cookie, dst, chunk_size); in fread_unlocked()
1136 fp->_flags |= (bytes_read == 0) ? __SEOF : __SERR; in fread_unlocked()
1147 size_t fwrite(const void* buf, size_t size, size_t count, FILE* fp) { in fwrite() argument
1148 CHECK_FP(fp); in fwrite()
1149 ScopedFileLock sfl(fp); in fwrite()
1150 return fwrite_unlocked(buf, size, count, fp); in fwrite()
1153 size_t fwrite_unlocked(const void* buf, size_t size, size_t count, FILE* fp) { in fwrite_unlocked() argument
1154 CHECK_FP(fp); in fwrite_unlocked()
1159 fp->_flags |= __SERR; in fwrite_unlocked()
1168 _SET_ORIENTATION(fp, ORIENT_BYTES); in fwrite_unlocked()
1172 return (__sfvwrite(fp, &uio) == 0) ? count : ((n - uio.uio_resid) / size); in fwrite_unlocked()
1228 FILE* fp = fdopen(fds[parent], mode); in popen() local
1229 if (fp == nullptr) return __popen_fail(fds); in popen()
1233 _EXT(fp)->_popen_pid = pid; in popen()
1234 return fp; in popen()
1237 int pclose(FILE* fp) { in pclose() argument
1238 CHECK_FP(fp); in pclose()
1239 return __FILE_close(fp); in pclose()
1242 void flockfile(FILE* fp) { in flockfile() argument
1243 CHECK_FP(fp); in flockfile()
1244 pthread_mutex_lock(&_EXT(fp)->_lock); in flockfile()
1247 int ftrylockfile(FILE* fp) { in ftrylockfile() argument
1248 CHECK_FP(fp); in ftrylockfile()
1251 return pthread_mutex_trylock(&_EXT(fp)->_lock); in ftrylockfile()
1254 void funlockfile(FILE* fp) { in funlockfile() argument
1255 CHECK_FP(fp); in funlockfile()
1256 pthread_mutex_unlock(&_EXT(fp)->_lock); in funlockfile()