[PATCH 2/9] lib: term: return error code from term_getsize()
Ahmad Fatoum
a.fatoum at barebox.org
Thu Apr 30 23:53:56 PDT 2026
Instead of silently writing a magic value of 256 into the output
parameters on error, let callers distinguish the case that
"no interactive console responded" from a legitimate terminal size
by means of an error code.
No current user makes use of the 256 magic value, so we only need to
change term_getsize() itself.
Signed-off-by: Ahmad Fatoum <a.fatoum at barebox.org>
---
include/term.h | 2 +-
lib/term.c | 32 +++++++++++++++++++-------------
2 files changed, 20 insertions(+), 14 deletions(-)
diff --git a/include/term.h b/include/term.h
index 097f84681d49..e53fcffcfc28 100644
--- a/include/term.h
+++ b/include/term.h
@@ -3,6 +3,6 @@
#define __TERM_H
void term_setpos(int x, int y);
-void term_getsize(int *screenwidth, int *screenheight);
+int term_getsize(int *screenwidth, int *screenheight);
#endif /* __LIBBB_H */
diff --git a/lib/term.c b/lib/term.c
index 0aa4612d0b08..78158a3036c3 100644
--- a/lib/term.c
+++ b/lib/term.c
@@ -11,20 +11,17 @@ void term_setpos(int x, int y)
printf("\x1b[%d;%dH", y + 2, x + 1);
}
-void term_getsize(int *screenwidth, int *screenheight)
+int term_getsize(int *screenwidth, int *screenheight)
{
int n;
+ int width = INT_MAX, height = INT_MAX;
+ bool found = false;
char *endp;
const char esc[] = "\e7" "\e[r" "\e[999;999H" "\e[6n";
char buf[64];
- if (screenwidth)
- *screenwidth = 256;
- if (screenheight)
- *screenheight = 256;
-
for_each_console(cdev) {
- int width, height;
+ int w, h;
uint64_t start;
if (!(cdev->f_active & CONSOLE_STDIN))
@@ -60,14 +57,23 @@ void term_getsize(int *screenwidth, int *screenheight)
if (buf[1] != '[')
continue;
- height = simple_strtoul(buf + 2, &endp, 10);
- width = simple_strtoul(endp + 1, NULL, 10);
+ h = simple_strtoul(buf + 2, &endp, 10);
+ w = simple_strtoul(endp + 1, NULL, 10);
- if (screenwidth)
- *screenwidth = min(*screenwidth, width);
- if (screenheight)
- *screenheight = min(*screenheight, height);
+ width = min(w, width);
+ height = min(h, height);
+ found = true;
}
term_setpos(0, 0);
+
+ if (!found)
+ return -ENOENT;
+
+ if (screenwidth)
+ *screenwidth = width;
+ if (screenheight)
+ *screenheight = height;
+
+ return 0;
}
--
2.47.3
More information about the barebox
mailing list