freopen_test.c revision 264737
1/*-
2 * Copyright (c) 2014 Jilles Tjoelker
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/tools/regression/lib/libc/stdio/test-freopen.c 264737 2014-04-21 17:40:23Z jilles $");
29
30#include	<stdbool.h>
31#include	<stdio.h>
32#include	<string.h>
33
34static int testnum = 1;
35
36static void
37runtest(const char *fname1, const char *mode1, const char *fname2,
38    const char *mode2, bool success)
39{
40	FILE *fp1, *fp2;
41	const char *fname2_print;
42
43	fname2_print = fname2 != NULL ? fname2 : "<NULL>";
44	fp1 = fopen(fname1, mode1);
45	if (fp1 == NULL) {
46		printf("not ok %d - fopen(\"%s\", \"%s\") failed\n",
47		    testnum++, fname1, mode1);
48		return;
49	}
50	fp2 = freopen(fname2, mode2, fp1);
51	if (fp2 == NULL) {
52		fclose(fp1);
53		if (success)
54			printf("not ok %d - "
55			    "freopen(\"%s\", \"%s\", fopen(\"%s\", \"%s\")) "
56			    "failed\n",
57			    testnum++, fname2_print, mode2, fname1, mode1);
58		else
59			printf("ok %d - "
60			    "freopen(\"%s\", \"%s\", fopen(\"%s\", \"%s\")) "
61			    "failed\n",
62			    testnum++, fname2_print, mode2, fname1, mode1);
63		return;
64	}
65	if (success)
66		printf("ok %d - "
67		    "freopen(\"%s\", \"%s\", fopen(\"%s\", \"%s\")) "
68		    "succeeded\n",
69		    testnum++, fname2_print, mode2, fname1, mode1);
70	else
71		printf("not ok %d - "
72		    "freopen(\"%s\", \"%s\", fopen(\"%s\", \"%s\")) "
73		    "succeeded\n",
74		    testnum++, fname2_print, mode2, fname1, mode1);
75	fclose(fp2);
76}
77
78/*
79 * Test program for freopen().
80 */
81int
82main(int argc, char *argv[])
83{
84	printf("1..19\n");
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);
104
105	return 0;
106}
107
108/* vim:ts=8:cin:sw=8
109 *  */
110