Remove libio C++ vtable definitions.
[platform/upstream/glibc.git] / libio / bug-fclose1.c
1 // BZ #12724
2
3 static void do_prepare (void);
4 #define PREPARE(argc, argv) do_prepare ()
5 static int do_test (void);
6 #define TEST_FUNCTION do_test()
7 #include "../test-skeleton.c"
8
9
10 static int fd;
11
12
13 static void
14 do_prepare (void)
15 {
16   fd = create_temp_file ("bug-fclose1.", NULL);
17   if (fd == -1)
18     {
19       printf ("cannot create temporary file: %m\n");
20       exit (1);
21     }
22 }
23
24
25 static int
26 do_test (void)
27 {
28   static const char pattern[] = "hello world";
29
30   /* Prepare a seekable file.  */
31   if (write (fd, pattern, sizeof pattern) != sizeof pattern)
32     {
33       printf ("cannot write pattern: %m\n");
34       return 1;
35     }
36   if (lseek (fd, 1, SEEK_SET) != 1)
37     {
38       printf ("cannot seek after write: %m\n");
39       return 1;
40     }
41
42   /* Create an output stream visiting the file; when it is closed, all
43      other file descriptors visiting the file must see the new file
44      position.  */
45   int fd2 = dup (fd);
46   if (fd2 < 0)
47     {
48       printf ("cannot duplicate descriptor for writing: %m\n");
49       return 1;
50     }
51   FILE *f = fdopen (fd2, "w");
52   if (f == NULL)
53     {
54       printf ("first fdopen failed: %m\n");
55       return 1;
56     }
57   if (fputc (pattern[1], f) != pattern[1])
58     {
59       printf ("fputc failed: %m\n");
60       return 1;
61     }
62   if (fclose (f) != 0)
63     {
64       printf ("first fclose failed: %m\n");
65       return 1;
66     }
67   errno = 0;
68   if (lseek (fd2, 0, SEEK_CUR) != -1)
69     {
70       printf ("lseek after fclose after write did not fail\n");
71       return 1;
72     }
73   if (errno != EBADF)
74     {
75       printf ("lseek after fclose after write did not fail with EBADF: %m\n");
76       return 1;
77     }
78   off_t o = lseek (fd, 0, SEEK_CUR);
79   if (o != 2)
80     {
81       printf ("\
82 lseek on original descriptor after first fclose returned %ld, expected 2\n",
83               (long int) o);
84       return 1;
85     }
86
87   /* Likewise for an input stream.  */
88   fd2 = dup (fd);
89   if (fd2 < 0)
90      {
91       printf ("cannot duplicate descriptor for reading: %m\n");
92       return 1;
93     }
94   f = fdopen (fd2, "r");
95    if (f == NULL)
96     {
97       printf ("second fdopen failed: %m\n");
98       return 1;
99     }
100    char c = fgetc (f);
101    if (c != pattern[2])
102      {
103        printf ("getc returned %c, expected %c\n", c, pattern[2]);
104        return 1;
105      }
106   if (fclose (f) != 0)
107     {
108       printf ("second fclose failed: %m\n");
109       return 1;
110     }
111   errno = 0;
112   if (lseek (fd2, 0, SEEK_CUR) != -1)
113     {
114       printf ("lseek after fclose after read did not fail\n");
115       return 1;
116     }
117   if (errno != EBADF)
118     {
119       printf ("lseek after fclose after read did not fail with EBADF: %m\n");
120       return 1;
121     }
122   o = lseek (fd, 0, SEEK_CUR);
123   if (o != 3)
124     {
125       printf ("\
126 lseek on original descriptor after second fclose returned %ld, expected 3\n",
127               (long int) o);
128       return 1;
129     }
130
131   return 0;
132 }