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.
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.
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.
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
38 const char outstr
[] = "hello world!\n";
39 char strbuf
[sizeof outstr
];
45 tmpdir
= getenv ("TMPDIR");
46 if (tmpdir
== NULL
|| tmpdir
[0] == '\0')
49 asprintf (&fname
, "%s/tst-fseek.XXXXXX", tmpdir
);
51 error (EXIT_FAILURE
, errno
, "cannot generate name for temporary file");
53 /* Create a temporary file. */
56 error (EXIT_FAILURE
, errno
, "cannot open temporary file");
58 fp
= fdopen (fd
, "w+");
60 error (EXIT_FAILURE
, errno
, "cannot get FILE for temporary file");
62 setbuffer (fp
, strbuf
, sizeof (outstr
) -1);
64 if (fwrite (outstr
, sizeof (outstr
) - 1, 1, fp
) != 1)
66 printf ("%d: write error\n", __LINE__
);
71 /* The EOF flag must be reset. */
72 if (fgetc (fp
) != EOF
)
74 printf ("%d: managed to read at end of file\n", __LINE__
);
79 printf ("%d: EOF flag not set\n", __LINE__
);
82 if (fseek (fp
, 0, SEEK_CUR
) != 0)
84 printf ("%d: fseek(fp, 0, SEEK_CUR) failed\n", __LINE__
);
89 printf ("%d: fseek() didn't reset EOF flag\n", __LINE__
);
93 /* Do the same for fseeko(). */
94 if (fgetc (fp
) != EOF
)
96 printf ("%d: managed to read at end of file\n", __LINE__
);
101 printf ("%d: EOF flag not set\n", __LINE__
);
104 if (fseeko (fp
, 0, SEEK_CUR
) != 0)
106 printf ("%d: fseek(fp, 0, SEEK_CUR) failed\n", __LINE__
);
111 printf ("%d: fseek() didn't reset EOF flag\n", __LINE__
);
115 /* Go back to the beginning of the file: absolute. */
116 if (fseek (fp
, 0, SEEK_SET
) != 0)
118 printf ("%d: fseek(fp, 0, SEEK_SET) failed\n", __LINE__
);
121 else if (fflush (fp
) != 0)
123 printf ("%d: fflush() failed\n", __LINE__
);
126 else if (lseek (fd
, 0, SEEK_CUR
) != 0)
128 printf ("%d: lseek() returned different position\n", __LINE__
);
131 else if (fread (buf
, sizeof (outstr
) - 1, 1, fp
) != 1)
133 printf ("%d: fread() failed\n", __LINE__
);
136 else if (memcmp (buf
, outstr
, sizeof (outstr
) - 1) != 0)
138 printf ("%d: content after fseek(,,SEEK_SET) wrong\n", __LINE__
);
142 /* Now with fseeko. */
143 if (fseeko (fp
, 0, SEEK_SET
) != 0)
145 printf ("%d: fseeko(fp, 0, SEEK_SET) failed\n", __LINE__
);
148 else if (fflush (fp
) != 0)
150 printf ("%d: fflush() failed\n", __LINE__
);
153 else if (lseek (fd
, 0, SEEK_CUR
) != 0)
155 printf ("%d: lseek() returned different position\n", __LINE__
);
158 else if (fread (buf
, sizeof (outstr
) - 1, 1, fp
) != 1)
160 printf ("%d: fread() failed\n", __LINE__
);
163 else if (memcmp (buf
, outstr
, sizeof (outstr
) - 1) != 0)
165 printf ("%d: content after fseeko(,,SEEK_SET) wrong\n", __LINE__
);
169 /* Go back to the beginning of the file: relative. */
170 if (fseek (fp
, -(sizeof (outstr
) - 1), SEEK_CUR
) != 0)
172 printf ("%d: fseek(fp, 0, SEEK_SET) failed\n", __LINE__
);
175 else if (fflush (fp
) != 0)
177 printf ("%d: fflush() failed\n", __LINE__
);
180 else if (lseek (fd
, 0, SEEK_CUR
) != 0)
182 printf ("%d: lseek() returned different position\n", __LINE__
);
185 else if (fread (buf
, sizeof (outstr
) - 1, 1, fp
) != 1)
187 printf ("%d: fread() failed\n", __LINE__
);
190 else if (memcmp (buf
, outstr
, sizeof (outstr
) - 1) != 0)
192 printf ("%d: content after fseek(,,SEEK_SET) wrong\n", __LINE__
);
196 /* Now with fseeko. */
197 if (fseeko (fp
, -(sizeof (outstr
) - 1), SEEK_CUR
) != 0)
199 printf ("%d: fseeko(fp, 0, SEEK_SET) failed\n", __LINE__
);
202 else if (fflush (fp
) != 0)
204 printf ("%d: fflush() failed\n", __LINE__
);
207 else if (lseek (fd
, 0, SEEK_CUR
) != 0)
209 printf ("%d: lseek() returned different position\n", __LINE__
);
212 else if (fread (buf
, sizeof (outstr
) - 1, 1, fp
) != 1)
214 printf ("%d: fread() failed\n", __LINE__
);
217 else if (memcmp (buf
, outstr
, sizeof (outstr
) - 1) != 0)
219 printf ("%d: content after fseeko(,,SEEK_SET) wrong\n", __LINE__
);
223 /* Go back to the beginning of the file: from the end. */
224 if (fseek (fp
, -(sizeof (outstr
) - 1), SEEK_END
) != 0)
226 printf ("%d: fseek(fp, 0, SEEK_SET) failed\n", __LINE__
);
229 else if (fflush (fp
) != 0)
231 printf ("%d: fflush() failed\n", __LINE__
);
234 else if (lseek (fd
, 0, SEEK_CUR
) != 0)
236 printf ("%d: lseek() returned different position\n", __LINE__
);
239 else if (fread (buf
, sizeof (outstr
) - 1, 1, fp
) != 1)
241 printf ("%d: fread() failed\n", __LINE__
);
244 else if (memcmp (buf
, outstr
, sizeof (outstr
) - 1) != 0)
246 printf ("%d: content after fseek(,,SEEK_SET) wrong\n", __LINE__
);
250 /* Now with fseeko. */
251 if (fseeko (fp
, -(sizeof (outstr
) - 1), SEEK_END
) != 0)
253 printf ("%d: fseeko(fp, 0, SEEK_SET) failed\n", __LINE__
);
256 else if (fflush (fp
) != 0)
258 printf ("%d: fflush() failed\n", __LINE__
);
261 else if (lseek (fd
, 0, SEEK_CUR
) != 0)
263 printf ("%d: lseek() returned different position\n", __LINE__
);
266 else if (fread (buf
, sizeof (outstr
) - 1, 1, fp
) != 1)
268 printf ("%d: fread() failed\n", __LINE__
);
271 else if (memcmp (buf
, outstr
, sizeof (outstr
) - 1) != 0)
273 printf ("%d: content after fseeko(,,SEEK_SET) wrong\n", __LINE__
);
277 if (fwrite (outstr
, sizeof (outstr
) - 1, 1, fp
) != 1)
279 printf ("%d: write error 2\n", __LINE__
);
284 if (fwrite (outstr
, sizeof (outstr
) - 1, 1, fp
) != 1)
286 printf ("%d: write error 3\n", __LINE__
);
291 if (fwrite (outstr
, sizeof (outstr
) - 1, 1, fp
) != 1)
293 printf ("%d: write error 4\n", __LINE__
);
298 if (fwrite (outstr
, sizeof (outstr
) - 1, 1, fp
) != 1)
300 printf ("%d: write error 5\n", __LINE__
);
305 if (fputc ('1', fp
) == EOF
|| fputc ('2', fp
) == EOF
)
307 printf ("%d: cannot add characters at the end\n", __LINE__
);
312 /* Check the access time. */
313 if (fstat64 (fd
, &st1
) < 0)
315 printf ("%d: fstat64() before fseeko() failed\n\n", __LINE__
);
322 if (fseek (fp
, -(2 + 2 * (sizeof (outstr
) - 1)), SEEK_CUR
) != 0)
324 printf ("%d: fseek() after write characters failed\n", __LINE__
);
332 /* Make sure the timestamp actually can be different. */
336 if (fstat64 (fd
, &st2
) < 0)
338 printf ("%d: fstat64() after fseeko() failed\n\n", __LINE__
);
341 if (st1
.st_ctime
>= t
)
343 printf ("%d: st_ctime not updated\n", __LINE__
);
346 if (st1
.st_mtime
>= t
)
348 printf ("%d: st_mtime not updated\n", __LINE__
);
351 if (st1
.st_ctime
>= st2
.st_ctime
)
353 printf ("%d: st_ctime not changed\n", __LINE__
);
356 if (st1
.st_mtime
>= st2
.st_mtime
)
358 printf ("%d: st_mtime not changed\n", __LINE__
);
364 if (fread (buf
, 1, 2 + 2 * (sizeof (outstr
) - 1), fp
)
365 != 2 + 2 * (sizeof (outstr
) - 1))
367 printf ("%d: reading 2 records plus bits failed\n", __LINE__
);
370 else if (memcmp (buf
, outstr
, sizeof (outstr
) - 1) != 0
371 || memcmp (&buf
[sizeof (outstr
) - 1], outstr
,
372 sizeof (outstr
) - 1) != 0
373 || buf
[2 * (sizeof (outstr
) - 1)] != '1'
374 || buf
[2 * (sizeof (outstr
) - 1) + 1] != '2')
376 printf ("%d: reading records failed\n", __LINE__
);
379 else if (ungetc ('9', fp
) == EOF
)
381 printf ("%d: ungetc() failed\n", __LINE__
);
384 else if (fseek (fp
, -(2 + 2 * (sizeof (outstr
) - 1)), SEEK_END
) != 0)
386 printf ("%d: fseek after ungetc failed\n", __LINE__
);
389 else if (fread (buf
, 1, 2 + 2 * (sizeof (outstr
) - 1), fp
)
390 != 2 + 2 * (sizeof (outstr
) - 1))
392 printf ("%d: reading 2 records plus bits failed\n", __LINE__
);
395 else if (memcmp (buf
, outstr
, sizeof (outstr
) - 1) != 0
396 || memcmp (&buf
[sizeof (outstr
) - 1], outstr
,
397 sizeof (outstr
) - 1) != 0
398 || buf
[2 * (sizeof (outstr
) - 1)] != '1')
400 printf ("%d: reading records for the second time failed\n", __LINE__
);
403 else if (buf
[2 * (sizeof (outstr
) - 1) + 1] == '9')
405 printf ("%d: unget character not ignored\n", __LINE__
);
408 else if (buf
[2 * (sizeof (outstr
) - 1) + 1] != '2')
410 printf ("%d: unget somehow changed character\n", __LINE__
);
416 fp
= fopen (fname
, "r");
419 printf ("%d: fopen() failed\n\n", __LINE__
);
422 else if (fstat64 (fileno (fp
), &st1
) < 0)
424 printf ("%d: fstat64() before fseeko() failed\n\n", __LINE__
);
427 else if (fseeko (fp
, 0, SEEK_END
) != 0)
429 printf ("%d: fseeko(fp, 0, SEEK_END) failed\n", __LINE__
);
432 else if (ftello (fp
) != st1
.st_size
)
434 printf ("%d: fstat64 st_size %zd ftello %zd\n", __LINE__
,
435 (size_t) st1
.st_size
, (size_t) ftello (fp
));
439 printf ("%d: SEEK_END works\n", __LINE__
);
443 fp
= fopen (fname
, "r");
446 printf ("%d: fopen() failed\n\n", __LINE__
);
449 else if (fstat64 (fileno (fp
), &st1
) < 0)
451 printf ("%d: fstat64() before fgetc() failed\n\n", __LINE__
);
454 else if (fgetc (fp
) == EOF
)
456 printf ("%d: fgetc() before fseeko() failed\n\n", __LINE__
);
459 else if (fseeko (fp
, 0, SEEK_END
) != 0)
461 printf ("%d: fseeko(fp, 0, SEEK_END) failed\n", __LINE__
);
464 else if (ftello (fp
) != st1
.st_size
)
466 printf ("%d: fstat64 st_size %zd ftello %zd\n", __LINE__
,
467 (size_t) st1
.st_size
, (size_t) ftello (fp
));
471 printf ("%d: SEEK_END works\n", __LINE__
);