[PATCH 1/7] console: return characters written from console_putc

Ahmad Fatoum
2026-04-13T10:11:54Z

console_putc() prints two characters instead of one when it does
LF -> CRLF conversion.

As console_putc already has to do the CRLF check, have it return the
number of printed characters as well to simplify its users.

Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
common/console.c | 17 +++++++----------
common/console_common.c | 2 +-
common/console_simple.c | 13 +++++++------
include/console.h | 4 ++--
pbl/console.c | 8 ++++++--
5 files changed, 23 insertions(+), 21 deletions(-)

diff --git a/common/console.c b/common/console.c
index 62ca57e43b05..30a72b2bb6d2 100644
--- a/common/console.c
+++ b/common/console.c
@@ -576,10 +576,11 @@ int tstc(void)
}
EXPORT_SYMBOL(tstc);

-void console_putc(unsigned int ch, char c)
+int console_putc(unsigned int ch, char c)
{
struct console_device *cdev;
int init = initialized;
+ bool crlf = c == '\n';

switch (init) {
case CONSOLE_UNINITIALIZED:
@@ -587,20 +588,20 @@ void console_putc(unsigned int ch, char c)
fallthrough;
case CONSOLE_INITIALIZED_BUFFER:
kfifo_putc(console_output_fifo, c);
- if (c == '\n')
+ if (crlf)
putc_ll('\r');
putc_ll(c);
- return;
+ return 1 + crlf;

case CONSOLE_INIT_FULL:
for_each_console(cdev) {
if (cdev->f_active & ch) {
- if (c == '\n')
+ if (crlf)
cdev->putc(cdev, '\r');
cdev->putc(cdev, c);
}
}
- return;
+ return 1 + crlf;
default:
/* If we have problems inititalizing our data
* get them early
@@ -626,11 +627,7 @@ int console_puts(unsigned int ch, const char *str)
}

while (*s) {
- if (*s == '\n')
- n++;
-
- console_putc(ch, *s);
- n++;
+ n += console_putc(con, *s);
s++;
}
return n;
diff --git a/common/console_common.c b/common/console_common.c
index 20c93de68cdc..e3b49200c538 100644
--- a/common/console_common.c
+++ b/common/console_common.c
@@ -423,7 +423,7 @@ int dputc(int fd, char c)
if (fd == 1)
putchar(c);
else if (fd == 2)
- console_putc(CONSOLE_STDERR, c);
+ return console_putc(CONSOLE_STDERR, c);
else
return write(fd, &c, 1);

diff --git a/common/console_simple.c b/common/console_simple.c
index 25fe6217448c..dfc180e70f98 100644
--- a/common/console_simple.c
+++ b/common/console_simple.c
@@ -17,28 +17,29 @@ int console_puts(unsigned int ch, const char *str)
int i = 0;

while (*s) {
- console_putc(ch, *s);
+ i += console_putc(ch, *s);
s++;
- i++;
}

return i;
}
EXPORT_SYMBOL(console_puts);

-void console_putc(unsigned int ch, char c)
+int console_putc(unsigned int ch, char c)
{
+ bool crlf = c == '\n';
if (!console) {
- if (c == '\n')
+ if (crlf)
putc_ll('\r');
putc_ll(c);
- return;
+ return 1 + crlf;
}

- if (c == '\n')
+ if (crlf)
console->putc(console, '\r');

console->putc(console, c);
+ return 1 + crlf;
}
EXPORT_SYMBOL(console_putc);

diff --git a/include/console.h b/include/console.h
index cd00e86a805c..353a5088a547 100644
--- a/include/console.h
+++ b/include/console.h
@@ -229,7 +229,7 @@ int arch_ctrlc(void);

#ifndef CONFIG_CONSOLE_NONE
/* stdout */
-void console_putc(unsigned int ch, const char c);
+int console_putc(unsigned int ch, const char c);
int console_puts(unsigned int ch, const char *s);
void console_putbin(unsigned int ch, const u8 *str, size_t len);
void console_flush(void);
@@ -241,7 +241,7 @@ void console_ctrlc_allow(void);
void console_ctrlc_forbid(void);
#else
static inline int console_puts(unsigned int ch, const char *str) { return 0; }
-static inline void console_putc(unsigned int ch, char c) {}
+static inline int console_putc(unsigned int ch, char c) { return 0;}
static inline void console_flush(void) {}

/* test if ctrl-c was pressed */
diff --git a/pbl/console.c b/pbl/console.c
index f2e7b3a20e9c..1274552b2088 100644
--- a/pbl/console.c
+++ b/pbl/console.c
@@ -32,12 +32,14 @@ static void __putc(void *ctx, int c)
putc(ctx, c);
}

-void console_putc(unsigned int ch, char c)
+int console_putc(unsigned int ch, char c)
{
if (putc_offset)
__putc(putc_ctx, c);
else
putc_ll(c);
+
+ return 1;
}

int console_puts(unsigned int ch, const char *str)
@@ -45,8 +47,10 @@ int console_puts(unsigned int ch, const char *str)
int n = 0;

while (*str) {
- if (*str == '\n')
+ if (*str == '\n') {
console_putc(ch, '\r');
+ n++;
+ }

console_putc(ch, *str);
str++;
--
2.47.3