Mercurial > ecos
annotate packages/language/c/libc/current/src/stdio/common/fopen.cxx @ 66:bf00f99aec69 ecos-sw-2000-02-02
Merge from eCos master repository on 2000-02-02-19:16:44-GMT
| author | jlarmour |
|---|---|
| date | Wed, 02 Feb 2000 19:57:02 +0000 |
| parents | c38311975d4f |
| children |
| 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#### | |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
34
diff
changeset
|
9 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
34
diff
changeset
|
10 // ------------------------------------------- |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
34
diff
changeset
|
11 // The contents of this file are subject to the Red Hat eCos Public License |
|
66
bf00f99aec69
Merge from eCos master repository on 2000-02-02-19:16:44-GMT
jlarmour
parents:
64
diff
changeset
|
12 // Version 1.1 (the "License"); you may not use this file except in |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
34
diff
changeset
|
13 // compliance with the License. You may obtain a copy of the License at |
|
66
bf00f99aec69
Merge from eCos master repository on 2000-02-02-19:16:44-GMT
jlarmour
parents:
64
diff
changeset
|
14 // http://www.redhat.com/ |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
34
diff
changeset
|
15 // |
|
66
bf00f99aec69
Merge from eCos master repository on 2000-02-02-19:16:44-GMT
jlarmour
parents:
64
diff
changeset
|
16 // Software distributed under the License is distributed on an "AS IS" |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
34
diff
changeset
|
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
34
diff
changeset
|
18 // License for the specific language governing rights and limitations under |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
34
diff
changeset
|
19 // the License. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
34
diff
changeset
|
20 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
34
diff
changeset
|
21 // The Original Code is eCos - Embedded Configurable Operating System, |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
34
diff
changeset
|
22 // released September 30, 1998. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
34
diff
changeset
|
23 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
34
diff
changeset
|
24 // The Initial Developer of the Original Code is Red Hat. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
34
diff
changeset
|
25 // Portions created by Red Hat are |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
34
diff
changeset
|
26 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
34
diff
changeset
|
27 // All Rights Reserved. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
34
diff
changeset
|
28 // ------------------------------------------- |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
34
diff
changeset
|
29 // |
| 0 | 30 //####COPYRIGHTEND#### |
| 31 //=========================================================================== | |
| 32 //#####DESCRIPTIONBEGIN#### | |
| 33 // | |
| 2 | 34 // Author(s): jlarmour |
| 35 // Contributors: jlarmour | |
| 36 // Date: 1999-01-21 | |
| 37 // Purpose: Implements ISO C fopen() function | |
| 0 | 38 // Description: |
| 39 // Usage: | |
| 40 // | |
| 41 //####DESCRIPTIONEND#### | |
| 42 // | |
| 43 //=========================================================================== | |
| 44 | |
| 45 // CONFIGURATION | |
| 46 | |
| 47 #include <pkgconf/libc.h> // Configuration header | |
| 48 | |
| 49 | |
| 2 | 50 // Do we want the stdio stuff? And fopen()? |
| 51 #if defined(CYGPKG_LIBC_STDIO) && defined(CYGPKG_LIBC_STDIO_OPEN) | |
| 0 | 52 |
| 53 // INCLUDES | |
| 54 | |
| 55 #include <cyg/infra/cyg_type.h> // Common project-wide type definitions | |
| 56 #include <stddef.h> // NULL and size_t from compiler | |
| 57 #include <errno.h> // Error codes | |
| 2 | 58 #include <cyg/io/io.h> // I/O system |
| 0 | 59 #include <stdio.h> // header for fopen() |
| 60 #include "clibincl/stdlibsupp.hxx" // _malloc() | |
| 61 #include "clibincl/stringsupp.hxx" // _strncmp() and _strcmp() | |
| 2 | 62 #include "clibincl/stdiofiles.hxx" // C library files |
| 0 | 63 #include "clibincl/stream.hxx" // C library streams |
| 64 | |
| 65 | |
| 66 // EXPORTED SYMBOLS | |
| 67 | |
| 68 externC FILE * | |
| 2 | 69 fopen( const char *, const char * ) CYGBLD_ATTRIB_WEAK_ALIAS(_fopen); |
| 0 | 70 |
| 71 | |
| 72 // FUNCTIONS | |
| 73 | |
| 74 // process the mode string. Return true on error | |
| 75 static cyg_bool | |
| 76 process_mode( const char *mode, Cyg_StdioStream::OpenMode *rw, | |
| 77 cyg_bool *binary, cyg_bool *append ) | |
| 78 { | |
| 79 *binary = *append = false; // default | |
| 80 | |
| 81 switch (mode[0]) { | |
| 82 case 'r': | |
| 83 *rw = Cyg_StdioStream::CYG_STREAM_READ; | |
| 84 break; | |
| 85 | |
| 86 case 'w': | |
| 87 case 'a': | |
| 88 *rw = Cyg_StdioStream::CYG_STREAM_WRITE; | |
| 89 *append = true; | |
| 90 break; | |
| 91 | |
| 92 default: | |
| 93 return true; | |
| 94 } // switch | |
| 95 | |
| 96 // ANSI says additional characters may follow the sequences, that we | |
| 97 // don't necessarily recognise so we just ignore them, and pretend that | |
| 98 // its the end of the string | |
| 99 | |
| 100 switch (mode[1]) { | |
| 101 case 'b': | |
| 102 *binary = true; | |
| 103 break; | |
| 104 case '+': | |
| 105 *rw = Cyg_StdioStream::CYG_STREAM_READWRITE; | |
| 106 break; | |
| 107 default: | |
| 108 return false; | |
| 109 } // switch | |
| 110 | |
| 111 switch (mode[2]) { | |
| 112 case 'b': | |
| 113 *binary = true; | |
| 114 break; | |
| 115 case '+': | |
| 116 *rw = Cyg_StdioStream::CYG_STREAM_READWRITE; | |
| 117 break; | |
| 118 default: | |
| 119 return false; | |
| 120 } // switch | |
| 121 | |
| 122 return false; | |
| 123 } // process_mode() | |
| 124 | |
| 125 | |
| 126 externC FILE * | |
| 127 _fopen( const char *filename, const char *mode ) | |
| 128 { | |
| 2 | 129 cyg_io_handle_t dev; |
| 0 | 130 Cyg_StdioStream *curr_stream; |
| 131 int i; | |
| 132 Cyg_ErrNo err; | |
| 133 Cyg_StdioStream::OpenMode open_mode; | |
| 134 cyg_bool binary, append; | |
| 135 | |
| 2 | 136 err = cyg_io_lookup( filename, &dev ); |
| 0 | 137 |
| 138 // if not found | |
| 2 | 139 if (err != ENOERR) { |
| 140 errno = ENOENT; | |
| 0 | 141 return NULL; |
| 142 } // if | |
| 143 | |
| 144 // process_mode returns true on error | |
| 145 if (process_mode( mode, &open_mode, &binary, &append )) { | |
| 2 | 146 errno = EINVAL; |
| 0 | 147 return NULL; |
| 148 } // if | |
| 149 | |
| 2 | 150 Cyg_libc_stdio_files::lock(); |
| 0 | 151 |
| 152 // find an empty slot | |
| 153 for (i=0; i < FOPEN_MAX; i++) { | |
| 2 | 154 curr_stream = Cyg_libc_stdio_files::get_file_stream(i); |
| 0 | 155 if (curr_stream == NULL) |
| 156 break; | |
| 157 } // for | |
| 158 | |
| 159 if (i == FOPEN_MAX) { // didn't find an empty slot | |
| 2 | 160 errno = EMFILE; |
| 0 | 161 return NULL; |
| 162 } // if | |
| 163 | |
| 164 // allocate it some memory and construct it. Currently we only have | |
| 165 // potentially interactive devices, so as per ANSI 7.9.3 it must not | |
| 166 // be buffered | |
| 167 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
|
168 _IONBF, 0 ); |
| 0 | 169 |
| 170 if (curr_stream == NULL) { | |
| 2 | 171 errno = ENOMEM; |
| 0 | 172 return NULL; |
| 173 } // if | |
| 174 | |
| 175 | |
| 176 // it puts any error in its own error flag | |
| 177 if (( err=curr_stream->get_error() )) { | |
| 178 | |
| 2 | 179 Cyg_libc_stdio_files::unlock(); |
| 0 | 180 |
| 181 _free( curr_stream ); | |
| 182 | |
| 2 | 183 errno = err; |
| 0 | 184 |
| 185 return NULL; | |
| 186 | |
| 187 } // if | |
| 188 | |
| 2 | 189 Cyg_libc_stdio_files::set_file_stream(i, curr_stream); |
| 0 | 190 |
| 2 | 191 Cyg_libc_stdio_files::unlock(); |
| 0 | 192 |
| 193 return (FILE *)(curr_stream); | |
| 194 | |
| 195 } // _fopen() | |
| 196 | |
| 2 | 197 #endif // defined(CYGPKG_LIBC_STDIO) && defined(CYGPKG_LIBC_STDIO_OPEN) |
| 0 | 198 |
| 199 // EOF fopen.cxx |
