diff --git a/platform/include/platform/mbed_retarget.h b/platform/include/platform/mbed_retarget.h index 380c8d25e6..b40125fcf3 100644 --- a/platform/include/platform/mbed_retarget.h +++ b/platform/include/platform/mbed_retarget.h @@ -184,11 +184,8 @@ FileHandle *mbed_override_console(int fd); */ FileHandle *mbed_file_handle(int fd); } - -typedef mbed::DirHandle DIR; -#else -typedef struct Dir DIR; #endif +typedef struct DIR_impl DIR; #endif // !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY /* The intent of this section is to unify the errno error values to match diff --git a/platform/source/mbed_retarget.cpp b/platform/source/mbed_retarget.cpp index f2f7774ea6..44b517d934 100644 --- a/platform/source/mbed_retarget.cpp +++ b/platform/source/mbed_retarget.cpp @@ -46,6 +46,12 @@ static SingletonPtr _mutex; +/* DIR is typedeffed to struct DIR_impl in header */ +struct DIR_impl { + mbed::DirHandle *handle; + struct dirent entry; +}; + #if defined(__ARMCC_VERSION) # include # include @@ -1297,9 +1303,15 @@ extern "C" DIR *opendir(const char *path) return NULL; } - DirHandle *dir; - int err = fs->open(&dir, fp.fileName()); + DIR *dir = new (std::nothrow) DIR; + if (!dir) { + errno = ENOMEM; + return NULL; + } + + int err = fs->open(&dir->handle, fp.fileName()); if (err < 0) { + delete dir; errno = -err; return NULL; } @@ -1309,8 +1321,7 @@ extern "C" DIR *opendir(const char *path) extern "C" struct dirent *readdir(DIR *dir) { - static struct dirent ent; - int err = dir->read(&ent); + int err = dir->handle->read(&dir->entry); if (err < 1) { if (err < 0) { errno = -err; @@ -1318,12 +1329,13 @@ extern "C" struct dirent *readdir(DIR *dir) return NULL; } - return &ent; + return &dir->entry; } extern "C" int closedir(DIR *dir) { - int err = dir->close(); + int err = dir->handle->close(); + delete dir; if (err < 0) { errno = -err; return -1; @@ -1334,17 +1346,17 @@ extern "C" int closedir(DIR *dir) extern "C" void rewinddir(DIR *dir) { - dir->rewind(); + dir->handle->rewind(); } extern "C" off_t telldir(DIR *dir) { - return dir->tell(); + return dir->handle->tell(); } extern "C" void seekdir(DIR *dir, off_t off) { - dir->seek(off); + dir->handle->seek(off); } extern "C" int mkdir(const char *path, mode_t mode)