fixes for host gcc 4.6.1
[zpugcc/jano.git] / toolchain / gcc / newlib / libc / stdio / fflush.c
blob30ac9967ff21797b04d1b5c1f5e61cff47609bd0
1 /*
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 FUNCTION
20 <<fflush>>---flush buffered file output
22 INDEX
23 fflush
25 ANSI_SYNOPSIS
26 #include <stdio.h>
27 int fflush(FILE *<[fp]>);
29 TRAD_SYNOPSIS
30 #include <stdio.h>
31 int fflush(<[fp]>)
32 FILE *<[fp]>;
34 DESCRIPTION
35 The <<stdio>> output functions can buffer output before delivering it
36 to the host system, in order to minimize the overhead of system calls.
38 Use <<fflush>> to deliver any such pending output (for the file
39 or stream identified by <[fp]>) to the host system.
41 If <[fp]> is <<NULL>>, <<fflush>> delivers pending output from all
42 open files.
44 RETURNS
45 <<fflush>> returns <<0>> unless it encounters a write error; in that
46 situation, it returns <<EOF>>.
48 PORTABILITY
49 ANSI C requires <<fflush>>.
51 No supporting OS subroutines are required.
54 #include <stdio.h>
55 #include "local.h"
57 /* Flush a single file, or (if fp is NULL) all files. */
59 int
60 _DEFUN (fflush, (fp),
61 register FILE * fp)
63 register unsigned char *p;
64 register int n, t;
66 if (fp == NULL)
67 return _fwalk (_GLOBAL_REENT, fflush);
69 _flockfile(fp);
71 CHECK_INIT (fp);
73 t = fp->_flags;
74 if ((t & __SWR) == 0 || (p = fp->_bf._base) == NULL)
76 _funlockfile(fp);
77 return 0;
79 n = fp->_p - p; /* write this much */
82 * Set these immediately to avoid problems with longjmp
83 * and to allow exchange buffering (via setvbuf) in user
84 * write function.
86 fp->_p = p;
87 fp->_w = t & (__SLBF | __SNBF) ? 0 : fp->_bf._size;
89 while (n > 0)
91 t = (*fp->_write) (fp->_cookie, (char *) p, n);
92 if (t <= 0)
94 fp->_flags |= __SERR;
95 _funlockfile(fp);
96 return EOF;
98 p += t;
99 n -= t;
101 _funlockfile(fp);
102 return 0;