source: branches/libc-0.6/src/libctests/glibc/stdio-common/tst-fseek.c

Last change on this file was 2089, checked in by bird, 20 years ago

testcase adjustments.

  • Property cvs2svn:cvs-rev set to 1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 12.7 KB
Line 
1/* Tests of fseek and fseeko.
2 Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@redhat.com>, 2000.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
20
21#include <error.h>
22#include <errno.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <unistd.h>
27#include <time.h>
28#include <sys/stat.h>
29
30#ifndef HAVE_64BIT_FILEIO_TYPES
31#define stat64 stat
32#define fstat64 fstat
33#endif
34
35int
36main (void)
37{
38 const char *tmpdir;
39 char *fname;
40 int fd;
41 FILE *fp;
42 const char outstr[] = "hello world!\n";
43 char strbuf[sizeof outstr];
44 char buf[200];
45 struct stat64 st1;
46 struct stat64 st2;
47 int result = 0;
48#ifdef __EMX__
49 extern int _fmode_bin;
50 _fmode_bin = 0;
51#endif
52
53 tmpdir = getenv ("TMPDIR");
54 if (tmpdir == NULL || tmpdir[0] == '\0')
55 tmpdir = "/tmp";
56
57#ifdef HAVE_ASPRINTF
58 asprintf (&fname, "%s/tst-fseek.XXXXXX", tmpdir);
59#else
60 sprintf (buf, "%s/tst-fseek.XXXXXX", tmpdir);
61 fname = strdup(buf);
62#endif
63 if (fname == NULL)
64 error (EXIT_FAILURE, errno, "cannot generate name for temporary file");
65
66 /* Create a temporary file. */
67 fd = mkstemp (fname);
68 if (fd == -1)
69 error (EXIT_FAILURE, errno, "cannot open temporary file");
70
71 fp = fdopen (fd, "w+b");
72 if (fp == NULL)
73 error (EXIT_FAILURE, errno, "cannot get FILE for temporary file");
74
75 setbuffer (fp, strbuf, sizeof (outstr) -1);
76
77 if (fwrite (outstr, sizeof (outstr) - 1, 1, fp) != 1)
78 {
79 printf ("%d: write error\n", __LINE__);
80 result = 1;
81 goto out;
82 }
83
84 /* The EOF flag must be reset. */
85 if (fgetc (fp) != EOF)
86 {
87 printf ("%d: managed to read at end of file\n", __LINE__);
88 result = 1;
89 }
90 else if (! feof (fp))
91 {
92 printf ("%d: EOF flag not set\n", __LINE__);
93 result = 1;
94 }
95 if (fseek (fp, 0, SEEK_CUR) != 0)
96 {
97 printf ("%d: fseek(fp, 0, SEEK_CUR) failed\n", __LINE__);
98 result = 1;
99 }
100 else if (feof (fp))
101 {
102 printf ("%d: fseek() didn't reset EOF flag\n", __LINE__);
103 result = 1;
104 }
105
106 /* Do the same for fseeko(). */
107 if (fgetc (fp) != EOF)
108 {
109 printf ("%d: managed to read at end of file\n", __LINE__);
110 result = 1;
111 }
112 else if (! feof (fp))
113 {
114 printf ("%d: EOF flag not set\n", __LINE__);
115 result = 1;
116 }
117 if (fseeko (fp, 0, SEEK_CUR) != 0)
118 {
119 printf ("%d: fseek(fp, 0, SEEK_CUR) failed\n", __LINE__);
120 result = 1;
121 }
122 else if (feof (fp))
123 {
124 printf ("%d: fseek() didn't reset EOF flag\n", __LINE__);
125 result = 1;
126 }
127
128 /* Go back to the beginning of the file: absolute. */
129 if (fseek (fp, 0, SEEK_SET) != 0)
130 {
131 printf ("%d: fseek(fp, 0, SEEK_SET) failed\n", __LINE__);
132 result = 1;
133 }
134 else if (fflush (fp) != 0)
135 {
136 printf ("%d: fflush() failed\n", __LINE__);
137 result = 1;
138 }
139 else if (lseek (fd, 0, SEEK_CUR) != 0)
140 {
141 printf ("%d: lseek() returned different position\n", __LINE__);
142 result = 1;
143 }
144 else if (fread (buf, sizeof (outstr) - 1, 1, fp) != 1)
145 {
146 printf ("%d: fread() failed\n", __LINE__);
147 result = 1;
148 }
149 else if (memcmp (buf, outstr, sizeof (outstr) - 1) != 0)
150 {
151 printf ("%d: content after fseek(,,SEEK_SET) wrong\n", __LINE__);
152 result = 1;
153 }
154
155 /* Now with fseeko. */
156 if (fseeko (fp, 0, SEEK_SET) != 0)
157 {
158 printf ("%d: fseeko(fp, 0, SEEK_SET) failed\n", __LINE__);
159 result = 1;
160 }
161 else if (fflush (fp) != 0)
162 {
163 printf ("%d: fflush() failed\n", __LINE__);
164 result = 1;
165 }
166 else if (lseek (fd, 0, SEEK_CUR) != 0)
167 {
168 printf ("%d: lseek() returned different position\n", __LINE__);
169 result = 1;
170 }
171 else if (fread (buf, sizeof (outstr) - 1, 1, fp) != 1)
172 {
173 printf ("%d: fread() failed\n", __LINE__);
174 result = 1;
175 }
176 else if (memcmp (buf, outstr, sizeof (outstr) - 1) != 0)
177 {
178 printf ("%d: content after fseeko(,,SEEK_SET) wrong\n", __LINE__);
179 result = 1;
180 }
181
182 /* Go back to the beginning of the file: relative. */
183 if (fseek (fp, -((int) sizeof (outstr) - 1), SEEK_CUR) != 0)
184 {
185 printf ("%d: fseek(fp, 0, SEEK_SET) failed\n", __LINE__);
186 result = 1;
187 }
188 else if (fflush (fp) != 0)
189 {
190 printf ("%d: fflush() failed\n", __LINE__);
191 result = 1;
192 }
193 else if (lseek (fd, 0, SEEK_CUR) != 0)
194 {
195 printf ("%d: lseek() returned different position\n", __LINE__);
196 result = 1;
197 }
198 else if (fread (buf, sizeof (outstr) - 1, 1, fp) != 1)
199 {
200 printf ("%d: fread() failed\n", __LINE__);
201 result = 1;
202 }
203 else if (memcmp (buf, outstr, sizeof (outstr) - 1) != 0)
204 {
205 printf ("%d: content after fseek(,,SEEK_SET) wrong\n", __LINE__);
206 result = 1;
207 }
208
209 /* Now with fseeko. */
210 if (fseeko (fp, -((int) sizeof (outstr) - 1), SEEK_CUR) != 0)
211 {
212 printf ("%d: fseeko(fp, 0, SEEK_SET) failed\n", __LINE__);
213 result = 1;
214 }
215 else if (fflush (fp) != 0)
216 {
217 printf ("%d: fflush() failed\n", __LINE__);
218 result = 1;
219 }
220 else if (lseek (fd, 0, SEEK_CUR) != 0)
221 {
222 printf ("%d: lseek() returned different position\n", __LINE__);
223 result = 1;
224 }
225 else if (fread (buf, sizeof (outstr) - 1, 1, fp) != 1)
226 {
227 printf ("%d: fread() failed\n", __LINE__);
228 result = 1;
229 }
230 else if (memcmp (buf, outstr, sizeof (outstr) - 1) != 0)
231 {
232 printf ("%d: content after fseeko(,,SEEK_SET) wrong\n", __LINE__);
233 result = 1;
234 }
235
236 /* Go back to the beginning of the file: from the end. */
237 if (fseek (fp, -((int) sizeof (outstr) - 1), SEEK_END) != 0)
238 {
239 printf ("%d: fseek(fp, 0, SEEK_SET) failed\n", __LINE__);
240 result = 1;
241 }
242 else if (fflush (fp) != 0)
243 {
244 printf ("%d: fflush() failed\n", __LINE__);
245 result = 1;
246 }
247 else if (lseek (fd, 0, SEEK_CUR) != 0)
248 {
249 printf ("%d: lseek() returned different position\n", __LINE__);
250 result = 1;
251 }
252 else if (fread (buf, sizeof (outstr) - 1, 1, fp) != 1)
253 {
254 printf ("%d: fread() failed\n", __LINE__);
255 result = 1;
256 }
257 else if (memcmp (buf, outstr, sizeof (outstr) - 1) != 0)
258 {
259 printf ("%d: content after fseek(,,SEEK_SET) wrong\n", __LINE__);
260 result = 1;
261 }
262
263 /* Now with fseeko. */
264 if (fseeko (fp, -((int) sizeof (outstr) - 1), SEEK_END) != 0)
265 {
266 printf ("%d: fseeko(fp, 0, SEEK_SET) failed\n", __LINE__);
267 result = 1;
268 }
269 else if (fflush (fp) != 0)
270 {
271 printf ("%d: fflush() failed\n", __LINE__);
272 result = 1;
273 }
274 else if (lseek (fd, 0, SEEK_CUR) != 0)
275 {
276 printf ("%d: lseek() returned different position\n", __LINE__);
277 result = 1;
278 }
279 else if (fread (buf, sizeof (outstr) - 1, 1, fp) != 1)
280 {
281 printf ("%d: fread() failed\n", __LINE__);
282 result = 1;
283 }
284 else if (memcmp (buf, outstr, sizeof (outstr) - 1) != 0)
285 {
286 printf ("%d: content after fseeko(,,SEEK_SET) wrong\n", __LINE__);
287 result = 1;
288 }
289
290 if (fwrite (outstr, sizeof (outstr) - 1, 1, fp) != 1)
291 {
292 printf ("%d: write error 2\n", __LINE__);
293 result = 1;
294 goto out;
295 }
296
297 if (fwrite (outstr, sizeof (outstr) - 1, 1, fp) != 1)
298 {
299 printf ("%d: write error 3\n", __LINE__);
300 result = 1;
301 goto out;
302 }
303
304 if (fwrite (outstr, sizeof (outstr) - 1, 1, fp) != 1)
305 {
306 printf ("%d: write error 4\n", __LINE__);
307 result = 1;
308 goto out;
309 }
310
311 if (fwrite (outstr, sizeof (outstr) - 1, 1, fp) != 1)
312 {
313 printf ("%d: write error 5\n", __LINE__);
314 result = 1;
315 goto out;
316 }
317
318 if (fputc ('1', fp) == EOF || fputc ('2', fp) == EOF)
319 {
320 printf ("%d: cannot add characters at the end\n", __LINE__);
321 result = 1;
322 goto out;
323 }
324
325 /* Check the access time. */
326 if (fstat64 (fd, &st1) < 0)
327 {
328 printf ("%d: fstat64() before fseeko() failed\n\n", __LINE__);
329 result = 1;
330 }
331 else
332 {
333 sleep (1);
334
335 if (fseek (fp, -(2 + 2 * (sizeof (outstr) - 1)), SEEK_CUR) != 0)
336 {
337 printf ("%d: fseek() after write characters failed\n", __LINE__);
338 result = 1;
339 goto out;
340 }
341 else
342 {
343
344 time_t t;
345 /* Make sure the timestamp actually can be different. */
346 sleep (1);
347 t = time (NULL);
348
349 if (fstat64 (fd, &st2) < 0)
350 {
351 printf ("%d: fstat64() after fseeko() failed\n\n", __LINE__);
352 result = 1;
353 }
354 if (st1.st_ctime >= t)
355 {
356 printf ("%d: st_ctime not updated\n", __LINE__);
357 result = 1;
358 }
359 if (st1.st_mtime >= t)
360 {
361 printf ("%d: st_mtime not updated\n", __LINE__);
362 result = 1;
363 }
364#ifndef __EMX__ /* TODO: figure out what the hell they are getting at here. There's not writing or
365 anything which should case any of those two to be changed... */
366 if (st1.st_ctime >= st2.st_ctime)
367 {
368 printf ("%d: st_ctime not changed\n", __LINE__);
369 result = 1;
370 }
371 if (st1.st_mtime >= st2.st_mtime)
372 {
373 printf ("%d: st_mtime not changed\n", __LINE__);
374 result = 1;
375 }
376#endif
377 }
378 }
379
380 if (fread (buf, 1, 2 + 2 * (sizeof (outstr) - 1), fp)
381 != 2 + 2 * (sizeof (outstr) - 1))
382 {
383 printf ("%d: reading 2 records plus bits failed\n", __LINE__);
384 result = 1;
385 }
386 else if (memcmp (buf, outstr, sizeof (outstr) - 1) != 0
387 || memcmp (&buf[sizeof (outstr) - 1], outstr,
388 sizeof (outstr) - 1) != 0
389 || buf[2 * (sizeof (outstr) - 1)] != '1'
390 || buf[2 * (sizeof (outstr) - 1) + 1] != '2')
391 {
392 printf ("%d: reading records failed\n", __LINE__);
393 result = 1;
394 }
395 else if (ungetc ('9', fp) == EOF)
396 {
397 printf ("%d: ungetc() failed\n", __LINE__);
398 result = 1;
399 }
400 else if (fseek (fp, -(2 + 2 * (sizeof (outstr) - 1)), SEEK_END) != 0)
401 {
402 printf ("%d: fseek after ungetc failed\n", __LINE__);
403 result = 1;
404 }
405 else if (fread (buf, 1, 2 + 2 * (sizeof (outstr) - 1), fp)
406 != 2 + 2 * (sizeof (outstr) - 1))
407 {
408 printf ("%d: reading 2 records plus bits failed\n", __LINE__);
409 result = 1;
410 }
411 else if (memcmp (buf, outstr, sizeof (outstr) - 1) != 0
412 || memcmp (&buf[sizeof (outstr) - 1], outstr,
413 sizeof (outstr) - 1) != 0
414 || buf[2 * (sizeof (outstr) - 1)] != '1')
415 {
416 printf ("%d: reading records for the second time failed\n", __LINE__);
417 result = 1;
418 }
419 else if (buf[2 * (sizeof (outstr) - 1) + 1] == '9')
420 {
421 printf ("%d: unget character not ignored\n", __LINE__);
422 result = 1;
423 }
424 else if (buf[2 * (sizeof (outstr) - 1) + 1] != '2')
425 {
426 printf ("%d: unget somehow changed character\n", __LINE__);
427 result = 1;
428 }
429
430 fclose (fp);
431
432 fp = fopen (fname, "r");
433 if (fp == NULL)
434 {
435 printf ("%d: fopen() failed\n\n", __LINE__);
436 result = 1;
437 }
438 else if (fstat64 (fileno (fp), &st1) < 0)
439 {
440 printf ("%d: fstat64() before fseeko() failed\n\n", __LINE__);
441 result = 1;
442 }
443 else if (fseeko (fp, 0, SEEK_END) != 0)
444 {
445 printf ("%d: fseeko(fp, 0, SEEK_END) failed\n", __LINE__);
446 result = 1;
447 }
448 else if (ftello (fp) != st1.st_size)
449 {
450 printf ("%d: fstat64 st_size %zd ftello %zd\n", __LINE__,
451 (size_t) st1.st_size, (size_t) ftello (fp));
452 result = 1;
453 }
454 else
455 printf ("%d: SEEK_END works\n", __LINE__);
456 if (fp != NULL)
457 fclose (fp);
458
459 fp = fopen (fname, "r");
460 if (fp == NULL)
461 {
462 printf ("%d: fopen() failed\n\n", __LINE__);
463 result = 1;
464 }
465 else if (fstat64 (fileno (fp), &st1) < 0)
466 {
467 printf ("%d: fstat64() before fgetc() failed\n\n", __LINE__);
468 result = 1;
469 }
470 else if (fgetc (fp) == EOF)
471 {
472 printf ("%d: fgetc() before fseeko() failed\n\n", __LINE__);
473 result = 1;
474 }
475 else if (fseeko (fp, 0, SEEK_END) != 0)
476 {
477 printf ("%d: fseeko(fp, 0, SEEK_END) failed\n", __LINE__);
478 result = 1;
479 }
480 else if (ftello (fp) != st1.st_size)
481 {
482 printf ("%d: fstat64 st_size %zd ftello %zd\n", __LINE__,
483 (size_t) st1.st_size, (size_t) ftello (fp));
484 result = 1;
485 }
486 else
487 printf ("%d: SEEK_END works\n", __LINE__);
488 if (fp != NULL)
489 fclose (fp);
490
491 out:
492 unlink (fname);
493
494 return result;
495}
Note: See TracBrowser for help on using the repository browser.