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@7:3a1b3e92fa02, 2013-04-24 (annotated)
- Committer:
- emilmont
- Date:
- Wed Apr 24 15:11:33 2013 +0000
- Revision:
- 7:3a1b3e92fa02
- Parent:
- 5:ab1c572cb536
- Child:
- 8:4e25b8576136
Prepare support for ARMCC 5.03 (mainline microlib)
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 | |
emilmont | 7:3a1b3e92fa02 | 49 | #if defined(__MICROLIB) && (__ARMCC_VERSION>5030000) |
emilmont | 7:3a1b3e92fa02 | 50 | // Before version 5.03, we were using a patched version of microlib with proper names |
emilmont | 7:3a1b3e92fa02 | 51 | extern const char __stdin_name[] = ":tt"; |
emilmont | 7:3a1b3e92fa02 | 52 | extern const char __stdout_name[] = ":tt"; |
emilmont | 7:3a1b3e92fa02 | 53 | extern const char __stderr_name[] = ":tt"; |
emilmont | 7:3a1b3e92fa02 | 54 | |
emilmont | 7:3a1b3e92fa02 | 55 | #else |
mbed_official | 0:fd0d7bdfcdc2 | 56 | extern const char __stdin_name[] = "/stdin"; |
mbed_official | 0:fd0d7bdfcdc2 | 57 | extern const char __stdout_name[] = "/stdout"; |
mbed_official | 0:fd0d7bdfcdc2 | 58 | extern const char __stderr_name[] = "/stderr"; |
emilmont | 7:3a1b3e92fa02 | 59 | #endif |
mbed_official | 0:fd0d7bdfcdc2 | 60 | |
mbed_official | 0:fd0d7bdfcdc2 | 61 | /* newlib has the filehandle field in the FILE struct as a short, so |
mbed_official | 0:fd0d7bdfcdc2 | 62 | * we can't just return a Filehandle* from _open and instead have to |
mbed_official | 0:fd0d7bdfcdc2 | 63 | * put it in a filehandles array and return the index into that array |
mbed_official | 0:fd0d7bdfcdc2 | 64 | * (or rather index+3, as filehandles 0-2 are stdin/out/err). |
mbed_official | 0:fd0d7bdfcdc2 | 65 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 66 | static FileHandle *filehandles[OPEN_MAX]; |
mbed_official | 0:fd0d7bdfcdc2 | 67 | |
mbed_official | 0:fd0d7bdfcdc2 | 68 | FileHandle::~FileHandle() { |
mbed_official | 0:fd0d7bdfcdc2 | 69 | /* Remove all open filehandles for this */ |
mbed_official | 0:fd0d7bdfcdc2 | 70 | for (unsigned int fh_i = 0; fh_i < sizeof(filehandles)/sizeof(*filehandles); fh_i++) { |
mbed_official | 0:fd0d7bdfcdc2 | 71 | if (filehandles[fh_i] == this) { |
mbed_official | 0:fd0d7bdfcdc2 | 72 | filehandles[fh_i] = NULL; |
mbed_official | 0:fd0d7bdfcdc2 | 73 | } |
mbed_official | 0:fd0d7bdfcdc2 | 74 | } |
mbed_official | 0:fd0d7bdfcdc2 | 75 | } |
mbed_official | 0:fd0d7bdfcdc2 | 76 | |
mbed_official | 0:fd0d7bdfcdc2 | 77 | #if DEVICE_SERIAL |
mbed_official | 0:fd0d7bdfcdc2 | 78 | extern int stdio_uart_inited; |
mbed_official | 0:fd0d7bdfcdc2 | 79 | extern serial_t stdio_uart; |
mbed_official | 0:fd0d7bdfcdc2 | 80 | #endif |
mbed_official | 0:fd0d7bdfcdc2 | 81 | |
mbed_official | 0:fd0d7bdfcdc2 | 82 | static void init_serial() { |
mbed_official | 0:fd0d7bdfcdc2 | 83 | #if DEVICE_SERIAL |
mbed_official | 0:fd0d7bdfcdc2 | 84 | if (stdio_uart_inited) return; |
mbed_official | 0:fd0d7bdfcdc2 | 85 | serial_init(&stdio_uart, STDIO_UART_TX, STDIO_UART_RX); |
mbed_official | 0:fd0d7bdfcdc2 | 86 | #endif |
mbed_official | 0:fd0d7bdfcdc2 | 87 | } |
mbed_official | 0:fd0d7bdfcdc2 | 88 | |
emilmont | 2:143cac498751 | 89 | static inline int openmode_to_posix(int openmode) { |
emilmont | 2:143cac498751 | 90 | int posix = openmode; |
mbed_official | 0:fd0d7bdfcdc2 | 91 | #ifdef __ARMCC_VERSION |
emilmont | 2:143cac498751 | 92 | if (openmode & OPEN_PLUS) { |
emilmont | 2:143cac498751 | 93 | posix = O_RDWR; |
emilmont | 2:143cac498751 | 94 | } else if(openmode & OPEN_W) { |
emilmont | 2:143cac498751 | 95 | posix = O_WRONLY; |
emilmont | 2:143cac498751 | 96 | } else if(openmode & OPEN_A) { |
emilmont | 2:143cac498751 | 97 | posix = O_WRONLY|O_APPEND; |
mbed_official | 0:fd0d7bdfcdc2 | 98 | } else { |
emilmont | 2:143cac498751 | 99 | posix = O_RDONLY; |
mbed_official | 0:fd0d7bdfcdc2 | 100 | } |
mbed_official | 0:fd0d7bdfcdc2 | 101 | /* a, w, a+, w+ all create if file does not already exist */ |
emilmont | 2:143cac498751 | 102 | if (openmode & (OPEN_A|OPEN_W)) { |
emilmont | 2:143cac498751 | 103 | posix |= O_CREAT; |
mbed_official | 0:fd0d7bdfcdc2 | 104 | } |
mbed_official | 0:fd0d7bdfcdc2 | 105 | /* w and w+ truncate */ |
emilmont | 2:143cac498751 | 106 | if (openmode & OPEN_W) { |
emilmont | 2:143cac498751 | 107 | posix |= O_TRUNC; |
mbed_official | 0:fd0d7bdfcdc2 | 108 | } |
emilmont | 2:143cac498751 | 109 | #elif defined(__ICCARM__) |
emilmont | 2:143cac498751 | 110 | switch (openmode & _LLIO_RDWRMASK) { |
emilmont | 2:143cac498751 | 111 | case _LLIO_RDONLY: posix = O_RDONLY; break; |
emilmont | 2:143cac498751 | 112 | case _LLIO_WRONLY: posix = O_WRONLY; break; |
emilmont | 2:143cac498751 | 113 | case _LLIO_RDWR : posix = O_RDWR ; break; |
emilmont | 2:143cac498751 | 114 | } |
emilmont | 2:143cac498751 | 115 | if (openmode & _LLIO_CREAT ) posix |= O_CREAT; |
emilmont | 2:143cac498751 | 116 | if (openmode & _LLIO_APPEND) posix |= O_APPEND; |
emilmont | 2:143cac498751 | 117 | if (openmode & _LLIO_TRUNC ) posix |= O_TRUNC; |
emilmont | 2:143cac498751 | 118 | #endif |
emilmont | 2:143cac498751 | 119 | return posix; |
mbed_official | 0:fd0d7bdfcdc2 | 120 | } |
mbed_official | 0:fd0d7bdfcdc2 | 121 | |
mbed_official | 0:fd0d7bdfcdc2 | 122 | extern "C" FILEHANDLE PREFIX(_open)(const char* name, int openmode) { |
mbed_official | 0:fd0d7bdfcdc2 | 123 | /* Use the posix convention that stdin,out,err are filehandles 0,1,2. |
mbed_official | 0:fd0d7bdfcdc2 | 124 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 125 | if (std::strcmp(name, __stdin_name) == 0) { |
mbed_official | 0:fd0d7bdfcdc2 | 126 | init_serial(); |
mbed_official | 0:fd0d7bdfcdc2 | 127 | return 0; |
mbed_official | 0:fd0d7bdfcdc2 | 128 | } else if (std::strcmp(name, __stdout_name) == 0) { |
mbed_official | 0:fd0d7bdfcdc2 | 129 | init_serial(); |
mbed_official | 0:fd0d7bdfcdc2 | 130 | return 1; |
mbed_official | 0:fd0d7bdfcdc2 | 131 | } else if (std::strcmp(name,__stderr_name) == 0) { |
mbed_official | 0:fd0d7bdfcdc2 | 132 | init_serial(); |
mbed_official | 0:fd0d7bdfcdc2 | 133 | return 2; |
mbed_official | 0:fd0d7bdfcdc2 | 134 | } |
emilmont | 2:143cac498751 | 135 | |
mbed_official | 0:fd0d7bdfcdc2 | 136 | // find the first empty slot in filehandles |
mbed_official | 0:fd0d7bdfcdc2 | 137 | unsigned int fh_i; |
mbed_official | 0:fd0d7bdfcdc2 | 138 | for (fh_i = 0; fh_i < sizeof(filehandles)/sizeof(*filehandles); fh_i++) { |
mbed_official | 0:fd0d7bdfcdc2 | 139 | if (filehandles[fh_i] == NULL) break; |
mbed_official | 0:fd0d7bdfcdc2 | 140 | } |
mbed_official | 0:fd0d7bdfcdc2 | 141 | if (fh_i >= sizeof(filehandles)/sizeof(*filehandles)) { |
mbed_official | 0:fd0d7bdfcdc2 | 142 | return -1; |
mbed_official | 0:fd0d7bdfcdc2 | 143 | } |
emilmont | 2:143cac498751 | 144 | |
mbed_official | 0:fd0d7bdfcdc2 | 145 | FileHandle *res; |
emilmont | 2:143cac498751 | 146 | |
mbed_official | 0:fd0d7bdfcdc2 | 147 | /* FILENAME: ":0x12345678" describes a FileLike* */ |
mbed_official | 0:fd0d7bdfcdc2 | 148 | if (name[0] == ':') { |
mbed_official | 0:fd0d7bdfcdc2 | 149 | void *p; |
mbed_official | 0:fd0d7bdfcdc2 | 150 | sscanf(name, ":%p", &p); |
mbed_official | 0:fd0d7bdfcdc2 | 151 | res = (FileHandle*)p; |
emilmont | 2:143cac498751 | 152 | |
mbed_official | 0:fd0d7bdfcdc2 | 153 | /* FILENAME: "/file_system/file_name" */ |
mbed_official | 0:fd0d7bdfcdc2 | 154 | } else { |
mbed_official | 0:fd0d7bdfcdc2 | 155 | FilePath path(name); |
emilmont | 2:143cac498751 | 156 | |
emilmont | 2:143cac498751 | 157 | if (path.isFile()) { |
emilmont | 2:143cac498751 | 158 | res = path.file(); |
emilmont | 2:143cac498751 | 159 | } else { |
emilmont | 2:143cac498751 | 160 | FileSystemLike *fs = path.fileSystem(); |
emilmont | 2:143cac498751 | 161 | if (fs == NULL) return -1; |
emilmont | 2:143cac498751 | 162 | int posix_mode = openmode_to_posix(openmode); |
emilmont | 2:143cac498751 | 163 | res = fs->open(path.fileName(), posix_mode); /* NULL if fails */ |
emilmont | 2:143cac498751 | 164 | } |
mbed_official | 0:fd0d7bdfcdc2 | 165 | } |
emilmont | 2:143cac498751 | 166 | |
mbed_official | 0:fd0d7bdfcdc2 | 167 | if (res == NULL) return -1; |
mbed_official | 0:fd0d7bdfcdc2 | 168 | filehandles[fh_i] = res; |
emilmont | 2:143cac498751 | 169 | |
mbed_official | 0:fd0d7bdfcdc2 | 170 | return fh_i + 3; // +3 as filehandles 0-2 are stdin/out/err |
mbed_official | 0:fd0d7bdfcdc2 | 171 | } |
mbed_official | 0:fd0d7bdfcdc2 | 172 | |
mbed_official | 0:fd0d7bdfcdc2 | 173 | extern "C" int PREFIX(_close)(FILEHANDLE fh) { |
mbed_official | 0:fd0d7bdfcdc2 | 174 | if (fh < 3) return 0; |
emilmont | 2:143cac498751 | 175 | |
mbed_official | 0:fd0d7bdfcdc2 | 176 | FileHandle* fhc = filehandles[fh-3]; |
mbed_official | 0:fd0d7bdfcdc2 | 177 | filehandles[fh-3] = NULL; |
mbed_official | 0:fd0d7bdfcdc2 | 178 | if (fhc == NULL) return -1; |
emilmont | 2:143cac498751 | 179 | |
mbed_official | 0:fd0d7bdfcdc2 | 180 | return fhc->close(); |
mbed_official | 0:fd0d7bdfcdc2 | 181 | } |
mbed_official | 0:fd0d7bdfcdc2 | 182 | |
emilmont | 2:143cac498751 | 183 | #if defined(__ICCARM__) |
emilmont | 2:143cac498751 | 184 | extern "C" size_t __write (int fh, const unsigned char *buffer, size_t length) { |
emilmont | 2:143cac498751 | 185 | #else |
emilmont | 2:143cac498751 | 186 | extern "C" int PREFIX(_write)(FILEHANDLE fh, const unsigned char *buffer, unsigned int length, int mode) { |
emilmont | 2:143cac498751 | 187 | #endif |
mbed_official | 0:fd0d7bdfcdc2 | 188 | int n; // n is the number of bytes written |
mbed_official | 0:fd0d7bdfcdc2 | 189 | if (fh < 3) { |
mbed_official | 0:fd0d7bdfcdc2 | 190 | #if DEVICE_SERIAL |
mbed_official | 0:fd0d7bdfcdc2 | 191 | if (!stdio_uart_inited) init_serial(); |
mbed_official | 0:fd0d7bdfcdc2 | 192 | for (unsigned int i = 0; i < length; i++) { |
mbed_official | 0:fd0d7bdfcdc2 | 193 | serial_putc(&stdio_uart, buffer[i]); |
mbed_official | 0:fd0d7bdfcdc2 | 194 | } |
mbed_official | 0:fd0d7bdfcdc2 | 195 | #endif |
mbed_official | 0:fd0d7bdfcdc2 | 196 | n = length; |
mbed_official | 0:fd0d7bdfcdc2 | 197 | } else { |
mbed_official | 0:fd0d7bdfcdc2 | 198 | FileHandle* fhc = filehandles[fh-3]; |
mbed_official | 0:fd0d7bdfcdc2 | 199 | if (fhc == NULL) return -1; |
emilmont | 2:143cac498751 | 200 | |
mbed_official | 0:fd0d7bdfcdc2 | 201 | n = fhc->write(buffer, length); |
mbed_official | 0:fd0d7bdfcdc2 | 202 | } |
mbed_official | 0:fd0d7bdfcdc2 | 203 | #ifdef __ARMCC_VERSION |
mbed_official | 0:fd0d7bdfcdc2 | 204 | return length-n; |
mbed_official | 0:fd0d7bdfcdc2 | 205 | #else |
mbed_official | 0:fd0d7bdfcdc2 | 206 | return n; |
emilmont | 2:143cac498751 | 207 | #endif |
mbed_official | 0:fd0d7bdfcdc2 | 208 | } |
mbed_official | 0:fd0d7bdfcdc2 | 209 | |
emilmont | 2:143cac498751 | 210 | #if defined(__ICCARM__) |
emilmont | 2:143cac498751 | 211 | extern "C" size_t __read (int fh, unsigned char *buffer, size_t length) { |
emilmont | 2:143cac498751 | 212 | #else |
emilmont | 2:143cac498751 | 213 | extern "C" int PREFIX(_read)(FILEHANDLE fh, unsigned char *buffer, unsigned int length, int mode) { |
emilmont | 2:143cac498751 | 214 | #endif |
mbed_official | 0:fd0d7bdfcdc2 | 215 | int n; // n is the number of bytes read |
mbed_official | 0:fd0d7bdfcdc2 | 216 | if (fh < 3) { |
mbed_official | 0:fd0d7bdfcdc2 | 217 | // only read a character at a time from stdin |
mbed_official | 0:fd0d7bdfcdc2 | 218 | #if DEVICE_SERIAL |
mbed_official | 0:fd0d7bdfcdc2 | 219 | *buffer = serial_getc(&stdio_uart); |
mbed_official | 0:fd0d7bdfcdc2 | 220 | #endif |
mbed_official | 0:fd0d7bdfcdc2 | 221 | n = 1; |
mbed_official | 0:fd0d7bdfcdc2 | 222 | } else { |
mbed_official | 0:fd0d7bdfcdc2 | 223 | FileHandle* fhc = filehandles[fh-3]; |
mbed_official | 0:fd0d7bdfcdc2 | 224 | if (fhc == NULL) return -1; |
emilmont | 2:143cac498751 | 225 | |
mbed_official | 0:fd0d7bdfcdc2 | 226 | n = fhc->read(buffer, length); |
mbed_official | 0:fd0d7bdfcdc2 | 227 | } |
mbed_official | 0:fd0d7bdfcdc2 | 228 | #ifdef __ARMCC_VERSION |
mbed_official | 0:fd0d7bdfcdc2 | 229 | return length-n; |
mbed_official | 0:fd0d7bdfcdc2 | 230 | #else |
mbed_official | 0:fd0d7bdfcdc2 | 231 | return n; |
emilmont | 2:143cac498751 | 232 | #endif |
mbed_official | 0:fd0d7bdfcdc2 | 233 | } |
mbed_official | 0:fd0d7bdfcdc2 | 234 | |
mbed_official | 0:fd0d7bdfcdc2 | 235 | #ifdef __ARMCC_VERSION |
mbed_official | 0:fd0d7bdfcdc2 | 236 | extern "C" int PREFIX(_istty)(FILEHANDLE fh) |
mbed_official | 0:fd0d7bdfcdc2 | 237 | #else |
emilmont | 2:143cac498751 | 238 | extern "C" int _isatty(FILEHANDLE fh) |
mbed_official | 0:fd0d7bdfcdc2 | 239 | #endif |
mbed_official | 0:fd0d7bdfcdc2 | 240 | { |
mbed_official | 0:fd0d7bdfcdc2 | 241 | /* stdin, stdout and stderr should be tty */ |
mbed_official | 0:fd0d7bdfcdc2 | 242 | if (fh < 3) return 1; |
emilmont | 2:143cac498751 | 243 | |
mbed_official | 0:fd0d7bdfcdc2 | 244 | FileHandle* fhc = filehandles[fh-3]; |
mbed_official | 0:fd0d7bdfcdc2 | 245 | if (fhc == NULL) return -1; |
emilmont | 2:143cac498751 | 246 | |
mbed_official | 0:fd0d7bdfcdc2 | 247 | return fhc->isatty(); |
mbed_official | 0:fd0d7bdfcdc2 | 248 | } |
mbed_official | 0:fd0d7bdfcdc2 | 249 | |
emilmont | 2:143cac498751 | 250 | extern "C" |
emilmont | 2:143cac498751 | 251 | #if defined(__ARMCC_VERSION) |
emilmont | 2:143cac498751 | 252 | int _sys_seek(FILEHANDLE fh, long position) |
emilmont | 2:143cac498751 | 253 | #elif defined(__ICCARM__) |
emilmont | 2:143cac498751 | 254 | long __lseek(int fh, long offset, int whence) |
mbed_official | 0:fd0d7bdfcdc2 | 255 | #else |
emilmont | 2:143cac498751 | 256 | int _lseek(FILEHANDLE fh, int offset, int whence) |
emilmont | 2:143cac498751 | 257 | #endif |
emilmont | 2:143cac498751 | 258 | { |
mbed_official | 0:fd0d7bdfcdc2 | 259 | if (fh < 3) return 0; |
emilmont | 2:143cac498751 | 260 | |
emilmont | 2:143cac498751 | 261 | FileHandle* fhc = filehandles[fh-3]; |
mbed_official | 0:fd0d7bdfcdc2 | 262 | if (fhc == NULL) return -1; |
emilmont | 2:143cac498751 | 263 | |
emilmont | 2:143cac498751 | 264 | #if defined(__ARMCC_VERSION) |
emilmont | 2:143cac498751 | 265 | return fhc->lseek(position, SEEK_SET); |
emilmont | 2:143cac498751 | 266 | #else |
mbed_official | 0:fd0d7bdfcdc2 | 267 | return fhc->lseek(offset, whence); |
emilmont | 2:143cac498751 | 268 | #endif |
mbed_official | 0:fd0d7bdfcdc2 | 269 | } |
mbed_official | 0:fd0d7bdfcdc2 | 270 | |
mbed_official | 0:fd0d7bdfcdc2 | 271 | #ifdef __ARMCC_VERSION |
mbed_official | 0:fd0d7bdfcdc2 | 272 | extern "C" int PREFIX(_ensure)(FILEHANDLE fh) { |
mbed_official | 0:fd0d7bdfcdc2 | 273 | if (fh < 3) return 0; |
emilmont | 2:143cac498751 | 274 | |
mbed_official | 0:fd0d7bdfcdc2 | 275 | FileHandle* fhc = filehandles[fh-3]; |
mbed_official | 0:fd0d7bdfcdc2 | 276 | if (fhc == NULL) return -1; |
emilmont | 2:143cac498751 | 277 | |
mbed_official | 0:fd0d7bdfcdc2 | 278 | return fhc->fsync(); |
mbed_official | 0:fd0d7bdfcdc2 | 279 | } |
mbed_official | 0:fd0d7bdfcdc2 | 280 | |
mbed_official | 0:fd0d7bdfcdc2 | 281 | extern "C" long PREFIX(_flen)(FILEHANDLE fh) { |
mbed_official | 0:fd0d7bdfcdc2 | 282 | if (fh < 3) return 0; |
emilmont | 2:143cac498751 | 283 | |
mbed_official | 0:fd0d7bdfcdc2 | 284 | FileHandle* fhc = filehandles[fh-3]; |
mbed_official | 0:fd0d7bdfcdc2 | 285 | if (fhc == NULL) return -1; |
emilmont | 2:143cac498751 | 286 | |
mbed_official | 0:fd0d7bdfcdc2 | 287 | return fhc->flen(); |
mbed_official | 0:fd0d7bdfcdc2 | 288 | } |
mbed_official | 0:fd0d7bdfcdc2 | 289 | #endif |
mbed_official | 0:fd0d7bdfcdc2 | 290 | |
mbed_official | 0:fd0d7bdfcdc2 | 291 | |
emilmont | 2:143cac498751 | 292 | #if !defined(__ARMCC_VERSION) && !defined(__ICCARM__) |
mbed_official | 0:fd0d7bdfcdc2 | 293 | extern "C" int _fstat(int fd, struct stat *st) { |
mbed_official | 0:fd0d7bdfcdc2 | 294 | if ((STDOUT_FILENO == fd) || (STDERR_FILENO == fd) || (STDIN_FILENO == fd)) { |
mbed_official | 0:fd0d7bdfcdc2 | 295 | st->st_mode = S_IFCHR; |
mbed_official | 0:fd0d7bdfcdc2 | 296 | return 0; |
mbed_official | 0:fd0d7bdfcdc2 | 297 | } |
emilmont | 2:143cac498751 | 298 | |
mbed_official | 0:fd0d7bdfcdc2 | 299 | errno = EBADF; |
mbed_official | 0:fd0d7bdfcdc2 | 300 | return -1; |
mbed_official | 0:fd0d7bdfcdc2 | 301 | } |
mbed_official | 0:fd0d7bdfcdc2 | 302 | #endif |
mbed_official | 0:fd0d7bdfcdc2 | 303 | |
mbed_official | 0:fd0d7bdfcdc2 | 304 | namespace std { |
mbed_official | 0:fd0d7bdfcdc2 | 305 | extern "C" int remove(const char *path) { |
emilmont | 1:62685faffa05 | 306 | FilePath fp(path); |
emilmont | 1:62685faffa05 | 307 | FileSystemLike *fs = fp.fileSystem(); |
mbed_official | 0:fd0d7bdfcdc2 | 308 | if (fs == NULL) return -1; |
emilmont | 2:143cac498751 | 309 | |
emilmont | 1:62685faffa05 | 310 | return fs->remove(fp.fileName()); |
mbed_official | 0:fd0d7bdfcdc2 | 311 | } |
mbed_official | 0:fd0d7bdfcdc2 | 312 | |
mbed_official | 0:fd0d7bdfcdc2 | 313 | extern "C" int rename(const char *oldname, const char *newname) { |
mbed_official | 0:fd0d7bdfcdc2 | 314 | return -1; |
mbed_official | 0:fd0d7bdfcdc2 | 315 | } |
mbed_official | 0:fd0d7bdfcdc2 | 316 | |
mbed_official | 0:fd0d7bdfcdc2 | 317 | extern "C" char *tmpnam(char *s) { |
mbed_official | 0:fd0d7bdfcdc2 | 318 | return NULL; |
mbed_official | 0:fd0d7bdfcdc2 | 319 | } |
mbed_official | 0:fd0d7bdfcdc2 | 320 | |
mbed_official | 0:fd0d7bdfcdc2 | 321 | extern "C" FILE *tmpfile() { |
mbed_official | 0:fd0d7bdfcdc2 | 322 | return NULL; |
mbed_official | 0:fd0d7bdfcdc2 | 323 | } |
mbed_official | 0:fd0d7bdfcdc2 | 324 | } // namespace std |
mbed_official | 0:fd0d7bdfcdc2 | 325 | |
mbed_official | 0:fd0d7bdfcdc2 | 326 | #ifdef __ARMCC_VERSION |
mbed_official | 0:fd0d7bdfcdc2 | 327 | extern "C" char *_sys_command_string(char *cmd, int len) { |
mbed_official | 0:fd0d7bdfcdc2 | 328 | return NULL; |
mbed_official | 0:fd0d7bdfcdc2 | 329 | } |
mbed_official | 0:fd0d7bdfcdc2 | 330 | #endif |
mbed_official | 0:fd0d7bdfcdc2 | 331 | |
mbed_official | 0:fd0d7bdfcdc2 | 332 | extern "C" DIR *opendir(const char *path) { |
mbed_official | 0:fd0d7bdfcdc2 | 333 | /* root dir is FileSystemLike */ |
mbed_official | 0:fd0d7bdfcdc2 | 334 | if (path[0] == '/' && path[1] == 0) { |
mbed_official | 0:fd0d7bdfcdc2 | 335 | return FileSystemLike::opendir(); |
mbed_official | 0:fd0d7bdfcdc2 | 336 | } |
emilmont | 2:143cac498751 | 337 | |
emilmont | 1:62685faffa05 | 338 | FilePath fp(path); |
emilmont | 1:62685faffa05 | 339 | FileSystemLike* fs = fp.fileSystem(); |
mbed_official | 0:fd0d7bdfcdc2 | 340 | if (fs == NULL) return NULL; |
emilmont | 2:143cac498751 | 341 | |
emilmont | 1:62685faffa05 | 342 | return fs->opendir(fp.fileName()); |
mbed_official | 0:fd0d7bdfcdc2 | 343 | } |
mbed_official | 0:fd0d7bdfcdc2 | 344 | |
mbed_official | 0:fd0d7bdfcdc2 | 345 | extern "C" struct dirent *readdir(DIR *dir) { |
mbed_official | 0:fd0d7bdfcdc2 | 346 | return dir->readdir(); |
mbed_official | 0:fd0d7bdfcdc2 | 347 | } |
mbed_official | 0:fd0d7bdfcdc2 | 348 | |
mbed_official | 0:fd0d7bdfcdc2 | 349 | extern "C" int closedir(DIR *dir) { |
mbed_official | 0:fd0d7bdfcdc2 | 350 | return dir->closedir(); |
mbed_official | 0:fd0d7bdfcdc2 | 351 | } |
mbed_official | 0:fd0d7bdfcdc2 | 352 | |
mbed_official | 0:fd0d7bdfcdc2 | 353 | extern "C" void rewinddir(DIR *dir) { |
mbed_official | 0:fd0d7bdfcdc2 | 354 | dir->rewinddir(); |
mbed_official | 0:fd0d7bdfcdc2 | 355 | } |
mbed_official | 0:fd0d7bdfcdc2 | 356 | |
mbed_official | 0:fd0d7bdfcdc2 | 357 | extern "C" off_t telldir(DIR *dir) { |
mbed_official | 0:fd0d7bdfcdc2 | 358 | return dir->telldir(); |
mbed_official | 0:fd0d7bdfcdc2 | 359 | } |
mbed_official | 0:fd0d7bdfcdc2 | 360 | |
mbed_official | 0:fd0d7bdfcdc2 | 361 | extern "C" void seekdir(DIR *dir, off_t off) { |
mbed_official | 0:fd0d7bdfcdc2 | 362 | dir->seekdir(off); |
mbed_official | 0:fd0d7bdfcdc2 | 363 | } |
mbed_official | 0:fd0d7bdfcdc2 | 364 | |
mbed_official | 0:fd0d7bdfcdc2 | 365 | extern "C" int mkdir(const char *path, mode_t mode) { |
emilmont | 1:62685faffa05 | 366 | FilePath fp(path); |
emilmont | 1:62685faffa05 | 367 | FileSystemLike *fs = fp.fileSystem(); |
mbed_official | 0:fd0d7bdfcdc2 | 368 | if (fs == NULL) return -1; |
emilmont | 2:143cac498751 | 369 | |
emilmont | 1:62685faffa05 | 370 | return fs->mkdir(fp.fileName(), mode); |
mbed_official | 0:fd0d7bdfcdc2 | 371 | } |
mbed_official | 0:fd0d7bdfcdc2 | 372 | |
mbed_official | 0:fd0d7bdfcdc2 | 373 | #if defined(TOOLCHAIN_GCC_CR) || defined(TOOLCHAIN_GCC_CS) || defined(TOOLCHAIN_GCC_ARM) |
mbed_official | 0:fd0d7bdfcdc2 | 374 | /* prevents the exception handling name demangling code getting pulled in */ |
mbed_official | 0:fd0d7bdfcdc2 | 375 | #include "error.h" |
mbed_official | 0:fd0d7bdfcdc2 | 376 | namespace __gnu_cxx { |
mbed_official | 0:fd0d7bdfcdc2 | 377 | void __verbose_terminate_handler() { |
mbed_official | 0:fd0d7bdfcdc2 | 378 | error("Exception"); |
mbed_official | 0:fd0d7bdfcdc2 | 379 | } |
mbed_official | 0:fd0d7bdfcdc2 | 380 | } |
mbed_official | 0:fd0d7bdfcdc2 | 381 | #endif |
mbed_official | 0:fd0d7bdfcdc2 | 382 | |
mbed_official | 0:fd0d7bdfcdc2 | 383 | // Make sure we are pulling in the retargeting module at link time |
mbed_official | 0:fd0d7bdfcdc2 | 384 | volatile int stdio_retargeting_module; |