os/ossrv/genericopenlibs/cstdlib/LSTDIO/WBUF.C
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/LSTDIO/WBUF.C	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,81 @@
     1.4 +/* WBUF.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.h>
    1.30 +#include "LOCAL.H"
    1.31 +#include "FVWRITE.H"
    1.32 +
    1.33 +/*
    1.34 + * Write the given character into the (probably full) buffer for
    1.35 + * the given file.  Flush the buffer out if it is or becomes full,
    1.36 + * or if c=='\n' and the file is line buffered.
    1.37 + */
    1.38 +
    1.39 +int
    1.40 +__swbuf (register int c,register FILE *fp)
    1.41 +{
    1.42 +  register int n;
    1.43 +
    1.44 +  /* Ensure stdio has been initialized.  */
    1.45 +
    1.46 +  CHECK_INIT (fp);
    1.47 +
    1.48 +  /*
    1.49 +   * In case we cannot write, or longjmp takes us out early,
    1.50 +   * make sure _w is 0 (if fully- or un-buffered) or -_bf._size
    1.51 +   * (if line buffered) so that we will get called again.
    1.52 +   * If we did not do this, a sufficient number of putc()
    1.53 +   * calls might wrap _w from negative to positive.
    1.54 +   */
    1.55 +
    1.56 +  fp->_w = fp->_lbfsize;
    1.57 +  if (cantwrite (fp))
    1.58 +    return EOF;
    1.59 +  c = (unsigned char) c;
    1.60 +
    1.61 +  /*
    1.62 +   * If it is completely full, flush it out.  Then, in any case,
    1.63 +   * stuff c into the buffer.  If this causes the buffer to fill
    1.64 +   * completely, or if c is '\n' and the file is line buffered,
    1.65 +   * flush it (perhaps a second time).  The second flush will always
    1.66 +   * happen on unbuffered streams, where _bf._size==1; fflush()
    1.67 +   * guarantees that putc() will always call wbuf() by setting _w
    1.68 +   * to 0, so we need not do anything else.
    1.69 +   */
    1.70 +
    1.71 +  n = fp->_p - fp->_bf._base;
    1.72 +  if (n >= fp->_bf._size)
    1.73 +    {
    1.74 +      if (fflush (fp))
    1.75 +	return EOF;
    1.76 +      n = 0;
    1.77 +    }
    1.78 +  fp->_w--;
    1.79 +  *fp->_p++ = (unsigned char)c;
    1.80 +  if (++n == fp->_bf._size || (fp->_flags & __SLBF && c == '\n'))
    1.81 +    if (fflush (fp))
    1.82 +      return EOF;
    1.83 +  return c;
    1.84 +}