1 | #define _GNU_SOURCE
|
---|
2 | #include <stdio.h>
|
---|
3 | #include <string.h>
|
---|
4 | #include <assert.h>
|
---|
5 |
|
---|
6 | #if __GLIBC__ == 2
|
---|
7 | # define fpurge(f) (__fpurge (f), 0)
|
---|
8 | extern void __fpurge (FILE *);
|
---|
9 | #endif
|
---|
10 |
|
---|
11 |
|
---|
12 | /* Preparations: printf 'foogarsh' > foo.tmp */
|
---|
13 |
|
---|
14 | int main ()
|
---|
15 | {
|
---|
16 | FILE *fp;
|
---|
17 |
|
---|
18 | /* Open it in read-only mode. */
|
---|
19 | fp = fopen ("foo.tmp", "r");
|
---|
20 | if (fp == NULL)
|
---|
21 | return 1;
|
---|
22 | /* Verify that the pending writes before the fpurge were really
|
---|
23 | discarded. */
|
---|
24 | {
|
---|
25 | char buf[8];
|
---|
26 | if (fread (buf, 1, 7, fp) < 7)
|
---|
27 | return 2;
|
---|
28 | assert (memcmp (buf, "foogars", 7) == 0);
|
---|
29 | }
|
---|
30 | /* Discard the buffered 'h'. */
|
---|
31 | assert (ftell (fp) == 7);
|
---|
32 | assert (fpurge (fp) == 0);
|
---|
33 | /* Verify that when discarding pending input, the file position is
|
---|
34 | advanced to match the end of the previously read input. */
|
---|
35 | printf ("ftell (fp) = %ld\n", ftell (fp));
|
---|
36 | assert (ftell (fp) == 8);
|
---|
37 | assert (getc (fp) == EOF);
|
---|
38 | assert (fclose (fp) == 0);
|
---|
39 | printf ("OK\n");
|
---|
40 | return 0;
|
---|
41 | }
|
---|