aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2023-04-14 22:50:39 -0500
committerRob Landley <rob@landley.net>2023-04-14 22:50:39 -0500
commit8626a6ac7dff93bf97f754c93c3e426ee5c6b1e0 (patch)
tree81c8e87a896b8e96a8d16c08be5f15cde6457f4c
parentacfcc9572fc7d61fd55042bc1b067dc41c312ad9 (diff)
downloadtoybox-8626a6ac7dff93bf97f754c93c3e426ee5c6b1e0.tar.gz
Treat space and newline the same, and don't theoretically overrun
toybuf by 5 bytes if kernel returns something weird.
-rw-r--r--toys/other/oneit.c14
1 files changed, 7 insertions, 7 deletions
diff --git a/toys/other/oneit.c b/toys/other/oneit.c
index 13f58078..c0b0c09d 100644
--- a/toys/other/oneit.c
+++ b/toys/other/oneit.c
@@ -72,15 +72,15 @@ void oneit_main(void)
// Autodetect console from sysfs if no -c
memcpy(toybuf, "/dev/", 5);
- if (!TT.c && (TT.c = readfile("/sys/class/tty/console/active", ss, 4096))) {
+ i = sizeof(toybuf)-6;
+ if (!TT.c && (TT.c = readfile("/sys/class/tty/console/active", ss, i))) {
// Take last entry, remove newline terminator
- for (;;) {
- if (!(ss = strchr(TT.c, ' '))) break;
- if (!ss[1]) *ss = 0;
- else TT.c = ++ss;
+ while (TT.c[i = strcspn(TT.c, " \n")]) {
+ TT.c[i++] = 0;
+ if (TT.c[i]) TT.c += i;
+ else break;
}
- if (ss = strchr(TT.c, '\n')) ss[1] = 0;
- // Ensure /dev prefix
+ // Ensure exactly one /dev prefix
strstart(&TT.c, "/dev/");
memmove(toybuf+5, TT.c, strlen(TT.c));
TT.c = toybuf;