2 * Copyright (c) 2013 Jilles Tjoelker
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
35 * O_ACCMODE is currently defined incorrectly. This is what it should be.
36 * Various code depends on the incorrect value.
38 #define CORRECT_O_ACCMODE (O_ACCMODE | O_EXEC)
40 static int testnum = 1;
43 runtest(const char *fname, const char *mode)
46 int fd, flags, wantedflags;
48 fp = fopen(fname, mode);
50 printf("not ok %d - fopen(\"%s\", \"%s\") failed\n",
51 testnum++, fname, mode);
52 printf("not ok %d - FD_CLOEXEC # SKIP\n",
58 printf("not ok %d - fileno() failed\n", testnum++);
60 printf("ok %d - fopen(\"%s\", \"%s\") and fileno() succeeded\n",
61 testnum++, fname, mode);
62 if (fcntl(fd, F_GETFD) == (strchr(mode, 'e') != NULL ? FD_CLOEXEC : 0))
63 printf("ok %d - FD_CLOEXEC flag correct\n", testnum++);
65 printf("not ok %d - FD_CLOEXEC flag incorrect\n", testnum++);
66 flags = fcntl(fd, F_GETFL);
67 if (strchr(mode, '+'))
68 wantedflags = O_RDWR | (*mode == 'a' ? O_APPEND : 0);
69 else if (*mode == 'r')
70 wantedflags = O_RDONLY;
71 else if (*mode == 'w')
72 wantedflags = O_WRONLY;
73 else if (*mode == 'a')
74 wantedflags = O_WRONLY | O_APPEND;
77 if (wantedflags == -1)
78 printf("not ok %d - unrecognized mode\n", testnum++);
79 else if ((flags & (CORRECT_O_ACCMODE | O_APPEND)) == wantedflags)
80 printf("ok %d - correct access mode\n", testnum++);
82 printf("not ok %d - incorrect access mode\n", testnum++);
87 * Test program for fopen().
90 main(int argc, char *argv[])
93 runtest("/dev/null", "r");
94 runtest("/dev/null", "r+");
95 runtest("/dev/null", "w");
96 runtest("/dev/null", "w+");
97 runtest("/dev/null", "a");
98 runtest("/dev/null", "a+");
99 runtest("/dev/null", "re");
100 runtest("/dev/null", "r+e");
101 runtest("/dev/null", "we");
102 runtest("/dev/null", "w+e");
103 runtest("/dev/null", "ae");
104 runtest("/dev/null", "a+e");
105 runtest("/dev/null", "re+");
106 runtest("/dev/null", "we+");
107 runtest("/dev/null", "ae+");