1 | /* Regression test for fseek and freopen bugs. */ |
2 | |
3 | #include <stdio.h> |
4 | #include <stdlib.h> |
5 | #include <unistd.h> |
6 | |
7 | int |
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 (template: filename); |
15 | if (fd == -1) |
16 | { |
17 | printf (format: "mkstemp failed\n" ); |
18 | lose = 1; |
19 | } |
20 | else |
21 | { |
22 | close (fd: 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 (format: "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 (template: filename1); |
44 | int fd2 = mkstemp (template: filename2); |
45 | if (fd1 == -1 || fd2 == -1) |
46 | { |
47 | printf (format: "mkstemp failed\n" ); |
48 | lose = 1; |
49 | } |
50 | else |
51 | { |
52 | close (fd: fd1); |
53 | close (fd: fd2); |
54 | |
55 | file1 = fopen (filename1, "w" ); |
56 | fclose (file1); |
57 | |
58 | file2 = fopen (filename2, "w" ); |
59 | fputc (c: 'x', stream: file2); |
60 | fclose (file2); |
61 | |
62 | file1 = fopen (filename1, "r" ); |
63 | file2 = freopen (filename: filename2, modes: "r" , stream: file1); |
64 | if ((ch = fgetc (stream: file2)) != 'x') |
65 | { |
66 | printf (format: "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 (s: lose ? "Test FAILED!" : "Test succeeded." ); |
76 | return lose; |
77 | } |
78 | |