xref: /aosp_15_r20/external/ltp/testcases/kernel/fs/stream/stream05.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/stream.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:  < ferror() feof() clearerr() fileno()
25*49cdfc7eSAndroid Build Coastguard Worker >WHAT:  < 1) check that ferror returns zero
26*49cdfc7eSAndroid Build Coastguard Worker 	< 2) check fileno returns valid file descriptor
27*49cdfc7eSAndroid Build Coastguard Worker 	< 3) check that feof returns zero (nonzero) appropriately
28*49cdfc7eSAndroid Build Coastguard Worker 	< 4) check that clearerr resets EOF indicator.
29*49cdfc7eSAndroid Build Coastguard Worker >HOW:   < 1) open a stream and immediately execute ferror
30*49cdfc7eSAndroid Build Coastguard Worker 	< 2) use the file des returned from fileno to read a file
31*49cdfc7eSAndroid Build Coastguard Worker 	<    written with stream - compare actual vs expected.
32*49cdfc7eSAndroid Build Coastguard Worker 	< 3) open stream and ensure feof returns zero, read to end of
33*49cdfc7eSAndroid Build Coastguard Worker 	<    file and ensure feof returns non-zero.
34*49cdfc7eSAndroid Build Coastguard Worker 	< 4) after 3) above use clearerr and then use feof to ensure
35*49cdfc7eSAndroid Build Coastguard Worker 	<    clearerr worked
36*49cdfc7eSAndroid Build Coastguard Worker >BUGS:  <
37*49cdfc7eSAndroid Build Coastguard Worker ======================================================================*/
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
40*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
41*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
42*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "stream05";
45*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 1;
46*49cdfc7eSAndroid Build Coastguard Worker int local_flag;
47*49cdfc7eSAndroid Build Coastguard Worker 
48*49cdfc7eSAndroid Build Coastguard Worker #define PASSED 1
49*49cdfc7eSAndroid Build Coastguard Worker #define FAILED 0
50*49cdfc7eSAndroid Build Coastguard Worker 
51*49cdfc7eSAndroid Build Coastguard Worker char progname[] = "stream05()";
52*49cdfc7eSAndroid Build Coastguard Worker char tempfile[40] = "";
53*49cdfc7eSAndroid Build Coastguard Worker 
54*49cdfc7eSAndroid Build Coastguard Worker /*--------------------------------------------------------------------*/
main(int ac,char * av[])55*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char *av[])
56*49cdfc7eSAndroid Build Coastguard Worker {
57*49cdfc7eSAndroid Build Coastguard Worker 	FILE *stream;
58*49cdfc7eSAndroid Build Coastguard Worker 	char buf[10];
59*49cdfc7eSAndroid Build Coastguard Worker 	int nr, fd;
60*49cdfc7eSAndroid Build Coastguard Worker 
61*49cdfc7eSAndroid Build Coastguard Worker 	int lc;
62*49cdfc7eSAndroid Build Coastguard Worker 
63*49cdfc7eSAndroid Build Coastguard Worker 	/*
64*49cdfc7eSAndroid Build Coastguard Worker 	 * parse standard options
65*49cdfc7eSAndroid Build Coastguard Worker 	 */
66*49cdfc7eSAndroid Build Coastguard Worker 	tst_parse_opts(ac, av, NULL, NULL);
67*49cdfc7eSAndroid Build Coastguard Worker 	tst_tmpdir();
68*49cdfc7eSAndroid Build Coastguard Worker 	local_flag = PASSED;
69*49cdfc7eSAndroid Build Coastguard Worker 
70*49cdfc7eSAndroid Build Coastguard Worker 	for (lc = 0; TEST_LOOPING(lc); lc++) {
71*49cdfc7eSAndroid Build Coastguard Worker 		local_flag = PASSED;
72*49cdfc7eSAndroid Build Coastguard Worker 
73*49cdfc7eSAndroid Build Coastguard Worker 		sprintf(tempfile, "stream05.%d", getpid());
74*49cdfc7eSAndroid Build Coastguard Worker 	/*--------------------------------------------------------------------*/
75*49cdfc7eSAndroid Build Coastguard Worker 		//block0:
76*49cdfc7eSAndroid Build Coastguard Worker 		if ((stream = fopen(tempfile, "a+")) == NULL) {
77*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TFAIL, NULL, "fopen(%s) a+ failed: %s",
78*49cdfc7eSAndroid Build Coastguard Worker 				 tempfile,
79*49cdfc7eSAndroid Build Coastguard Worker 				 strerror(errno));
80*49cdfc7eSAndroid Build Coastguard Worker 		}
81*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(stream, "a");
82*49cdfc7eSAndroid Build Coastguard Worker 		fclose(stream);
83*49cdfc7eSAndroid Build Coastguard Worker 
84*49cdfc7eSAndroid Build Coastguard Worker 		if ((stream = fopen(tempfile, "r+")) == NULL) {
85*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TFAIL, NULL, "fopen(%s) r+ failed: %s",
86*49cdfc7eSAndroid Build Coastguard Worker 				 tempfile,
87*49cdfc7eSAndroid Build Coastguard Worker 				 strerror(errno));
88*49cdfc7eSAndroid Build Coastguard Worker 		}
89*49cdfc7eSAndroid Build Coastguard Worker 
90*49cdfc7eSAndroid Build Coastguard Worker 		/* check that ferror returns zero */
91*49cdfc7eSAndroid Build Coastguard Worker 		if (ferror(stream) != 0) {
92*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "ferror did not return zero: %s",
93*49cdfc7eSAndroid Build Coastguard Worker 				 strerror(errno));
94*49cdfc7eSAndroid Build Coastguard Worker 			local_flag = FAILED;
95*49cdfc7eSAndroid Build Coastguard Worker 		}
96*49cdfc7eSAndroid Build Coastguard Worker 
97*49cdfc7eSAndroid Build Coastguard Worker 		if (local_flag == PASSED) {
98*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TPASS, "Test passed in block0.");
99*49cdfc7eSAndroid Build Coastguard Worker 		} else {
100*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "Test failed in block0.");
101*49cdfc7eSAndroid Build Coastguard Worker 		}
102*49cdfc7eSAndroid Build Coastguard Worker 
103*49cdfc7eSAndroid Build Coastguard Worker 		local_flag = PASSED;
104*49cdfc7eSAndroid Build Coastguard Worker 
105*49cdfc7eSAndroid Build Coastguard Worker 	/*--------------------------------------------------------------------*/
106*49cdfc7eSAndroid Build Coastguard Worker 		//block1:
107*49cdfc7eSAndroid Build Coastguard Worker 
108*49cdfc7eSAndroid Build Coastguard Worker 		/* check that fileno returns valid file descriptor */
109*49cdfc7eSAndroid Build Coastguard Worker 		fd = fileno(stream);
110*49cdfc7eSAndroid Build Coastguard Worker 		if ((nr = read(fd, buf, 1)) < 0) {
111*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TFAIL, NULL, "read failed: %s",
112*49cdfc7eSAndroid Build Coastguard Worker 				 strerror(errno));
113*49cdfc7eSAndroid Build Coastguard Worker 		}
114*49cdfc7eSAndroid Build Coastguard Worker 		if (nr != 1) {
115*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "read did not read right number");
116*49cdfc7eSAndroid Build Coastguard Worker 			local_flag = FAILED;
117*49cdfc7eSAndroid Build Coastguard Worker 		}
118*49cdfc7eSAndroid Build Coastguard Worker 		if (buf[0] != 'a') {
119*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "read returned bad values");
120*49cdfc7eSAndroid Build Coastguard Worker 			local_flag = FAILED;
121*49cdfc7eSAndroid Build Coastguard Worker 		}
122*49cdfc7eSAndroid Build Coastguard Worker 		if (local_flag == PASSED) {
123*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TPASS, "Test passed in block1.");
124*49cdfc7eSAndroid Build Coastguard Worker 		} else {
125*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "Test failed in block1.");
126*49cdfc7eSAndroid Build Coastguard Worker 		}
127*49cdfc7eSAndroid Build Coastguard Worker 
128*49cdfc7eSAndroid Build Coastguard Worker 		local_flag = PASSED;
129*49cdfc7eSAndroid Build Coastguard Worker 	/*--------------------------------------------------------------------*/
130*49cdfc7eSAndroid Build Coastguard Worker 		//block2:
131*49cdfc7eSAndroid Build Coastguard Worker 
132*49cdfc7eSAndroid Build Coastguard Worker 		/* read to EOF and ensure feof returns non-zero */
133*49cdfc7eSAndroid Build Coastguard Worker 		fclose(stream);
134*49cdfc7eSAndroid Build Coastguard Worker 
135*49cdfc7eSAndroid Build Coastguard Worker 		if ((stream = fopen(tempfile, "r+")) == NULL) {
136*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TFAIL, NULL, "fopen(%s) r+ failed: %s",
137*49cdfc7eSAndroid Build Coastguard Worker 				 tempfile,
138*49cdfc7eSAndroid Build Coastguard Worker 				 strerror(errno));
139*49cdfc7eSAndroid Build Coastguard Worker 		}
140*49cdfc7eSAndroid Build Coastguard Worker 		if (feof(stream) != 0) {
141*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL,
142*49cdfc7eSAndroid Build Coastguard Worker 				 "feof returned non-zero when it should not: %s",
143*49cdfc7eSAndroid Build Coastguard Worker 				 strerror(errno));
144*49cdfc7eSAndroid Build Coastguard Worker 			local_flag = FAILED;
145*49cdfc7eSAndroid Build Coastguard Worker 		}
146*49cdfc7eSAndroid Build Coastguard Worker 		fread(buf, 1, 2, stream);	/* read to EOF */
147*49cdfc7eSAndroid Build Coastguard Worker 		if (feof(stream) == 0) {
148*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL,
149*49cdfc7eSAndroid Build Coastguard Worker 				 "feof returned zero when it should not: %s",
150*49cdfc7eSAndroid Build Coastguard Worker 				 strerror(errno));
151*49cdfc7eSAndroid Build Coastguard Worker 			local_flag = FAILED;
152*49cdfc7eSAndroid Build Coastguard Worker 		}
153*49cdfc7eSAndroid Build Coastguard Worker 
154*49cdfc7eSAndroid Build Coastguard Worker 		if (local_flag == PASSED) {
155*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TPASS, "Test passed in block2.");
156*49cdfc7eSAndroid Build Coastguard Worker 		} else {
157*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "Test failed in block2.");
158*49cdfc7eSAndroid Build Coastguard Worker 		}
159*49cdfc7eSAndroid Build Coastguard Worker 
160*49cdfc7eSAndroid Build Coastguard Worker 		local_flag = PASSED;
161*49cdfc7eSAndroid Build Coastguard Worker 	/*--------------------------------------------------------------------*/
162*49cdfc7eSAndroid Build Coastguard Worker 		//block3:
163*49cdfc7eSAndroid Build Coastguard Worker 		/* ensure clearerr works */
164*49cdfc7eSAndroid Build Coastguard Worker 		clearerr(stream);
165*49cdfc7eSAndroid Build Coastguard Worker 		if (feof(stream) != 0) {
166*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "clearerr failed: %s", strerror(errno));
167*49cdfc7eSAndroid Build Coastguard Worker 			local_flag = FAILED;
168*49cdfc7eSAndroid Build Coastguard Worker 		}
169*49cdfc7eSAndroid Build Coastguard Worker 		if (local_flag == PASSED) {
170*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TPASS, "Test passed in block3.");
171*49cdfc7eSAndroid Build Coastguard Worker 		} else {
172*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "Test failed in block3.");
173*49cdfc7eSAndroid Build Coastguard Worker 		}
174*49cdfc7eSAndroid Build Coastguard Worker 
175*49cdfc7eSAndroid Build Coastguard Worker 		local_flag = PASSED;
176*49cdfc7eSAndroid Build Coastguard Worker 	/*--------------------------------------------------------------------*/
177*49cdfc7eSAndroid Build Coastguard Worker 		//block4:
178*49cdfc7eSAndroid Build Coastguard Worker 
179*49cdfc7eSAndroid Build Coastguard Worker 		/* test fopen "b" flags -- should be allowed but ignored */
180*49cdfc7eSAndroid Build Coastguard Worker 		(void)fclose(stream);
181*49cdfc7eSAndroid Build Coastguard Worker 
182*49cdfc7eSAndroid Build Coastguard Worker 		if ((stream = fopen(tempfile, "rb")) == NULL) {
183*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TFAIL, NULL, "fopen(%s) rb failed: %s",
184*49cdfc7eSAndroid Build Coastguard Worker 				 tempfile,
185*49cdfc7eSAndroid Build Coastguard Worker 				 strerror(errno));
186*49cdfc7eSAndroid Build Coastguard Worker 		}
187*49cdfc7eSAndroid Build Coastguard Worker 		(void)fclose(stream);
188*49cdfc7eSAndroid Build Coastguard Worker 
189*49cdfc7eSAndroid Build Coastguard Worker 		if ((stream = fopen(tempfile, "wb")) == NULL) {
190*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TFAIL, NULL, "fopen(%s) wb failed: %s",
191*49cdfc7eSAndroid Build Coastguard Worker 				 tempfile,
192*49cdfc7eSAndroid Build Coastguard Worker 				 strerror(errno));
193*49cdfc7eSAndroid Build Coastguard Worker 		}
194*49cdfc7eSAndroid Build Coastguard Worker 		(void)fclose(stream);
195*49cdfc7eSAndroid Build Coastguard Worker 
196*49cdfc7eSAndroid Build Coastguard Worker 		if ((stream = fopen(tempfile, "ab")) == NULL) {
197*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TFAIL, NULL, "fopen(%s) ab failed: %s",
198*49cdfc7eSAndroid Build Coastguard Worker 				 tempfile,
199*49cdfc7eSAndroid Build Coastguard Worker 				 strerror(errno));
200*49cdfc7eSAndroid Build Coastguard Worker 		}
201*49cdfc7eSAndroid Build Coastguard Worker 		(void)fclose(stream);
202*49cdfc7eSAndroid Build Coastguard Worker 
203*49cdfc7eSAndroid Build Coastguard Worker 		if ((stream = fopen(tempfile, "rb+")) == NULL) {
204*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TFAIL, NULL, "fopen(%s) rb+ failed: %s",
205*49cdfc7eSAndroid Build Coastguard Worker 				 tempfile,
206*49cdfc7eSAndroid Build Coastguard Worker 				 strerror(errno));
207*49cdfc7eSAndroid Build Coastguard Worker 		}
208*49cdfc7eSAndroid Build Coastguard Worker 		(void)fclose(stream);
209*49cdfc7eSAndroid Build Coastguard Worker 
210*49cdfc7eSAndroid Build Coastguard Worker 		if ((stream = fopen(tempfile, "wb+")) == NULL) {
211*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TFAIL, NULL, "fopen(%s) wb+ failed: %s",
212*49cdfc7eSAndroid Build Coastguard Worker 				 tempfile,
213*49cdfc7eSAndroid Build Coastguard Worker 				 strerror(errno));
214*49cdfc7eSAndroid Build Coastguard Worker 		}
215*49cdfc7eSAndroid Build Coastguard Worker 		(void)fclose(stream);
216*49cdfc7eSAndroid Build Coastguard Worker 
217*49cdfc7eSAndroid Build Coastguard Worker 		if ((stream = fopen(tempfile, "ab+")) == NULL) {
218*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TFAIL, NULL, "fopen(%s) ab+ failed: %s",
219*49cdfc7eSAndroid Build Coastguard Worker 				 tempfile,
220*49cdfc7eSAndroid Build Coastguard Worker 				 strerror(errno));
221*49cdfc7eSAndroid Build Coastguard Worker 		}
222*49cdfc7eSAndroid Build Coastguard Worker 		(void)fclose(stream);
223*49cdfc7eSAndroid Build Coastguard Worker 
224*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TPASS, "Test passed in block4.");
225*49cdfc7eSAndroid Build Coastguard Worker 	/*--------------------------------------------------------------------*/
226*49cdfc7eSAndroid Build Coastguard Worker 		unlink(tempfile);
227*49cdfc7eSAndroid Build Coastguard Worker 	}			/* end for */
228*49cdfc7eSAndroid Build Coastguard Worker 	tst_rmdir();
229*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
230*49cdfc7eSAndroid Build Coastguard Worker }
231