mbed library sources

Committer:
ebrus
Date:
Wed Jul 27 18:35:32 2016 +0000
Revision:
0:0a673c671a56
4

Who changed what in which revision?

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