Skip to content

Commit 04b333e

Browse files
committed
Microlib: Fix console printing
Return the correct filehandle based on the mode requested. The mode is used as the pathname is always the default one.
1 parent c8ab263 commit 04b333e

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

platform/source/mbed_retarget.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@ asm(" .global __use_full_stdio\n");
9292
using namespace mbed;
9393

9494
#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-
95+
// Microlib currently does not allow re-defining the pathnames for the
96+
// standard I/O device handles (STDIN, STDOUT, and STDERR).
97+
// It uses the default pathname ":tt" at library initialization to identify
98+
// them all.
99+
extern const char default_pathname[] = ":tt";
100100
#else
101101
extern const char __stdin_name[] = "/stdin";
102102
extern const char __stdout_name[] = "/stdout";
@@ -555,11 +555,15 @@ std::FILE *fdopen(FileHandle *fh, const char *mode)
555555
extern "C" FILEHANDLE PREFIX(_open)(const char *name, int openflags)
556556
{
557557
#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++;
558+
// Use the mode requested to select the standard I/O device handle to return.
559+
if (std::strcmp(name, default_pathname) == 0) {
560+
if (openflags & OPEN_R) {
561+
return STDIN_FILENO;
562+
} else if (openflags & OPEN_W) {
563+
return STDOUT_FILENO;
564+
} else if (openflags & OPEN_A) {
565+
return STDERR_FILENO;
566+
}
563567
}
564568
#else
565569
/* Use the posix convention that stdin,out,err are filehandles 0,1,2.

0 commit comments

Comments
 (0)