os/ossrv/genericopenlibs/cstdlib/LSTDIO/REFILL.C
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* REFILL.C
sl@0
     2
 * 
sl@0
     3
 * Portions Copyright (c) 1990-1999 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     4
 * All rights reserved.
sl@0
     5
 */
sl@0
     6
sl@0
     7
/* No user fns here.  Pesch 15apr92. */
sl@0
     8
sl@0
     9
/*
sl@0
    10
 * Copyright (c) 1990 The Regents of the University of California.
sl@0
    11
 * All rights reserved.
sl@0
    12
 *
sl@0
    13
 * Redistribution and use in source and binary forms are permitted
sl@0
    14
 * provided that the above copyright notice and this paragraph are
sl@0
    15
 * duplicated in all such forms and that any documentation,
sl@0
    16
 * advertising materials, and other materials related to such
sl@0
    17
 * distribution and use acknowledge that the software was developed
sl@0
    18
 * by the University of California, Berkeley.  The name of the
sl@0
    19
 * University may not be used to endorse or promote products derived
sl@0
    20
 * from this software without specific prior written permission.
sl@0
    21
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
sl@0
    22
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
sl@0
    23
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
sl@0
    24
 */
sl@0
    25
sl@0
    26
#include <stdio_r.h>
sl@0
    27
#include "LOCAL.H"
sl@0
    28
#include <stdlib_r.h>
sl@0
    29
sl@0
    30
static int
sl@0
    31
lflush (FILE *fp)
sl@0
    32
{
sl@0
    33
  if ((fp->_flags & (__SLBF | __SWR)) == (__SLBF | __SWR))
sl@0
    34
    return fflush (fp);
sl@0
    35
  return 0;
sl@0
    36
}
sl@0
    37
sl@0
    38
/*
sl@0
    39
 * Refill a stdio buffer.
sl@0
    40
 * Return EOF on eof or error, 0 otherwise.
sl@0
    41
 */
sl@0
    42
sl@0
    43
int
sl@0
    44
__srefill (register FILE * fp)
sl@0
    45
{
sl@0
    46
  /* make sure stdio is set up */
sl@0
    47
sl@0
    48
  CHECK_INIT (fp);
sl@0
    49
sl@0
    50
  fp->_r = 0;			/* largely a convenience for callers */
sl@0
    51
sl@0
    52
  /* SysV does not make this test; take it out for compatibility */
sl@0
    53
  if (fp->_flags & __SEOF)
sl@0
    54
    return EOF;
sl@0
    55
sl@0
    56
  /* if not already reading, have to be reading and writing */
sl@0
    57
  if ((fp->_flags & __SRD) == 0)
sl@0
    58
    {
sl@0
    59
      if ((fp->_flags & __SRW) == 0)
sl@0
    60
	return EOF;
sl@0
    61
      /* switch to reading */
sl@0
    62
      if (fp->_flags & __SWR)
sl@0
    63
	{
sl@0
    64
	  if (fflush (fp))
sl@0
    65
	    return EOF;
sl@0
    66
	  fp->_flags &= ~__SWR;
sl@0
    67
	  fp->_w = 0;
sl@0
    68
	  fp->_lbfsize = 0;
sl@0
    69
	}
sl@0
    70
      fp->_flags |= __SRD;
sl@0
    71
    }
sl@0
    72
  else
sl@0
    73
    {
sl@0
    74
      /*
sl@0
    75
       * We were reading.  If there is an ungetc buffer,
sl@0
    76
       * we must have been reading from that.  Drop it,
sl@0
    77
       * restoring the previous buffer (if any).  If there
sl@0
    78
       * is anything in that buffer, return.
sl@0
    79
       */
sl@0
    80
      if (HASUB (fp))
sl@0
    81
	{
sl@0
    82
	  FREEUB (fp);
sl@0
    83
	  if ((fp->_r = fp->_ur) != 0)
sl@0
    84
	    {
sl@0
    85
	      fp->_p = fp->_up;
sl@0
    86
	      return 0;
sl@0
    87
	    }
sl@0
    88
	}
sl@0
    89
    }
sl@0
    90
sl@0
    91
  if (fp->_bf._base == NULL)
sl@0
    92
    __smakebuf (fp);
sl@0
    93
sl@0
    94
  /*
sl@0
    95
   * Before reading from a line buffered or unbuffered file,
sl@0
    96
   * flush all line buffered output files, per the ANSI C
sl@0
    97
   * standard.
sl@0
    98
   */
sl@0
    99
sl@0
   100
  if (fp->_flags & (__SLBF | __SNBF))
sl@0
   101
    (void) _fwalk (fp->_data, lflush);
sl@0
   102
  fp->_p = fp->_bf._base;
sl@0
   103
  fp->_r = (*fp->_read) (fp->_cookie, (char *) fp->_p, fp->_bf._size);
sl@0
   104
  fp->_flags &= ~__SMOD;	/* buffer contents are again pristine */
sl@0
   105
  if (fp->_r <= 0)
sl@0
   106
    {
sl@0
   107
      if (fp->_r == 0)
sl@0
   108
	fp->_flags |= __SEOF;
sl@0
   109
      else
sl@0
   110
	{
sl@0
   111
	  fp->_r = 0;
sl@0
   112
	  fp->_flags |= __SERR;
sl@0
   113
	}
sl@0
   114
      return EOF;
sl@0
   115
    }
sl@0
   116
  return 0;
sl@0
   117
}