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 (stream: fp)) |
10 | { |
11 | puts (s: "feof !" ); |
12 | result = 1; |
13 | } |
14 | if (ferror (stream: fp)) |
15 | { |
16 | puts (s: "ferror !" ); |
17 | result = 1; |
18 | } |
19 | if (ftello (stream: fp) != o) |
20 | { |
21 | printf (format: "position = %lu, not %lu\n" , (unsigned long int) ftello (stream: 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 (s: "tmpfile failed" ); |
36 | return 1; |
37 | } |
38 | if (check (fp, o: 0) != 0) |
39 | return 1; |
40 | |
41 | puts (s: "going to write" ); |
42 | if (fputs ("hello" , fp) == EOF) |
43 | { |
44 | puts (s: "fputs failed" ); |
45 | return 1; |
46 | } |
47 | if (check (fp, o: 5) != 0) |
48 | return 1; |
49 | |
50 | puts (s: "going to rewind" ); |
51 | rewind (fp); |
52 | if (check (fp, o: 0) != 0) |
53 | return 1; |
54 | |
55 | puts (s: "going to read char" ); |
56 | int c = fgetc (stream: fp); |
57 | if (c != 'h') |
58 | { |
59 | printf (format: "read %c, not %c\n" , c, 'h'); |
60 | return 1; |
61 | } |
62 | if (check (fp, o: 1) != 0) |
63 | return 1; |
64 | |
65 | puts (s: "going to put back" ); |
66 | if (ungetc (' ', fp) == EOF) |
67 | { |
68 | puts (s: "ungetc failed" ); |
69 | return 1; |
70 | } |
71 | if (check (fp, o: 0) != 0) |
72 | return 1; |
73 | |
74 | puts (s: "going to write again" ); |
75 | if (fputs ("world" , fp) == EOF) |
76 | { |
77 | puts (s: "2nd fputs failed" ); |
78 | return 1; |
79 | } |
80 | if (check (fp, o: 5) != 0) |
81 | return 1; |
82 | |
83 | puts (s: "going to rewind again" ); |
84 | rewind (fp); |
85 | if (check (fp, o: 0) != 0) |
86 | return 1; |
87 | |
88 | if (fclose (fp) != 0) |
89 | { |
90 | puts (s: "fclose failed" ); |
91 | return 1; |
92 | } |
93 | |
94 | return 0; |
95 | } |
96 | |
97 | #define TEST_FUNCTION do_test () |
98 | #include "../test-skeleton.c" |
99 | |