Skip to content

Commit d4da298

Browse files
authored
Merge pull request #12758 from hugueskamba/hk_fix_microlib_minimal_console
Fix: Return the correct std I/O device handle for Microlib in retarget code
2 parents c9515af + eca09b7 commit d4da298

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

platform/source/mbed_retarget.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,11 @@ asm(" .global __use_full_stdio\n");
9191

9292
using namespace mbed;
9393

94-
#if defined(__MICROLIB)
95-
// Before version 5.03, we were using a patched version of microlib with proper names
96-
extern const char __stdin_name[] = ":tt";
97-
extern const char __stdout_name[] = ":tt";
98-
extern const char __stderr_name[] = ":tt";
99-
100-
#else
94+
// Microlib currently does not allow re-defining the pathnames for the
95+
// standard I/O device handles (STDIN, STDOUT, and STDERR).
96+
// It uses the default pathname ":tt" at library initialization to identify
97+
// them all.
98+
#if !defined(__MICROLIB)
10199
extern const char __stdin_name[] = "/stdin";
102100
extern const char __stdout_name[] = "/stdout";
103101
extern const char __stderr_name[] = "/stderr";
@@ -555,11 +553,15 @@ std::FILE *fdopen(FileHandle *fh, const char *mode)
555553
extern "C" FILEHANDLE PREFIX(_open)(const char *name, int openflags)
556554
{
557555
#if defined(__MICROLIB)
558-
// Before version 5.03, we were using a patched version of microlib with proper names
559-
// This is the workaround that the microlib author suggested us
560-
static int n = 0;
561-
if (std::strcmp(name, ":tt") == 0 && n < 3) {
562-
return n++;
556+
// Use the mode requested to select the standard I/O device handle to return.
557+
if (std::strcmp(name, ":tt") == 0) {
558+
if (openflags & OPEN_W) {
559+
return STDOUT_FILENO;
560+
} else if (openflags & OPEN_A) {
561+
return STDERR_FILENO;
562+
} else {
563+
return STDIN_FILENO;
564+
}
563565
}
564566
#else
565567
/* Use the posix convention that stdin,out,err are filehandles 0,1,2.

0 commit comments

Comments
 (0)