1*3cedbec3SEnji Cooper /*-
2*3cedbec3SEnji Cooper * Copyright (c) 2013 Jilles Tjoelker
3*3cedbec3SEnji Cooper * All rights reserved.
4*3cedbec3SEnji Cooper *
5*3cedbec3SEnji Cooper * Redistribution and use in source and binary forms, with or without
6*3cedbec3SEnji Cooper * modification, are permitted provided that the following conditions
7*3cedbec3SEnji Cooper * are met:
8*3cedbec3SEnji Cooper * 1. Redistributions of source code must retain the above copyright
9*3cedbec3SEnji Cooper * notice, this list of conditions and the following disclaimer.
10*3cedbec3SEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright
11*3cedbec3SEnji Cooper * notice, this list of conditions and the following disclaimer in the
12*3cedbec3SEnji Cooper * documentation and/or other materials provided with the distribution.
13*3cedbec3SEnji Cooper *
14*3cedbec3SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*3cedbec3SEnji Cooper * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*3cedbec3SEnji Cooper * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*3cedbec3SEnji Cooper * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*3cedbec3SEnji Cooper * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*3cedbec3SEnji Cooper * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*3cedbec3SEnji Cooper * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*3cedbec3SEnji Cooper * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*3cedbec3SEnji Cooper * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*3cedbec3SEnji Cooper * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*3cedbec3SEnji Cooper * SUCH DAMAGE.
25*3cedbec3SEnji Cooper */
26*3cedbec3SEnji Cooper
27*3cedbec3SEnji Cooper #include <sys/cdefs.h>
28*3cedbec3SEnji Cooper
29*3cedbec3SEnji Cooper #include <fcntl.h>
30*3cedbec3SEnji Cooper #include <stdio.h>
31*3cedbec3SEnji Cooper #include <unistd.h>
32*3cedbec3SEnji Cooper
33*3cedbec3SEnji Cooper /*
34*3cedbec3SEnji Cooper * O_ACCMODE is currently defined incorrectly. This is what it should be.
35*3cedbec3SEnji Cooper * Various code depends on the incorrect value.
36*3cedbec3SEnji Cooper */
37*3cedbec3SEnji Cooper #define CORRECT_O_ACCMODE (O_ACCMODE | O_EXEC)
38*3cedbec3SEnji Cooper
39*3cedbec3SEnji Cooper static int testnum;
40*3cedbec3SEnji Cooper
41*3cedbec3SEnji Cooper static void
subtests(const char * path,int omode,const char * omodetext)42*3cedbec3SEnji Cooper subtests(const char *path, int omode, const char *omodetext)
43*3cedbec3SEnji Cooper {
44*3cedbec3SEnji Cooper int fd, flags1, flags2, flags3;
45*3cedbec3SEnji Cooper
46*3cedbec3SEnji Cooper fd = open(path, omode);
47*3cedbec3SEnji Cooper if (fd == -1)
48*3cedbec3SEnji Cooper printf("not ok %d - open(\"%s\", %s) failed\n",
49*3cedbec3SEnji Cooper testnum++, path, omodetext);
50*3cedbec3SEnji Cooper else
51*3cedbec3SEnji Cooper printf("ok %d - open(\"%s\", %s) succeeded\n",
52*3cedbec3SEnji Cooper testnum++, path, omodetext);
53*3cedbec3SEnji Cooper flags1 = fcntl(fd, F_GETFL);
54*3cedbec3SEnji Cooper if (flags1 == -1)
55*3cedbec3SEnji Cooper printf("not ok %d - fcntl(F_GETFL) failed\n", testnum++);
56*3cedbec3SEnji Cooper else if ((flags1 & CORRECT_O_ACCMODE) == omode)
57*3cedbec3SEnji Cooper printf("ok %d - fcntl(F_GETFL) gave correct result\n",
58*3cedbec3SEnji Cooper testnum++);
59*3cedbec3SEnji Cooper else
60*3cedbec3SEnji Cooper printf("not ok %d - fcntl(F_GETFL) gave incorrect result "
61*3cedbec3SEnji Cooper "(%#x & %#x != %#x)\n",
62*3cedbec3SEnji Cooper testnum++, flags1, CORRECT_O_ACCMODE, omode);
63*3cedbec3SEnji Cooper if (fcntl(fd, F_SETFL, flags1) == -1)
64*3cedbec3SEnji Cooper printf("not ok %d - fcntl(F_SETFL) same flags failed\n",
65*3cedbec3SEnji Cooper testnum++);
66*3cedbec3SEnji Cooper else
67*3cedbec3SEnji Cooper printf("ok %d - fcntl(F_SETFL) same flags succeeded\n",
68*3cedbec3SEnji Cooper testnum++);
69*3cedbec3SEnji Cooper flags2 = fcntl(fd, F_GETFL);
70*3cedbec3SEnji Cooper if (flags2 == -1)
71*3cedbec3SEnji Cooper printf("not ok %d - fcntl(F_GETFL) failed\n", testnum++);
72*3cedbec3SEnji Cooper else if (flags2 == flags1)
73*3cedbec3SEnji Cooper printf("ok %d - fcntl(F_GETFL) gave same result\n",
74*3cedbec3SEnji Cooper testnum++);
75*3cedbec3SEnji Cooper else
76*3cedbec3SEnji Cooper printf("not ok %d - fcntl(F_SETFL) caused fcntl(F_GETFL) to "
77*3cedbec3SEnji Cooper "change from %#x to %#x\n",
78*3cedbec3SEnji Cooper testnum++, flags1, flags2);
79*3cedbec3SEnji Cooper if (fcntl(fd, F_SETFL, flags2 | O_NONBLOCK) == -1)
80*3cedbec3SEnji Cooper printf("not ok %d - fcntl(F_SETFL) O_NONBLOCK failed\n",
81*3cedbec3SEnji Cooper testnum++);
82*3cedbec3SEnji Cooper else
83*3cedbec3SEnji Cooper printf("ok %d - fcntl(F_SETFL) O_NONBLOCK succeeded\n",
84*3cedbec3SEnji Cooper testnum++);
85*3cedbec3SEnji Cooper flags3 = fcntl(fd, F_GETFL);
86*3cedbec3SEnji Cooper if (flags3 == -1)
87*3cedbec3SEnji Cooper printf("not ok %d - fcntl(F_GETFL) failed\n", testnum++);
88*3cedbec3SEnji Cooper else if (flags3 == (flags2 | O_NONBLOCK))
89*3cedbec3SEnji Cooper printf("ok %d - fcntl(F_GETFL) gave expected result\n",
90*3cedbec3SEnji Cooper testnum++);
91*3cedbec3SEnji Cooper else
92*3cedbec3SEnji Cooper printf("not ok %d - fcntl(F_SETFL) gave unexpected result "
93*3cedbec3SEnji Cooper "(%#x != %#x)\n",
94*3cedbec3SEnji Cooper testnum++, flags3, flags2 | O_NONBLOCK);
95*3cedbec3SEnji Cooper (void)close(fd);
96*3cedbec3SEnji Cooper }
97*3cedbec3SEnji Cooper
98*3cedbec3SEnji Cooper int
main(int argc __unused,char ** argv __unused)99*3cedbec3SEnji Cooper main(int argc __unused, char **argv __unused)
100*3cedbec3SEnji Cooper {
101*3cedbec3SEnji Cooper printf("1..24\n");
102*3cedbec3SEnji Cooper testnum = 1;
103*3cedbec3SEnji Cooper subtests("/dev/null", O_RDONLY, "O_RDONLY");
104*3cedbec3SEnji Cooper subtests("/dev/null", O_WRONLY, "O_WRONLY");
105*3cedbec3SEnji Cooper subtests("/dev/null", O_RDWR, "O_RDWR");
106*3cedbec3SEnji Cooper subtests("/bin/sh", O_EXEC, "O_EXEC");
107*3cedbec3SEnji Cooper return (0);
108*3cedbec3SEnji Cooper }
109