Mercurial > ecos
annotate packages/language/c/libc/current/src/stdio/common/fopen.cxx @ 34:29bc183297e1 ecos-sw-1999-09-02
Merge from eCos master repository on 1999-09-02-16:26:10-BST
| author | jlarmour |
|---|---|
| date | Thu, 02 Sep 1999 16:11:22 +0000 |
| parents | 443894e2e912 |
| children | c38311975d4f |
| rev | line source |
|---|---|
| 0 | 1 //=========================================================================== |
| 2 // | |
| 3 // fopen.cxx | |
| 4 // | |
| 5 // Implementation of C library file open function as per ANSI 7.9.5.3 | |
| 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: Implements ISO C fopen() function | |
| 0 | 36 // Description: |
| 37 // Usage: | |
| 38 // | |
| 39 //####DESCRIPTIONEND#### | |
| 40 // | |
| 41 //=========================================================================== | |
| 42 | |
| 43 // CONFIGURATION | |
| 44 | |
| 45 #include <pkgconf/libc.h> // Configuration header | |
| 46 | |
| 47 | |
| 2 | 48 // Do we want the stdio stuff? And fopen()? |
| 49 #if defined(CYGPKG_LIBC_STDIO) && defined(CYGPKG_LIBC_STDIO_OPEN) | |
| 0 | 50 |
| 51 // INCLUDES | |
| 52 | |
| 53 #include <cyg/infra/cyg_type.h> // Common project-wide type definitions | |
| 54 #include <stddef.h> // NULL and size_t from compiler | |
| 55 #include <errno.h> // Error codes | |
| 2 | 56 #include <cyg/io/io.h> // I/O system |
| 0 | 57 #include <stdio.h> // header for fopen() |
| 58 #include "clibincl/stdlibsupp.hxx" // _malloc() | |
| 59 #include "clibincl/stringsupp.hxx" // _strncmp() and _strcmp() | |
| 2 | 60 #include "clibincl/stdiofiles.hxx" // C library files |
| 0 | 61 #include "clibincl/stream.hxx" // C library streams |
| 62 | |
| 63 | |
| 64 // EXPORTED SYMBOLS | |
| 65 | |
| 66 externC FILE * | |
| 2 | 67 fopen( const char *, const char * ) CYGBLD_ATTRIB_WEAK_ALIAS(_fopen); |
| 0 | 68 |
| 69 | |
| 70 // FUNCTIONS | |
| 71 | |
| 72 // process the mode string. Return true on error | |
| 73 static cyg_bool | |
| 74 process_mode( const char *mode, Cyg_StdioStream::OpenMode *rw, | |
| 75 cyg_bool *binary, cyg_bool *append ) | |
| 76 { | |
| 77 *binary = *append = false; // default | |
| 78 | |
| 79 switch (mode[0]) { | |
| 80 case 'r': | |
| 81 *rw = Cyg_StdioStream::CYG_STREAM_READ; | |
| 82 break; | |
| 83 | |
| 84 case 'w': | |
| 85 case 'a': | |
| 86 *rw = Cyg_StdioStream::CYG_STREAM_WRITE; | |
| 87 *append = true; | |
| 88 break; | |
| 89 | |
| 90 default: | |
| 91 return true; | |
| 92 } // switch | |
| 93 | |
| 94 // ANSI says additional characters may follow the sequences, that we | |
| 95 // don't necessarily recognise so we just ignore them, and pretend that | |
| 96 // its the end of the string | |
| 97 | |
| 98 switch (mode[1]) { | |
| 99 case 'b': | |
| 100 *binary = true; | |
| 101 break; | |
| 102 case '+': | |
| 103 *rw = Cyg_StdioStream::CYG_STREAM_READWRITE; | |
| 104 break; | |
| 105 default: | |
| 106 return false; | |
| 107 } // switch | |
| 108 | |
| 109 switch (mode[2]) { | |
| 110 case 'b': | |
| 111 *binary = true; | |
| 112 break; | |
| 113 case '+': | |
| 114 *rw = Cyg_StdioStream::CYG_STREAM_READWRITE; | |
| 115 break; | |
| 116 default: | |
| 117 return false; | |
| 118 } // switch | |
| 119 | |
| 120 return false; | |
| 121 } // process_mode() | |
| 122 | |
| 123 | |
| 124 externC FILE * | |
| 125 _fopen( const char *filename, const char *mode ) | |
| 126 { | |
| 2 | 127 cyg_io_handle_t dev; |
| 0 | 128 Cyg_StdioStream *curr_stream; |
| 129 int i; | |
| 130 Cyg_ErrNo err; | |
| 131 Cyg_StdioStream::OpenMode open_mode; | |
| 132 cyg_bool binary, append; | |
| 133 | |
| 2 | 134 err = cyg_io_lookup( filename, &dev ); |
| 0 | 135 |
| 136 // if not found | |
| 2 | 137 if (err != ENOERR) { |
| 138 errno = ENOENT; | |
| 0 | 139 return NULL; |
| 140 } // if | |
| 141 | |
| 142 // process_mode returns true on error | |
| 143 if (process_mode( mode, &open_mode, &binary, &append )) { | |
| 2 | 144 errno = EINVAL; |
| 0 | 145 return NULL; |
| 146 } // if | |
| 147 | |
| 2 | 148 Cyg_libc_stdio_files::lock(); |
| 0 | 149 |
| 150 // find an empty slot | |
| 151 for (i=0; i < FOPEN_MAX; i++) { | |
| 2 | 152 curr_stream = Cyg_libc_stdio_files::get_file_stream(i); |
| 0 | 153 if (curr_stream == NULL) |
| 154 break; | |
| 155 } // for | |
| 156 | |
| 157 if (i == FOPEN_MAX) { // didn't find an empty slot | |
| 2 | 158 errno = EMFILE; |
| 0 | 159 return NULL; |
| 160 } // if | |
| 161 | |
| 162 // allocate it some memory and construct it. Currently we only have | |
| 163 // potentially interactive devices, so as per ANSI 7.9.3 it must not | |
| 164 // be buffered | |
| 165 curr_stream = new Cyg_StdioStream( dev, open_mode, append, binary, | |
|
34
29bc183297e1
Merge from eCos master repository on 1999-09-02-16:26:10-BST
jlarmour
parents:
2
diff
changeset
|
166 _IONBF, 0 ); |
| 0 | 167 |
| 168 if (curr_stream == NULL) { | |
| 2 | 169 errno = ENOMEM; |
| 0 | 170 return NULL; |
| 171 } // if | |
| 172 | |
| 173 | |
| 174 // it puts any error in its own error flag | |
| 175 if (( err=curr_stream->get_error() )) { | |
| 176 | |
| 2 | 177 Cyg_libc_stdio_files::unlock(); |
| 0 | 178 |
| 179 _free( curr_stream ); | |
| 180 | |
| 2 | 181 errno = err; |
| 0 | 182 |
| 183 return NULL; | |
| 184 | |
| 185 } // if | |
| 186 | |
| 2 | 187 Cyg_libc_stdio_files::set_file_stream(i, curr_stream); |
| 0 | 188 |
| 2 | 189 Cyg_libc_stdio_files::unlock(); |
| 0 | 190 |
| 191 return (FILE *)(curr_stream); | |
| 192 | |
| 193 } // _fopen() | |
| 194 | |
| 2 | 195 #endif // defined(CYGPKG_LIBC_STDIO) && defined(CYGPKG_LIBC_STDIO_OPEN) |
| 0 | 196 |
| 197 // EOF fopen.cxx |
