|
0
|
1 //======================================================================== |
|
|
2 // |
|
|
3 // fflush.cxx |
|
|
4 // |
|
|
5 // Implementation of C library file flush function as per ANSI 7.9.5.2 |
|
|
6 // |
|
|
7 //======================================================================== |
|
|
8 //####COPYRIGHTBEGIN#### |
|
|
9 // |
|
|
10 // ------------------------------------------- |
|
|
11 // The contents of this file are subject to the Cygnus eCos Public License |
|
|
12 // Version 1.0 (the "License"); you may not use this file except in |
|
|
13 // compliance with the License. You may obtain a copy of the License at |
|
|
14 // http://sourceware.cygnus.com/ecos |
|
|
15 // |
|
|
16 // Software distributed under the License is distributed on an "AS IS" |
|
|
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|
|
18 // License for the specific language governing rights and limitations under |
|
|
19 // the License. |
|
|
20 // |
|
|
21 // The Original Code is eCos - Embedded Cygnus Operating System, released |
|
|
22 // September 30, 1998. |
|
|
23 // |
|
|
24 // The Initial Developer of the Original Code is Cygnus. Portions created |
|
2
|
25 // by Cygnus are Copyright (C) 1998,1999 Cygnus Solutions. All Rights Reserved. |
|
0
|
26 // ------------------------------------------- |
|
|
27 // |
|
|
28 //####COPYRIGHTEND#### |
|
|
29 //======================================================================== |
|
|
30 //#####DESCRIPTIONBEGIN#### |
|
|
31 // |
|
2
|
32 // Author(s): jlarmour |
|
|
33 // Contributors: jlarmour |
|
|
34 // Date: 1999-01-21 |
|
|
35 // Purpose: Provides ISO C fflush() function |
|
0
|
36 // Description: |
|
|
37 // Usage: |
|
|
38 // |
|
|
39 //####DESCRIPTIONEND#### |
|
|
40 // |
|
|
41 //======================================================================== |
|
|
42 |
|
|
43 // CONFIGURATION |
|
|
44 |
|
|
45 #include <pkgconf/libc.h> // Configuration header |
|
|
46 |
|
2
|
47 // Do we want the stdio stuff? |
|
|
48 #ifdef CYGPKG_LIBC_STDIO |
|
0
|
49 |
|
|
50 // INCLUDES |
|
|
51 |
|
|
52 #include <cyg/infra/cyg_type.h> // Common project-wide type definitions |
|
|
53 #include <stddef.h> // NULL and size_t from compiler |
|
|
54 #include <errno.h> // Error codes |
|
|
55 #include <stdio.h> // header for fflush() |
|
|
56 #include "clibincl/stream.hxx" // C library streams |
|
2
|
57 #include "clibincl/stdiofiles.hxx" // C library files |
|
0
|
58 #include "clibincl/stdiosupp.hxx" // support for stdio |
|
|
59 |
|
|
60 |
|
|
61 // EXPORTED SYMBOLS |
|
|
62 |
|
|
63 externC int |
|
2
|
64 fflush( FILE * ) CYGBLD_ATTRIB_WEAK_ALIAS(_fflush); |
|
0
|
65 |
|
|
66 |
|
|
67 // FUNCTIONS |
|
|
68 |
|
2
|
69 // flush all but one stream |
|
|
70 externC Cyg_ErrNo |
|
|
71 cyg_libc_stdio_flush_all_but( Cyg_StdioStream *not_this_stream ) |
|
|
72 { |
|
|
73 cyg_bool files_flushed[FOPEN_MAX] = { false }; // sets all to 0 |
|
|
74 cyg_bool loop_again; |
|
|
75 cyg_ucount32 i; |
|
|
76 Cyg_ErrNo err=ENOERR; |
|
|
77 Cyg_StdioStream *stream; |
|
|
78 |
|
|
79 do { |
|
|
80 loop_again = false; |
|
|
81 |
|
|
82 for (i=0; (i<FOPEN_MAX) && !err; i++) { |
|
|
83 if (files_flushed[i] == false) { |
|
|
84 |
|
|
85 stream = Cyg_libc_stdio_files::get_file_stream(i); |
|
|
86 |
|
|
87 if ((stream == NULL) || (stream == not_this_stream)) { |
|
|
88 // if it isn't a valid stream, set its entry in the |
|
|
89 // list of files flushed since we don't want to |
|
|
90 // flush it |
|
|
91 // Ditto if its the one we're meant to miss |
|
|
92 |
|
|
93 files_flushed[i] = true; |
|
|
94 } // if |
|
|
95 else { |
|
|
96 // valid stream |
|
|
97 |
|
|
98 if ( stream->trylock_me() ) { |
|
|
99 err = stream->flush_output_unlocked(); |
|
|
100 stream->unlock_me(); |
|
|
101 files_flushed[i] = true; |
|
|
102 } // if |
|
|
103 else |
|
|
104 loop_again = true; |
|
|
105 } // else |
|
|
106 } // if |
|
|
107 } // for |
|
|
108 } // do |
|
|
109 while(loop_again && !err); |
|
|
110 |
|
|
111 return err; |
|
|
112 } // cyg_libc_stdio_flush_all_but() |
|
0
|
113 |
|
|
114 externC int |
|
|
115 _fflush( FILE *stream ) |
|
|
116 { |
|
|
117 Cyg_StdioStream *real_stream = (Cyg_StdioStream *)stream; |
|
2
|
118 Cyg_ErrNo err; |
|
0
|
119 |
|
|
120 if (stream == NULL) { // tells us to flush ALL streams |
|
2
|
121 err = cyg_libc_stdio_flush_all_but(NULL); |
|
0
|
122 } // if |
|
|
123 else { |
|
|
124 err = real_stream->flush_output(); |
|
|
125 } // else |
|
|
126 |
|
|
127 if (err) { |
|
|
128 errno = err; |
|
|
129 return EOF; |
|
|
130 } // if |
|
|
131 |
|
|
132 return 0; |
|
|
133 |
|
|
134 } // _fflush() |
|
|
135 |
|
2
|
136 #endif // ifdef CYGPKG_LIBC_STDIO |
|
0
|
137 |
|
|
138 // EOF fflush.cxx |