Skip to content

Commit da138f2

Browse files
Merge pull request #5203 from geky/fix-fstat-newlib
fs: Fix fstat retarget for regular files
2 parents 9b62dce + 0132691 commit da138f2

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
@@ -451,6 +451,7 @@ int _lseek(FILEHANDLE fh, int offset, int whence)
451451
#if defined(__ARMCC_VERSION)
452452
int whence = SEEK_SET;
453453
#endif
454+
454455
if (fh < 3) {
455456
errno = ESPIPE;
456457
return -1;
@@ -541,13 +542,21 @@ extern "C" __value_in_regs struct __initial_stackheap __user_setup_stackheap(uin
541542

542543

543544
#if !defined(__ARMCC_VERSION) && !defined(__ICCARM__)
544-
extern "C" int _fstat(int fd, struct stat *st) {
545-
if (fd < 3) {
545+
extern "C" int _fstat(int fh, struct stat *st) {
546+
if (fh < 3) {
546547
st->st_mode = S_IFCHR;
547548
return 0;
548549
}
549-
errno = EBADF;
550-
return -1;
550+
551+
FileHandle* fhc = filehandles[fh-3];
552+
if (fhc == NULL) {
553+
errno = EBADF;
554+
return -1;
555+
}
556+
557+
st->st_mode = fhc->isatty() ? S_IFCHR : S_IFREG;
558+
st->st_size = fhc->size();
559+
return 0;
551560
}
552561
#endif
553562

0 commit comments

Comments
 (0)