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>
34 * O_ACCMODE is currently defined incorrectly. This is what it should be.
35 * Various code depends on the incorrect value.
37 #define CORRECT_O_ACCMODE (O_ACCMODE | O_EXEC)
42 subtests(const char *path, int omode, const char *omodetext)
44 int fd, flags1, flags2, flags3;
46 fd = open(path, omode);
48 printf("not ok %d - open(\"%s\", %s) failed\n",
49 testnum++, path, omodetext);
51 printf("ok %d - open(\"%s\", %s) succeeded\n",
52 testnum++, path, omodetext);
53 flags1 = fcntl(fd, F_GETFL);
55 printf("not ok %d - fcntl(F_GETFL) failed\n", testnum++);
56 else if ((flags1 & CORRECT_O_ACCMODE) == omode)
57 printf("ok %d - fcntl(F_GETFL) gave correct result\n",
60 printf("not ok %d - fcntl(F_GETFL) gave incorrect result "
61 "(%#x & %#x != %#x)\n",
62 testnum++, flags1, CORRECT_O_ACCMODE, omode);
63 if (fcntl(fd, F_SETFL, flags1) == -1)
64 printf("not ok %d - fcntl(F_SETFL) same flags failed\n",
67 printf("ok %d - fcntl(F_SETFL) same flags succeeded\n",
69 flags2 = fcntl(fd, F_GETFL);
71 printf("not ok %d - fcntl(F_GETFL) failed\n", testnum++);
72 else if (flags2 == flags1)
73 printf("ok %d - fcntl(F_GETFL) gave same result\n",
76 printf("not ok %d - fcntl(F_SETFL) caused fcntl(F_GETFL) to "
77 "change from %#x to %#x\n",
78 testnum++, flags1, flags2);
79 if (fcntl(fd, F_SETFL, flags2 | O_NONBLOCK) == -1)
80 printf("not ok %d - fcntl(F_SETFL) O_NONBLOCK failed\n",
83 printf("ok %d - fcntl(F_SETFL) O_NONBLOCK succeeded\n",
85 flags3 = fcntl(fd, F_GETFL);
87 printf("not ok %d - fcntl(F_GETFL) failed\n", testnum++);
88 else if (flags3 == (flags2 | O_NONBLOCK))
89 printf("ok %d - fcntl(F_GETFL) gave expected result\n",
92 printf("not ok %d - fcntl(F_SETFL) gave unexpected result "
94 testnum++, flags3, flags2 | O_NONBLOCK);
99 main(int argc __unused, char **argv __unused)
103 subtests("/dev/null", O_RDONLY, "O_RDONLY");
104 subtests("/dev/null", O_WRONLY, "O_WRONLY");
105 subtests("/dev/null", O_RDWR, "O_RDWR");
106 subtests("/bin/sh", O_EXEC, "O_EXEC");