summaryrefslogtreecommitdiff
path: root/libio/bug-fopena+.c
blob: 5446de15925d25cff8f0ff56ec77f384a4c1f72f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

static int fd;
static char *fname;


static void prepare (void);
#define PREPARE(argc, argv) prepare ()


#define TEST_FUNCTION do_test ()
static int do_test (void);
#include "../test-skeleton.c"


static void
prepare (void)
{
  fd = create_temp_file ("wrewind.", &fname);
  if (fd == -1)
    exit (3);
}


static int
do_test (void)
{
  char buf[100];
  FILE *fp;
  int result = 0;

  fp = fdopen (fd, "w");
  if (fp == NULL)
    {
      puts ("cannot create file");
      exit (1);
    }

  if (fputs ("one\n", fp) == EOF || fputs ("two\n", fp) == EOF)
    {
      puts ("cannot create filec content");
      exit (1);
    }

  fclose (fp);

  fp = fopen (fname, "a+");
  if (fp == NULL)
    {
      puts ("cannot fopen a+");
      exit (1);
    }

  if (fgets (buf, sizeof (buf), fp) == NULL)
    {
      puts ("cannot read after fopen a+");
      exit (1);
    }

  if (strcmp (buf, "one\n") != 0)
    {
      puts ("read after fopen a+ produced wrong result");
      result = 1;
    }

  fclose (fp);

  fd = open (fname, O_RDWR);
  if (fd == -1)
    {
      puts ("open failed");
      exit (1);
    }

  fp = fdopen (fd, "a+");
  if (fp == NULL)
    {
      puts ("fopen after open failed");
      exit (1);
    }

  if (fgets (buf, sizeof (buf), fp) == NULL)
    {
      puts ("cannot read after fdopen a+");
      exit (1);
    }

  if (strcmp (buf, "one\n") != 0)
    {
      puts ("read after fdopen a+ produced wrong result");
      result = 1;
    }

  fclose (fp);

  return result;
}