From 68ff73c5fb365f9780c1f28fd004d9e662f13044 Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Thu, 23 Feb 2023 02:25:20 +0800 Subject: [PATCH] stdio: lib_fgets convert \r\n to \n Signed-off-by: Xiang Xiao --- libs/libc/stdio/lib_libfgets.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/libs/libc/stdio/lib_libfgets.c b/libs/libc/stdio/lib_libfgets.c index 9e95b6884e..d3088f9fbc 100644 --- a/libs/libc/stdio/lib_libfgets.c +++ b/libs/libc/stdio/lib_libfgets.c @@ -142,6 +142,13 @@ FAR char *lib_fgets(FAR char *buf, size_t buflen, FILE *stream, if (ch == '\n') { + /* Convert \r\n to \n */ + + if (nch > 0 && buf[nch - 1] == '\r') + { + --nch; + } + if (keepnl) { /* Store newline is stored in the buffer */ @@ -161,7 +168,7 @@ FAR char *lib_fgets(FAR char *buf, size_t buflen, FILE *stream, { /* End of file with no data? */ - if (!nch) + if (nch == 0) { /* Yes.. return NULL as the end of file mark */