This source file includes following definitions.
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #include <config.h>
20
21
22
23 #define _GL_NO_LARGE_FILES
24 #include <stdio.h>
25
26 #include <string.h>
27
28 #include "macros.h"
29
30 #define TESTFILE "t-fpurge.tmp"
31
32 int
33 main (void)
34 {
35 int check_filepos;
36
37 for (check_filepos = 0; check_filepos <= 1; check_filepos++)
38 {
39 FILE *fp;
40
41
42 fp = fopen (TESTFILE, "w");
43 if (fp == NULL)
44 goto skip;
45 if (fwrite ("foobarsh", 1, 8, fp) < 8)
46 goto skip;
47 if (fclose (fp))
48 goto skip;
49
50
51
52
53 fp = fopen (TESTFILE, "r+");
54 if (fp == NULL)
55 goto skip;
56 if (fseek (fp, 3, SEEK_CUR))
57 goto skip;
58 if (fwrite ("g", 1, 1, fp) < 1)
59 goto skip;
60 if (fflush (fp))
61 goto skip;
62 if (fwrite ("bz", 1, 2, fp) < 2)
63 goto skip;
64
65 ASSERT (fpurge (fp) == 0);
66
67
68 if (check_filepos)
69 ASSERT (ftell (fp) == 4);
70 ASSERT (fclose (fp) == 0);
71
72
73 fp = fopen (TESTFILE, "r");
74 if (fp == NULL)
75 goto skip;
76
77
78 {
79 char buf[8];
80 if (fread (buf, 1, 7, fp) < 7)
81 goto skip;
82 ASSERT (memcmp (buf, "foogars", 7) == 0);
83 }
84
85 if (check_filepos)
86 ASSERT (ftell (fp) == 7);
87 ASSERT (fpurge (fp) == 0);
88
89
90 if (check_filepos)
91 ASSERT (ftell (fp) == 8);
92 ASSERT (getc (fp) == EOF);
93 ASSERT (fclose (fp) == 0);
94
95
96
97
98 fp = fopen (TESTFILE, "r+");
99 if (fp == NULL)
100 goto skip;
101 if (fseek (fp, -1, SEEK_END))
102 goto skip;
103 ASSERT (getc (fp) == 'h');
104 ASSERT (getc (fp) == EOF);
105 if (check_filepos)
106 ASSERT (ftell (fp) == 8);
107 ASSERT (fpurge (fp) == 0);
108 if (check_filepos)
109 ASSERT (ftell (fp) == 8);
110 ASSERT (putc ('!', fp) == '!');
111 if (check_filepos)
112 ASSERT (ftell (fp) == 9);
113 ASSERT (fclose (fp) == 0);
114 fp = fopen (TESTFILE, "r");
115 if (fp == NULL)
116 goto skip;
117 {
118 char buf[10];
119 ASSERT (fread (buf, 1, 10, fp) == 9);
120 ASSERT (memcmp (buf, "foogarsh!", 9) == 0);
121 }
122 ASSERT (fclose (fp) == 0);
123
124
125 }
126
127 remove (TESTFILE);
128 return 0;
129
130 skip:
131 fprintf (stderr, "Skipping test: prerequisite file operations failed.\n");
132 remove (TESTFILE);
133 return 77;
134 }