1 /* Tests of fseek and fseeko.
2 Copyright (C) 2000-2018 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, see
18 <http://www.gnu.org/licenses/>. */
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)
65 printf ("%d: write error\n", __LINE__
);
70 /* The EOF flag must be reset. */
71 if (fgetc (fp
) != EOF
)
73 printf ("%d: managed to read at end of file\n", __LINE__
);
78 printf ("%d: EOF flag not set\n", __LINE__
);
81 if (fseek (fp
, 0, SEEK_CUR
) != 0)
83 printf ("%d: fseek(fp, 0, SEEK_CUR) failed\n", __LINE__
);
88 printf ("%d: fseek() didn't reset EOF flag\n", __LINE__
);
92 /* Do the same for fseeko(). */
93 if (fgetc (fp
) != EOF
)
95 printf ("%d: managed to read at end of file\n", __LINE__
);
100 printf ("%d: EOF flag not set\n", __LINE__
);
103 if (fseeko (fp
, 0, SEEK_CUR
) != 0)
105 printf ("%d: fseek(fp, 0, SEEK_CUR) failed\n", __LINE__
);
110 printf ("%d: fseek() didn't reset EOF flag\n", __LINE__
);
114 /* Go back to the beginning of the file: absolute. */
115 if (fseek (fp
, 0, SEEK_SET
) != 0)
117 printf ("%d: fseek(fp, 0, SEEK_SET) failed\n", __LINE__
);
120 else if (fflush (fp
) != 0)
122 printf ("%d: fflush() failed\n", __LINE__
);
125 else if (lseek (fd
, 0, SEEK_CUR
) != 0)
127 printf ("%d: lseek() returned different position\n", __LINE__
);
130 else if (fread (buf
, sizeof (outstr
) - 1, 1, fp
) != 1)
132 printf ("%d: fread() failed\n", __LINE__
);
135 else if (memcmp (buf
, outstr
, sizeof (outstr
) - 1) != 0)
137 printf ("%d: content after fseek(,,SEEK_SET) wrong\n", __LINE__
);
141 /* Now with fseeko. */
142 if (fseeko (fp
, 0, SEEK_SET
) != 0)
144 printf ("%d: fseeko(fp, 0, SEEK_SET) failed\n", __LINE__
);
147 else if (fflush (fp
) != 0)
149 printf ("%d: fflush() failed\n", __LINE__
);
152 else if (lseek (fd
, 0, SEEK_CUR
) != 0)
154 printf ("%d: lseek() returned different position\n", __LINE__
);
157 else if (fread (buf
, sizeof (outstr
) - 1, 1, fp
) != 1)
159 printf ("%d: fread() failed\n", __LINE__
);
162 else if (memcmp (buf
, outstr
, sizeof (outstr
) - 1) != 0)
164 printf ("%d: content after fseeko(,,SEEK_SET) wrong\n", __LINE__
);
168 /* Go back to the beginning of the file: relative. */
169 if (fseek (fp
, -((int) sizeof (outstr
) - 1), SEEK_CUR
) != 0)
171 printf ("%d: fseek(fp, 0, SEEK_SET) failed\n", __LINE__
);
174 else if (fflush (fp
) != 0)
176 printf ("%d: fflush() failed\n", __LINE__
);
179 else if (lseek (fd
, 0, SEEK_CUR
) != 0)
181 printf ("%d: lseek() returned different position\n", __LINE__
);
184 else if (fread (buf
, sizeof (outstr
) - 1, 1, fp
) != 1)
186 printf ("%d: fread() failed\n", __LINE__
);
189 else if (memcmp (buf
, outstr
, sizeof (outstr
) - 1) != 0)
191 printf ("%d: content after fseek(,,SEEK_SET) wrong\n", __LINE__
);
195 /* Now with fseeko. */
196 if (fseeko (fp
, -((int) sizeof (outstr
) - 1), SEEK_CUR
) != 0)
198 printf ("%d: fseeko(fp, 0, SEEK_SET) failed\n", __LINE__
);
201 else if (fflush (fp
) != 0)
203 printf ("%d: fflush() failed\n", __LINE__
);
206 else if (lseek (fd
, 0, SEEK_CUR
) != 0)
208 printf ("%d: lseek() returned different position\n", __LINE__
);
211 else if (fread (buf
, sizeof (outstr
) - 1, 1, fp
) != 1)
213 printf ("%d: fread() failed\n", __LINE__
);
216 else if (memcmp (buf
, outstr
, sizeof (outstr
) - 1) != 0)
218 printf ("%d: content after fseeko(,,SEEK_SET) wrong\n", __LINE__
);
222 /* Go back to the beginning of the file: from the end. */
223 if (fseek (fp
, -((int) sizeof (outstr
) - 1), SEEK_END
) != 0)
225 printf ("%d: fseek(fp, 0, SEEK_SET) failed\n", __LINE__
);
228 else if (fflush (fp
) != 0)
230 printf ("%d: fflush() failed\n", __LINE__
);
233 else if (lseek (fd
, 0, SEEK_CUR
) != 0)
235 printf ("%d: lseek() returned different position\n", __LINE__
);
238 else if (fread (buf
, sizeof (outstr
) - 1, 1, fp
) != 1)
240 printf ("%d: fread() failed\n", __LINE__
);
243 else if (memcmp (buf
, outstr
, sizeof (outstr
) - 1) != 0)
245 printf ("%d: content after fseek(,,SEEK_SET) wrong\n", __LINE__
);
249 /* Now with fseeko. */
250 if (fseeko (fp
, -((int) sizeof (outstr
) - 1), SEEK_END
) != 0)
252 printf ("%d: fseeko(fp, 0, SEEK_SET) failed\n", __LINE__
);
255 else if (fflush (fp
) != 0)
257 printf ("%d: fflush() failed\n", __LINE__
);
260 else if (lseek (fd
, 0, SEEK_CUR
) != 0)
262 printf ("%d: lseek() returned different position\n", __LINE__
);
265 else if (fread (buf
, sizeof (outstr
) - 1, 1, fp
) != 1)
267 printf ("%d: fread() failed\n", __LINE__
);
270 else if (memcmp (buf
, outstr
, sizeof (outstr
) - 1) != 0)
272 printf ("%d: content after fseeko(,,SEEK_SET) wrong\n", __LINE__
);
276 if (fwrite (outstr
, sizeof (outstr
) - 1, 1, fp
) != 1)
278 printf ("%d: write error 2\n", __LINE__
);
283 if (fwrite (outstr
, sizeof (outstr
) - 1, 1, fp
) != 1)
285 printf ("%d: write error 3\n", __LINE__
);
290 if (fwrite (outstr
, sizeof (outstr
) - 1, 1, fp
) != 1)
292 printf ("%d: write error 4\n", __LINE__
);
297 if (fwrite (outstr
, sizeof (outstr
) - 1, 1, fp
) != 1)
299 printf ("%d: write error 5\n", __LINE__
);
304 if (fputc ('1', fp
) == EOF
|| fputc ('2', fp
) == EOF
)
306 printf ("%d: cannot add characters at the end\n", __LINE__
);
311 /* Check the access time. */
312 if (fstat64 (fd
, &st1
) < 0)
314 printf ("%d: fstat64() before fseeko() failed\n\n", __LINE__
);
321 if (fseek (fp
, -(2 + 2 * (sizeof (outstr
) - 1)), SEEK_CUR
) != 0)
323 printf ("%d: fseek() after write characters failed\n", __LINE__
);
331 /* Make sure the timestamp actually can be different. */
335 if (fstat64 (fd
, &st2
) < 0)
337 printf ("%d: fstat64() after fseeko() failed\n\n", __LINE__
);
340 if (st1
.st_ctime
>= t
)
342 printf ("%d: st_ctime not updated\n", __LINE__
);
345 if (st1
.st_mtime
>= t
)
347 printf ("%d: st_mtime not updated\n", __LINE__
);
350 if (st1
.st_ctime
>= st2
.st_ctime
)
352 printf ("%d: st_ctime not changed\n", __LINE__
);
355 if (st1
.st_mtime
>= st2
.st_mtime
)
357 printf ("%d: st_mtime not changed\n", __LINE__
);
363 if (fread (buf
, 1, 2 + 2 * (sizeof (outstr
) - 1), fp
)
364 != 2 + 2 * (sizeof (outstr
) - 1))
366 printf ("%d: reading 2 records plus bits failed\n", __LINE__
);
369 else if (memcmp (buf
, outstr
, sizeof (outstr
) - 1) != 0
370 || memcmp (&buf
[sizeof (outstr
) - 1], outstr
,
371 sizeof (outstr
) - 1) != 0
372 || buf
[2 * (sizeof (outstr
) - 1)] != '1'
373 || buf
[2 * (sizeof (outstr
) - 1) + 1] != '2')
375 printf ("%d: reading records failed\n", __LINE__
);
378 else if (ungetc ('9', fp
) == EOF
)
380 printf ("%d: ungetc() failed\n", __LINE__
);
383 else if (fseek (fp
, -(2 + 2 * (sizeof (outstr
) - 1)), SEEK_END
) != 0)
385 printf ("%d: fseek after ungetc failed\n", __LINE__
);
388 else if (fread (buf
, 1, 2 + 2 * (sizeof (outstr
) - 1), fp
)
389 != 2 + 2 * (sizeof (outstr
) - 1))
391 printf ("%d: reading 2 records plus bits failed\n", __LINE__
);
394 else if (memcmp (buf
, outstr
, sizeof (outstr
) - 1) != 0
395 || memcmp (&buf
[sizeof (outstr
) - 1], outstr
,
396 sizeof (outstr
) - 1) != 0
397 || buf
[2 * (sizeof (outstr
) - 1)] != '1')
399 printf ("%d: reading records for the second time failed\n", __LINE__
);
402 else if (buf
[2 * (sizeof (outstr
) - 1) + 1] == '9')
404 printf ("%d: unget character not ignored\n", __LINE__
);
407 else if (buf
[2 * (sizeof (outstr
) - 1) + 1] != '2')
409 printf ("%d: unget somehow changed character\n", __LINE__
);
415 fp
= fopen (fname
, "r");
418 printf ("%d: fopen() failed\n\n", __LINE__
);
421 else if (fstat64 (fileno (fp
), &st1
) < 0)
423 printf ("%d: fstat64() before fseeko() failed\n\n", __LINE__
);
426 else if (fseeko (fp
, 0, SEEK_END
) != 0)
428 printf ("%d: fseeko(fp, 0, SEEK_END) failed\n", __LINE__
);
431 else if (ftello (fp
) != st1
.st_size
)
433 printf ("%d: fstat64 st_size %zd ftello %zd\n", __LINE__
,
434 (size_t) st1
.st_size
, (size_t) ftello (fp
));
438 printf ("%d: SEEK_END works\n", __LINE__
);
442 fp
= fopen (fname
, "r");
445 printf ("%d: fopen() failed\n\n", __LINE__
);
448 else if (fstat64 (fileno (fp
), &st1
) < 0)
450 printf ("%d: fstat64() before fgetc() failed\n\n", __LINE__
);
453 else if (fgetc (fp
) == EOF
)
455 printf ("%d: fgetc() before fseeko() failed\n\n", __LINE__
);
458 else if (fseeko (fp
, 0, SEEK_END
) != 0)
460 printf ("%d: fseeko(fp, 0, SEEK_END) failed\n", __LINE__
);
463 else if (ftello (fp
) != st1
.st_size
)
465 printf ("%d: fstat64 st_size %zd ftello %zd\n", __LINE__
,
466 (size_t) st1
.st_size
, (size_t) ftello (fp
));
470 printf ("%d: SEEK_END works\n", __LINE__
);
481 #define TEST_FUNCTION do_test ()
482 #include "../test-skeleton.c"