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