1 /* Regression test for fseek and freopen bugs. */
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6
7 int
main(int argc,char * argv[])8 main (int argc, char *argv[])
9 {
10 int lose = 0;
11 char filename[] = "/tmp/bug7.XXXXXX";
12 FILE *fp;
13
14 int fd = mkstemp (filename);
15 if (fd == -1)
16 {
17 printf ("mkstemp failed\n");
18 lose = 1;
19 }
20 else
21 {
22 close (fd);
23 fp = fopen (filename, "w+");
24 fprintf (fp, "Hello world!\n");
25 fflush (fp);
26 fseek (fp, 5L, SEEK_SET);
27 if (fseek (fp, -1L, SEEK_CUR) < 0)
28 {
29 printf ("seek failed\n");
30 lose = 1;
31 }
32 fclose (fp);
33 remove (filename);
34 }
35
36 {
37 FILE *file1;
38 FILE *file2;
39 char filename1[] = "/tmp/bug7.XXXXXX";
40 char filename2[] = "/tmp/bug7.XXXXXX";
41 int ch;
42
43 int fd1 = mkstemp (filename1);
44 int fd2 = mkstemp (filename2);
45 if (fd1 == -1 || fd2 == -1)
46 {
47 printf ("mkstemp failed\n");
48 lose = 1;
49 }
50 else
51 {
52 close (fd1);
53 close (fd2);
54
55 file1 = fopen (filename1, "w");
56 fclose (file1);
57
58 file2 = fopen (filename2, "w");
59 fputc ('x', file2);
60 fclose (file2);
61
62 file1 = fopen (filename1, "r");
63 file2 = freopen (filename2, "r", file1);
64 if ((ch = fgetc (file2)) != 'x')
65 {
66 printf ("wrong character in reopened file, value = %d\n", ch);
67 lose = 1;
68 }
69 fclose (file2);
70 remove (filename1);
71 remove (filename2);
72 }
73 }
74
75 puts (lose ? "Test FAILED!" : "Test succeeded.");
76 return lose;
77 }
78