os/ossrv/genericopenlibs/cstdlib/LSTDIO/REFILL.C
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/LSTDIO/REFILL.C	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,117 @@
     1.4 +/* REFILL.C
     1.5 + * 
     1.6 + * Portions Copyright (c) 1990-1999 Nokia Corporation and/or its subsidiary(-ies).
     1.7 + * All rights reserved.
     1.8 + */
     1.9 +
    1.10 +/* No user fns here.  Pesch 15apr92. */
    1.11 +
    1.12 +/*
    1.13 + * Copyright (c) 1990 The Regents of the University of California.
    1.14 + * All rights reserved.
    1.15 + *
    1.16 + * Redistribution and use in source and binary forms are permitted
    1.17 + * provided that the above copyright notice and this paragraph are
    1.18 + * duplicated in all such forms and that any documentation,
    1.19 + * advertising materials, and other materials related to such
    1.20 + * distribution and use acknowledge that the software was developed
    1.21 + * by the University of California, Berkeley.  The name of the
    1.22 + * University may not be used to endorse or promote products derived
    1.23 + * from this software without specific prior written permission.
    1.24 + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
    1.25 + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
    1.26 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
    1.27 + */
    1.28 +
    1.29 +#include <stdio_r.h>
    1.30 +#include "LOCAL.H"
    1.31 +#include <stdlib_r.h>
    1.32 +
    1.33 +static int
    1.34 +lflush (FILE *fp)
    1.35 +{
    1.36 +  if ((fp->_flags & (__SLBF | __SWR)) == (__SLBF | __SWR))
    1.37 +    return fflush (fp);
    1.38 +  return 0;
    1.39 +}
    1.40 +
    1.41 +/*
    1.42 + * Refill a stdio buffer.
    1.43 + * Return EOF on eof or error, 0 otherwise.
    1.44 + */
    1.45 +
    1.46 +int
    1.47 +__srefill (register FILE * fp)
    1.48 +{
    1.49 +  /* make sure stdio is set up */
    1.50 +
    1.51 +  CHECK_INIT (fp);
    1.52 +
    1.53 +  fp->_r = 0;			/* largely a convenience for callers */
    1.54 +
    1.55 +  /* SysV does not make this test; take it out for compatibility */
    1.56 +  if (fp->_flags & __SEOF)
    1.57 +    return EOF;
    1.58 +
    1.59 +  /* if not already reading, have to be reading and writing */
    1.60 +  if ((fp->_flags & __SRD) == 0)
    1.61 +    {
    1.62 +      if ((fp->_flags & __SRW) == 0)
    1.63 +	return EOF;
    1.64 +      /* switch to reading */
    1.65 +      if (fp->_flags & __SWR)
    1.66 +	{
    1.67 +	  if (fflush (fp))
    1.68 +	    return EOF;
    1.69 +	  fp->_flags &= ~__SWR;
    1.70 +	  fp->_w = 0;
    1.71 +	  fp->_lbfsize = 0;
    1.72 +	}
    1.73 +      fp->_flags |= __SRD;
    1.74 +    }
    1.75 +  else
    1.76 +    {
    1.77 +      /*
    1.78 +       * We were reading.  If there is an ungetc buffer,
    1.79 +       * we must have been reading from that.  Drop it,
    1.80 +       * restoring the previous buffer (if any).  If there
    1.81 +       * is anything in that buffer, return.
    1.82 +       */
    1.83 +      if (HASUB (fp))
    1.84 +	{
    1.85 +	  FREEUB (fp);
    1.86 +	  if ((fp->_r = fp->_ur) != 0)
    1.87 +	    {
    1.88 +	      fp->_p = fp->_up;
    1.89 +	      return 0;
    1.90 +	    }
    1.91 +	}
    1.92 +    }
    1.93 +
    1.94 +  if (fp->_bf._base == NULL)
    1.95 +    __smakebuf (fp);
    1.96 +
    1.97 +  /*
    1.98 +   * Before reading from a line buffered or unbuffered file,
    1.99 +   * flush all line buffered output files, per the ANSI C
   1.100 +   * standard.
   1.101 +   */
   1.102 +
   1.103 +  if (fp->_flags & (__SLBF | __SNBF))
   1.104 +    (void) _fwalk (fp->_data, lflush);
   1.105 +  fp->_p = fp->_bf._base;
   1.106 +  fp->_r = (*fp->_read) (fp->_cookie, (char *) fp->_p, fp->_bf._size);
   1.107 +  fp->_flags &= ~__SMOD;	/* buffer contents are again pristine */
   1.108 +  if (fp->_r <= 0)
   1.109 +    {
   1.110 +      if (fp->_r == 0)
   1.111 +	fp->_flags |= __SEOF;
   1.112 +      else
   1.113 +	{
   1.114 +	  fp->_r = 0;
   1.115 +	  fp->_flags |= __SERR;
   1.116 +	}
   1.117 +      return EOF;
   1.118 +    }
   1.119 +  return 0;
   1.120 +}