Update.
[glibc.git] / stdio-common / tst-fseek.c
blob0bfedf6866a8c1708eed3a3c9bc1b55ed6c00d3d
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. */
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 <sys/stat.h>
30 int
31 main (void)
33 const char *tmpdir;
34 char *fname;
35 int fd;
36 FILE *fp;
37 const char outstr[] = "hello world!\n";
38 char strbuf[sizeof outstr];
39 char buf[200];
40 struct stat64 st1;
41 struct stat64 st2;
42 int result = 0;
44 tmpdir = getenv ("TMPDIR");
45 if (tmpdir == NULL || tmpdir[0] == '\0')
46 tmpdir = "/tmp";
48 asprintf (&fname, "%s/tst-fseek.XXXXXX", tmpdir);
49 if (fname == NULL)
50 error (EXIT_FAILURE, errno, "cannot generate name for temporary file");
52 /* Create a temporary file. */
53 fd = mkstemp (fname);
54 if (fd == -1)
55 error (EXIT_FAILURE, errno, "cannot open temporary file");
57 fp = fdopen (fd, "w+");
58 if (fp == NULL)
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 puts ("write error");
66 result = 1;
67 goto out;
70 /* The EOF flag must be reset. */
71 if (fgetc (fp) != EOF)
73 puts ("managed to read at end of file");
74 result = 1;
76 else if (! feof (fp))
78 puts ("EOF flag not set");
79 result = 1;
81 if (fseek (fp, 0, SEEK_CUR) != 0)
83 puts ("fseek(fp, 0, SEEK_CUR) failed");
84 result = 1;
86 else if (feof (fp))
88 puts ("fseek() didn't reset EOF flag");
89 result = 1;
92 /* Do the same for fseeko(). */
93 #ifdef USE_IN_LIBIO
94 if (fgetc (fp) != EOF)
96 puts ("managed to read at end of file");
97 result = 1;
99 else if (! feof (fp))
101 puts ("EOF flag not set");
102 result = 1;
104 if (fseeko (fp, 0, SEEK_CUR) != 0)
106 puts ("fseek(fp, 0, SEEK_CUR) failed");
107 result = 1;
109 else if (feof (fp))
111 puts ("fseek() didn't reset EOF flag");
112 result = 1;
114 #endif
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");
120 result = 1;
122 else if (fflush (fp) != 0)
124 puts ("fflush() failed");
125 result = 1;
127 else if (lseek (fd, 0, SEEK_CUR) != 0)
129 puts ("lseek() returned different position");
130 result = 1;
132 else if (fread (buf, sizeof (outstr) - 1, 1, fp) != 1)
134 puts ("fread() failed");
135 result = 1;
137 else if (memcmp (buf, outstr, sizeof (outstr) - 1) != 0)
139 puts ("content after fseek(,,SEEK_SET) wrong");
140 result = 1;
143 #ifdef USE_IN_LIBIO
144 /* Now with fseeko. */
145 if (fseeko (fp, 0, SEEK_SET) != 0)
147 puts ("fseeko(fp, 0, SEEK_SET) failed");
148 result = 1;
150 else if (fflush (fp) != 0)
152 puts ("fflush() failed");
153 result = 1;
155 else if (lseek (fd, 0, SEEK_CUR) != 0)
157 puts ("lseek() returned different position");
158 result = 1;
160 else if (fread (buf, sizeof (outstr) - 1, 1, fp) != 1)
162 puts ("fread() failed");
163 result = 1;
165 else if (memcmp (buf, outstr, sizeof (outstr) - 1) != 0)
167 puts ("content after fseeko(,,SEEK_SET) wrong");
168 result = 1;
170 #endif
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");
176 result = 1;
178 else if (fflush (fp) != 0)
180 puts ("fflush() failed");
181 result = 1;
183 else if (lseek (fd, 0, SEEK_CUR) != 0)
185 puts ("lseek() returned different position");
186 result = 1;
188 else if (fread (buf, sizeof (outstr) - 1, 1, fp) != 1)
190 puts ("fread() failed");
191 result = 1;
193 else if (memcmp (buf, outstr, sizeof (outstr) - 1) != 0)
195 puts ("content after fseek(,,SEEK_SET) wrong");
196 result = 1;
199 #ifdef USE_IN_LIBIO
200 /* Now with fseeko. */
201 if (fseeko (fp, -(sizeof (outstr) - 1), SEEK_CUR) != 0)
203 puts ("fseeko(fp, 0, SEEK_SET) failed");
204 result = 1;
206 else if (fflush (fp) != 0)
208 puts ("fflush() failed");
209 result = 1;
211 else if (lseek (fd, 0, SEEK_CUR) != 0)
213 puts ("lseek() returned different position");
214 result = 1;
216 else if (fread (buf, sizeof (outstr) - 1, 1, fp) != 1)
218 puts ("fread() failed");
219 result = 1;
221 else if (memcmp (buf, outstr, sizeof (outstr) - 1) != 0)
223 puts ("content after fseeko(,,SEEK_SET) wrong");
224 result = 1;
226 #endif
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");
232 result = 1;
234 else if (fflush (fp) != 0)
236 puts ("fflush() failed");
237 result = 1;
239 else if (lseek (fd, 0, SEEK_CUR) != 0)
241 puts ("lseek() returned different position");
242 result = 1;
244 else if (fread (buf, sizeof (outstr) - 1, 1, fp) != 1)
246 puts ("fread() failed");
247 result = 1;
249 else if (memcmp (buf, outstr, sizeof (outstr) - 1) != 0)
251 puts ("content after fseek(,,SEEK_SET) wrong");
252 result = 1;
255 #ifdef USE_IN_LIBIO
256 /* Now with fseeko. */
257 if (fseeko (fp, -(sizeof (outstr) - 1), SEEK_END) != 0)
259 puts ("fseeko(fp, 0, SEEK_SET) failed");
260 result = 1;
262 else if (fflush (fp) != 0)
264 puts ("fflush() failed");
265 result = 1;
267 else if (lseek (fd, 0, SEEK_CUR) != 0)
269 puts ("lseek() returned different position");
270 result = 1;
272 else if (fread (buf, sizeof (outstr) - 1, 1, fp) != 1)
274 puts ("fread() failed");
275 result = 1;
277 else if (memcmp (buf, outstr, sizeof (outstr) - 1) != 0)
279 puts ("content after fseeko(,,SEEK_SET) wrong");
280 result = 1;
282 #endif
284 if (fwrite (outstr, sizeof (outstr) - 1, 1, fp) != 1)
286 puts ("write error 2");
287 result = 1;
288 goto out;
291 if (fwrite (outstr, sizeof (outstr) - 1, 1, fp) != 1)
293 puts ("write error 3");
294 result = 1;
295 goto out;
298 if (fwrite (outstr, sizeof (outstr) - 1, 1, fp) != 1)
300 puts ("write error 4");
301 result = 1;
302 goto out;
305 if (fwrite (outstr, sizeof (outstr) - 1, 1, fp) != 1)
307 puts ("write error 5");
308 result = 1;
309 goto out;
312 if (fputc ('1', fp) == EOF || fputc ('2', fp) == EOF)
314 puts ("cannot add characters at the end");
315 result = 1;
316 goto out;
319 /* Check the access time. */
320 if (fstat64 (fd, &st1) < 0)
322 puts ("fstat64() before fseeko() failed\n");
323 result = 1;
325 else
327 sleep (1);
329 if (fseek (fp, -(2 + 2 * (sizeof (outstr) - 1)), SEEK_CUR) != 0)
331 puts ("fseek() after write characters failed");
332 result = 1;
333 goto out;
335 else
338 time_t t;
339 /* Make sure the timestamp actually can be different. */
340 sleep (1);
341 t = time (NULL);
343 if (fstat64 (fd, &st2) < 0)
345 puts ("fstat64() after fseeko() failed\n");
346 result = 1;
348 if (st1.st_ctime >= t)
350 puts ("st_ctime not updated");
351 result = 1;
353 if (st1.st_mtime >= t)
355 puts ("st_mtime not updated");
356 result = 1;
358 if (st1.st_ctime >= st2.st_ctime)
360 puts ("st_ctime not changed");
361 result = 1;
363 if (st1.st_mtime >= st2.st_mtime)
365 puts ("st_mtime not changed");
366 result = 1;
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");
375 result = 1;
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");
384 result = 1;
386 else if (ungetc ('9', fp) == EOF)
388 puts ("ungetc() failed");
389 result = 1;
391 else if (fseek (fp, -(2 + 2 * (sizeof (outstr) - 1)), SEEK_END) != 0)
393 puts ("fseek after ungetc failed");
394 result = 1;
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");
400 result = 1;
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");
408 result = 1;
410 else if (buf[2 * (sizeof (outstr) - 1) + 1] == '9')
412 puts ("unget character not ignored");
413 result = 1;
415 else if (buf[2 * (sizeof (outstr) - 1) + 1] != '2')
417 puts ("unget somehow changed character");
418 result = 1;
421 out:
422 unlink (fname);
424 return result;