12b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes/*
22b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes * Copyright (C) 2014 The Android Open Source Project
32b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes * All rights reserved.
42b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes *
52b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes * Redistribution and use in source and binary forms, with or without
62b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes * modification, are permitted provided that the following conditions
72b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes * are met:
82b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes *  * Redistributions of source code must retain the above copyright
92b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes *    notice, this list of conditions and the following disclaimer.
102b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes *  * Redistributions in binary form must reproduce the above copyright
112b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes *    notice, this list of conditions and the following disclaimer in
122b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes *    the documentation and/or other materials provided with the
132b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes *    distribution.
142b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes *
152b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
162b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
172b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
182b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
192b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
202b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
212b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
222b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
232b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
242b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
252b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
262b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes * SUCH DAMAGE.
272b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes */
282b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes
292b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes#include <stdio_ext.h>
308c4994bbc1a9a01e34ea92c91eb5b2d1a27bd074Elliott Hughes#include <stdlib.h>
312b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes
322b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes#include "local.h"
338c4994bbc1a9a01e34ea92c91eb5b2d1a27bd074Elliott Hughes#include "private/libc_logging.h"
342b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes
352b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughessize_t __fbufsize(FILE* fp) {
362b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes  return fp->_bf._size;
372b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes}
382b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes
392b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes/* For a _SRW stream, we don't know whether we last read or wrote.
402b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughesint __freading(FILE* fp) {
412b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes  return (fp->_flags & _SRD) != 0 || ...;
422b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes}
432b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes*/
442b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes
452b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes/* For a _SRW stream, we don't know whether we last read or wrote.
462b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughesint __fwriting(FILE*) {
472b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes  return (fp->_flags & _SWR) != 0 || ...;
482b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes}
492b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes*/
502b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes
512b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughesint __freadable(FILE* fp) {
522b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes  return (fp->_flags & (__SRD|__SRW)) != 0;
532b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes}
542b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes
552b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughesint __fwritable(FILE* fp) {
562b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes  return (fp->_flags & (__SWR|__SRW)) != 0;
572b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes}
582b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes
592b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughesint __flbf(FILE* fp) {
602b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes  return (fp->_flags & __SLBF) != 0;
612b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes}
622b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes
632b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughesvoid __fpurge(FILE* fp) {
642b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes  fpurge(fp);
652b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes}
662b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes
672b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughessize_t __fpending(FILE* fp) {
682b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes  return fp->_p - fp->_bf._base;
692b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes}
702b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes
712b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughesvoid _flushlbf() {
722b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes  // If we flush all streams, we know we've flushed all the line-buffered streams.
732b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes  fflush(NULL);
742b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes}
752b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes
768c4994bbc1a9a01e34ea92c91eb5b2d1a27bd074Elliott Hughesint __fsetlocking(FILE* fp, int type) {
778c4994bbc1a9a01e34ea92c91eb5b2d1a27bd074Elliott Hughes  int old_state = _EXT(fp)->_stdio_handles_locking ? FSETLOCKING_INTERNAL : FSETLOCKING_BYCALLER;
788c4994bbc1a9a01e34ea92c91eb5b2d1a27bd074Elliott Hughes  if (type == FSETLOCKING_QUERY) {
798c4994bbc1a9a01e34ea92c91eb5b2d1a27bd074Elliott Hughes    return old_state;
808c4994bbc1a9a01e34ea92c91eb5b2d1a27bd074Elliott Hughes  }
818c4994bbc1a9a01e34ea92c91eb5b2d1a27bd074Elliott Hughes
828c4994bbc1a9a01e34ea92c91eb5b2d1a27bd074Elliott Hughes  if (type != FSETLOCKING_INTERNAL && type != FSETLOCKING_BYCALLER) {
838c4994bbc1a9a01e34ea92c91eb5b2d1a27bd074Elliott Hughes    // The API doesn't let us report an error, so blow up.
848c4994bbc1a9a01e34ea92c91eb5b2d1a27bd074Elliott Hughes    __libc_fatal("Bad type (%d) passed to __fsetlocking", type);
858c4994bbc1a9a01e34ea92c91eb5b2d1a27bd074Elliott Hughes  }
868c4994bbc1a9a01e34ea92c91eb5b2d1a27bd074Elliott Hughes
878c4994bbc1a9a01e34ea92c91eb5b2d1a27bd074Elliott Hughes  _EXT(fp)->_stdio_handles_locking = (type == FSETLOCKING_INTERNAL);
888c4994bbc1a9a01e34ea92c91eb5b2d1a27bd074Elliott Hughes  return old_state;
892b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes}
902b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes
912b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughesvoid clearerr_unlocked(FILE* fp) {
922b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes  return __sclearerr(fp);
932b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes}
942b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes
952b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughesint feof_unlocked(FILE* fp) {
962b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes  return __sfeof(fp);
972b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes}
982b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes
992b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughesint ferror_unlocked(FILE* fp) {
1002b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes  return __sferror(fp);
1012b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes}
102