OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [language/] [c/] [libc/] [stdio/] [v2_0/] [src/] [common/] [fopen.cxx] - Blame information for rev 174

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//===========================================================================
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
// Do we want fopen()?
60
#if defined(CYGPKG_LIBC_STDIO_OPEN)
61
 
62
// INCLUDES
63
 
64
#include <cyg/infra/cyg_type.h>     // Common project-wide type definitions
65
#include <stddef.h>                 // NULL and size_t from compiler
66
#include <errno.h>                  // Error codes
67
#include <stdio.h>                  // header for fopen()
68
#include <stdlib.h>                 // malloc()
69
#include <string.h>                 // strncmp() and strcmp()
70
#include <cyg/libc/stdio/stdiofiles.hxx> // C library files
71
#include <cyg/libc/stdio/stream.hxx>     // C library streams
72
 
73
#include <cyg/libc/stdio/io.inl>     // I/O system inlines
74
 
75
// FUNCTIONS
76
 
77
// placement new
78
inline void *operator new(size_t size, void *ptr)
79
{
80
    CYG_CHECK_DATA_PTR( ptr, "Bad pointer" );
81
    return ptr;
82
}
83
 
84
// process the mode string. Return true on error
85
static cyg_bool
86
process_mode( const char *mode, Cyg_StdioStream::OpenMode *rw,
87
              cyg_bool *binary, cyg_bool *append )
88
{
89
    *binary = *append = false; // default
90
 
91
    switch (mode[0]) {
92
    case 'r':
93
        *rw = Cyg_StdioStream::CYG_STREAM_READ;
94
        break;
95
 
96
    case 'a':
97
        *append = true;
98
    case 'w':
99
        *rw = Cyg_StdioStream::CYG_STREAM_WRITE;
100
        break;
101
 
102
    default:
103
        return true;
104
    } // switch
105
 
106
    // ANSI says additional characters may follow the sequences, that we
107
    // don't necessarily recognise so we just ignore them, and pretend that
108
    // its the end of the string
109
 
110
    switch (mode[1]) {
111
    case 'b':
112
        *binary = true;
113
        break;
114
    case '+':
115
        *rw = Cyg_StdioStream::CYG_STREAM_READWRITE;
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
        *rw = Cyg_StdioStream::CYG_STREAM_READWRITE;
127
        break;
128
    default:
129
        return false;
130
    } // switch
131
 
132
    return false;
133
} // process_mode()
134
 
135
 
136
static FILE *fopen_inner( cyg_stdio_handle_t dev,
137
                          Cyg_StdioStream::OpenMode open_mode,
138
                          cyg_bool binary,
139
                          cyg_bool append)
140
{
141
    Cyg_StdioStream *curr_stream;
142
    int i;
143
    Cyg_ErrNo err;
144
    int bufmode = _IOFBF;
145
    cyg_ucount32 bufsize = BUFSIZ;
146
 
147
    Cyg_libc_stdio_files::lock();
148
 
149
    // find an empty slot
150
    for (i=0; i < FOPEN_MAX; i++) {
151
        curr_stream = Cyg_libc_stdio_files::get_file_stream(i);
152
        if (curr_stream == NULL)
153
            break;
154
    } // for
155
 
156
    if (i == FOPEN_MAX) { // didn't find an empty slot
157
        errno = EMFILE;
158
        cyg_stdio_close( dev );
159
        return NULL;
160
    } // if
161
 
162
    // Decide the buffering mode. The default is fully buffered, but if
163
    // this is an interactive stream then set it to non buffered. 
164
    if( (dev != CYG_STDIO_HANDLE_NULL) &&
165
        cyg_stdio_interactive( dev ) )
166
        bufmode = _IONBF, bufsize = 0;
167
 
168
    // Allocate it some memory and construct it.
169
    curr_stream = (Cyg_StdioStream *)malloc(sizeof(*curr_stream));
170
    if (curr_stream == NULL) {
171
        cyg_stdio_close( dev );
172
        errno = ENOMEM;
173
        return NULL;
174
    } // if
175
 
176
    curr_stream = new ((void *)curr_stream) Cyg_StdioStream( dev, open_mode,
177
                                                             append, binary,
178
                                                             bufmode, bufsize );
179
    // it puts any error in its own error flag
180
    if (( err=curr_stream->get_error() )) {
181
 
182
        Cyg_libc_stdio_files::unlock();
183
 
184
        free( curr_stream );
185
 
186
        cyg_stdio_close( dev );
187
 
188
        errno = err;
189
 
190
        return NULL;
191
 
192
    } // if
193
 
194
    Cyg_libc_stdio_files::set_file_stream(i, curr_stream);
195
 
196
    Cyg_libc_stdio_files::unlock();
197
 
198
    return (FILE *)(curr_stream);
199
 
200
} // fopen_inner()
201
 
202
externC FILE *
203
fopen( const char *filename, const char *mode )
204
{
205
    cyg_stdio_handle_t dev;
206
    Cyg_ErrNo err;
207
    Cyg_StdioStream::OpenMode open_mode;
208
    cyg_bool binary, append;
209
 
210
    // process_mode returns true on error
211
    if (process_mode( mode, &open_mode, &binary, &append )) {
212
        errno = EINVAL;
213
        return NULL;
214
    } // if
215
 
216
    err = cyg_stdio_open( filename, open_mode, binary, append, &dev );
217
 
218
    // if not found
219
    if (err != ENOERR) {
220
        errno = ENOENT;
221
        return NULL;
222
    } // if
223
 
224
    return fopen_inner( dev, open_mode, binary, append );
225
 
226
} // fopen()
227
 
228
 
229
#endif // defined(CYGPKG_LIBC_STDIO_OPEN)
230
 
231
#ifdef CYGPKG_LIBC_STDIO_FILEIO
232
 
233
externC int fileno( FILE *stream )
234
{
235
    Cyg_StdioStream *real_stream = (Cyg_StdioStream *)stream;
236
 
237
    return real_stream->get_dev();
238
}
239
 
240
externC FILE *fdopen( int fd, const char *mode )
241
{
242
    Cyg_StdioStream::OpenMode open_mode;
243
    cyg_bool binary, append;
244
    FILE *f;
245
 
246
    // process_mode returns true on error
247
    if (process_mode( mode, &open_mode, &binary, &append )) {
248
        errno = EINVAL;
249
        return NULL;
250
    } // if
251
 
252
    f = fopen_inner( (cyg_stdio_handle_t)fd, open_mode, binary, append );
253
 
254
    if( f == NULL )
255
        return f;
256
 
257
    // Do a null seek to initialize the file position.
258
    Cyg_StdioStream *real_stream = (Cyg_StdioStream *)f;
259
    fpos_t pos = 0;
260
    real_stream->set_position( pos, SEEK_CUR );
261
    return f;
262
}
263
 
264
#endif // def CYGPKG_LIBC_STDIO_FILEIO
265
 
266
 
267
// EOF fopen.cxx

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.