os/ossrv/genericopenlibs/cstdlib/LSTDIO/FFLUSH.C
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /* FFLUSH.C
     2  * 
     3  * Portions Copyright (c) 1990-2006 Nokia Corporation and/or its subsidiary(-ies).
     4  * All rights reserved.
     5  */
     6 
     7 /*
     8  * Copyright (c) 1990 The Regents of the University of California.
     9  * All rights reserved.
    10  *
    11  * Redistribution and use in source and binary forms are permitted
    12  * provided that the above copyright notice and this paragraph are
    13  * duplicated in all such forms and that any documentation,
    14  * advertising materials, and other materials related to such
    15  * distribution and use acknowledge that the software was developed
    16  * by the University of California, Berkeley.  The name of the
    17  * University may not be used to endorse or promote products derived
    18  * from this software without specific prior written permission.
    19  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
    20  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
    21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
    22  */
    23 
    24 /*
    25 FUNCTION
    26 <<fflush>>---flush buffered file output
    27 
    28 INDEX
    29 	fflush
    30 
    31 ANSI_SYNOPSIS
    32 	#include <stdio.h>
    33 	int fflush(FILE *<[fp]>);
    34 
    35 TRAD_SYNOPSIS
    36 	#include <stdio.h>
    37 	int fflush(<[fp]>)
    38 	FILE *<[fp]>;
    39 
    40 DESCRIPTION
    41 The <<stdio>> output functions can buffer output before delivering it
    42 to the host system, in order to minimize the overhead of system calls.
    43 
    44 Use <<fflush>> to deliver any such pending output (for the file
    45 or stream identified by <[fp]>) to the host system.
    46 
    47 If <[fp]> is <<NULL>>, <<fflush>> delivers pending output from all
    48 open files.
    49 
    50 RETURNS
    51 <<fflush>> returns <<0>> unless it encounters a write error; in that
    52 situation, it returns <<EOF>>.
    53 
    54 PORTABILITY
    55 ANSI C requires <<fflush>>.
    56 
    57 No supporting OS subroutines are required.
    58 */
    59 
    60 #include <stdio.h>
    61 #include "LOCAL.H"
    62 
    63 /**
    64 Flush a stream. If the given stream has been opened for writing operations
    65 the output buffer is phisically written to the file. If the stream was open
    66 for reading operations the content of the input buffer is cleared. The stream
    67 remains open after this call.
    68 
    69 @param fp pointer to an open file.
    70 
    71 @return On Success, a 0 value indicates success.
    72 		On Failure, EOF is returned and errno may be set.
    73 */
    74 EXPORT_C int
    75 fflush (FILE * fp)
    76 {
    77   register unsigned char *p;
    78   register int n, t;
    79 
    80   if (fp == NULL)
    81   	{
    82     struct _reent *r = _REENT2;
    83     if (!r)
    84 	  return EOF; // Memory for library globals is not allocated (errno not set).
    85     return _fwalk (r, fflush);
    86   	}
    87   CHECK_INIT (fp);
    88 
    89   t = fp->_flags;
    90   if ((t & __SWR) == 0 || (p = fp->_bf._base) == NULL)
    91     return 0;
    92   n = fp->_p - p;		/* write this much */
    93 
    94   /*
    95    * Set these immediately to avoid problems with longjmp
    96    * and to allow exchange buffering (via setvbuf) in user
    97    * write function.
    98    */
    99   fp->_p = p;
   100   fp->_w = t & (__SLBF | __SNBF) ? 0 : fp->_bf._size;
   101 
   102   while (n > 0)
   103     {
   104       t = (*fp->_write) (fp->_cookie, (char *) p, n);
   105       if (t <= 0)
   106 	{
   107 	  fp->_flags |= __SERR;
   108 	  return EOF;
   109 	}
   110       p += t;
   111       n -= t;
   112     }
   113   return 0;
   114 }