diff mbox series

btrfs-progs: utils: ask_user: flush stdout after prompt

Message ID 20241119014339.3640843-1-dominique.martinet@atmark-techno.com (mailing list archive)
State New
Headers show
Series btrfs-progs: utils: ask_user: flush stdout after prompt | expand

Commit Message

Dominique Martinet Nov. 19, 2024, 1:43 a.m. UTC
when stdio is line buffered printf will not flush anything (on musl?),
leaving the program hanging without displaying any prompt and weird
dialogs such as the following:
```
alpine:~# btrfstune -S 0 /dev/mmcblk1p1
WARNING: this is dangerous, clearing the seeding flag may cause the derived device not to be mountable!
y
WARNING: seeding flag is not set on /dev/mmcblk1p1
We are going to clear the seeding flag, are you sure? [y/N]: alpine:~#
```

forcing flush makes the prompt display properly

Signed-off-by: Dominique Martinet <dominique.martinet@atmark-techno.com>
---

I don't think this behaviour is musl-specific, but it seems odd this was
never reported before.. perhaps glibc flushes on fgets?
Anyway, it's easy to fix and there is probably no downside so here's a patch.

I've tested it works as expected, e.g. prompt is now properly displayed
before waiting for input.

 common/utils.c | 1 +
 1 file changed, 1 insertion(+)

--
2.39.5

Comments

Qu Wenruo Nov. 19, 2024, 4:33 a.m. UTC | #1
在 2024/11/19 12:13, Dominique Martinet 写道:
> when stdio is line buffered printf will not flush anything (on musl?),
> leaving the program hanging without displaying any prompt and weird
> dialogs such as the following:
> ```
> alpine:~# btrfstune -S 0 /dev/mmcblk1p1
> WARNING: this is dangerous, clearing the seeding flag may cause the derived device not to be mountable!
> y
> WARNING: seeding flag is not set on /dev/mmcblk1p1
> We are going to clear the seeding flag, are you sure? [y/N]: alpine:~#
> ```
>
> forcing flush makes the prompt display properly
>
> Signed-off-by: Dominique Martinet <dominique.martinet@atmark-techno.com>

Reviewed-by: Qu Wenruo <wqu@suse.com>

Thanks,
Qu

> ---
>
> I don't think this behaviour is musl-specific, but it seems odd this was
> never reported before.. perhaps glibc flushes on fgets?
> Anyway, it's easy to fix and there is probably no downside so here's a patch.
>
> I've tested it works as expected, e.g. prompt is now properly displayed
> before waiting for input.
>
>   common/utils.c | 1 +
>   1 file changed, 1 insertion(+)
>
> diff --git a/common/utils.c b/common/utils.c
> index 3ca7cff396fe..9515abd47af8 100644
> --- a/common/utils.c
> +++ b/common/utils.c
> @@ -416,6 +416,7 @@ int ask_user(const char *question)
>   	char *answer;
>
>   	printf("%s [y/N]: ", question);
> +	fflush(stdout);
>
>   	return fgets(buf, sizeof(buf) - 1, stdin) &&
>   	       (answer = strtok_r(buf, " \t\n\r", &saveptr)) &&
> --
> 2.39.5
>
>
>
diff mbox series

Patch

diff --git a/common/utils.c b/common/utils.c
index 3ca7cff396fe..9515abd47af8 100644
--- a/common/utils.c
+++ b/common/utils.c
@@ -416,6 +416,7 @@  int ask_user(const char *question)
 	char *answer;

 	printf("%s [y/N]: ", question);
+	fflush(stdout);

 	return fgets(buf, sizeof(buf) - 1, stdin) &&
 	       (answer = strtok_r(buf, " \t\n\r", &saveptr)) &&