From 4f2f96168d2cded3707ed2064df931913caef84b Mon Sep 17 00:00:00 2001 From: yasuoka <> Date: Sun, 8 Jun 2025 08:53:53 +0000 Subject: Test to verify the handling of fflush() for the pushed-back buffer that has been read or that has not. --- src/regress/lib/libc/stdio/test_fflush.c | 41 +++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) (limited to 'src/regress/lib/libc/stdio/test_fflush.c') diff --git a/src/regress/lib/libc/stdio/test_fflush.c b/src/regress/lib/libc/stdio/test_fflush.c index 76f2d88aba..a0586b7d14 100644 --- a/src/regress/lib/libc/stdio/test_fflush.c +++ b/src/regress/lib/libc/stdio/test_fflush.c @@ -1,4 +1,4 @@ -/* $OpenBSD: test_fflush.c,v 1.2 2025/05/25 05:35:13 yasuoka Exp $ */ +/* $OpenBSD: test_fflush.c,v 1.3 2025/06/08 08:53:53 yasuoka Exp $ */ /* * Copyright (c) 2025 YASUOKA Masahiko @@ -38,6 +38,7 @@ void test_fflush_read3(void); void test_fflush_read4(void); void setupw(void); void test_fflush_read5(void); +void test_fflush_read6(void); void setup(void) @@ -290,6 +291,43 @@ test_fflush_read5(void) r = fclose(fp); assert(r == 0); } + +void +test_fflush_read6(void) +{ + int r, c; + FILE *fp; + + setup(); + fp = fopen(TMPFILENAME, "r"); + assert(fp != NULL); + + /* + * https://pubs.opengroup.org/onlinepubs/9699919799/functions/fflush.html + * .. any characters pushed back onto the stream by ungetc() or ungetwc() + * that have not subsequently been read from the stream shall be discarded + * (without further changing the file offset). + */ + + assert(fgetc(fp) == 'H'); + c = getc(fp); + ungetc(c, fp); /* push back the character has been read */ + r = fflush(fp); + assert(r == 0); + assert(getc(fp) == c); + + fseek(fp, 0, SEEK_SET); + assert(fgetc(fp) == 'H'); + c = getc(fp); + ungetc('X', fp); /* push back the character has not been read */ + r = fflush(fp); + assert(r == 0); + assert(getc(fp) == 'l'); + + r = fclose(fp); + assert(r == 0); +} + int main(int argc, char *argv[]) { @@ -301,6 +339,7 @@ main(int argc, char *argv[]) test_fflush_read3(); test_fflush_read4(); test_fflush_read5(); + test_fflush_read6(); exit(0); } -- cgit v1.2.3-55-g6feb