1#include <fcntl.h>
2#include <stdio.h>
3#include <string.h>
4#include <unistd.h>
5
6static int fd;
7static char *fname;
8
9
10static void prepare (void);
11#define PREPARE(argc, argv) prepare ()
12
13
14#define TEST_FUNCTION do_test ()
15static int do_test (void);
16#include "../test-skeleton.c"
17
18
19static void
20prepare (void)
21{
22 fd = create_temp_file (base: "wrewind.", filename: &fname);
23 if (fd == -1)
24 exit (3);
25}
26
27
28static int
29do_test (void)
30{
31 char buf[100];
32 FILE *fp;
33 int result = 0;
34
35 fp = fdopen (fd, "w");
36 if (fp == NULL)
37 {
38 puts (s: "cannot create file");
39 exit (1);
40 }
41
42 if (fputs ("one\n", fp) == EOF || fputs ("two\n", fp) == EOF)
43 {
44 puts (s: "cannot create filec content");
45 exit (1);
46 }
47
48 fclose (fp);
49
50 fp = fopen (fname, "a+");
51 if (fp == NULL)
52 {
53 puts (s: "cannot fopen a+");
54 exit (1);
55 }
56
57 if (fgets (s: buf, n: sizeof (buf), stream: fp) == NULL)
58 {
59 puts (s: "cannot read after fopen a+");
60 exit (1);
61 }
62
63 if (strcmp (buf, "one\n") != 0)
64 {
65 puts (s: "read after fopen a+ produced wrong result");
66 result = 1;
67 }
68
69 fclose (fp);
70
71 fd = open (file: fname, O_RDWR);
72 if (fd == -1)
73 {
74 puts (s: "open failed");
75 exit (1);
76 }
77
78 fp = fdopen (fd, "a+");
79 if (fp == NULL)
80 {
81 puts (s: "fopen after open failed");
82 exit (1);
83 }
84
85 if (fgets (s: buf, n: sizeof (buf), stream: fp) == NULL)
86 {
87 puts (s: "cannot read after fdopen a+");
88 exit (1);
89 }
90
91 if (strcmp (buf, "one\n") != 0)
92 {
93 puts (s: "read after fdopen a+ produced wrong result");
94 result = 1;
95 }
96
97 fclose (fp);
98
99 return result;
100}
101

source code of glibc/libio/bug-fopena+.c