xref: /aosp_15_r20/external/ltp/testcases/kernel/fs/stream/stream04.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/iosuite/stream4.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:  < fwrite() fread()
25*49cdfc7eSAndroid Build Coastguard Worker >WHAT:  < 1) Ensure fwrite appends data to stream.
26*49cdfc7eSAndroid Build Coastguard Worker 	< 2) Ensure fread and fwrite return values are valid.
27*49cdfc7eSAndroid Build Coastguard Worker >HOW:   < 1) Open a file, write to it, and then check it.
28*49cdfc7eSAndroid Build Coastguard Worker 	< 2) Fwrite a know quanity, check return value.
29*49cdfc7eSAndroid Build Coastguard Worker 	<    Fread a know quanity, check return value.
30*49cdfc7eSAndroid Build Coastguard Worker >BUGS:  <
31*49cdfc7eSAndroid Build Coastguard Worker ======================================================================*/
32*49cdfc7eSAndroid Build Coastguard Worker 
33*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
34*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
35*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
36*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
37*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
38*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
39*49cdfc7eSAndroid Build Coastguard Worker 
40*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "stream04";
41*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 1;
42*49cdfc7eSAndroid Build Coastguard Worker int local_flag;
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker #define PASSED 1
45*49cdfc7eSAndroid Build Coastguard Worker #define FAILED 0
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker char progname[] = "stream04()";
48*49cdfc7eSAndroid Build Coastguard Worker char tempfile1[40] = "";
49*49cdfc7eSAndroid Build Coastguard Worker long ftell();
50*49cdfc7eSAndroid Build Coastguard Worker 
51*49cdfc7eSAndroid Build Coastguard Worker /* XXX: add setup and cleanup */
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker /*--------------------------------------------------------------------*/
main(int ac,char * av[])54*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char *av[])
55*49cdfc7eSAndroid Build Coastguard Worker {
56*49cdfc7eSAndroid Build Coastguard Worker 	FILE *stream;
57*49cdfc7eSAndroid Build Coastguard Worker 	char *junk = "abcdefghijklmnopqrstuvwxyz";
58*49cdfc7eSAndroid Build Coastguard Worker 	char *inbuf;
59*49cdfc7eSAndroid Build Coastguard Worker 	int ret;
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 	for (lc = 0; TEST_LOOPING(lc); lc++) {
69*49cdfc7eSAndroid Build Coastguard Worker 
70*49cdfc7eSAndroid Build Coastguard Worker 		local_flag = PASSED;
71*49cdfc7eSAndroid Build Coastguard Worker 
72*49cdfc7eSAndroid Build Coastguard Worker 		sprintf(tempfile1, "stream04.%d", getpid());
73*49cdfc7eSAndroid Build Coastguard Worker 	/*--------------------------------------------------------------------*/
74*49cdfc7eSAndroid Build Coastguard Worker 		//block0:
75*49cdfc7eSAndroid Build Coastguard Worker 		if ((stream = fopen(tempfile1, "a+")) == NULL) {
76*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TFAIL | TERRNO, tst_rmdir, "fopen(%s) a+ failed",
77*49cdfc7eSAndroid Build Coastguard Worker 				 tempfile1);
78*49cdfc7eSAndroid Build Coastguard Worker 		}
79*49cdfc7eSAndroid Build Coastguard Worker 		/* write something and check */
80*49cdfc7eSAndroid Build Coastguard Worker 		if ((ret =
81*49cdfc7eSAndroid Build Coastguard Worker 		     fwrite(junk, sizeof(*junk), strlen(junk), stream)) == 0) {
82*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TFAIL, tst_rmdir, "fwrite failed: %s",
83*49cdfc7eSAndroid Build Coastguard Worker 				 strerror(errno));
84*49cdfc7eSAndroid Build Coastguard Worker 		}
85*49cdfc7eSAndroid Build Coastguard Worker 
86*49cdfc7eSAndroid Build Coastguard Worker 		if ((size_t) ret != strlen(junk)) {
87*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL,
88*49cdfc7eSAndroid Build Coastguard Worker 				 "strlen(junk) = %zi != return value from fwrite = %zi",
89*49cdfc7eSAndroid Build Coastguard Worker 				 strlen(junk), ret);
90*49cdfc7eSAndroid Build Coastguard Worker 			local_flag = FAILED;
91*49cdfc7eSAndroid Build Coastguard Worker 		}
92*49cdfc7eSAndroid Build Coastguard Worker 
93*49cdfc7eSAndroid Build Coastguard Worker 		fclose(stream);
94*49cdfc7eSAndroid Build Coastguard Worker 		if ((stream = fopen(tempfile1, "r+")) == NULL) {
95*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TFAIL, tst_rmdir, "fopen(%s) r+ failed: %s", tempfile1,
96*49cdfc7eSAndroid Build Coastguard Worker 				 strerror(errno));
97*49cdfc7eSAndroid Build Coastguard Worker 		}
98*49cdfc7eSAndroid Build Coastguard Worker 		if ((inbuf = malloc(strlen(junk))) == 0) {
99*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TBROK, tst_rmdir, "test failed because of malloc: %s",
100*49cdfc7eSAndroid Build Coastguard Worker 				 strerror(errno));
101*49cdfc7eSAndroid Build Coastguard Worker 		}
102*49cdfc7eSAndroid Build Coastguard Worker 		if ((ret =
103*49cdfc7eSAndroid Build Coastguard Worker 		     fread(inbuf, sizeof(*junk), strlen(junk), stream)) == 0) {
104*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TFAIL, tst_rmdir, "fread failed: %s",
105*49cdfc7eSAndroid Build Coastguard Worker 				 strerror(errno));
106*49cdfc7eSAndroid Build Coastguard Worker 		}
107*49cdfc7eSAndroid Build Coastguard Worker 		if ((size_t) ret != strlen(junk)) {
108*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL,
109*49cdfc7eSAndroid Build Coastguard Worker 				 "strlen(junk) = %zi != return value from fread = %zi",
110*49cdfc7eSAndroid Build Coastguard Worker 				 strlen(junk), ret);
111*49cdfc7eSAndroid Build Coastguard Worker 			local_flag = FAILED;
112*49cdfc7eSAndroid Build Coastguard Worker 		}
113*49cdfc7eSAndroid Build Coastguard Worker 		fclose(stream);
114*49cdfc7eSAndroid Build Coastguard Worker 		if (local_flag == PASSED) {
115*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TPASS, "Test passed.");
116*49cdfc7eSAndroid Build Coastguard Worker 		} else {
117*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "Test failed.");
118*49cdfc7eSAndroid Build Coastguard Worker 		}
119*49cdfc7eSAndroid Build Coastguard Worker 	/*--------------------------------------------------------------------*/
120*49cdfc7eSAndroid Build Coastguard Worker 		unlink(tempfile1);
121*49cdfc7eSAndroid Build Coastguard Worker 	}			/* end for */
122*49cdfc7eSAndroid Build Coastguard Worker 	tst_rmdir();
123*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
124*49cdfc7eSAndroid Build Coastguard Worker }
125