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