xref: /aosp_15_r20/external/ltp/testcases/kernel/fs/stream/stream01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  *
3*49cdfc7eSAndroid Build Coastguard Worker  *   Copyright (c) International Business Machines  Corp., 2002
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  *   This program is free software;  you can redistribute it and/or modify
6*49cdfc7eSAndroid Build Coastguard Worker  *   it under the terms of the GNU General Public License as published by
7*49cdfc7eSAndroid Build Coastguard Worker  *   the Free Software Foundation; either version 2 of the License, or
8*49cdfc7eSAndroid Build Coastguard Worker  *   (at your option) any later version.
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  *   This program is distributed in the hope that it will be useful,
11*49cdfc7eSAndroid Build Coastguard Worker  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12*49cdfc7eSAndroid Build Coastguard Worker  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13*49cdfc7eSAndroid Build Coastguard Worker  *   the GNU General Public License for more details.
14*49cdfc7eSAndroid Build Coastguard Worker  *
15*49cdfc7eSAndroid Build Coastguard Worker  *   You should have received a copy of the GNU General Public License
16*49cdfc7eSAndroid Build Coastguard Worker  *   along with this program;  if not, write to the Free Software
17*49cdfc7eSAndroid Build Coastguard Worker  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*49cdfc7eSAndroid Build Coastguard Worker  */
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker /* ported from SPIE section2/filesuite/stream1.c, by Airong Zhang */
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker /*======================================================================
23*49cdfc7eSAndroid Build Coastguard Worker 	=================== TESTPLAN SEGMENT ===================
24*49cdfc7eSAndroid Build Coastguard Worker >KEYS:  < freopen()
25*49cdfc7eSAndroid Build Coastguard Worker >WHAT:  < 1) check that freopen substitutes the named file in place of stream.
26*49cdfc7eSAndroid Build Coastguard Worker >HOW:   < 1) open a stream, write something to it, perform freopen and
27*49cdfc7eSAndroid Build Coastguard Worker 	<    write some more. Check that second write to stream went to
28*49cdfc7eSAndroid Build Coastguard Worker 	<    the file specified by freopen.
29*49cdfc7eSAndroid Build Coastguard Worker >BUGS:  <
30*49cdfc7eSAndroid Build Coastguard Worker ======================================================================*/
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
33*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
34*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "stream01";
37*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 1;
38*49cdfc7eSAndroid Build Coastguard Worker int local_flag;
39*49cdfc7eSAndroid Build Coastguard Worker 
40*49cdfc7eSAndroid Build Coastguard Worker #define PASSED 1
41*49cdfc7eSAndroid Build Coastguard Worker #define FAILED 0
42*49cdfc7eSAndroid Build Coastguard Worker 
43*49cdfc7eSAndroid Build Coastguard Worker /* XXX: add setup and cleanup. */
44*49cdfc7eSAndroid Build Coastguard Worker 
45*49cdfc7eSAndroid Build Coastguard Worker char progname[] = "stream01()";
46*49cdfc7eSAndroid Build Coastguard Worker char tempfile1[40] = "";
47*49cdfc7eSAndroid Build Coastguard Worker char tempfile2[40] = "";
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker /*--------------------------------------------------------------------*/
main(int ac,char * av[])50*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char *av[])
51*49cdfc7eSAndroid Build Coastguard Worker {
52*49cdfc7eSAndroid Build Coastguard Worker 	FILE *stream;
53*49cdfc7eSAndroid Build Coastguard Worker 	char buf[10];
54*49cdfc7eSAndroid Build Coastguard Worker 	int i;
55*49cdfc7eSAndroid Build Coastguard Worker 	int lc;
56*49cdfc7eSAndroid Build Coastguard Worker 
57*49cdfc7eSAndroid Build Coastguard Worker 	/*
58*49cdfc7eSAndroid Build Coastguard Worker 	 * parse standard options
59*49cdfc7eSAndroid Build Coastguard Worker 	 */
60*49cdfc7eSAndroid Build Coastguard Worker 	tst_parse_opts(ac, av, NULL, NULL);
61*49cdfc7eSAndroid Build Coastguard Worker 
62*49cdfc7eSAndroid Build Coastguard Worker 	local_flag = PASSED;
63*49cdfc7eSAndroid Build Coastguard Worker 	tst_tmpdir();
64*49cdfc7eSAndroid Build Coastguard Worker 	for (lc = 0; TEST_LOOPING(lc); lc++) {
65*49cdfc7eSAndroid Build Coastguard Worker 
66*49cdfc7eSAndroid Build Coastguard Worker 		sprintf(tempfile1, "stream011.%d", getpid());
67*49cdfc7eSAndroid Build Coastguard Worker 		sprintf(tempfile2, "stream012.%d", getpid());
68*49cdfc7eSAndroid Build Coastguard Worker 	/*--------------------------------------------------------------------*/
69*49cdfc7eSAndroid Build Coastguard Worker 		//block0:
70*49cdfc7eSAndroid Build Coastguard Worker 		if ((stream = fopen(tempfile1, "a+")) == NULL) {
71*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TFAIL, NULL, "fopen(%s) a+ failed: %s",
72*49cdfc7eSAndroid Build Coastguard Worker 				 tempfile1,
73*49cdfc7eSAndroid Build Coastguard Worker 				 strerror(errno));
74*49cdfc7eSAndroid Build Coastguard Worker 		}
75*49cdfc7eSAndroid Build Coastguard Worker 		fwrite("a", 1, 1, stream);
76*49cdfc7eSAndroid Build Coastguard Worker 		if ((stream = freopen(tempfile2, "a+", stream)) == NULL) {
77*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TFAIL | TERRNO, NULL, "freopen(%s) a+ failed",
78*49cdfc7eSAndroid Build Coastguard Worker 				 tempfile2);
79*49cdfc7eSAndroid Build Coastguard Worker 		}
80*49cdfc7eSAndroid Build Coastguard Worker 		fwrite("a", 1, 1, stream);
81*49cdfc7eSAndroid Build Coastguard Worker 		fclose(stream);
82*49cdfc7eSAndroid Build Coastguard Worker 
83*49cdfc7eSAndroid Build Coastguard Worker 		/* now check that a single "a" is in each file */
84*49cdfc7eSAndroid Build Coastguard Worker 		if ((stream = fopen(tempfile1, "r")) == NULL) {
85*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TFAIL | TERRNO, NULL, "fopen(%s) r failed",
86*49cdfc7eSAndroid Build Coastguard Worker 				 tempfile1);
87*49cdfc7eSAndroid Build Coastguard Worker 		} else {
88*49cdfc7eSAndroid Build Coastguard Worker 			for (i = 0; i < 10; i++)
89*49cdfc7eSAndroid Build Coastguard Worker 				buf[i] = 0;
90*49cdfc7eSAndroid Build Coastguard Worker 			fread(buf, 1, 1, stream);
91*49cdfc7eSAndroid Build Coastguard Worker 			if ((buf[0] != 'a') || (buf[1] != 0)) {
92*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TFAIL, "bad contents in %s",
93*49cdfc7eSAndroid Build Coastguard Worker 					 tempfile1);
94*49cdfc7eSAndroid Build Coastguard Worker 				local_flag = FAILED;
95*49cdfc7eSAndroid Build Coastguard Worker 			}
96*49cdfc7eSAndroid Build Coastguard Worker 			fclose(stream);
97*49cdfc7eSAndroid Build Coastguard Worker 		}
98*49cdfc7eSAndroid Build Coastguard Worker 		if ((stream = fopen(tempfile2, "r")) == NULL) {
99*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TFAIL | TERRNO, NULL, "fopen(%s) r failed",
100*49cdfc7eSAndroid Build Coastguard Worker 				 tempfile2);
101*49cdfc7eSAndroid Build Coastguard Worker 		} else {
102*49cdfc7eSAndroid Build Coastguard Worker 			for (i = 0; i < 10; i++)
103*49cdfc7eSAndroid Build Coastguard Worker 				buf[i] = 0;
104*49cdfc7eSAndroid Build Coastguard Worker 			fread(buf, 1, 1, stream);
105*49cdfc7eSAndroid Build Coastguard Worker 			if ((buf[0] != 'a') || (buf[1] != 0)) {
106*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TFAIL, "bad contents in %s",
107*49cdfc7eSAndroid Build Coastguard Worker 					 tempfile2);
108*49cdfc7eSAndroid Build Coastguard Worker 				local_flag = FAILED;
109*49cdfc7eSAndroid Build Coastguard Worker 			}
110*49cdfc7eSAndroid Build Coastguard Worker 			fclose(stream);
111*49cdfc7eSAndroid Build Coastguard Worker 		}
112*49cdfc7eSAndroid Build Coastguard Worker 		if (local_flag == PASSED) {
113*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TPASS, "Test passed.");
114*49cdfc7eSAndroid Build Coastguard Worker 		} else {
115*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "Test failed.");
116*49cdfc7eSAndroid Build Coastguard Worker 		}
117*49cdfc7eSAndroid Build Coastguard Worker 
118*49cdfc7eSAndroid Build Coastguard Worker 		local_flag = PASSED;
119*49cdfc7eSAndroid Build Coastguard Worker 
120*49cdfc7eSAndroid Build Coastguard Worker 	/*--------------------------------------------------------------------*/
121*49cdfc7eSAndroid Build Coastguard Worker 		unlink(tempfile1);
122*49cdfc7eSAndroid Build Coastguard Worker 		unlink(tempfile2);
123*49cdfc7eSAndroid Build Coastguard Worker 
124*49cdfc7eSAndroid Build Coastguard Worker 	}			/* end for */
125*49cdfc7eSAndroid Build Coastguard Worker 	tst_rmdir();
126*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
127*49cdfc7eSAndroid Build Coastguard Worker }
128