xref: /minix3/tests/lib/libc/stdio/t_fopen.c (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc /*	$NetBSD: t_fopen.c,v 1.3 2011/09/14 14:34:37 martin Exp $ */
2*11be35a1SLionel Sambuc 
3*11be35a1SLionel Sambuc /*-
4*11be35a1SLionel Sambuc  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5*11be35a1SLionel Sambuc  * All rights reserved.
6*11be35a1SLionel Sambuc  *
7*11be35a1SLionel Sambuc  * This code is derived from software contributed to The NetBSD Foundation
8*11be35a1SLionel Sambuc  * by Jukka Ruohonen.
9*11be35a1SLionel Sambuc  *
10*11be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
11*11be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
12*11be35a1SLionel Sambuc  * are met:
13*11be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
14*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
15*11be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17*11be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18*11be35a1SLionel Sambuc  *
19*11be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*11be35a1SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*11be35a1SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*11be35a1SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*11be35a1SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*11be35a1SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*11be35a1SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*11be35a1SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*11be35a1SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*11be35a1SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*11be35a1SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
30*11be35a1SLionel Sambuc  */
31*11be35a1SLionel Sambuc #include <sys/cdefs.h>
32*11be35a1SLionel Sambuc __RCSID("$NetBSD: t_fopen.c,v 1.3 2011/09/14 14:34:37 martin Exp $");
33*11be35a1SLionel Sambuc 
34*11be35a1SLionel Sambuc #include <atf-c.h>
35*11be35a1SLionel Sambuc #include <errno.h>
36*11be35a1SLionel Sambuc #include <fcntl.h>
37*11be35a1SLionel Sambuc #include <limits.h>
38*11be35a1SLionel Sambuc #include <paths.h>
39*11be35a1SLionel Sambuc #include <stdio.h>
40*11be35a1SLionel Sambuc #include <string.h>
41*11be35a1SLionel Sambuc #include <unistd.h>
42*11be35a1SLionel Sambuc 
43*11be35a1SLionel Sambuc static const char *path = "fopen";
44*11be35a1SLionel Sambuc 
45*11be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(fdopen_close);
ATF_TC_HEAD(fdopen_close,tc)46*11be35a1SLionel Sambuc ATF_TC_HEAD(fdopen_close, tc)
47*11be35a1SLionel Sambuc {
48*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "See that descriptors are closed");
49*11be35a1SLionel Sambuc }
50*11be35a1SLionel Sambuc 
ATF_TC_BODY(fdopen_close,tc)51*11be35a1SLionel Sambuc ATF_TC_BODY(fdopen_close, tc)
52*11be35a1SLionel Sambuc {
53*11be35a1SLionel Sambuc 	FILE *f;
54*11be35a1SLionel Sambuc 	int fd;
55*11be35a1SLionel Sambuc 
56*11be35a1SLionel Sambuc 	/*
57*11be35a1SLionel Sambuc 	 * Check that the file descriptor
58*11be35a1SLionel Sambuc 	 * used to fdopen(3) a stream is
59*11be35a1SLionel Sambuc 	 * closed once the stream is closed.
60*11be35a1SLionel Sambuc 	 */
61*11be35a1SLionel Sambuc 	fd = open(path, O_RDWR | O_CREAT);
62*11be35a1SLionel Sambuc 
63*11be35a1SLionel Sambuc 	ATF_REQUIRE(fd >= 0);
64*11be35a1SLionel Sambuc 
65*11be35a1SLionel Sambuc 	f = fdopen(fd, "w+");
66*11be35a1SLionel Sambuc 
67*11be35a1SLionel Sambuc 	ATF_REQUIRE(f != NULL);
68*11be35a1SLionel Sambuc 	ATF_REQUIRE(fclose(f) == 0);
69*11be35a1SLionel Sambuc 	ATF_REQUIRE(close(fd) == -1);
70*11be35a1SLionel Sambuc 	ATF_REQUIRE(unlink(path) == 0);
71*11be35a1SLionel Sambuc }
72*11be35a1SLionel Sambuc 
ATF_TC_CLEANUP(fdopen_close,tc)73*11be35a1SLionel Sambuc ATF_TC_CLEANUP(fdopen_close, tc)
74*11be35a1SLionel Sambuc {
75*11be35a1SLionel Sambuc 	(void)unlink(path);
76*11be35a1SLionel Sambuc }
77*11be35a1SLionel Sambuc 
78*11be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(fdopen_err);
ATF_TC_HEAD(fdopen_err,tc)79*11be35a1SLionel Sambuc ATF_TC_HEAD(fdopen_err, tc)
80*11be35a1SLionel Sambuc {
81*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test errors from fdopen(3)");
82*11be35a1SLionel Sambuc }
83*11be35a1SLionel Sambuc 
ATF_TC_BODY(fdopen_err,tc)84*11be35a1SLionel Sambuc ATF_TC_BODY(fdopen_err, tc)
85*11be35a1SLionel Sambuc {
86*11be35a1SLionel Sambuc 	int fd;
87*11be35a1SLionel Sambuc 
88*11be35a1SLionel Sambuc 	fd = open(path, O_RDONLY | O_CREAT);
89*11be35a1SLionel Sambuc 	ATF_REQUIRE(fd >= 0);
90*11be35a1SLionel Sambuc 
91*11be35a1SLionel Sambuc 	errno = 0;
92*11be35a1SLionel Sambuc 	ATF_REQUIRE_ERRNO(EINVAL, fdopen(fd, "w") == NULL);
93*11be35a1SLionel Sambuc 
94*11be35a1SLionel Sambuc 	errno = 0;
95*11be35a1SLionel Sambuc 	ATF_REQUIRE_ERRNO(EINVAL, fdopen(fd, "a") == NULL);
96*11be35a1SLionel Sambuc 
97*11be35a1SLionel Sambuc 	ATF_REQUIRE(close(fd) == 0);
98*11be35a1SLionel Sambuc 
99*11be35a1SLionel Sambuc 	errno = 0;
100*11be35a1SLionel Sambuc 	ATF_REQUIRE_ERRNO(EBADF, fdopen(fd, "r") == NULL);
101*11be35a1SLionel Sambuc 
102*11be35a1SLionel Sambuc 	errno = 0;
103*11be35a1SLionel Sambuc 	ATF_REQUIRE_ERRNO(EBADF, fdopen(-1, "w+") == NULL);
104*11be35a1SLionel Sambuc 
105*11be35a1SLionel Sambuc 	(void)unlink(path);
106*11be35a1SLionel Sambuc }
107*11be35a1SLionel Sambuc 
ATF_TC_CLEANUP(fdopen_err,tc)108*11be35a1SLionel Sambuc ATF_TC_CLEANUP(fdopen_err, tc)
109*11be35a1SLionel Sambuc {
110*11be35a1SLionel Sambuc 	(void)unlink(path);
111*11be35a1SLionel Sambuc }
112*11be35a1SLionel Sambuc 
113*11be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(fdopen_seek);
ATF_TC_HEAD(fdopen_seek,tc)114*11be35a1SLionel Sambuc ATF_TC_HEAD(fdopen_seek, tc)
115*11be35a1SLionel Sambuc {
116*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test stream position with fdopen(3)");
117*11be35a1SLionel Sambuc }
118*11be35a1SLionel Sambuc 
ATF_TC_BODY(fdopen_seek,tc)119*11be35a1SLionel Sambuc ATF_TC_BODY(fdopen_seek, tc)
120*11be35a1SLionel Sambuc {
121*11be35a1SLionel Sambuc 	FILE *f;
122*11be35a1SLionel Sambuc 	int fd;
123*11be35a1SLionel Sambuc 
124*11be35a1SLionel Sambuc 	/*
125*11be35a1SLionel Sambuc 	 * Verify that the file position associated
126*11be35a1SLionel Sambuc 	 * with the stream corresponds with the offset
127*11be35a1SLionel Sambuc 	 * set earlier for the file descriptor.
128*11be35a1SLionel Sambuc 	 */
129*11be35a1SLionel Sambuc 	fd = open(path, O_RDWR | O_CREAT);
130*11be35a1SLionel Sambuc 
131*11be35a1SLionel Sambuc 	ATF_REQUIRE(fd >= 0);
132*11be35a1SLionel Sambuc 	ATF_REQUIRE(write(fd, "garbage", 7) == 7);
133*11be35a1SLionel Sambuc 	ATF_REQUIRE(lseek(fd, 3, SEEK_SET) == 3);
134*11be35a1SLionel Sambuc 
135*11be35a1SLionel Sambuc 	f = fdopen(fd, "r+");
136*11be35a1SLionel Sambuc 
137*11be35a1SLionel Sambuc 	ATF_REQUIRE(f != NULL);
138*11be35a1SLionel Sambuc 	ATF_REQUIRE(ftell(f) == 3);
139*11be35a1SLionel Sambuc 	ATF_REQUIRE(fclose(f) == 0);
140*11be35a1SLionel Sambuc 	ATF_REQUIRE(unlink(path) == 0);
141*11be35a1SLionel Sambuc }
142*11be35a1SLionel Sambuc 
ATF_TC_CLEANUP(fdopen_seek,tc)143*11be35a1SLionel Sambuc ATF_TC_CLEANUP(fdopen_seek, tc)
144*11be35a1SLionel Sambuc {
145*11be35a1SLionel Sambuc 	(void)unlink(path);
146*11be35a1SLionel Sambuc }
147*11be35a1SLionel Sambuc 
148*11be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(fopen_err);
ATF_TC_HEAD(fopen_err,tc)149*11be35a1SLionel Sambuc ATF_TC_HEAD(fopen_err, tc)
150*11be35a1SLionel Sambuc {
151*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test errors from fopen(3)");
152*11be35a1SLionel Sambuc }
153*11be35a1SLionel Sambuc 
ATF_TC_BODY(fopen_err,tc)154*11be35a1SLionel Sambuc ATF_TC_BODY(fopen_err, tc)
155*11be35a1SLionel Sambuc {
156*11be35a1SLionel Sambuc 	static const char *mode[] = {
157*11be35a1SLionel Sambuc 		"x", "xr", "xr", "+r+", "R", "W+", " aXX", "Xr", " r+", "" };
158*11be35a1SLionel Sambuc 
159*11be35a1SLionel Sambuc 	char buf[PATH_MAX + 1];
160*11be35a1SLionel Sambuc 	size_t i;
161*11be35a1SLionel Sambuc 	FILE *f;
162*11be35a1SLionel Sambuc 
163*11be35a1SLionel Sambuc 	f = fopen(path, "w+");
164*11be35a1SLionel Sambuc 
165*11be35a1SLionel Sambuc 	ATF_REQUIRE(f != NULL);
166*11be35a1SLionel Sambuc 	ATF_REQUIRE(fclose(f) == 0);
167*11be35a1SLionel Sambuc 
168*11be35a1SLionel Sambuc 	/*
169*11be35a1SLionel Sambuc 	 * Note that also "invalid" characters
170*11be35a1SLionel Sambuc 	 * may follow the mode-string whenever
171*11be35a1SLionel Sambuc 	 * the first character is valid.
172*11be35a1SLionel Sambuc 	 */
173*11be35a1SLionel Sambuc 	for (i = 0; i < __arraycount(mode); i++) {
174*11be35a1SLionel Sambuc 
175*11be35a1SLionel Sambuc 		errno = 0;
176*11be35a1SLionel Sambuc 		f = fopen(path, mode[i]);
177*11be35a1SLionel Sambuc 
178*11be35a1SLionel Sambuc 		if (f == NULL && errno == EINVAL)
179*11be35a1SLionel Sambuc 			continue;
180*11be35a1SLionel Sambuc 
181*11be35a1SLionel Sambuc 		if (f != NULL)
182*11be35a1SLionel Sambuc 			(void)fclose(f);
183*11be35a1SLionel Sambuc 
184*11be35a1SLionel Sambuc 		atf_tc_fail_nonfatal("opened file as '%s'",  mode[i]);
185*11be35a1SLionel Sambuc 	}
186*11be35a1SLionel Sambuc 
187*11be35a1SLionel Sambuc 	(void)unlink(path);
188*11be35a1SLionel Sambuc 	(void)memset(buf, 'x', sizeof(buf));
189*11be35a1SLionel Sambuc 
190*11be35a1SLionel Sambuc 	errno = 0;
191*11be35a1SLionel Sambuc 	ATF_REQUIRE_ERRNO(EISDIR, fopen("/usr/bin", "w") == NULL);
192*11be35a1SLionel Sambuc 
193*11be35a1SLionel Sambuc 	errno = 0;
194*11be35a1SLionel Sambuc 	ATF_REQUIRE_ERRNO(ENOENT, fopen("/a/b/c/d/e/f", "r") == NULL);
195*11be35a1SLionel Sambuc 
196*11be35a1SLionel Sambuc 	errno = 0;
197*11be35a1SLionel Sambuc 	ATF_REQUIRE_ERRNO(ENAMETOOLONG, fopen(buf, "r+") == NULL);
198*11be35a1SLionel Sambuc }
199*11be35a1SLionel Sambuc 
ATF_TC_CLEANUP(fopen_err,tc)200*11be35a1SLionel Sambuc ATF_TC_CLEANUP(fopen_err, tc)
201*11be35a1SLionel Sambuc {
202*11be35a1SLionel Sambuc 	(void)unlink(path);
203*11be35a1SLionel Sambuc }
204*11be35a1SLionel Sambuc 
205*11be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(fopen_append);
ATF_TC_HEAD(fopen_append,tc)206*11be35a1SLionel Sambuc ATF_TC_HEAD(fopen_append, tc)
207*11be35a1SLionel Sambuc {
208*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test that append-mode works");
209*11be35a1SLionel Sambuc }
210*11be35a1SLionel Sambuc 
ATF_TC_BODY(fopen_append,tc)211*11be35a1SLionel Sambuc ATF_TC_BODY(fopen_append, tc)
212*11be35a1SLionel Sambuc {
213*11be35a1SLionel Sambuc 	char buf[15];
214*11be35a1SLionel Sambuc 	FILE *f;
215*11be35a1SLionel Sambuc 
216*11be35a1SLionel Sambuc 	(void)memset(buf, 'x', sizeof(buf));
217*11be35a1SLionel Sambuc 
218*11be35a1SLionel Sambuc 	f = fopen(path, "w+");
219*11be35a1SLionel Sambuc 
220*11be35a1SLionel Sambuc 	ATF_REQUIRE(f != NULL);
221*11be35a1SLionel Sambuc 	ATF_REQUIRE(fwrite("garbage", 1, 7, f) == 7);
222*11be35a1SLionel Sambuc 	ATF_REQUIRE(fclose(f) == 0);
223*11be35a1SLionel Sambuc 
224*11be35a1SLionel Sambuc 	f = fopen(path, "a");
225*11be35a1SLionel Sambuc 
226*11be35a1SLionel Sambuc 	ATF_REQUIRE(fwrite("garbage", 1, 7, f) == 7);
227*11be35a1SLionel Sambuc 	ATF_REQUIRE(fclose(f) == 0);
228*11be35a1SLionel Sambuc 
229*11be35a1SLionel Sambuc 	f = fopen(path, "r");
230*11be35a1SLionel Sambuc 
231*11be35a1SLionel Sambuc 	ATF_REQUIRE(fread(buf, 1, sizeof(buf), f) == 14);
232*11be35a1SLionel Sambuc 	ATF_REQUIRE(strncmp(buf, "garbagegarbage", 14) == 0);
233*11be35a1SLionel Sambuc 
234*11be35a1SLionel Sambuc 	ATF_REQUIRE(fclose(f) == 0);
235*11be35a1SLionel Sambuc 	ATF_REQUIRE(unlink(path) == 0);
236*11be35a1SLionel Sambuc }
237*11be35a1SLionel Sambuc 
ATF_TC_CLEANUP(fopen_append,tc)238*11be35a1SLionel Sambuc ATF_TC_CLEANUP(fopen_append, tc)
239*11be35a1SLionel Sambuc {
240*11be35a1SLionel Sambuc 	(void)unlink(path);
241*11be35a1SLionel Sambuc }
242*11be35a1SLionel Sambuc 
243*11be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(fopen_mode);
ATF_TC_HEAD(fopen_mode,tc)244*11be35a1SLionel Sambuc ATF_TC_HEAD(fopen_mode, tc)
245*11be35a1SLionel Sambuc {
246*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test fopen(3) modes");
247*11be35a1SLionel Sambuc }
248*11be35a1SLionel Sambuc 
ATF_TC_BODY(fopen_mode,tc)249*11be35a1SLionel Sambuc ATF_TC_BODY(fopen_mode, tc)
250*11be35a1SLionel Sambuc {
251*11be35a1SLionel Sambuc 	size_t i;
252*11be35a1SLionel Sambuc 	FILE *f;
253*11be35a1SLionel Sambuc 
254*11be35a1SLionel Sambuc 	static const char *mode[] = {
255*11be35a1SLionel Sambuc 		"r", "r+", "w", "w+", "a", "a+",
256*11be35a1SLionel Sambuc 		"rb", "r+b", "wb", "w+b", "ab", "a+b"
257*11be35a1SLionel Sambuc 		"re", "r+e", "we", "w+e", "ae", "a+e"
258*11be35a1SLionel Sambuc 		"rf", "r+f", "wf", "w+f", "af", "a+f"
259*11be35a1SLionel Sambuc 	};
260*11be35a1SLionel Sambuc 
261*11be35a1SLionel Sambuc 	f = fopen(path, "w+");
262*11be35a1SLionel Sambuc 
263*11be35a1SLionel Sambuc 	ATF_REQUIRE(f != NULL);
264*11be35a1SLionel Sambuc 	ATF_REQUIRE(fclose(f) == 0);
265*11be35a1SLionel Sambuc 
266*11be35a1SLionel Sambuc 	/*
267*11be35a1SLionel Sambuc 	 * Verify that various modes work.
268*11be35a1SLionel Sambuc 	 */
269*11be35a1SLionel Sambuc 	for (i = 0; i < __arraycount(mode); i++) {
270*11be35a1SLionel Sambuc 
271*11be35a1SLionel Sambuc 		f = fopen(path, mode[i]);
272*11be35a1SLionel Sambuc 
273*11be35a1SLionel Sambuc 		if (f != NULL) {
274*11be35a1SLionel Sambuc 			ATF_REQUIRE(fclose(f) == 0);
275*11be35a1SLionel Sambuc 			continue;
276*11be35a1SLionel Sambuc 		}
277*11be35a1SLionel Sambuc 
278*11be35a1SLionel Sambuc 		atf_tc_fail_nonfatal("failed to open file as %s",  mode[i]);
279*11be35a1SLionel Sambuc 	}
280*11be35a1SLionel Sambuc 
281*11be35a1SLionel Sambuc 	(void)unlink(path);
282*11be35a1SLionel Sambuc }
283*11be35a1SLionel Sambuc 
ATF_TC_CLEANUP(fopen_mode,tc)284*11be35a1SLionel Sambuc ATF_TC_CLEANUP(fopen_mode, tc)
285*11be35a1SLionel Sambuc {
286*11be35a1SLionel Sambuc 	(void)unlink(path);
287*11be35a1SLionel Sambuc }
288*11be35a1SLionel Sambuc 
289*11be35a1SLionel Sambuc ATF_TC(fopen_perm);
ATF_TC_HEAD(fopen_perm,tc)290*11be35a1SLionel Sambuc ATF_TC_HEAD(fopen_perm, tc)
291*11be35a1SLionel Sambuc {
292*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test permissions with fopen(3)");
293*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "require.user", "unprivileged");
294*11be35a1SLionel Sambuc }
295*11be35a1SLionel Sambuc 
ATF_TC_BODY(fopen_perm,tc)296*11be35a1SLionel Sambuc ATF_TC_BODY(fopen_perm, tc)
297*11be35a1SLionel Sambuc {
298*11be35a1SLionel Sambuc 
299*11be35a1SLionel Sambuc 	errno = 0;
300*11be35a1SLionel Sambuc 	ATF_REQUIRE_ERRNO(EACCES, fopen("/bin/ls", "a+") == NULL);
301*11be35a1SLionel Sambuc 
302*11be35a1SLionel Sambuc 	errno = 0;
303*11be35a1SLionel Sambuc 	ATF_REQUIRE_ERRNO(EACCES, fopen("/bin/ls", "w+") == NULL);
304*11be35a1SLionel Sambuc }
305*11be35a1SLionel Sambuc 
306*11be35a1SLionel Sambuc ATF_TC(fopen_regular);
ATF_TC_HEAD(fopen_regular,tc)307*11be35a1SLionel Sambuc ATF_TC_HEAD(fopen_regular, tc)
308*11be35a1SLionel Sambuc {
309*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test fopen(3) with 'f' mode");
310*11be35a1SLionel Sambuc }
311*11be35a1SLionel Sambuc 
ATF_TC_BODY(fopen_regular,tc)312*11be35a1SLionel Sambuc ATF_TC_BODY(fopen_regular, tc)
313*11be35a1SLionel Sambuc {
314*11be35a1SLionel Sambuc 	static const char *mode[] = { "rf", "r+f", "wf", "w+f", "af", "a+f" };
315*11be35a1SLionel Sambuc 	static const char *devs[] = { _PATH_DEVNULL };
316*11be35a1SLionel Sambuc 
317*11be35a1SLionel Sambuc 	size_t i, j;
318*11be35a1SLionel Sambuc 	FILE *f;
319*11be35a1SLionel Sambuc 
320*11be35a1SLionel Sambuc 	for (i = 0; i < __arraycount(devs); i++) {
321*11be35a1SLionel Sambuc 
322*11be35a1SLionel Sambuc 		for (j = 0; j < __arraycount(mode); j++) {
323*11be35a1SLionel Sambuc 
324*11be35a1SLionel Sambuc 			errno = 0;
325*11be35a1SLionel Sambuc 			f = fopen(devs[i], mode[j]);
326*11be35a1SLionel Sambuc 
327*11be35a1SLionel Sambuc 			if (f == NULL && errno == EFTYPE)
328*11be35a1SLionel Sambuc 				continue;
329*11be35a1SLionel Sambuc 
330*11be35a1SLionel Sambuc 			if (f != NULL)
331*11be35a1SLionel Sambuc 				(void)fclose(f);
332*11be35a1SLionel Sambuc 
333*11be35a1SLionel Sambuc 			atf_tc_fail_nonfatal("opened %s as %s",
334*11be35a1SLionel Sambuc 			    devs[i], mode[j]);
335*11be35a1SLionel Sambuc 		}
336*11be35a1SLionel Sambuc 	}
337*11be35a1SLionel Sambuc }
338*11be35a1SLionel Sambuc 
339*11be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(fopen_seek);
ATF_TC_HEAD(fopen_seek,tc)340*11be35a1SLionel Sambuc ATF_TC_HEAD(fopen_seek, tc)
341*11be35a1SLionel Sambuc {
342*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test initial stream position");
343*11be35a1SLionel Sambuc }
344*11be35a1SLionel Sambuc 
ATF_TC_BODY(fopen_seek,tc)345*11be35a1SLionel Sambuc ATF_TC_BODY(fopen_seek, tc)
346*11be35a1SLionel Sambuc {
347*11be35a1SLionel Sambuc 	FILE *f;
348*11be35a1SLionel Sambuc 
349*11be35a1SLionel Sambuc 	f = fopen(path, "w+");
350*11be35a1SLionel Sambuc 
351*11be35a1SLionel Sambuc 	ATF_REQUIRE(f != NULL);
352*11be35a1SLionel Sambuc 	ATF_REQUIRE(fwrite("garbage", 1, 7, f) == 7);
353*11be35a1SLionel Sambuc 	ATF_REQUIRE(fclose(f) == 0);
354*11be35a1SLionel Sambuc 
355*11be35a1SLionel Sambuc 	/*
356*11be35a1SLionel Sambuc 	 * The position of the stream should be
357*11be35a1SLionel Sambuc 	 * at the start, except for append-mode.
358*11be35a1SLionel Sambuc 	 */
359*11be35a1SLionel Sambuc 	f = fopen(path, "r");
360*11be35a1SLionel Sambuc 
361*11be35a1SLionel Sambuc 	ATF_REQUIRE(f != NULL);
362*11be35a1SLionel Sambuc 	ATF_REQUIRE(ftello(f) == 0);
363*11be35a1SLionel Sambuc 	ATF_REQUIRE(fclose(f) == 0);
364*11be35a1SLionel Sambuc 
365*11be35a1SLionel Sambuc 	f = fopen(path, "a");
366*11be35a1SLionel Sambuc 
367*11be35a1SLionel Sambuc 	ATF_REQUIRE(f != NULL);
368*11be35a1SLionel Sambuc 	ATF_REQUIRE(ftello(f) == 7);
369*11be35a1SLionel Sambuc 	ATF_REQUIRE(fclose(f) == 0);
370*11be35a1SLionel Sambuc 	ATF_REQUIRE(unlink(path) == 0);
371*11be35a1SLionel Sambuc }
372*11be35a1SLionel Sambuc 
ATF_TC_CLEANUP(fopen_seek,tc)373*11be35a1SLionel Sambuc ATF_TC_CLEANUP(fopen_seek, tc)
374*11be35a1SLionel Sambuc {
375*11be35a1SLionel Sambuc 	(void)unlink(path);
376*11be35a1SLionel Sambuc }
377*11be35a1SLionel Sambuc 
378*11be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(freopen_std);
ATF_TC_HEAD(freopen_std,tc)379*11be35a1SLionel Sambuc ATF_TC_HEAD(freopen_std, tc)
380*11be35a1SLionel Sambuc {
381*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "A basic test of freopen(3)");
382*11be35a1SLionel Sambuc }
383*11be35a1SLionel Sambuc 
ATF_TC_BODY(freopen_std,tc)384*11be35a1SLionel Sambuc ATF_TC_BODY(freopen_std, tc)
385*11be35a1SLionel Sambuc {
386*11be35a1SLionel Sambuc 	FILE *std[2] = { stdin, stdout };
387*11be35a1SLionel Sambuc 	char buf[15];
388*11be35a1SLionel Sambuc 	size_t i;
389*11be35a1SLionel Sambuc 	FILE *f;
390*11be35a1SLionel Sambuc 
391*11be35a1SLionel Sambuc 	/*
392*11be35a1SLionel Sambuc 	 * Associate a standard stream with a custom stream.
393*11be35a1SLionel Sambuc 	 * Then write to the standard stream and verify that
394*11be35a1SLionel Sambuc 	 * the result now appears in the custom stream.
395*11be35a1SLionel Sambuc 	 */
396*11be35a1SLionel Sambuc 	for (i = 0; i < __arraycount(std); i++) {
397*11be35a1SLionel Sambuc 
398*11be35a1SLionel Sambuc 		(void)memset(buf, 'x', sizeof(buf));
399*11be35a1SLionel Sambuc 
400*11be35a1SLionel Sambuc 		f = fopen(path, "w+");
401*11be35a1SLionel Sambuc 		ATF_REQUIRE(f != NULL);
402*11be35a1SLionel Sambuc 
403*11be35a1SLionel Sambuc 		f = freopen(path, "w+", std[i]);
404*11be35a1SLionel Sambuc 		ATF_REQUIRE(f != NULL);
405*11be35a1SLionel Sambuc 
406*11be35a1SLionel Sambuc 		ATF_REQUIRE(fwrite("garbage", 1, 7, f) == 7);
407*11be35a1SLionel Sambuc 		ATF_REQUIRE(fprintf(std[i], "garbage") == 7);
408*11be35a1SLionel Sambuc 		ATF_REQUIRE(fclose(f) == 0);
409*11be35a1SLionel Sambuc 
410*11be35a1SLionel Sambuc 		f = fopen(path, "r");
411*11be35a1SLionel Sambuc 
412*11be35a1SLionel Sambuc 		ATF_REQUIRE(f != NULL);
413*11be35a1SLionel Sambuc 		ATF_REQUIRE(fread(buf, 1, sizeof(buf), f) == 14);
414*11be35a1SLionel Sambuc 		ATF_REQUIRE(strncmp(buf, "garbagegarbage", 14) == 0);
415*11be35a1SLionel Sambuc 
416*11be35a1SLionel Sambuc 		ATF_REQUIRE(fclose(f) == 0);
417*11be35a1SLionel Sambuc 	}
418*11be35a1SLionel Sambuc 
419*11be35a1SLionel Sambuc 	ATF_REQUIRE(unlink(path) == 0);
420*11be35a1SLionel Sambuc }
421*11be35a1SLionel Sambuc 
ATF_TC_CLEANUP(freopen_std,tc)422*11be35a1SLionel Sambuc ATF_TC_CLEANUP(freopen_std, tc)
423*11be35a1SLionel Sambuc {
424*11be35a1SLionel Sambuc 	(void)unlink(path);
425*11be35a1SLionel Sambuc }
426*11be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)427*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
428*11be35a1SLionel Sambuc {
429*11be35a1SLionel Sambuc 
430*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, fdopen_close);
431*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, fdopen_err);
432*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, fdopen_seek);
433*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, fopen_append);
434*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, fopen_err);
435*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, fopen_mode);
436*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, fopen_perm);
437*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, fopen_regular);
438*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, fopen_seek);
439*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, freopen_std);
440*11be35a1SLionel Sambuc 
441*11be35a1SLionel Sambuc 	return atf_no_error();
442*11be35a1SLionel Sambuc }
443