This source file includes following definitions.
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #include <config.h>
20
21
22 #include <stdio.h>
23
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27
28 #include "macros.h"
29
30 #define BASE "test-fopen-gnu.t"
31
32
33 #define DATA "abc\x1axyz"
34
35 int
36 main (void)
37 {
38 FILE *f;
39 int fd;
40 int flags;
41 char buf[16];
42
43
44 unlink (BASE "file");
45 unlink (BASE "binary");
46
47
48 f = fopen (BASE "file", "w");
49 ASSERT (f);
50 fd = fileno (f);
51 ASSERT (fd >= 0);
52 flags = fcntl (fd, F_GETFD);
53 ASSERT (flags >= 0);
54 ASSERT ((flags & FD_CLOEXEC) == 0);
55 ASSERT (fclose (f) == 0);
56
57
58 f = fopen (BASE "file", "we");
59 ASSERT (f);
60 fd = fileno (f);
61 ASSERT (fd >= 0);
62 flags = fcntl (fd, F_GETFD);
63 ASSERT (flags >= 0);
64 ASSERT ((flags & FD_CLOEXEC) != 0);
65 ASSERT (fclose (f) == 0);
66
67
68 f = fopen (BASE "file", "ax");
69 ASSERT (f == NULL);
70 ASSERT (errno == EEXIST);
71
72
73 f = fopen (BASE "binary", "wbe");
74 ASSERT (f);
75 ASSERT (fwrite (DATA, 1, sizeof (DATA)-1, f) == sizeof (DATA)-1);
76 ASSERT (fclose (f) == 0);
77
78 f = fopen (BASE "binary", "rbe");
79 ASSERT (f);
80 ASSERT (fread (buf, 1, sizeof (buf), f) == sizeof (DATA)-1);
81 ASSERT (fclose (f) == 0);
82
83
84 ASSERT (unlink (BASE "file") == 0);
85 ASSERT (unlink (BASE "binary") == 0);
86
87 return 0;
88 }