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