1 | // Licensed to the .NET Foundation under one or more agreements. |
2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | // See the LICENSE file in the project root for more information. |
4 | |
5 | /*============================================================================ |
6 | ** |
7 | ** Source: test1.c |
8 | ** |
9 | ** Purpose: Call fseek to move a file pointer to the start of a file, |
10 | ** a position offset from the start, a position offset from the |
11 | ** current position, and a position offset from the end of the |
12 | ** file. Check that the file pointer is at the correct position |
13 | ** after each seek. |
14 | ** |
15 | ** |
16 | **==========================================================================*/ |
17 | |
18 | #include <palsuite.h> |
19 | |
20 | const char filename[] = "testfile.txt" ; |
21 | |
22 | static BOOL Cleanup(HANDLE hFile) |
23 | { |
24 | BOOL result= TRUE; |
25 | |
26 | if (fclose((PAL_FILE*)hFile)) |
27 | { |
28 | Trace("fseek: ERROR -> Unable to close file \"%s\".\n" , |
29 | filename); |
30 | result= FALSE; |
31 | } |
32 | if (!DeleteFileA(filename)) |
33 | { |
34 | result= FALSE; |
35 | Trace("fseek: ERROR -> Unable to delete file \"%s\". " , |
36 | "GetLastError returned %u.\n" , |
37 | filename, |
38 | GetLastError()); |
39 | } |
40 | return result; |
41 | } |
42 | |
43 | int __cdecl main(int argc, char **argv) |
44 | { |
45 | char outBuf[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" ; |
46 | char inBuf[20]; |
47 | FILE * fp; |
48 | int size = ( sizeof(outBuf)/sizeof(char) ) - 1; |
49 | |
50 | if (PAL_Initialize(argc, argv)) |
51 | { |
52 | return FAIL; |
53 | } |
54 | |
55 | |
56 | /*create the file*/ |
57 | fp = fopen(filename, "w" ); |
58 | if (fp == NULL) |
59 | { |
60 | Fail("Unable to open a file for write.\n" ); |
61 | } |
62 | if(fprintf(fp, outBuf) != size) |
63 | { |
64 | Trace("Unable to write to %s.\n" , filename); |
65 | Cleanup(fp); |
66 | Fail("" ); |
67 | } |
68 | |
69 | if (fclose(fp) != 0) |
70 | { |
71 | Trace("Unable to close newly written file.\n" ); |
72 | if (!DeleteFileA(filename)) |
73 | { |
74 | Trace("fseek: ERROR -> Unable to delete file \"%s\". " , |
75 | "GetLastError returned %u.\n" , |
76 | filename, |
77 | GetLastError()); |
78 | } |
79 | Fail("" ); |
80 | } |
81 | |
82 | fp = fopen(filename, "r" ); |
83 | if (fp == NULL) |
84 | { |
85 | if (!DeleteFileA(filename)) |
86 | { |
87 | Trace("_putw: ERROR -> Unable to delete file \"%s\". " , |
88 | "GetLastError returned %u.\n" , |
89 | filename, |
90 | GetLastError()); |
91 | } |
92 | Fail("Unable to open a file for read.\n" ); |
93 | } |
94 | |
95 | /*seek to the start*/ |
96 | if (fseek(fp, 0, SEEK_SET) != 0) |
97 | { |
98 | Cleanup(fp); |
99 | Fail("fseek failed when seeking the start of a file.\n" ); |
100 | } |
101 | if (fgets(inBuf, 11, fp) != inBuf) |
102 | { |
103 | Cleanup(fp); |
104 | Fail("Unable to read from file after using fseek to move to the start.\n" ); |
105 | } |
106 | if (strncmp(inBuf, outBuf, 10) != 0) |
107 | { |
108 | Cleanup(fp); |
109 | Fail("fseek was asked to seek the start of a file," |
110 | "but didn't get there.\n" ); |
111 | } |
112 | |
113 | /*Seek with an offset from the start*/ |
114 | |
115 | if (fseek(fp, 10, SEEK_SET) != 0) |
116 | { |
117 | Cleanup(fp); |
118 | Fail("fseek failed when called with SEEK_SET and a positive offset.\n" ); |
119 | } |
120 | |
121 | if (fgets(inBuf, 6, fp) != inBuf) |
122 | { |
123 | Cleanup(fp); |
124 | Fail("fgets failed after feek was called with SEEK_SET" |
125 | "and a positive offset.\n" ); |
126 | } |
127 | |
128 | |
129 | if (strncmp(inBuf, "ABCDE" , 5) != 0) |
130 | { |
131 | Cleanup(fp); |
132 | Fail("fseek did not move to the correct position when passed SEEK_SET" |
133 | " and a positive offset.\n" ); |
134 | } |
135 | |
136 | /*now move backwards and read the same string*/ |
137 | if (fseek(fp, -5, SEEK_CUR) != 0) |
138 | { |
139 | Cleanup(fp); |
140 | Fail("fseek failed when passed SEEK_CUR and a negative offset.\n" ); |
141 | } |
142 | |
143 | if (fgets(inBuf, 6, fp) != inBuf) |
144 | { |
145 | Cleanup(fp); |
146 | Fail("fgets failed after fseek was called with SEEK_CUR and a " |
147 | "negative offset.\n" ); |
148 | } |
149 | |
150 | if (strncmp(inBuf, "ABCDE" , 5) != 0) |
151 | { |
152 | Cleanup(fp); |
153 | Fail("fseek did not move to the correct position when called with" |
154 | " SEEK_CUR and a negative offset.\n" ); |
155 | } |
156 | |
157 | /*Try seeking relative to the end of the file.*/ |
158 | if (fseek(fp, -10, SEEK_END) != 0) |
159 | { |
160 | Cleanup(fp); |
161 | Fail("fseek failed when called with SEEK_END and a negative" |
162 | " offset.\n" ); |
163 | } |
164 | if (fgets(inBuf, 2, fp) != inBuf) |
165 | { |
166 | Cleanup(fp); |
167 | Fail("fgets failed after fseek was called with SEEK_END and a " |
168 | "negative offset\n" ); |
169 | } |
170 | |
171 | if (strncmp(inBuf, "Q" , 1) != 0) |
172 | { |
173 | Cleanup(fp); |
174 | Fail("fseek did not move to the correct position when called with " |
175 | "SEEK_END and a negative offset.\n" ); |
176 | } |
177 | |
178 | |
179 | /*close the file*/ |
180 | if(!Cleanup(fp)) |
181 | { |
182 | Fail("" ); |
183 | } |
184 | |
185 | PAL_Terminate(); |
186 | return PASS; |
187 | } |
188 | |
189 | |
190 | |
191 | |
192 | |
193 | |
194 | |