Update to 2.1.x development version
[glibc/history.git] / stdio-common / test-fseek.c
blobd56c669a547749e825e054f6249a7b018efbce0f
1 #include <ansidecl.h>
2 #include <stdio.h>
4 #define TESTFILE "/tmp/test.dat"
6 int
7 main __P((void))
9 FILE *fp;
10 int i, j;
12 puts ("\nFile seek test");
13 fp = fopen (TESTFILE, "w");
14 if (fp == NULL)
16 perror (TESTFILE);
17 return 1;
20 for (i = 0; i < 256; i++)
21 putc (i, fp);
22 if (freopen (TESTFILE, "r", fp) != fp)
24 perror ("Cannot open file for reading");
25 return 1;
28 for (i = 1; i <= 255; i++)
30 printf ("%3d\n", i);
31 fseek (fp, (long) -i, SEEK_END);
32 if ((j = getc (fp)) != 256 - i)
34 printf ("SEEK_END failed %d\n", j);
35 break;
37 if (fseek (fp, (long) i, SEEK_SET))
39 puts ("Cannot SEEK_SET");
40 break;
42 if ((j = getc (fp)) != i)
44 printf ("SEEK_SET failed %d\n", j);
45 break;
47 if (fseek (fp, (long) i, SEEK_SET))
49 puts ("Cannot SEEK_SET");
50 break;
52 if (fseek (fp, (long) (i >= 128 ? -128 : 128), SEEK_CUR))
54 puts ("Cannot SEEK_CUR");
55 break;
57 if ((j = getc (fp)) != (i >= 128 ? i - 128 : i + 128))
59 printf ("SEEK_CUR failed %d\n", j);
60 break;
63 fclose (fp);
65 puts ((i > 255) ? "Test succeeded." : "Test FAILED!");
66 return (i > 255) ? 0 : 1;