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