From 01326910c001afd88b8a02d9ff3ee655d331e420 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Tue, 26 Sep 2017 15:10:04 -0500 Subject: [PATCH] fs: Fixed fstat retarget for regular files GCC's newlib library depends on fstat to get in-flight information about a file's type an size. A working fstat for regular files is needed for seek and related functions to work correctly. --- platform/mbed_retarget.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/platform/mbed_retarget.cpp b/platform/mbed_retarget.cpp index e9f6d3c67c..d75316c6b1 100644 --- a/platform/mbed_retarget.cpp +++ b/platform/mbed_retarget.cpp @@ -446,6 +446,7 @@ int _lseek(FILEHANDLE fh, int offset, int whence) #if defined(__ARMCC_VERSION) int whence = SEEK_SET; #endif + if (fh < 3) { errno = ESPIPE; return -1; @@ -536,13 +537,21 @@ extern "C" __value_in_regs struct __initial_stackheap __user_setup_stackheap(uin #if !defined(__ARMCC_VERSION) && !defined(__ICCARM__) -extern "C" int _fstat(int fd, struct stat *st) { - if (fd < 3) { +extern "C" int _fstat(int fh, struct stat *st) { + if (fh < 3) { st->st_mode = S_IFCHR; return 0; } - errno = EBADF; - return -1; + + FileHandle* fhc = filehandles[fh-3]; + if (fhc == NULL) { + errno = EBADF; + return -1; + } + + st->st_mode = fhc->isatty() ? S_IFCHR : S_IFREG; + st->st_size = fhc->size(); + return 0; } #endif