fix for mbed lib issue 3 (i2c problem) see also https://mbed.org/users/mbed_official/code/mbed/issues/3 affected implementations: LPC812, LPC11U24, LPC1768, LPC2368, LPC4088
Fork of mbed-src by
cpp/stdio.cpp@5:ab1c572cb536, 2013-03-15 (annotated)
- Committer:
- emilmont
- Date:
- Fri Mar 15 17:00:57 2013 +0000
- Revision:
- 5:ab1c572cb536
- Parent:
- 2:143cac498751
- Child:
- 7:3a1b3e92fa02
Remove redundant stdio serial initialization.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:fd0d7bdfcdc2 | 1 | /* mbed Microcontroller Library |
emilmont | 2:143cac498751 | 2 | * Copyright (c) 2006-2013 ARM Limited |
mbed_official | 0:fd0d7bdfcdc2 | 3 | * |
emilmont | 2:143cac498751 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
emilmont | 2:143cac498751 | 5 | * you may not use this file except in compliance with the License. |
emilmont | 2:143cac498751 | 6 | * You may obtain a copy of the License at |
mbed_official | 0:fd0d7bdfcdc2 | 7 | * |
emilmont | 2:143cac498751 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 0:fd0d7bdfcdc2 | 9 | * |
emilmont | 2:143cac498751 | 10 | * Unless required by applicable law or agreed to in writing, software |
emilmont | 2:143cac498751 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
emilmont | 2:143cac498751 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
emilmont | 2:143cac498751 | 13 | * See the License for the specific language governing permissions and |
emilmont | 2:143cac498751 | 14 | * limitations under the License. |
mbed_official | 0:fd0d7bdfcdc2 | 15 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 16 | #include "platform.h" |
mbed_official | 0:fd0d7bdfcdc2 | 17 | #include "FileHandle.h" |
mbed_official | 0:fd0d7bdfcdc2 | 18 | #include "FileSystemLike.h" |
emilmont | 2:143cac498751 | 19 | #include "FilePath.h" |
mbed_official | 0:fd0d7bdfcdc2 | 20 | #include "serial_api.h" |
mbed_official | 0:fd0d7bdfcdc2 | 21 | #include <errno.h> |
mbed_official | 0:fd0d7bdfcdc2 | 22 | |
emilmont | 2:143cac498751 | 23 | #if defined(__ARMCC_VERSION) |
mbed_official | 0:fd0d7bdfcdc2 | 24 | # include <rt_sys.h> |
mbed_official | 0:fd0d7bdfcdc2 | 25 | # define PREFIX(x) _sys##x |
mbed_official | 0:fd0d7bdfcdc2 | 26 | # define OPEN_MAX _SYS_OPEN |
mbed_official | 0:fd0d7bdfcdc2 | 27 | # ifdef __MICROLIB |
mbed_official | 0:fd0d7bdfcdc2 | 28 | # pragma import(__use_full_stdio) |
mbed_official | 0:fd0d7bdfcdc2 | 29 | # endif |
mbed_official | 0:fd0d7bdfcdc2 | 30 | |
emilmont | 2:143cac498751 | 31 | #elif defined(__ICCARM__) |
emilmont | 2:143cac498751 | 32 | # include <yfuns.h> |
emilmont | 2:143cac498751 | 33 | # define PREFIX(x) _##x |
emilmont | 2:143cac498751 | 34 | # define OPEN_MAX 16 |
emilmont | 2:143cac498751 | 35 | |
emilmont | 2:143cac498751 | 36 | # define STDIN_FILENO 0 |
emilmont | 2:143cac498751 | 37 | # define STDOUT_FILENO 1 |
emilmont | 2:143cac498751 | 38 | # define STDERR_FILENO 2 |
emilmont | 2:143cac498751 | 39 | |
mbed_official | 0:fd0d7bdfcdc2 | 40 | #else |
mbed_official | 0:fd0d7bdfcdc2 | 41 | # include <sys/stat.h> |
mbed_official | 0:fd0d7bdfcdc2 | 42 | # include <sys/unistd.h> |
mbed_official | 0:fd0d7bdfcdc2 | 43 | # include <sys/syslimits.h> |
mbed_official | 0:fd0d7bdfcdc2 | 44 | # define PREFIX(x) x |
mbed_official | 0:fd0d7bdfcdc2 | 45 | #endif |
mbed_official | 0:fd0d7bdfcdc2 | 46 | |
mbed_official | 0:fd0d7bdfcdc2 | 47 | using namespace mbed; |
mbed_official | 0:fd0d7bdfcdc2 | 48 | |
mbed_official | 0:fd0d7bdfcdc2 | 49 | extern const char __stdin_name[] = "/stdin"; |
mbed_official | 0:fd0d7bdfcdc2 | 50 | extern const char __stdout_name[] = "/stdout"; |
mbed_official | 0:fd0d7bdfcdc2 | 51 | extern const char __stderr_name[] = "/stderr"; |
mbed_official | 0:fd0d7bdfcdc2 | 52 | |
mbed_official | 0:fd0d7bdfcdc2 | 53 | /* newlib has the filehandle field in the FILE struct as a short, so |
mbed_official | 0:fd0d7bdfcdc2 | 54 | * we can't just return a Filehandle* from _open and instead have to |
mbed_official | 0:fd0d7bdfcdc2 | 55 | * put it in a filehandles array and return the index into that array |
mbed_official | 0:fd0d7bdfcdc2 | 56 | * (or rather index+3, as filehandles 0-2 are stdin/out/err). |
mbed_official | 0:fd0d7bdfcdc2 | 57 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 58 | static FileHandle *filehandles[OPEN_MAX]; |
mbed_official | 0:fd0d7bdfcdc2 | 59 | |
mbed_official | 0:fd0d7bdfcdc2 | 60 | FileHandle::~FileHandle() { |
mbed_official | 0:fd0d7bdfcdc2 | 61 | /* Remove all open filehandles for this */ |
mbed_official | 0:fd0d7bdfcdc2 | 62 | for (unsigned int fh_i = 0; fh_i < sizeof(filehandles)/sizeof(*filehandles); fh_i++) { |
mbed_official | 0:fd0d7bdfcdc2 | 63 | if (filehandles[fh_i] == this) { |
mbed_official | 0:fd0d7bdfcdc2 | 64 | filehandles[fh_i] = NULL; |
mbed_official | 0:fd0d7bdfcdc2 | 65 | } |
mbed_official | 0:fd0d7bdfcdc2 | 66 | } |
mbed_official | 0:fd0d7bdfcdc2 | 67 | } |
mbed_official | 0:fd0d7bdfcdc2 | 68 | |
mbed_official | 0:fd0d7bdfcdc2 | 69 | #if DEVICE_SERIAL |
mbed_official | 0:fd0d7bdfcdc2 | 70 | extern int stdio_uart_inited; |
mbed_official | 0:fd0d7bdfcdc2 | 71 | extern serial_t stdio_uart; |
mbed_official | 0:fd0d7bdfcdc2 | 72 | #endif |
mbed_official | 0:fd0d7bdfcdc2 | 73 | |
mbed_official | 0:fd0d7bdfcdc2 | 74 | static void init_serial() { |
mbed_official | 0:fd0d7bdfcdc2 | 75 | #if DEVICE_SERIAL |
mbed_official | 0:fd0d7bdfcdc2 | 76 | if (stdio_uart_inited) return; |
mbed_official | 0:fd0d7bdfcdc2 | 77 | serial_init(&stdio_uart, STDIO_UART_TX, STDIO_UART_RX); |
mbed_official | 0:fd0d7bdfcdc2 | 78 | #endif |
mbed_official | 0:fd0d7bdfcdc2 | 79 | } |
mbed_official | 0:fd0d7bdfcdc2 | 80 | |
emilmont | 2:143cac498751 | 81 | static inline int openmode_to_posix(int openmode) { |
emilmont | 2:143cac498751 | 82 | int posix = openmode; |
mbed_official | 0:fd0d7bdfcdc2 | 83 | #ifdef __ARMCC_VERSION |
emilmont | 2:143cac498751 | 84 | if (openmode & OPEN_PLUS) { |
emilmont | 2:143cac498751 | 85 | posix = O_RDWR; |
emilmont | 2:143cac498751 | 86 | } else if(openmode & OPEN_W) { |
emilmont | 2:143cac498751 | 87 | posix = O_WRONLY; |
emilmont | 2:143cac498751 | 88 | } else if(openmode & OPEN_A) { |
emilmont | 2:143cac498751 | 89 | posix = O_WRONLY|O_APPEND; |
mbed_official | 0:fd0d7bdfcdc2 | 90 | } else { |
emilmont | 2:143cac498751 | 91 | posix = O_RDONLY; |
mbed_official | 0:fd0d7bdfcdc2 | 92 | } |
mbed_official | 0:fd0d7bdfcdc2 | 93 | /* a, w, a+, w+ all create if file does not already exist */ |
emilmont | 2:143cac498751 | 94 | if (openmode & (OPEN_A|OPEN_W)) { |
emilmont | 2:143cac498751 | 95 | posix |= O_CREAT; |
mbed_official | 0:fd0d7bdfcdc2 | 96 | } |
mbed_official | 0:fd0d7bdfcdc2 | 97 | /* w and w+ truncate */ |
emilmont | 2:143cac498751 | 98 | if (openmode & OPEN_W) { |
emilmont | 2:143cac498751 | 99 | posix |= O_TRUNC; |
mbed_official | 0:fd0d7bdfcdc2 | 100 | } |
emilmont | 2:143cac498751 | 101 | #elif defined(__ICCARM__) |
emilmont | 2:143cac498751 | 102 | switch (openmode & _LLIO_RDWRMASK) { |
emilmont | 2:143cac498751 | 103 | case _LLIO_RDONLY: posix = O_RDONLY; break; |
emilmont | 2:143cac498751 | 104 | case _LLIO_WRONLY: posix = O_WRONLY; break; |
emilmont | 2:143cac498751 | 105 | case _LLIO_RDWR : posix = O_RDWR ; break; |
emilmont | 2:143cac498751 | 106 | } |
emilmont | 2:143cac498751 | 107 | if (openmode & _LLIO_CREAT ) posix |= O_CREAT; |
emilmont | 2:143cac498751 | 108 | if (openmode & _LLIO_APPEND) posix |= O_APPEND; |
emilmont | 2:143cac498751 | 109 | if (openmode & _LLIO_TRUNC ) posix |= O_TRUNC; |
emilmont | 2:143cac498751 | 110 | #endif |
emilmont | 2:143cac498751 | 111 | return posix; |
mbed_official | 0:fd0d7bdfcdc2 | 112 | } |
mbed_official | 0:fd0d7bdfcdc2 | 113 | |
mbed_official | 0:fd0d7bdfcdc2 | 114 | extern "C" FILEHANDLE PREFIX(_open)(const char* name, int openmode) { |
mbed_official | 0:fd0d7bdfcdc2 | 115 | /* Use the posix convention that stdin,out,err are filehandles 0,1,2. |
mbed_official | 0:fd0d7bdfcdc2 | 116 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 117 | if (std::strcmp(name, __stdin_name) == 0) { |
mbed_official | 0:fd0d7bdfcdc2 | 118 | init_serial(); |
mbed_official | 0:fd0d7bdfcdc2 | 119 | return 0; |
mbed_official | 0:fd0d7bdfcdc2 | 120 | } else if (std::strcmp(name, __stdout_name) == 0) { |
mbed_official | 0:fd0d7bdfcdc2 | 121 | init_serial(); |
mbed_official | 0:fd0d7bdfcdc2 | 122 | return 1; |
mbed_official | 0:fd0d7bdfcdc2 | 123 | } else if (std::strcmp(name,__stderr_name) == 0) { |
mbed_official | 0:fd0d7bdfcdc2 | 124 | init_serial(); |
mbed_official | 0:fd0d7bdfcdc2 | 125 | return 2; |
mbed_official | 0:fd0d7bdfcdc2 | 126 | } |
emilmont | 2:143cac498751 | 127 | |
mbed_official | 0:fd0d7bdfcdc2 | 128 | // find the first empty slot in filehandles |
mbed_official | 0:fd0d7bdfcdc2 | 129 | unsigned int fh_i; |
mbed_official | 0:fd0d7bdfcdc2 | 130 | for (fh_i = 0; fh_i < sizeof(filehandles)/sizeof(*filehandles); fh_i++) { |
mbed_official | 0:fd0d7bdfcdc2 | 131 | if (filehandles[fh_i] == NULL) break; |
mbed_official | 0:fd0d7bdfcdc2 | 132 | } |
mbed_official | 0:fd0d7bdfcdc2 | 133 | if (fh_i >= sizeof(filehandles)/sizeof(*filehandles)) { |
mbed_official | 0:fd0d7bdfcdc2 | 134 | return -1; |
mbed_official | 0:fd0d7bdfcdc2 | 135 | } |
emilmont | 2:143cac498751 | 136 | |
mbed_official | 0:fd0d7bdfcdc2 | 137 | FileHandle *res; |
emilmont | 2:143cac498751 | 138 | |
mbed_official | 0:fd0d7bdfcdc2 | 139 | /* FILENAME: ":0x12345678" describes a FileLike* */ |
mbed_official | 0:fd0d7bdfcdc2 | 140 | if (name[0] == ':') { |
mbed_official | 0:fd0d7bdfcdc2 | 141 | void *p; |
mbed_official | 0:fd0d7bdfcdc2 | 142 | sscanf(name, ":%p", &p); |
mbed_official | 0:fd0d7bdfcdc2 | 143 | res = (FileHandle*)p; |
emilmont | 2:143cac498751 | 144 | |
mbed_official | 0:fd0d7bdfcdc2 | 145 | /* FILENAME: "/file_system/file_name" */ |
mbed_official | 0:fd0d7bdfcdc2 | 146 | } else { |
mbed_official | 0:fd0d7bdfcdc2 | 147 | FilePath path(name); |
emilmont | 2:143cac498751 | 148 | |
emilmont | 2:143cac498751 | 149 | if (path.isFile()) { |
emilmont | 2:143cac498751 | 150 | res = path.file(); |
emilmont | 2:143cac498751 | 151 | } else { |
emilmont | 2:143cac498751 | 152 | FileSystemLike *fs = path.fileSystem(); |
emilmont | 2:143cac498751 | 153 | if (fs == NULL) return -1; |
emilmont | 2:143cac498751 | 154 | int posix_mode = openmode_to_posix(openmode); |
emilmont | 2:143cac498751 | 155 | res = fs->open(path.fileName(), posix_mode); /* NULL if fails */ |
emilmont | 2:143cac498751 | 156 | } |
mbed_official | 0:fd0d7bdfcdc2 | 157 | } |
emilmont | 2:143cac498751 | 158 | |
mbed_official | 0:fd0d7bdfcdc2 | 159 | if (res == NULL) return -1; |
mbed_official | 0:fd0d7bdfcdc2 | 160 | filehandles[fh_i] = res; |
emilmont | 2:143cac498751 | 161 | |
mbed_official | 0:fd0d7bdfcdc2 | 162 | return fh_i + 3; // +3 as filehandles 0-2 are stdin/out/err |
mbed_official | 0:fd0d7bdfcdc2 | 163 | } |
mbed_official | 0:fd0d7bdfcdc2 | 164 | |
mbed_official | 0:fd0d7bdfcdc2 | 165 | extern "C" int PREFIX(_close)(FILEHANDLE fh) { |
mbed_official | 0:fd0d7bdfcdc2 | 166 | if (fh < 3) return 0; |
emilmont | 2:143cac498751 | 167 | |
mbed_official | 0:fd0d7bdfcdc2 | 168 | FileHandle* fhc = filehandles[fh-3]; |
mbed_official | 0:fd0d7bdfcdc2 | 169 | filehandles[fh-3] = NULL; |
mbed_official | 0:fd0d7bdfcdc2 | 170 | if (fhc == NULL) return -1; |
emilmont | 2:143cac498751 | 171 | |
mbed_official | 0:fd0d7bdfcdc2 | 172 | return fhc->close(); |
mbed_official | 0:fd0d7bdfcdc2 | 173 | } |
mbed_official | 0:fd0d7bdfcdc2 | 174 | |
emilmont | 2:143cac498751 | 175 | #if defined(__ICCARM__) |
emilmont | 2:143cac498751 | 176 | extern "C" size_t __write (int fh, const unsigned char *buffer, size_t length) { |
emilmont | 2:143cac498751 | 177 | #else |
emilmont | 2:143cac498751 | 178 | extern "C" int PREFIX(_write)(FILEHANDLE fh, const unsigned char *buffer, unsigned int length, int mode) { |
emilmont | 2:143cac498751 | 179 | #endif |
mbed_official | 0:fd0d7bdfcdc2 | 180 | int n; // n is the number of bytes written |
mbed_official | 0:fd0d7bdfcdc2 | 181 | if (fh < 3) { |
mbed_official | 0:fd0d7bdfcdc2 | 182 | #if DEVICE_SERIAL |
mbed_official | 0:fd0d7bdfcdc2 | 183 | if (!stdio_uart_inited) init_serial(); |
mbed_official | 0:fd0d7bdfcdc2 | 184 | for (unsigned int i = 0; i < length; i++) { |
mbed_official | 0:fd0d7bdfcdc2 | 185 | serial_putc(&stdio_uart, buffer[i]); |
mbed_official | 0:fd0d7bdfcdc2 | 186 | } |
mbed_official | 0:fd0d7bdfcdc2 | 187 | #endif |
mbed_official | 0:fd0d7bdfcdc2 | 188 | n = length; |
mbed_official | 0:fd0d7bdfcdc2 | 189 | } else { |
mbed_official | 0:fd0d7bdfcdc2 | 190 | FileHandle* fhc = filehandles[fh-3]; |
mbed_official | 0:fd0d7bdfcdc2 | 191 | if (fhc == NULL) return -1; |
emilmont | 2:143cac498751 | 192 | |
mbed_official | 0:fd0d7bdfcdc2 | 193 | n = fhc->write(buffer, length); |
mbed_official | 0:fd0d7bdfcdc2 | 194 | } |
mbed_official | 0:fd0d7bdfcdc2 | 195 | #ifdef __ARMCC_VERSION |
mbed_official | 0:fd0d7bdfcdc2 | 196 | return length-n; |
mbed_official | 0:fd0d7bdfcdc2 | 197 | #else |
mbed_official | 0:fd0d7bdfcdc2 | 198 | return n; |
emilmont | 2:143cac498751 | 199 | #endif |
mbed_official | 0:fd0d7bdfcdc2 | 200 | } |
mbed_official | 0:fd0d7bdfcdc2 | 201 | |
emilmont | 2:143cac498751 | 202 | #if defined(__ICCARM__) |
emilmont | 2:143cac498751 | 203 | extern "C" size_t __read (int fh, unsigned char *buffer, size_t length) { |
emilmont | 2:143cac498751 | 204 | #else |
emilmont | 2:143cac498751 | 205 | extern "C" int PREFIX(_read)(FILEHANDLE fh, unsigned char *buffer, unsigned int length, int mode) { |
emilmont | 2:143cac498751 | 206 | #endif |
mbed_official | 0:fd0d7bdfcdc2 | 207 | int n; // n is the number of bytes read |
mbed_official | 0:fd0d7bdfcdc2 | 208 | if (fh < 3) { |
mbed_official | 0:fd0d7bdfcdc2 | 209 | // only read a character at a time from stdin |
mbed_official | 0:fd0d7bdfcdc2 | 210 | #if DEVICE_SERIAL |
mbed_official | 0:fd0d7bdfcdc2 | 211 | *buffer = serial_getc(&stdio_uart); |
mbed_official | 0:fd0d7bdfcdc2 | 212 | #endif |
mbed_official | 0:fd0d7bdfcdc2 | 213 | n = 1; |
mbed_official | 0:fd0d7bdfcdc2 | 214 | } else { |
mbed_official | 0:fd0d7bdfcdc2 | 215 | FileHandle* fhc = filehandles[fh-3]; |
mbed_official | 0:fd0d7bdfcdc2 | 216 | if (fhc == NULL) return -1; |
emilmont | 2:143cac498751 | 217 | |
mbed_official | 0:fd0d7bdfcdc2 | 218 | n = fhc->read(buffer, length); |
mbed_official | 0:fd0d7bdfcdc2 | 219 | } |
mbed_official | 0:fd0d7bdfcdc2 | 220 | #ifdef __ARMCC_VERSION |
mbed_official | 0:fd0d7bdfcdc2 | 221 | return length-n; |
mbed_official | 0:fd0d7bdfcdc2 | 222 | #else |
mbed_official | 0:fd0d7bdfcdc2 | 223 | return n; |
emilmont | 2:143cac498751 | 224 | #endif |
mbed_official | 0:fd0d7bdfcdc2 | 225 | } |
mbed_official | 0:fd0d7bdfcdc2 | 226 | |
mbed_official | 0:fd0d7bdfcdc2 | 227 | #ifdef __ARMCC_VERSION |
mbed_official | 0:fd0d7bdfcdc2 | 228 | extern "C" int PREFIX(_istty)(FILEHANDLE fh) |
mbed_official | 0:fd0d7bdfcdc2 | 229 | #else |
emilmont | 2:143cac498751 | 230 | extern "C" int _isatty(FILEHANDLE fh) |
mbed_official | 0:fd0d7bdfcdc2 | 231 | #endif |
mbed_official | 0:fd0d7bdfcdc2 | 232 | { |
mbed_official | 0:fd0d7bdfcdc2 | 233 | /* stdin, stdout and stderr should be tty */ |
mbed_official | 0:fd0d7bdfcdc2 | 234 | if (fh < 3) return 1; |
emilmont | 2:143cac498751 | 235 | |
mbed_official | 0:fd0d7bdfcdc2 | 236 | FileHandle* fhc = filehandles[fh-3]; |
mbed_official | 0:fd0d7bdfcdc2 | 237 | if (fhc == NULL) return -1; |
emilmont | 2:143cac498751 | 238 | |
mbed_official | 0:fd0d7bdfcdc2 | 239 | return fhc->isatty(); |
mbed_official | 0:fd0d7bdfcdc2 | 240 | } |
mbed_official | 0:fd0d7bdfcdc2 | 241 | |
emilmont | 2:143cac498751 | 242 | extern "C" |
emilmont | 2:143cac498751 | 243 | #if defined(__ARMCC_VERSION) |
emilmont | 2:143cac498751 | 244 | int _sys_seek(FILEHANDLE fh, long position) |
emilmont | 2:143cac498751 | 245 | #elif defined(__ICCARM__) |
emilmont | 2:143cac498751 | 246 | long __lseek(int fh, long offset, int whence) |
mbed_official | 0:fd0d7bdfcdc2 | 247 | #else |
emilmont | 2:143cac498751 | 248 | int _lseek(FILEHANDLE fh, int offset, int whence) |
emilmont | 2:143cac498751 | 249 | #endif |
emilmont | 2:143cac498751 | 250 | { |
mbed_official | 0:fd0d7bdfcdc2 | 251 | if (fh < 3) return 0; |
emilmont | 2:143cac498751 | 252 | |
emilmont | 2:143cac498751 | 253 | FileHandle* fhc = filehandles[fh-3]; |
mbed_official | 0:fd0d7bdfcdc2 | 254 | if (fhc == NULL) return -1; |
emilmont | 2:143cac498751 | 255 | |
emilmont | 2:143cac498751 | 256 | #if defined(__ARMCC_VERSION) |
emilmont | 2:143cac498751 | 257 | return fhc->lseek(position, SEEK_SET); |
emilmont | 2:143cac498751 | 258 | #else |
mbed_official | 0:fd0d7bdfcdc2 | 259 | return fhc->lseek(offset, whence); |
emilmont | 2:143cac498751 | 260 | #endif |
mbed_official | 0:fd0d7bdfcdc2 | 261 | } |
mbed_official | 0:fd0d7bdfcdc2 | 262 | |
mbed_official | 0:fd0d7bdfcdc2 | 263 | #ifdef __ARMCC_VERSION |
mbed_official | 0:fd0d7bdfcdc2 | 264 | extern "C" int PREFIX(_ensure)(FILEHANDLE fh) { |
mbed_official | 0:fd0d7bdfcdc2 | 265 | if (fh < 3) return 0; |
emilmont | 2:143cac498751 | 266 | |
mbed_official | 0:fd0d7bdfcdc2 | 267 | FileHandle* fhc = filehandles[fh-3]; |
mbed_official | 0:fd0d7bdfcdc2 | 268 | if (fhc == NULL) return -1; |
emilmont | 2:143cac498751 | 269 | |
mbed_official | 0:fd0d7bdfcdc2 | 270 | return fhc->fsync(); |
mbed_official | 0:fd0d7bdfcdc2 | 271 | } |
mbed_official | 0:fd0d7bdfcdc2 | 272 | |
mbed_official | 0:fd0d7bdfcdc2 | 273 | extern "C" long PREFIX(_flen)(FILEHANDLE fh) { |
mbed_official | 0:fd0d7bdfcdc2 | 274 | if (fh < 3) return 0; |
emilmont | 2:143cac498751 | 275 | |
mbed_official | 0:fd0d7bdfcdc2 | 276 | FileHandle* fhc = filehandles[fh-3]; |
mbed_official | 0:fd0d7bdfcdc2 | 277 | if (fhc == NULL) return -1; |
emilmont | 2:143cac498751 | 278 | |
mbed_official | 0:fd0d7bdfcdc2 | 279 | return fhc->flen(); |
mbed_official | 0:fd0d7bdfcdc2 | 280 | } |
mbed_official | 0:fd0d7bdfcdc2 | 281 | #endif |
mbed_official | 0:fd0d7bdfcdc2 | 282 | |
mbed_official | 0:fd0d7bdfcdc2 | 283 | |
emilmont | 2:143cac498751 | 284 | #if !defined(__ARMCC_VERSION) && !defined(__ICCARM__) |
mbed_official | 0:fd0d7bdfcdc2 | 285 | extern "C" int _fstat(int fd, struct stat *st) { |
mbed_official | 0:fd0d7bdfcdc2 | 286 | if ((STDOUT_FILENO == fd) || (STDERR_FILENO == fd) || (STDIN_FILENO == fd)) { |
mbed_official | 0:fd0d7bdfcdc2 | 287 | st->st_mode = S_IFCHR; |
mbed_official | 0:fd0d7bdfcdc2 | 288 | return 0; |
mbed_official | 0:fd0d7bdfcdc2 | 289 | } |
emilmont | 2:143cac498751 | 290 | |
mbed_official | 0:fd0d7bdfcdc2 | 291 | errno = EBADF; |
mbed_official | 0:fd0d7bdfcdc2 | 292 | return -1; |
mbed_official | 0:fd0d7bdfcdc2 | 293 | } |
mbed_official | 0:fd0d7bdfcdc2 | 294 | #endif |
mbed_official | 0:fd0d7bdfcdc2 | 295 | |
mbed_official | 0:fd0d7bdfcdc2 | 296 | namespace std { |
mbed_official | 0:fd0d7bdfcdc2 | 297 | extern "C" int remove(const char *path) { |
emilmont | 1:62685faffa05 | 298 | FilePath fp(path); |
emilmont | 1:62685faffa05 | 299 | FileSystemLike *fs = fp.fileSystem(); |
mbed_official | 0:fd0d7bdfcdc2 | 300 | if (fs == NULL) return -1; |
emilmont | 2:143cac498751 | 301 | |
emilmont | 1:62685faffa05 | 302 | return fs->remove(fp.fileName()); |
mbed_official | 0:fd0d7bdfcdc2 | 303 | } |
mbed_official | 0:fd0d7bdfcdc2 | 304 | |
mbed_official | 0:fd0d7bdfcdc2 | 305 | extern "C" int rename(const char *oldname, const char *newname) { |
mbed_official | 0:fd0d7bdfcdc2 | 306 | return -1; |
mbed_official | 0:fd0d7bdfcdc2 | 307 | } |
mbed_official | 0:fd0d7bdfcdc2 | 308 | |
mbed_official | 0:fd0d7bdfcdc2 | 309 | extern "C" char *tmpnam(char *s) { |
mbed_official | 0:fd0d7bdfcdc2 | 310 | return NULL; |
mbed_official | 0:fd0d7bdfcdc2 | 311 | } |
mbed_official | 0:fd0d7bdfcdc2 | 312 | |
mbed_official | 0:fd0d7bdfcdc2 | 313 | extern "C" FILE *tmpfile() { |
mbed_official | 0:fd0d7bdfcdc2 | 314 | return NULL; |
mbed_official | 0:fd0d7bdfcdc2 | 315 | } |
mbed_official | 0:fd0d7bdfcdc2 | 316 | } // namespace std |
mbed_official | 0:fd0d7bdfcdc2 | 317 | |
mbed_official | 0:fd0d7bdfcdc2 | 318 | #ifdef __ARMCC_VERSION |
mbed_official | 0:fd0d7bdfcdc2 | 319 | extern "C" char *_sys_command_string(char *cmd, int len) { |
mbed_official | 0:fd0d7bdfcdc2 | 320 | return NULL; |
mbed_official | 0:fd0d7bdfcdc2 | 321 | } |
mbed_official | 0:fd0d7bdfcdc2 | 322 | #endif |
mbed_official | 0:fd0d7bdfcdc2 | 323 | |
mbed_official | 0:fd0d7bdfcdc2 | 324 | extern "C" DIR *opendir(const char *path) { |
mbed_official | 0:fd0d7bdfcdc2 | 325 | /* root dir is FileSystemLike */ |
mbed_official | 0:fd0d7bdfcdc2 | 326 | if (path[0] == '/' && path[1] == 0) { |
mbed_official | 0:fd0d7bdfcdc2 | 327 | return FileSystemLike::opendir(); |
mbed_official | 0:fd0d7bdfcdc2 | 328 | } |
emilmont | 2:143cac498751 | 329 | |
emilmont | 1:62685faffa05 | 330 | FilePath fp(path); |
emilmont | 1:62685faffa05 | 331 | FileSystemLike* fs = fp.fileSystem(); |
mbed_official | 0:fd0d7bdfcdc2 | 332 | if (fs == NULL) return NULL; |
emilmont | 2:143cac498751 | 333 | |
emilmont | 1:62685faffa05 | 334 | return fs->opendir(fp.fileName()); |
mbed_official | 0:fd0d7bdfcdc2 | 335 | } |
mbed_official | 0:fd0d7bdfcdc2 | 336 | |
mbed_official | 0:fd0d7bdfcdc2 | 337 | extern "C" struct dirent *readdir(DIR *dir) { |
mbed_official | 0:fd0d7bdfcdc2 | 338 | return dir->readdir(); |
mbed_official | 0:fd0d7bdfcdc2 | 339 | } |
mbed_official | 0:fd0d7bdfcdc2 | 340 | |
mbed_official | 0:fd0d7bdfcdc2 | 341 | extern "C" int closedir(DIR *dir) { |
mbed_official | 0:fd0d7bdfcdc2 | 342 | return dir->closedir(); |
mbed_official | 0:fd0d7bdfcdc2 | 343 | } |
mbed_official | 0:fd0d7bdfcdc2 | 344 | |
mbed_official | 0:fd0d7bdfcdc2 | 345 | extern "C" void rewinddir(DIR *dir) { |
mbed_official | 0:fd0d7bdfcdc2 | 346 | dir->rewinddir(); |
mbed_official | 0:fd0d7bdfcdc2 | 347 | } |
mbed_official | 0:fd0d7bdfcdc2 | 348 | |
mbed_official | 0:fd0d7bdfcdc2 | 349 | extern "C" off_t telldir(DIR *dir) { |
mbed_official | 0:fd0d7bdfcdc2 | 350 | return dir->telldir(); |
mbed_official | 0:fd0d7bdfcdc2 | 351 | } |
mbed_official | 0:fd0d7bdfcdc2 | 352 | |
mbed_official | 0:fd0d7bdfcdc2 | 353 | extern "C" void seekdir(DIR *dir, off_t off) { |
mbed_official | 0:fd0d7bdfcdc2 | 354 | dir->seekdir(off); |
mbed_official | 0:fd0d7bdfcdc2 | 355 | } |
mbed_official | 0:fd0d7bdfcdc2 | 356 | |
mbed_official | 0:fd0d7bdfcdc2 | 357 | extern "C" int mkdir(const char *path, mode_t mode) { |
emilmont | 1:62685faffa05 | 358 | FilePath fp(path); |
emilmont | 1:62685faffa05 | 359 | FileSystemLike *fs = fp.fileSystem(); |
mbed_official | 0:fd0d7bdfcdc2 | 360 | if (fs == NULL) return -1; |
emilmont | 2:143cac498751 | 361 | |
emilmont | 1:62685faffa05 | 362 | return fs->mkdir(fp.fileName(), mode); |
mbed_official | 0:fd0d7bdfcdc2 | 363 | } |
mbed_official | 0:fd0d7bdfcdc2 | 364 | |
mbed_official | 0:fd0d7bdfcdc2 | 365 | #if defined(TOOLCHAIN_GCC_CR) || defined(TOOLCHAIN_GCC_CS) || defined(TOOLCHAIN_GCC_ARM) |
mbed_official | 0:fd0d7bdfcdc2 | 366 | /* prevents the exception handling name demangling code getting pulled in */ |
mbed_official | 0:fd0d7bdfcdc2 | 367 | #include "error.h" |
mbed_official | 0:fd0d7bdfcdc2 | 368 | namespace __gnu_cxx { |
mbed_official | 0:fd0d7bdfcdc2 | 369 | void __verbose_terminate_handler() { |
mbed_official | 0:fd0d7bdfcdc2 | 370 | error("Exception"); |
mbed_official | 0:fd0d7bdfcdc2 | 371 | } |
mbed_official | 0:fd0d7bdfcdc2 | 372 | } |
mbed_official | 0:fd0d7bdfcdc2 | 373 | #endif |
mbed_official | 0:fd0d7bdfcdc2 | 374 | |
mbed_official | 0:fd0d7bdfcdc2 | 375 | // Make sure we are pulling in the retargeting module at link time |
mbed_official | 0:fd0d7bdfcdc2 | 376 | volatile int stdio_retargeting_module; |