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/stream1.c, by Airong Zhang */
21
22 /*======================================================================
23 =================== TESTPLAN SEGMENT ===================
24 >KEYS: < freopen()
25 >WHAT: < 1) check that freopen substitutes the named file in place of stream.
26 >HOW: < 1) open a stream, write something to it, perform freopen and
27 < write some more. Check that second write to stream went to
28 < the file specified by freopen.
29 >BUGS: <
30 ======================================================================*/
31
32 #include <stdio.h>
33 #include <errno.h>
34 #include "test.h"
35
36 char *TCID = "stream01";
37 int TST_TOTAL = 1;
38 int local_flag;
39
40 #define PASSED 1
41 #define FAILED 0
42
43 /* XXX: add setup and cleanup. */
44
45 char progname[] = "stream01()";
46 char tempfile1[40] = "";
47 char tempfile2[40] = "";
48
49 /*--------------------------------------------------------------------*/
main(int ac,char * av[])50 int main(int ac, char *av[])
51 {
52 FILE *stream;
53 char buf[10];
54 int i;
55 int lc;
56
57 /*
58 * parse standard options
59 */
60 tst_parse_opts(ac, av, NULL, NULL);
61
62 local_flag = PASSED;
63 tst_tmpdir();
64 for (lc = 0; TEST_LOOPING(lc); lc++) {
65
66 sprintf(tempfile1, "stream011.%d", getpid());
67 sprintf(tempfile2, "stream012.%d", getpid());
68 /*--------------------------------------------------------------------*/
69 //block0:
70 if ((stream = fopen(tempfile1, "a+")) == NULL) {
71 tst_brkm(TFAIL, NULL, "fopen(%s) a+ failed: %s",
72 tempfile1,
73 strerror(errno));
74 }
75 fwrite("a", 1, 1, stream);
76 if ((stream = freopen(tempfile2, "a+", stream)) == NULL) {
77 tst_brkm(TFAIL | TERRNO, NULL, "freopen(%s) a+ failed",
78 tempfile2);
79 }
80 fwrite("a", 1, 1, stream);
81 fclose(stream);
82
83 /* now check that a single "a" is in each file */
84 if ((stream = fopen(tempfile1, "r")) == NULL) {
85 tst_brkm(TFAIL | TERRNO, NULL, "fopen(%s) r failed",
86 tempfile1);
87 } else {
88 for (i = 0; i < 10; i++)
89 buf[i] = 0;
90 fread(buf, 1, 1, stream);
91 if ((buf[0] != 'a') || (buf[1] != 0)) {
92 tst_resm(TFAIL, "bad contents in %s",
93 tempfile1);
94 local_flag = FAILED;
95 }
96 fclose(stream);
97 }
98 if ((stream = fopen(tempfile2, "r")) == NULL) {
99 tst_brkm(TFAIL | TERRNO, NULL, "fopen(%s) r failed",
100 tempfile2);
101 } else {
102 for (i = 0; i < 10; i++)
103 buf[i] = 0;
104 fread(buf, 1, 1, stream);
105 if ((buf[0] != 'a') || (buf[1] != 0)) {
106 tst_resm(TFAIL, "bad contents in %s",
107 tempfile2);
108 local_flag = FAILED;
109 }
110 fclose(stream);
111 }
112 if (local_flag == PASSED) {
113 tst_resm(TPASS, "Test passed.");
114 } else {
115 tst_resm(TFAIL, "Test failed.");
116 }
117
118 local_flag = PASSED;
119
120 /*--------------------------------------------------------------------*/
121 unlink(tempfile1);
122 unlink(tempfile2);
123
124 } /* end for */
125 tst_rmdir();
126 tst_exit();
127 }
128