blob: 9be61312f17f495f166b20fdd23debcabcba92bf [file] [log] [blame]
robbiewc8445632003-01-06 21:35:55 +00001/*
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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
robbiewc8445632003-01-06 21:35:55 +000018 */
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
subrata_modak4bb656a2009-02-26 12:02:09 +000036>BUGS: <
robbiewc8445632003-01-06 21:35:55 +000037======================================================================*/
38
39#include <unistd.h>
40#include <stdio.h>
robbiewa70576c2003-03-04 18:33:41 +000041#include <errno.h>
robbiewc8445632003-01-06 21:35:55 +000042#include "test.h"
robbiewc8445632003-01-06 21:35:55 +000043
44char *TCID = "stream05";
45int TST_TOTAL = 1;
Wanlong Gao354ebb42012-12-07 10:10:04 +080046int local_flag;
robbiewc8445632003-01-06 21:35:55 +000047
48#define PASSED 1
49#define FAILED 0
50
Wanlong Gao354ebb42012-12-07 10:10:04 +080051char progname[] = "stream05()";
52char tempfile[40] = "";
robbiewc8445632003-01-06 21:35:55 +000053
54/*--------------------------------------------------------------------*/
55int main(int ac, char *av[])
56{
57 FILE *stream;
58 char buf[10];
Wanlong Gao354ebb42012-12-07 10:10:04 +080059 int nr, fd;
robbiewc8445632003-01-06 21:35:55 +000060
Cyril Hrubis89af32a2012-10-24 16:39:11 +020061 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020062 const char *msg;
robbiewc8445632003-01-06 21:35:55 +000063
Wanlong Gao354ebb42012-12-07 10:10:04 +080064 /*
65 * parse standard options
66 */
67 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
68 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiewc8445632003-01-06 21:35:55 +000069 tst_tmpdir();
Wanlong Gao354ebb42012-12-07 10:10:04 +080070 local_flag = PASSED;
robbiewc8445632003-01-06 21:35:55 +000071
72 for (lc = 0; TEST_LOOPING(lc); lc++) {
Wanlong Gao354ebb42012-12-07 10:10:04 +080073 local_flag = PASSED;
robbiewc8445632003-01-06 21:35:55 +000074
75 sprintf(tempfile, "stream05.%d", getpid());
76 /*--------------------------------------------------------------------*/
Wanlong Gao354ebb42012-12-07 10:10:04 +080077 //block0:
78 if ((stream = fopen(tempfile, "a+")) == NULL) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +010079 tst_brkm(TFAIL, NULL, "fopen(%s) a+ failed: %s",
80 tempfile,
Wanlong Gao354ebb42012-12-07 10:10:04 +080081 strerror(errno));
robbiewc8445632003-01-06 21:35:55 +000082 }
Wanlong Gao354ebb42012-12-07 10:10:04 +080083 fprintf(stream, "a");
robbiewc8445632003-01-06 21:35:55 +000084 fclose(stream);
85
Wanlong Gao354ebb42012-12-07 10:10:04 +080086 if ((stream = fopen(tempfile, "r+")) == NULL) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +010087 tst_brkm(TFAIL, NULL, "fopen(%s) r+ failed: %s",
88 tempfile,
Wanlong Gao354ebb42012-12-07 10:10:04 +080089 strerror(errno));
robbiewc8445632003-01-06 21:35:55 +000090 }
91
92 /* check that ferror returns zero */
Garrett Cooperdf3eb162010-11-28 22:44:32 -080093 if (ferror(stream) != 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +080094 tst_resm(TFAIL, "ferror did not return zero: %s",
95 strerror(errno));
robbiewc8445632003-01-06 21:35:55 +000096 local_flag = FAILED;
97 }
98
99 if (local_flag == PASSED) {
vapierfab8d742006-02-15 05:47:07 +0000100 tst_resm(TPASS, "Test passed in block0.");
robbiewc8445632003-01-06 21:35:55 +0000101 } else {
vapierfab8d742006-02-15 05:47:07 +0000102 tst_resm(TFAIL, "Test failed in block0.");
robbiewc8445632003-01-06 21:35:55 +0000103 }
104
105 local_flag = PASSED;
106
107 /*--------------------------------------------------------------------*/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800108 //block1:
robbiewc8445632003-01-06 21:35:55 +0000109
110 /* check that fileno returns valid file descriptor */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800111 fd = fileno(stream);
112 if ((nr = read(fd, buf, 1)) < 0) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100113 tst_brkm(TFAIL, NULL, "read failed: %s",
114 strerror(errno));
robbiewc8445632003-01-06 21:35:55 +0000115 }
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800116 if (nr != 1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800117 tst_resm(TFAIL, "read did not read right number");
robbiewc8445632003-01-06 21:35:55 +0000118 local_flag = FAILED;
119 }
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800120 if (buf[0] != 'a') {
vapierfab8d742006-02-15 05:47:07 +0000121 tst_resm(TFAIL, "read returned bad values");
robbiewc8445632003-01-06 21:35:55 +0000122 local_flag = FAILED;
123 }
124 if (local_flag == PASSED) {
vapierfab8d742006-02-15 05:47:07 +0000125 tst_resm(TPASS, "Test passed in block1.");
robbiewc8445632003-01-06 21:35:55 +0000126 } else {
vapierfab8d742006-02-15 05:47:07 +0000127 tst_resm(TFAIL, "Test failed in block1.");
robbiewc8445632003-01-06 21:35:55 +0000128 }
129
130 local_flag = PASSED;
131 /*--------------------------------------------------------------------*/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800132 //block2:
robbiewc8445632003-01-06 21:35:55 +0000133
134 /* read to EOF and ensure feof returns non-zero */
135 fclose(stream);
136
Wanlong Gao354ebb42012-12-07 10:10:04 +0800137 if ((stream = fopen(tempfile, "r+")) == NULL) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100138 tst_brkm(TFAIL, NULL, "fopen(%s) r+ failed: %s",
139 tempfile,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800140 strerror(errno));
robbiewc8445632003-01-06 21:35:55 +0000141 }
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800142 if (feof(stream) != 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800143 tst_resm(TFAIL,
144 "feof returned non-zero when it should not: %s",
145 strerror(errno));
robbiewc8445632003-01-06 21:35:55 +0000146 local_flag = FAILED;
147 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800148 fread(buf, 1, 2, stream); /* read to EOF */
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800149 if (feof(stream) == 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800150 tst_resm(TFAIL,
151 "feof returned zero when it should not: %s",
152 strerror(errno));
robbiewc8445632003-01-06 21:35:55 +0000153 local_flag = FAILED;
154 }
155
156 if (local_flag == PASSED) {
vapierfab8d742006-02-15 05:47:07 +0000157 tst_resm(TPASS, "Test passed in block2.");
robbiewc8445632003-01-06 21:35:55 +0000158 } else {
vapierfab8d742006-02-15 05:47:07 +0000159 tst_resm(TFAIL, "Test failed in block2.");
robbiewc8445632003-01-06 21:35:55 +0000160 }
161
162 local_flag = PASSED;
163 /*--------------------------------------------------------------------*/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800164 //block3:
robbiewc8445632003-01-06 21:35:55 +0000165 /* ensure clearerr works */
166 clearerr(stream);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800167 if (feof(stream) != 0) {
vapierfab8d742006-02-15 05:47:07 +0000168 tst_resm(TFAIL, "clearerr failed: %s", strerror(errno));
robbiewc8445632003-01-06 21:35:55 +0000169 local_flag = FAILED;
170 }
171 if (local_flag == PASSED) {
vapierfab8d742006-02-15 05:47:07 +0000172 tst_resm(TPASS, "Test passed in block3.");
robbiewc8445632003-01-06 21:35:55 +0000173 } else {
vapierfab8d742006-02-15 05:47:07 +0000174 tst_resm(TFAIL, "Test failed in block3.");
robbiewc8445632003-01-06 21:35:55 +0000175 }
176
177 local_flag = PASSED;
178 /*--------------------------------------------------------------------*/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800179 //block4:
robbiewc8445632003-01-06 21:35:55 +0000180
181 /* test fopen "b" flags -- should be allowed but ignored */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800182 (void)fclose(stream);
robbiewc8445632003-01-06 21:35:55 +0000183
184 if ((stream = fopen(tempfile, "rb")) == NULL) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100185 tst_brkm(TFAIL, NULL, "fopen(%s) rb failed: %s",
186 tempfile,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800187 strerror(errno));
robbiewc8445632003-01-06 21:35:55 +0000188 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800189 (void)fclose(stream);
robbiewc8445632003-01-06 21:35:55 +0000190
191 if ((stream = fopen(tempfile, "wb")) == NULL) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100192 tst_brkm(TFAIL, NULL, "fopen(%s) wb failed: %s",
193 tempfile,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800194 strerror(errno));
robbiewc8445632003-01-06 21:35:55 +0000195 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800196 (void)fclose(stream);
robbiewc8445632003-01-06 21:35:55 +0000197
198 if ((stream = fopen(tempfile, "ab")) == NULL) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100199 tst_brkm(TFAIL, NULL, "fopen(%s) ab failed: %s",
200 tempfile,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800201 strerror(errno));
robbiewc8445632003-01-06 21:35:55 +0000202 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800203 (void)fclose(stream);
robbiewc8445632003-01-06 21:35:55 +0000204
205 if ((stream = fopen(tempfile, "rb+")) == NULL) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100206 tst_brkm(TFAIL, NULL, "fopen(%s) rb+ failed: %s",
207 tempfile,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800208 strerror(errno));
robbiewc8445632003-01-06 21:35:55 +0000209 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800210 (void)fclose(stream);
robbiewc8445632003-01-06 21:35:55 +0000211
212 if ((stream = fopen(tempfile, "wb+")) == NULL) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100213 tst_brkm(TFAIL, NULL, "fopen(%s) wb+ failed: %s",
214 tempfile,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800215 strerror(errno));
robbiewc8445632003-01-06 21:35:55 +0000216 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800217 (void)fclose(stream);
robbiewc8445632003-01-06 21:35:55 +0000218
219 if ((stream = fopen(tempfile, "ab+")) == NULL) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100220 tst_brkm(TFAIL, NULL, "fopen(%s) ab+ failed: %s",
221 tempfile,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800222 strerror(errno));
robbiewc8445632003-01-06 21:35:55 +0000223 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800224 (void)fclose(stream);
robbiewc8445632003-01-06 21:35:55 +0000225
vapierfab8d742006-02-15 05:47:07 +0000226 tst_resm(TPASS, "Test passed in block4.");
robbiewc8445632003-01-06 21:35:55 +0000227 /*--------------------------------------------------------------------*/
228 unlink(tempfile);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800229 } /* end for */
robbiewc8445632003-01-06 21:35:55 +0000230 tst_rmdir();
231 tst_exit();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700232}