A couple facts:
1. POSIX requires a single whole line to be read (as Roberto pointed out
already).
2. POSIX doesn't define what "affirmative" response is.
GNU Coreutils reads the first char and discards the rest [0].
Busybox skips blanks, then takes a char as answer and discards the rest [1].
Both of these are "valid" according to POSIX.
[0]:
https://github.com/coreutils/gnulib/blob/71288b12a5eb299e173a17245f548d5e2adb85c0/lib/yesno.c#L54-L59
[1]:
https://github.com/mirror/busybox/blob/371fe9f71d445d18be28c82a2a6d82115c8af19d/libbb/ask_confirmation.c#L11-L27
Now to Roberto's proposal:
while (isspace(ch = getchar()))
;
This is wrong because isspace() will return true on newlines, thus it'll
end up reading *multiple* lines. We must read only one. You want
isblank() instead.
Other than that:
1) Skipping leading blanks seems okay to me.
2) Rejecting any non-space trailing char is valid by POSIX but will also
lead to some surprising results, such as "yes" being rejected.
3) IMO busybox's approach seems like a good behavior: skip leading
blanks, ignore trailing chars.
- NRK
Received on Thu Apr 17 2025 - 23:44:31 CEST