Update.
[glibc.git] / libio / bug-ungetc2.c
blob652f7e956c43b28f57e1b63a3fa16c6036df19fb
1 #include <stdio.h>
2 #include <sys/types.h>
5 static int
6 check (FILE *fp, off_t o)
8 int result = 0;
9 if (feof (fp))
11 puts ("feof !");
12 result = 1;
14 if (ferror (fp))
16 puts ("ferror !");
17 result = 1;
19 if (ftello (fp) != o)
21 printf ("position = %lu, not %lu\n", (unsigned long int) ftello (fp),
22 (unsigned long int) o);
23 result = 1;
25 return result;
29 static int
30 do_test (void)
32 FILE *fp = tmpfile ();
33 if (fp == NULL)
35 puts ("tmpfile failed");
36 return 1;
38 if (check (fp, 0) != 0)
39 return 1;
41 puts ("going to write");
42 if (fputs ("hello", fp) == EOF)
44 puts ("fputs failed");
45 return 1;
47 if (check (fp, 5) != 0)
48 return 1;
50 puts ("going to rewind");
51 rewind (fp);
52 if (check (fp, 0) != 0)
53 return 1;
55 puts ("going to read char");
56 int c = fgetc (fp);
57 if (c != 'h')
59 printf ("read %c, not %c\n", c, 'h');
60 return 1;
62 if (check (fp, 1) != 0)
63 return 1;
65 puts ("going to put back");
66 if (ungetc (' ', fp) == EOF)
68 puts ("ungetc failed");
69 return 1;
71 if (check (fp, 0) != 0)
72 return 1;
74 puts ("going to write again");
75 if (fputs ("world", fp) == EOF)
77 puts ("2nd fputs failed");
78 return 1;
80 if (check (fp, 5) != 0)
81 return 1;
83 puts ("going to rewind again");
84 rewind (fp);
85 if (check (fp, 0) != 0)
86 return 1;
88 if (fclose (fp) != 0)
90 puts ("fclose failed");
91 return 1;
94 return 0;
97 #define TEST_FUNCTION do_test ()
98 #include "../test-skeleton.c"