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>
30923f165b29866cba1bd077117127f576763b384dElliott Hughes
31923f165b29866cba1bd077117127f576763b384dElliott Hughes#include <errno.h>
328c4994bbc1a9a01e34ea92c91eb5b2d1a27bd074Elliott Hughes#include <stdlib.h>
332b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes
342b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes#include "local.h"
358c4994bbc1a9a01e34ea92c91eb5b2d1a27bd074Elliott Hughes#include "private/libc_logging.h"
362b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes
372b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughessize_t __fbufsize(FILE* fp) {
382b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes  return fp->_bf._size;
392b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes}
402b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes
412b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes/* For a _SRW stream, we don't know whether we last read or wrote.
422b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughesint __freading(FILE* fp) {
432b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes  return (fp->_flags & _SRD) != 0 || ...;
442b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes}
452b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes*/
462b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes
472b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes/* For a _SRW stream, we don't know whether we last read or wrote.
482b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughesint __fwriting(FILE*) {
492b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes  return (fp->_flags & _SWR) != 0 || ...;
502b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes}
512b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes*/
522b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes
532b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughesint __freadable(FILE* fp) {
542b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes  return (fp->_flags & (__SRD|__SRW)) != 0;
552b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes}
562b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes
572b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughesint __fwritable(FILE* fp) {
582b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes  return (fp->_flags & (__SWR|__SRW)) != 0;
592b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes}
602b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes
612b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughesint __flbf(FILE* fp) {
622b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes  return (fp->_flags & __SLBF) != 0;
632b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes}
642b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes
652b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughesvoid __fpurge(FILE* fp) {
662b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes  fpurge(fp);
672b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes}
682b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes
692b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughessize_t __fpending(FILE* fp) {
702b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes  return fp->_p - fp->_bf._base;
712b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes}
722b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes
732b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughesvoid _flushlbf() {
742b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes  // If we flush all streams, we know we've flushed all the line-buffered streams.
752b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes  fflush(NULL);
762b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes}
772b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes
788c4994bbc1a9a01e34ea92c91eb5b2d1a27bd074Elliott Hughesint __fsetlocking(FILE* fp, int type) {
7976144aaa6397fe9e16893882cf59c5c9c0684a66Yabin Cui  int old_state = _EXT(fp)->_caller_handles_locking ? FSETLOCKING_BYCALLER : FSETLOCKING_INTERNAL;
808c4994bbc1a9a01e34ea92c91eb5b2d1a27bd074Elliott Hughes  if (type == FSETLOCKING_QUERY) {
818c4994bbc1a9a01e34ea92c91eb5b2d1a27bd074Elliott Hughes    return old_state;
828c4994bbc1a9a01e34ea92c91eb5b2d1a27bd074Elliott Hughes  }
838c4994bbc1a9a01e34ea92c91eb5b2d1a27bd074Elliott Hughes
848c4994bbc1a9a01e34ea92c91eb5b2d1a27bd074Elliott Hughes  if (type != FSETLOCKING_INTERNAL && type != FSETLOCKING_BYCALLER) {
858c4994bbc1a9a01e34ea92c91eb5b2d1a27bd074Elliott Hughes    // The API doesn't let us report an error, so blow up.
868c4994bbc1a9a01e34ea92c91eb5b2d1a27bd074Elliott Hughes    __libc_fatal("Bad type (%d) passed to __fsetlocking", type);
878c4994bbc1a9a01e34ea92c91eb5b2d1a27bd074Elliott Hughes  }
888c4994bbc1a9a01e34ea92c91eb5b2d1a27bd074Elliott Hughes
8976144aaa6397fe9e16893882cf59c5c9c0684a66Yabin Cui  _EXT(fp)->_caller_handles_locking = (type == FSETLOCKING_BYCALLER);
908c4994bbc1a9a01e34ea92c91eb5b2d1a27bd074Elliott Hughes  return old_state;
912b021e10664c3938249eb18b48eeac253cbb3e20Elliott Hughes}
92