Skip to content

Commit 0132691

Browse files
committed
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.
1 parent c6f655c commit 0132691

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

platform/mbed_retarget.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ int _lseek(FILEHANDLE fh, int offset, int whence)
446446
#if defined(__ARMCC_VERSION)
447447
int whence = SEEK_SET;
448448
#endif
449+
449450
if (fh < 3) {
450451
errno = ESPIPE;
451452
return -1;
@@ -536,13 +537,21 @@ extern "C" __value_in_regs struct __initial_stackheap __user_setup_stackheap(uin
536537

537538

538539
#if !defined(__ARMCC_VERSION) && !defined(__ICCARM__)
539-
extern "C" int _fstat(int fd, struct stat *st) {
540-
if (fd < 3) {
540+
extern "C" int _fstat(int fh, struct stat *st) {
541+
if (fh < 3) {
541542
st->st_mode = S_IFCHR;
542543
return 0;
543544
}
544-
errno = EBADF;
545-
return -1;
545+
546+
FileHandle* fhc = filehandles[fh-3];
547+
if (fhc == NULL) {
548+
errno = EBADF;
549+
return -1;
550+
}
551+
552+
st->st_mode = fhc->isatty() ? S_IFCHR : S_IFREG;
553+
st->st_size = fhc->size();
554+
return 0;
546555
}
547556
#endif
548557

0 commit comments

Comments
 (0)