2 * Copyright (c) 2014 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$");
34 static int testnum = 1;
37 runtest(const char *fname1, const char *mode1, const char *fname2,
38 const char *mode2, bool success)
41 const char *fname2_print;
43 fname2_print = fname2 != NULL ? fname2 : "<NULL>";
44 fp1 = fopen(fname1, mode1);
46 printf("not ok %d - fopen(\"%s\", \"%s\") failed\n",
47 testnum++, fname1, mode1);
50 fp2 = freopen(fname2, mode2, fp1);
55 "freopen(\"%s\", \"%s\", fopen(\"%s\", \"%s\")) "
57 testnum++, fname2_print, mode2, fname1, mode1);
60 "freopen(\"%s\", \"%s\", fopen(\"%s\", \"%s\")) "
62 testnum++, fname2_print, mode2, fname1, mode1);
67 "freopen(\"%s\", \"%s\", fopen(\"%s\", \"%s\")) "
69 testnum++, fname2_print, mode2, fname1, mode1);
72 "freopen(\"%s\", \"%s\", fopen(\"%s\", \"%s\")) "
74 testnum++, fname2_print, mode2, fname1, mode1);
79 * Test program for freopen().
82 main(int argc, char *argv[])
85 runtest("/dev/null", "r", NULL, "r", true);
86 runtest("/dev/null", "w", NULL, "r", false);
87 runtest("/dev/null", "r+", NULL, "r", true);
88 runtest("/dev/null", "r", NULL, "w", false);
89 runtest("/dev/null", "w", NULL, "w", true);
90 runtest("/dev/null", "r+", NULL, "w", true);
91 runtest("/dev/null", "r", NULL, "a", false);
92 runtest("/dev/null", "w", NULL, "a", true);
93 runtest("/dev/null", "r+", NULL, "a", true);
94 runtest("/dev/null", "r", NULL, "r+", false);
95 runtest("/dev/null", "w", NULL, "r+", false);
96 runtest("/dev/null", "r+", NULL, "r+", true);
97 runtest("/dev/null", "r", NULL, "w+", false);
98 runtest("/dev/null", "w", NULL, "w+", false);
99 runtest("/dev/null", "r+", NULL, "w+", true);
100 runtest("/bin/sh", "r", NULL, "r", true);
101 runtest("/bin/sh", "r", "/bin/sh", "r", true);
102 runtest("/bin/sh", "r", "/dev/null", "r", true);
103 runtest("/bin/sh", "r", "/dev/null", "w", true);