• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  *   Copyright (c) International Business Machines  Corp., 2002
4  *
5  *   This program is free software;  you can redistribute it and/or modify
6  *   it under the terms of the GNU General Public License as published by
7  *   the Free Software Foundation; either version 2 of the License, or
8  *   (at your option) any later version.
9  *
10  *   This program is distributed in the hope that it will be useful,
11  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13  *   the GNU General Public License for more details.
14  *
15  *   You should have received a copy of the GNU General Public License
16  *   along with this program;  if not, write to the Free Software
17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 /* Ported from SPIE, section2/iosuite/stream4.c, by Airong Zhang */
21 
22 /*======================================================================
23 	=================== TESTPLAN SEGMENT ===================
24 >KEYS:  < fwrite() fread()
25 >WHAT:  < 1) Ensure fwrite appends data to stream.
26 	< 2) Ensure fread and fwrite return values are valid.
27 >HOW:   < 1) Open a file, write to it, and then check it.
28 	< 2) Fwrite a know quanity, check return value.
29 	<    Fread a know quanity, check return value.
30 >BUGS:  <
31 ======================================================================*/
32 
33 #include <stdio.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include "test.h"
39 
40 char *TCID = "stream04";
41 int TST_TOTAL = 1;
42 int local_flag;
43 
44 #define PASSED 1
45 #define FAILED 0
46 
47 char progname[] = "stream04()";
48 char tempfile1[40] = "";
49 long ftell();
50 
51 /* XXX: add setup and cleanup */
52 
53 /*--------------------------------------------------------------------*/
main(int ac,char * av[])54 int main(int ac, char *av[])
55 {
56 	FILE *stream;
57 	char *junk = "abcdefghijklmnopqrstuvwxyz";
58 	char *inbuf;
59 	int ret;
60 
61 	int lc;
62 
63 	/*
64 	 * parse standard options
65 	 */
66 	tst_parse_opts(ac, av, NULL, NULL);
67 	tst_tmpdir();
68 	for (lc = 0; TEST_LOOPING(lc); lc++) {
69 
70 		local_flag = PASSED;
71 
72 		sprintf(tempfile1, "stream04.%d", getpid());
73 	/*--------------------------------------------------------------------*/
74 		//block0:
75 		if ((stream = fopen(tempfile1, "a+")) == NULL) {
76 			tst_brkm(TFAIL | TERRNO, tst_rmdir, "fopen(%s) a+ failed",
77 				 tempfile1);
78 		}
79 		/* write something and check */
80 		if ((ret =
81 		     fwrite(junk, sizeof(*junk), strlen(junk), stream)) == 0) {
82 			tst_brkm(TFAIL, tst_rmdir, "fwrite failed: %s",
83 				 strerror(errno));
84 		}
85 
86 		if ((size_t) ret != strlen(junk)) {
87 			tst_resm(TFAIL,
88 				 "strlen(junk) = %zi != return value from fwrite = %zi",
89 				 strlen(junk), ret);
90 			local_flag = FAILED;
91 		}
92 
93 		fclose(stream);
94 		if ((stream = fopen(tempfile1, "r+")) == NULL) {
95 			tst_brkm(TFAIL, tst_rmdir, "fopen(%s) r+ failed: %s", tempfile1,
96 				 strerror(errno));
97 		}
98 		if ((inbuf = malloc(strlen(junk))) == 0) {
99 			tst_brkm(TBROK, tst_rmdir, "test failed because of malloc: %s",
100 				 strerror(errno));
101 		}
102 		if ((ret =
103 		     fread(inbuf, sizeof(*junk), strlen(junk), stream)) == 0) {
104 			tst_brkm(TFAIL, tst_rmdir, "fread failed: %s",
105 				 strerror(errno));
106 		}
107 		if ((size_t) ret != strlen(junk)) {
108 			tst_resm(TFAIL,
109 				 "strlen(junk) = %zi != return value from fread = %zi",
110 				 strlen(junk), ret);
111 			local_flag = FAILED;
112 		}
113 		fclose(stream);
114 		if (local_flag == PASSED) {
115 			tst_resm(TPASS, "Test passed.");
116 		} else {
117 			tst_resm(TFAIL, "Test failed.");
118 		}
119 	/*--------------------------------------------------------------------*/
120 		unlink(tempfile1);
121 	}			/* end for */
122 	tst_rmdir();
123 	tst_exit();
124 }
125