1 /* Tests of fseek and fseeko.
2 Copyright (C) 2000,01 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)
71 /* The EOF flag must be reset. */
72 if (fgetc (fp
) != EOF
)
74 puts ("managed to read at end of file");
79 puts ("EOF flag not set");
82 if (fseek (fp
, 0, SEEK_CUR
) != 0)
84 puts ("fseek(fp, 0, SEEK_CUR) failed");
89 puts ("fseek() didn't reset EOF flag");
93 /* Do the same for fseeko(). */
95 if (fgetc (fp
) != EOF
)
97 puts ("managed to read at end of file");
100 else if (! feof (fp
))
102 puts ("EOF flag not set");
105 if (fseeko (fp
, 0, SEEK_CUR
) != 0)
107 puts ("fseek(fp, 0, SEEK_CUR) failed");
112 puts ("fseek() didn't reset EOF flag");
117 /* Go back to the beginning of the file: absolute. */
118 if (fseek (fp
, 0, SEEK_SET
) != 0)
120 puts ("fseek(fp, 0, SEEK_SET) failed");
123 else if (fflush (fp
) != 0)
125 puts ("fflush() failed");
128 else if (lseek (fd
, 0, SEEK_CUR
) != 0)
130 puts ("lseek() returned different position");
133 else if (fread (buf
, sizeof (outstr
) - 1, 1, fp
) != 1)
135 puts ("fread() failed");
138 else if (memcmp (buf
, outstr
, sizeof (outstr
) - 1) != 0)
140 puts ("content after fseek(,,SEEK_SET) wrong");
145 /* Now with fseeko. */
146 if (fseeko (fp
, 0, SEEK_SET
) != 0)
148 puts ("fseeko(fp, 0, SEEK_SET) failed");
151 else if (fflush (fp
) != 0)
153 puts ("fflush() failed");
156 else if (lseek (fd
, 0, SEEK_CUR
) != 0)
158 puts ("lseek() returned different position");
161 else if (fread (buf
, sizeof (outstr
) - 1, 1, fp
) != 1)
163 puts ("fread() failed");
166 else if (memcmp (buf
, outstr
, sizeof (outstr
) - 1) != 0)
168 puts ("content after fseeko(,,SEEK_SET) wrong");
173 /* Go back to the beginning of the file: relative. */
174 if (fseek (fp
, -(sizeof (outstr
) - 1), SEEK_CUR
) != 0)
176 puts ("fseek(fp, 0, SEEK_SET) failed");
179 else if (fflush (fp
) != 0)
181 puts ("fflush() failed");
184 else if (lseek (fd
, 0, SEEK_CUR
) != 0)
186 puts ("lseek() returned different position");
189 else if (fread (buf
, sizeof (outstr
) - 1, 1, fp
) != 1)
191 puts ("fread() failed");
194 else if (memcmp (buf
, outstr
, sizeof (outstr
) - 1) != 0)
196 puts ("content after fseek(,,SEEK_SET) wrong");
201 /* Now with fseeko. */
202 if (fseeko (fp
, -(sizeof (outstr
) - 1), SEEK_CUR
) != 0)
204 puts ("fseeko(fp, 0, SEEK_SET) failed");
207 else if (fflush (fp
) != 0)
209 puts ("fflush() failed");
212 else if (lseek (fd
, 0, SEEK_CUR
) != 0)
214 puts ("lseek() returned different position");
217 else if (fread (buf
, sizeof (outstr
) - 1, 1, fp
) != 1)
219 puts ("fread() failed");
222 else if (memcmp (buf
, outstr
, sizeof (outstr
) - 1) != 0)
224 puts ("content after fseeko(,,SEEK_SET) wrong");
229 /* Go back to the beginning of the file: from the end. */
230 if (fseek (fp
, -(sizeof (outstr
) - 1), SEEK_END
) != 0)
232 puts ("fseek(fp, 0, SEEK_SET) failed");
235 else if (fflush (fp
) != 0)
237 puts ("fflush() failed");
240 else if (lseek (fd
, 0, SEEK_CUR
) != 0)
242 puts ("lseek() returned different position");
245 else if (fread (buf
, sizeof (outstr
) - 1, 1, fp
) != 1)
247 puts ("fread() failed");
250 else if (memcmp (buf
, outstr
, sizeof (outstr
) - 1) != 0)
252 puts ("content after fseek(,,SEEK_SET) wrong");
257 /* Now with fseeko. */
258 if (fseeko (fp
, -(sizeof (outstr
) - 1), SEEK_END
) != 0)
260 puts ("fseeko(fp, 0, SEEK_SET) failed");
263 else if (fflush (fp
) != 0)
265 puts ("fflush() failed");
268 else if (lseek (fd
, 0, SEEK_CUR
) != 0)
270 puts ("lseek() returned different position");
273 else if (fread (buf
, sizeof (outstr
) - 1, 1, fp
) != 1)
275 puts ("fread() failed");
278 else if (memcmp (buf
, outstr
, sizeof (outstr
) - 1) != 0)
280 puts ("content after fseeko(,,SEEK_SET) wrong");
285 if (fwrite (outstr
, sizeof (outstr
) - 1, 1, fp
) != 1)
287 puts ("write error 2");
292 if (fwrite (outstr
, sizeof (outstr
) - 1, 1, fp
) != 1)
294 puts ("write error 3");
299 if (fwrite (outstr
, sizeof (outstr
) - 1, 1, fp
) != 1)
301 puts ("write error 4");
306 if (fwrite (outstr
, sizeof (outstr
) - 1, 1, fp
) != 1)
308 puts ("write error 5");
313 if (fputc ('1', fp
) == EOF
|| fputc ('2', fp
) == EOF
)
315 puts ("cannot add characters at the end");
320 /* Check the access time. */
321 if (fstat64 (fd
, &st1
) < 0)
323 puts ("fstat64() before fseeko() failed\n");
330 if (fseek (fp
, -(2 + 2 * (sizeof (outstr
) - 1)), SEEK_CUR
) != 0)
332 puts ("fseek() after write characters failed");
340 /* Make sure the timestamp actually can be different. */
344 if (fstat64 (fd
, &st2
) < 0)
346 puts ("fstat64() after fseeko() failed\n");
349 if (st1
.st_ctime
>= t
)
351 puts ("st_ctime not updated");
354 if (st1
.st_mtime
>= t
)
356 puts ("st_mtime not updated");
359 if (st1
.st_ctime
>= st2
.st_ctime
)
361 puts ("st_ctime not changed");
364 if (st1
.st_mtime
>= st2
.st_mtime
)
366 puts ("st_mtime not changed");
372 if (fread (buf
, 1, 2 + 2 * (sizeof (outstr
) - 1), fp
)
373 != 2 + 2 * (sizeof (outstr
) - 1))
375 puts ("reading 2 records plus bits failed");
378 else if (memcmp (buf
, outstr
, sizeof (outstr
) - 1) != 0
379 || memcmp (&buf
[sizeof (outstr
) - 1], outstr
,
380 sizeof (outstr
) - 1) != 0
381 || buf
[2 * (sizeof (outstr
) - 1)] != '1'
382 || buf
[2 * (sizeof (outstr
) - 1) + 1] != '2')
384 puts ("reading records failed");
387 else if (ungetc ('9', fp
) == EOF
)
389 puts ("ungetc() failed");
392 else if (fseek (fp
, -(2 + 2 * (sizeof (outstr
) - 1)), SEEK_END
) != 0)
394 puts ("fseek after ungetc failed");
397 else if (fread (buf
, 1, 2 + 2 * (sizeof (outstr
) - 1), fp
)
398 != 2 + 2 * (sizeof (outstr
) - 1))
400 puts ("reading 2 records plus bits failed");
403 else if (memcmp (buf
, outstr
, sizeof (outstr
) - 1) != 0
404 || memcmp (&buf
[sizeof (outstr
) - 1], outstr
,
405 sizeof (outstr
) - 1) != 0
406 || buf
[2 * (sizeof (outstr
) - 1)] != '1')
408 puts ("reading records for the second time failed");
411 else if (buf
[2 * (sizeof (outstr
) - 1) + 1] == '9')
413 puts ("unget character not ignored");
416 else if (buf
[2 * (sizeof (outstr
) - 1) + 1] != '2')
418 puts ("unget somehow changed character");