1 /* Tests of fseek and fseeko.
2 Copyright (C) 2000 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 Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 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 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
37 const char outstr
[] = "hello world!\n";
38 char strbuf
[sizeof outstr
];
44 tmpdir
= getenv ("TMPDIR");
45 if (tmpdir
== NULL
|| tmpdir
[0] == '\0')
48 asprintf (&fname
, "%s/tst-fseek.XXXXXX", tmpdir
);
50 error (EXIT_FAILURE
, errno
, "cannot generate name for temporary file");
52 /* Create a temporary file. */
55 error (EXIT_FAILURE
, errno
, "cannot open temporary file");
57 fp
= fdopen (fd
, "w+");
59 error (EXIT_FAILURE
, errno
, "cannot get FILE for temporary file");
61 setbuffer (fp
, strbuf
, sizeof (outstr
) -1);
63 if (fwrite (outstr
, sizeof (outstr
) - 1, 1, fp
) != 1)
70 /* The EOF flag must be reset. */
71 if (fgetc (fp
) != EOF
)
73 puts ("managed to read at end of file");
78 puts ("EOF flag not set");
81 if (fseek (fp
, 0, SEEK_CUR
) != 0)
83 puts ("fseek(fp, 0, SEEK_CUR) failed");
88 puts ("fseek() didn't reset EOF flag");
92 /* Do the same for fseeko(). */
94 if (fgetc (fp
) != EOF
)
96 puts ("managed to read at end of file");
101 puts ("EOF flag not set");
104 if (fseeko (fp
, 0, SEEK_CUR
) != 0)
106 puts ("fseek(fp, 0, SEEK_CUR) failed");
111 puts ("fseek() didn't reset EOF flag");
116 /* Go back to the beginning of the file: absolute. */
117 if (fseek (fp
, 0, SEEK_SET
) != 0)
119 puts ("fseek(fp, 0, SEEK_SET) failed");
122 else if (fflush (fp
) != 0)
124 puts ("fflush() failed");
127 else if (lseek (fd
, 0, SEEK_CUR
) != 0)
129 puts ("lseek() returned different position");
132 else if (fread (buf
, sizeof (outstr
) - 1, 1, fp
) != 1)
134 puts ("fread() failed");
137 else if (memcmp (buf
, outstr
, sizeof (outstr
) - 1) != 0)
139 puts ("content after fseek(,,SEEK_SET) wrong");
144 /* Now with fseeko. */
145 if (fseeko (fp
, 0, SEEK_SET
) != 0)
147 puts ("fseeko(fp, 0, SEEK_SET) failed");
150 else if (fflush (fp
) != 0)
152 puts ("fflush() failed");
155 else if (lseek (fd
, 0, SEEK_CUR
) != 0)
157 puts ("lseek() returned different position");
160 else if (fread (buf
, sizeof (outstr
) - 1, 1, fp
) != 1)
162 puts ("fread() failed");
165 else if (memcmp (buf
, outstr
, sizeof (outstr
) - 1) != 0)
167 puts ("content after fseeko(,,SEEK_SET) wrong");
172 /* Go back to the beginning of the file: relative. */
173 if (fseek (fp
, -(sizeof (outstr
) - 1), SEEK_CUR
) != 0)
175 puts ("fseek(fp, 0, SEEK_SET) failed");
178 else if (fflush (fp
) != 0)
180 puts ("fflush() failed");
183 else if (lseek (fd
, 0, SEEK_CUR
) != 0)
185 puts ("lseek() returned different position");
188 else if (fread (buf
, sizeof (outstr
) - 1, 1, fp
) != 1)
190 puts ("fread() failed");
193 else if (memcmp (buf
, outstr
, sizeof (outstr
) - 1) != 0)
195 puts ("content after fseek(,,SEEK_SET) wrong");
200 /* Now with fseeko. */
201 if (fseeko (fp
, -(sizeof (outstr
) - 1), SEEK_CUR
) != 0)
203 puts ("fseeko(fp, 0, SEEK_SET) failed");
206 else if (fflush (fp
) != 0)
208 puts ("fflush() failed");
211 else if (lseek (fd
, 0, SEEK_CUR
) != 0)
213 puts ("lseek() returned different position");
216 else if (fread (buf
, sizeof (outstr
) - 1, 1, fp
) != 1)
218 puts ("fread() failed");
221 else if (memcmp (buf
, outstr
, sizeof (outstr
) - 1) != 0)
223 puts ("content after fseeko(,,SEEK_SET) wrong");
228 /* Go back to the beginning of the file: from the end. */
229 if (fseek (fp
, -(sizeof (outstr
) - 1), SEEK_END
) != 0)
231 puts ("fseek(fp, 0, SEEK_SET) failed");
234 else if (fflush (fp
) != 0)
236 puts ("fflush() failed");
239 else if (lseek (fd
, 0, SEEK_CUR
) != 0)
241 puts ("lseek() returned different position");
244 else if (fread (buf
, sizeof (outstr
) - 1, 1, fp
) != 1)
246 puts ("fread() failed");
249 else if (memcmp (buf
, outstr
, sizeof (outstr
) - 1) != 0)
251 puts ("content after fseek(,,SEEK_SET) wrong");
256 /* Now with fseeko. */
257 if (fseeko (fp
, -(sizeof (outstr
) - 1), SEEK_END
) != 0)
259 puts ("fseeko(fp, 0, SEEK_SET) failed");
262 else if (fflush (fp
) != 0)
264 puts ("fflush() failed");
267 else if (lseek (fd
, 0, SEEK_CUR
) != 0)
269 puts ("lseek() returned different position");
272 else if (fread (buf
, sizeof (outstr
) - 1, 1, fp
) != 1)
274 puts ("fread() failed");
277 else if (memcmp (buf
, outstr
, sizeof (outstr
) - 1) != 0)
279 puts ("content after fseeko(,,SEEK_SET) wrong");
284 if (fwrite (outstr
, sizeof (outstr
) - 1, 1, fp
) != 1)
286 puts ("write error 2");
291 if (fwrite (outstr
, sizeof (outstr
) - 1, 1, fp
) != 1)
293 puts ("write error 3");
298 if (fwrite (outstr
, sizeof (outstr
) - 1, 1, fp
) != 1)
300 puts ("write error 4");
305 if (fwrite (outstr
, sizeof (outstr
) - 1, 1, fp
) != 1)
307 puts ("write error 5");
312 if (fputc ('1', fp
) == EOF
|| fputc ('2', fp
) == EOF
)
314 puts ("cannot add characters at the end");
319 /* Check the access time. */
320 if (fstat64 (fd
, &st1
) < 0)
322 puts ("fstat64() before fseeko() failed\n");
329 if (fseek (fp
, -(2 + 2 * (sizeof (outstr
) - 1)), SEEK_CUR
) != 0)
331 puts ("fseek() after write characters failed");
339 /* Make sure the timestamp actually can be different. */
343 if (fstat64 (fd
, &st2
) < 0)
345 puts ("fstat64() after fseeko() failed\n");
348 if (st1
.st_ctime
>= t
)
350 puts ("st_ctime not updated");
353 if (st1
.st_mtime
>= t
)
355 puts ("st_mtime not updated");
358 if (st1
.st_ctime
>= st2
.st_ctime
)
360 puts ("st_ctime not changed");
363 if (st1
.st_mtime
>= st2
.st_mtime
)
365 puts ("st_mtime not changed");
371 if (fread (buf
, 1, 2 + 2 * (sizeof (outstr
) - 1), fp
)
372 != 2 + 2 * (sizeof (outstr
) - 1))
374 puts ("reading 2 records plus bits failed");
377 else if (memcmp (buf
, outstr
, sizeof (outstr
) - 1) != 0
378 || memcmp (&buf
[sizeof (outstr
) - 1], outstr
,
379 sizeof (outstr
) - 1) != 0
380 || buf
[2 * (sizeof (outstr
) - 1)] != '1'
381 || buf
[2 * (sizeof (outstr
) - 1) + 1] != '2')
383 puts ("reading records failed");
386 else if (ungetc ('9', fp
) == EOF
)
388 puts ("ungetc() failed");
391 else if (fseek (fp
, -(2 + 2 * (sizeof (outstr
) - 1)), SEEK_END
) != 0)
393 puts ("fseek after ungetc failed");
396 else if (fread (buf
, 1, 2 + 2 * (sizeof (outstr
) - 1), fp
)
397 != 2 + 2 * (sizeof (outstr
) - 1))
399 puts ("reading 2 records plus bits failed");
402 else if (memcmp (buf
, outstr
, sizeof (outstr
) - 1) != 0
403 || memcmp (&buf
[sizeof (outstr
) - 1], outstr
,
404 sizeof (outstr
) - 1) != 0
405 || buf
[2 * (sizeof (outstr
) - 1)] != '1')
407 puts ("reading records for the second time failed");
410 else if (buf
[2 * (sizeof (outstr
) - 1) + 1] == '9')
412 puts ("unget character not ignored");
415 else if (buf
[2 * (sizeof (outstr
) - 1) + 1] != '2')
417 puts ("unget somehow changed character");