]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/language/c/libc/stdio/v2_0/src/common/fopen.cxx
Initial revision
[karo-tx-redboot.git] / packages / language / c / libc / stdio / v2_0 / src / common / fopen.cxx
1 //===========================================================================
2 //
3 //      fopen.cxx
4 //
5 //      Implementation of C library file open function as per ANSI 7.9.5.3
6 //
7 //===========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
12 //
13 // eCos is free software; you can redistribute it and/or modify it under
14 // the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 2 or (at your option) any later version.
16 //
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20 // for more details.
21 //
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25 //
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
32 //
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
35 //
36 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37 // at http://sources.redhat.com/ecos/ecos-license/
38 // -------------------------------------------
39 //####ECOSGPLCOPYRIGHTEND####
40 //===========================================================================
41 //#####DESCRIPTIONBEGIN####
42 //
43 // Author(s):     jlarmour
44 // Contributors:  
45 // Date:          2000-04-20
46 // Purpose:       Implements ISO C fopen() function
47 // Description: 
48 // Usage:       
49 //
50 //####DESCRIPTIONEND####
51 //
52 //===========================================================================
53
54 // CONFIGURATION
55
56 #include <pkgconf/libc_stdio.h>   // Configuration header
57
58
59 // INCLUDES
60
61 #include <cyg/infra/cyg_type.h>     // Common project-wide type definitions
62 #include <stddef.h>                 // NULL and size_t from compiler
63 #include <errno.h>                  // Error codes
64 #include <stdio.h>                  // header for fopen()
65 #include <stdlib.h>                 // malloc()
66 #include <string.h>                 // strncmp() and strcmp()
67 #include <cyg/libc/stdio/stdiofiles.hxx> // C library files
68 #include <cyg/libc/stdio/stream.hxx>     // C library streams
69
70 #include <cyg/libc/stdio/io.inl>     // I/O system inlines
71
72 // FUNCTIONS
73
74 // placement new
75 inline void *operator new(size_t size, void *ptr)
76 {
77     CYG_CHECK_DATA_PTR( ptr, "Bad pointer" );
78     return ptr;
79 }
80
81 // process the mode string. Return true on error
82 static cyg_bool
83 process_mode( const char *mode, Cyg_StdioStream::OpenMode *rw,
84               cyg_bool *binary, cyg_bool *append )
85 {
86     *binary = *append = false; // default
87
88     switch (mode[0]) {
89     case 'r':
90         *rw = Cyg_StdioStream::CYG_STREAM_READ;
91         break;
92
93     case 'a':
94         *append = true;
95     case 'w':
96         *rw = Cyg_StdioStream::CYG_STREAM_WRITE;
97         break;
98         
99     default:
100         return true;
101     } // switch
102
103     // ANSI says additional characters may follow the sequences, that we
104     // don't necessarily recognise so we just ignore them, and pretend that
105     // its the end of the string
106
107     switch (mode[1]) {
108     case 'b':
109         *binary = true;
110         break;
111     case '+':
112         if (mode[0] == 'r')
113             *rw = Cyg_StdioStream::CYG_STREAM_READWRITE_NOCREATE;
114         else
115             *rw = Cyg_StdioStream::CYG_STREAM_READWRITE_CREATE;
116         break;
117     default:
118         return false;
119     } // switch
120
121     switch (mode[2]) {
122     case 'b':
123         *binary = true;
124         break;
125     case '+':
126         if (mode[0] == 'r')
127             *rw = Cyg_StdioStream::CYG_STREAM_READWRITE_NOCREATE;
128         else
129             *rw = Cyg_StdioStream::CYG_STREAM_READWRITE_CREATE;
130         break;
131     default:
132         return false;
133     } // switch
134     
135     return false;
136 } // process_mode()
137
138
139 static FILE *fopen_inner( cyg_stdio_handle_t dev,
140                           Cyg_StdioStream::OpenMode open_mode,
141                           cyg_bool binary,
142                           cyg_bool append)
143 {
144     Cyg_StdioStream *curr_stream;
145     int i;
146     Cyg_ErrNo err;
147     int bufmode = _IOFBF;
148     cyg_ucount32 bufsize = BUFSIZ;
149     
150     Cyg_libc_stdio_files::lock();
151
152     // find an empty slot
153     for (i=0; i < FOPEN_MAX; i++) {
154         curr_stream = Cyg_libc_stdio_files::get_file_stream(i);
155         if (curr_stream == NULL)
156             break;
157     } // for
158
159     if (i == FOPEN_MAX) { // didn't find an empty slot
160         errno = EMFILE;
161         cyg_stdio_close( dev );
162         Cyg_libc_stdio_files::unlock();
163         return NULL;
164     } // if
165
166     // Decide the buffering mode. The default is fully buffered, but if
167     // this is an interactive stream then set it to non buffered. 
168     if( (dev != CYG_STDIO_HANDLE_NULL) &&
169         cyg_stdio_interactive( dev ) )
170         bufmode = _IONBF, bufsize = 0;
171     
172     // Allocate it some memory and construct it.
173     curr_stream = (Cyg_StdioStream *)malloc(sizeof(*curr_stream));
174     if (curr_stream == NULL) {
175         cyg_stdio_close( dev );
176         Cyg_libc_stdio_files::unlock();
177         errno = ENOMEM;
178         return NULL;
179     } // if
180
181     curr_stream = new ((void *)curr_stream) Cyg_StdioStream( dev, open_mode,
182                                                              append, binary,
183                                                              bufmode, bufsize );
184     // it puts any error in its own error flag
185     if (( err=curr_stream->get_error() )) {
186
187         Cyg_libc_stdio_files::unlock();
188         
189         free( curr_stream );
190
191         cyg_stdio_close( dev );
192         
193         errno = err;
194
195         return NULL;
196
197     } // if
198
199     Cyg_libc_stdio_files::set_file_stream(i, curr_stream);
200         
201     Cyg_libc_stdio_files::unlock();
202
203     return (FILE *)(curr_stream);
204
205 } // fopen_inner()
206
207 externC FILE *
208 fopen( const char *filename, const char *mode ) __THROW
209 {
210     cyg_stdio_handle_t dev = 0;
211     Cyg_ErrNo err;
212     Cyg_StdioStream::OpenMode open_mode = Cyg_StdioStream::CYG_STREAM_READ;
213     cyg_bool binary, append;
214     
215     // process_mode returns true on error
216     if (process_mode( mode, &open_mode, &binary, &append )) {
217         errno = EINVAL;
218         return NULL;
219     } // if
220
221     err = cyg_stdio_open( filename, open_mode, binary, append, &dev );
222
223     // if not found
224     if (err != ENOERR) {
225         errno = ENOENT;
226         return NULL;
227     } // if
228
229     return fopen_inner( dev, open_mode, binary, append );
230     
231 } // fopen()
232
233
234 #ifdef CYGFUN_LIBC_STDIO_OPEN_POSIX_FDFUNCS
235
236 externC int fileno( FILE *stream ) __THROW
237 {
238     Cyg_StdioStream *real_stream = (Cyg_StdioStream *)stream;
239
240     return real_stream->get_dev();
241 }
242
243 externC FILE *fdopen( int fd, const char *mode ) __THROW
244 {
245     Cyg_StdioStream::OpenMode open_mode;
246     cyg_bool binary, append;
247     FILE *f;
248     
249     // process_mode returns true on error
250     if (process_mode( mode, &open_mode, &binary, &append )) {
251         errno = EINVAL;
252         return NULL;
253     } // if
254
255     f = fopen_inner( (cyg_stdio_handle_t)fd, open_mode, binary, append );
256
257     if( f == NULL )
258         return f;
259
260     // Do a null seek to initialize the file position.
261     Cyg_StdioStream *real_stream = (Cyg_StdioStream *)f;
262     fpos_t pos = 0;
263     real_stream->set_position( pos, SEEK_CUR );
264     return f;
265 }
266
267 #endif // def CYGFUN_LIBC_STDIO_OPEN_POSIX_FDFUNCS
268
269
270 // EOF fopen.cxx