os/ossrv/genericopenlibs/cstdlib/LSTDIO/FFLUSH.C
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/LSTDIO/FFLUSH.C	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,114 @@
     1.4 +/* FFLUSH.C
     1.5 + * 
     1.6 + * Portions Copyright (c) 1990-2006 Nokia Corporation and/or its subsidiary(-ies).
     1.7 + * All rights reserved.
     1.8 + */
     1.9 +
    1.10 +/*
    1.11 + * Copyright (c) 1990 The Regents of the University of California.
    1.12 + * All rights reserved.
    1.13 + *
    1.14 + * Redistribution and use in source and binary forms are permitted
    1.15 + * provided that the above copyright notice and this paragraph are
    1.16 + * duplicated in all such forms and that any documentation,
    1.17 + * advertising materials, and other materials related to such
    1.18 + * distribution and use acknowledge that the software was developed
    1.19 + * by the University of California, Berkeley.  The name of the
    1.20 + * University may not be used to endorse or promote products derived
    1.21 + * from this software without specific prior written permission.
    1.22 + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
    1.23 + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
    1.24 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 +FUNCTION
    1.29 +<<fflush>>---flush buffered file output
    1.30 +
    1.31 +INDEX
    1.32 +	fflush
    1.33 +
    1.34 +ANSI_SYNOPSIS
    1.35 +	#include <stdio.h>
    1.36 +	int fflush(FILE *<[fp]>);
    1.37 +
    1.38 +TRAD_SYNOPSIS
    1.39 +	#include <stdio.h>
    1.40 +	int fflush(<[fp]>)
    1.41 +	FILE *<[fp]>;
    1.42 +
    1.43 +DESCRIPTION
    1.44 +The <<stdio>> output functions can buffer output before delivering it
    1.45 +to the host system, in order to minimize the overhead of system calls.
    1.46 +
    1.47 +Use <<fflush>> to deliver any such pending output (for the file
    1.48 +or stream identified by <[fp]>) to the host system.
    1.49 +
    1.50 +If <[fp]> is <<NULL>>, <<fflush>> delivers pending output from all
    1.51 +open files.
    1.52 +
    1.53 +RETURNS
    1.54 +<<fflush>> returns <<0>> unless it encounters a write error; in that
    1.55 +situation, it returns <<EOF>>.
    1.56 +
    1.57 +PORTABILITY
    1.58 +ANSI C requires <<fflush>>.
    1.59 +
    1.60 +No supporting OS subroutines are required.
    1.61 +*/
    1.62 +
    1.63 +#include <stdio.h>
    1.64 +#include "LOCAL.H"
    1.65 +
    1.66 +/**
    1.67 +Flush a stream. If the given stream has been opened for writing operations
    1.68 +the output buffer is phisically written to the file. If the stream was open
    1.69 +for reading operations the content of the input buffer is cleared. The stream
    1.70 +remains open after this call.
    1.71 +
    1.72 +@param fp pointer to an open file.
    1.73 +
    1.74 +@return On Success, a 0 value indicates success.
    1.75 +		On Failure, EOF is returned and errno may be set.
    1.76 +*/
    1.77 +EXPORT_C int
    1.78 +fflush (FILE * fp)
    1.79 +{
    1.80 +  register unsigned char *p;
    1.81 +  register int n, t;
    1.82 +
    1.83 +  if (fp == NULL)
    1.84 +  	{
    1.85 +    struct _reent *r = _REENT2;
    1.86 +    if (!r)
    1.87 +	  return EOF; // Memory for library globals is not allocated (errno not set).
    1.88 +    return _fwalk (r, fflush);
    1.89 +  	}
    1.90 +  CHECK_INIT (fp);
    1.91 +
    1.92 +  t = fp->_flags;
    1.93 +  if ((t & __SWR) == 0 || (p = fp->_bf._base) == NULL)
    1.94 +    return 0;
    1.95 +  n = fp->_p - p;		/* write this much */
    1.96 +
    1.97 +  /*
    1.98 +   * Set these immediately to avoid problems with longjmp
    1.99 +   * and to allow exchange buffering (via setvbuf) in user
   1.100 +   * write function.
   1.101 +   */
   1.102 +  fp->_p = p;
   1.103 +  fp->_w = t & (__SLBF | __SNBF) ? 0 : fp->_bf._size;
   1.104 +
   1.105 +  while (n > 0)
   1.106 +    {
   1.107 +      t = (*fp->_write) (fp->_cookie, (char *) p, n);
   1.108 +      if (t <= 0)
   1.109 +	{
   1.110 +	  fp->_flags |= __SERR;
   1.111 +	  return EOF;
   1.112 +	}
   1.113 +      p += t;
   1.114 +      n -= t;
   1.115 +    }
   1.116 +  return 0;
   1.117 +}