summaryrefslogtreecommitdiff
path: root/src/regress/lib/libc/stdio/test_fflush.c
diff options
context:
space:
mode:
authoryasuoka <>2025-06-08 08:53:53 +0000
committeryasuoka <>2025-06-08 08:53:53 +0000
commit4f2f96168d2cded3707ed2064df931913caef84b (patch)
tree645006c232dbc15ded24091db54e1367c1f7ed5c /src/regress/lib/libc/stdio/test_fflush.c
parent254f2861c453e9ff5b599a2f87225ae368de8fd1 (diff)
downloadopenbsd-4f2f96168d2cded3707ed2064df931913caef84b.tar.gz
openbsd-4f2f96168d2cded3707ed2064df931913caef84b.tar.bz2
openbsd-4f2f96168d2cded3707ed2064df931913caef84b.zip
Test to verify the handling of fflush() for the pushed-back buffer that
has been read or that has not.
Diffstat (limited to 'src/regress/lib/libc/stdio/test_fflush.c')
-rw-r--r--src/regress/lib/libc/stdio/test_fflush.c41
1 files changed, 40 insertions, 1 deletions
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 @@
1/* $OpenBSD: test_fflush.c,v 1.2 2025/05/25 05:35:13 yasuoka Exp $ */ 1/* $OpenBSD: test_fflush.c,v 1.3 2025/06/08 08:53:53 yasuoka Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2025 YASUOKA Masahiko <yasuoka@yasuoka.net> 4 * Copyright (c) 2025 YASUOKA Masahiko <yasuoka@yasuoka.net>
@@ -38,6 +38,7 @@ void test_fflush_read3(void);
38void test_fflush_read4(void); 38void test_fflush_read4(void);
39void setupw(void); 39void setupw(void);
40void test_fflush_read5(void); 40void test_fflush_read5(void);
41void test_fflush_read6(void);
41 42
42void 43void
43setup(void) 44setup(void)
@@ -290,6 +291,43 @@ test_fflush_read5(void)
290 r = fclose(fp); 291 r = fclose(fp);
291 assert(r == 0); 292 assert(r == 0);
292} 293}
294
295void
296test_fflush_read6(void)
297{
298 int r, c;
299 FILE *fp;
300
301 setup();
302 fp = fopen(TMPFILENAME, "r");
303 assert(fp != NULL);
304
305 /*
306 * https://pubs.opengroup.org/onlinepubs/9699919799/functions/fflush.html
307 * .. any characters pushed back onto the stream by ungetc() or ungetwc()
308 * that have not subsequently been read from the stream shall be discarded
309 * (without further changing the file offset).
310 */
311
312 assert(fgetc(fp) == 'H');
313 c = getc(fp);
314 ungetc(c, fp); /* push back the character has been read */
315 r = fflush(fp);
316 assert(r == 0);
317 assert(getc(fp) == c);
318
319 fseek(fp, 0, SEEK_SET);
320 assert(fgetc(fp) == 'H');
321 c = getc(fp);
322 ungetc('X', fp); /* push back the character has not been read */
323 r = fflush(fp);
324 assert(r == 0);
325 assert(getc(fp) == 'l');
326
327 r = fclose(fp);
328 assert(r == 0);
329}
330
293int 331int
294main(int argc, char *argv[]) 332main(int argc, char *argv[])
295{ 333{
@@ -301,6 +339,7 @@ main(int argc, char *argv[])
301 test_fflush_read3(); 339 test_fflush_read3();
302 test_fflush_read4(); 340 test_fflush_read4();
303 test_fflush_read5(); 341 test_fflush_read5();
342 test_fflush_read6();
304 343
305 exit(0); 344 exit(0);
306} 345}