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