]> git.ipfire.org Git - thirdparty/glibc.git/blob - libio/bug-ungetc2.c
hurd: Fix build
[thirdparty/glibc.git] / libio / bug-ungetc2.c
1 #include <stdio.h>
2 #include <sys/types.h>
3
4
5 static int
6 check (FILE *fp, off_t o)
7 {
8 int result = 0;
9 if (feof (fp))
10 {
11 puts ("feof !");
12 result = 1;
13 }
14 if (ferror (fp))
15 {
16 puts ("ferror !");
17 result = 1;
18 }
19 if (ftello (fp) != o)
20 {
21 printf ("position = %lu, not %lu\n", (unsigned long int) ftello (fp),
22 (unsigned long int) o);
23 result = 1;
24 }
25 return result;
26 }
27
28
29 static int
30 do_test (void)
31 {
32 FILE *fp = tmpfile ();
33 if (fp == NULL)
34 {
35 puts ("tmpfile failed");
36 return 1;
37 }
38 if (check (fp, 0) != 0)
39 return 1;
40
41 puts ("going to write");
42 if (fputs ("hello", fp) == EOF)
43 {
44 puts ("fputs failed");
45 return 1;
46 }
47 if (check (fp, 5) != 0)
48 return 1;
49
50 puts ("going to rewind");
51 rewind (fp);
52 if (check (fp, 0) != 0)
53 return 1;
54
55 puts ("going to read char");
56 int c = fgetc (fp);
57 if (c != 'h')
58 {
59 printf ("read %c, not %c\n", c, 'h');
60 return 1;
61 }
62 if (check (fp, 1) != 0)
63 return 1;
64
65 puts ("going to put back");
66 if (ungetc (' ', fp) == EOF)
67 {
68 puts ("ungetc failed");
69 return 1;
70 }
71 if (check (fp, 0) != 0)
72 return 1;
73
74 puts ("going to write again");
75 if (fputs ("world", fp) == EOF)
76 {
77 puts ("2nd fputs failed");
78 return 1;
79 }
80 if (check (fp, 5) != 0)
81 return 1;
82
83 puts ("going to rewind again");
84 rewind (fp);
85 if (check (fp, 0) != 0)
86 return 1;
87
88 if (fclose (fp) != 0)
89 {
90 puts ("fclose failed");
91 return 1;
92 }
93
94 return 0;
95 }
96
97 #define TEST_FUNCTION do_test ()
98 #include "../test-skeleton.c"