blob: eff181bb0a196a46a1c56deb7b1750e18e4fac20 [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/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.
subrata_modak4bb656a2009-02-26 12:02:09 +000029>BUGS: <
robbiewc8445632003-01-06 21:35:55 +000030======================================================================*/
31
32#include <stdio.h>
robbiewa70576c2003-03-04 18:33:41 +000033#include <errno.h>
robbiewc8445632003-01-06 21:35:55 +000034#include "test.h"
35#include "usctest.h"
36
37char *TCID = "stream01";
38int TST_TOTAL = 1;
robbiewc8445632003-01-06 21:35:55 +000039int local_flag;
40
41#define PASSED 1
42#define FAILED 0
43
Garrett Cooper53740502010-12-16 00:04:01 -080044/* XXX: add setup and cleanup. */
robbiewc8445632003-01-06 21:35:55 +000045
robbiewc8445632003-01-06 21:35:55 +000046char progname[] = "stream01()" ;
47char tempfile1[40]="";
48char tempfile2[40]="";
49
50/*--------------------------------------------------------------------*/
51int main(int ac, char *av[])
52{
53 FILE *stream;
54 char buf[10];
55 int i;
Cyril Hrubis89af32a2012-10-24 16:39:11 +020056 int lc;
57 char *msg;
robbiewc8445632003-01-06 21:35:55 +000058
59 /*
60 * parse standard options
61 */
Garrett Cooper53740502010-12-16 00:04:01 -080062 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
63 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiewc8445632003-01-06 21:35:55 +000064
65 local_flag = PASSED;
66 tst_tmpdir();
67 for (lc = 0; TEST_LOOPING(lc); lc++) {
68
69 sprintf(tempfile1, "stream011.%d", getpid());
70 sprintf(tempfile2, "stream012.%d", getpid());
71 /*--------------------------------------------------------------------*/
subrata_modakbdbaec52009-02-26 12:14:51 +000072 //block0:
Garrett Cooperdf3eb162010-11-28 22:44:32 -080073 if ((stream=fopen(tempfile1,"a+")) == NULL) {
vapierfab8d742006-02-15 05:47:07 +000074 tst_resm(TFAIL,"fopen(%s) a+ failed: %s", tempfile1, strerror(errno));
robbiewc8445632003-01-06 21:35:55 +000075 tst_exit();
76 }
77 fwrite("a",1,1,stream);
Garrett Cooperdf3eb162010-11-28 22:44:32 -080078 if ((stream=freopen(tempfile2,"a+",stream)) == NULL) {
Garrett Cooper53740502010-12-16 00:04:01 -080079 tst_brkm(TFAIL|TERRNO, NULL, "freopen(%s) a+ failed", tempfile2);
robbiewc8445632003-01-06 21:35:55 +000080 }
81 fwrite("a",1,1,stream);
82 fclose(stream);
83
84 /* now check that a single "a" is in each file */
Garrett Cooperdf3eb162010-11-28 22:44:32 -080085 if ((stream=fopen(tempfile1,"r")) == NULL) {
Garrett Cooper53740502010-12-16 00:04:01 -080086 tst_brkm(TFAIL|TERRNO, NULL, "fopen(%s) r failed", tempfile1);
robbiewc8445632003-01-06 21:35:55 +000087 }
88 else {
Garrett Cooperdf3eb162010-11-28 22:44:32 -080089 for (i=0; i<10; i++) buf[i]=0;
robbiewc8445632003-01-06 21:35:55 +000090 fread(buf,1,1,stream);
Garrett Cooperdf3eb162010-11-28 22:44:32 -080091 if ((buf[0] != 'a') || (buf[1] != 0)) {
vapierfab8d742006-02-15 05:47:07 +000092 tst_resm(TFAIL,"bad contents in %s", tempfile1);
robbiewc8445632003-01-06 21:35:55 +000093 local_flag = FAILED;
94 }
95 fclose(stream);
96 }
Garrett Cooperdf3eb162010-11-28 22:44:32 -080097 if ((stream=fopen(tempfile2,"r")) == NULL) {
Garrett Cooper53740502010-12-16 00:04:01 -080098 tst_brkm(TFAIL|TERRNO, NULL, "fopen(%s) r failed", tempfile2);
robbiewc8445632003-01-06 21:35:55 +000099 }
100 else {
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800101 for (i=0; i<10; i++) buf[i]=0;
robbiewc8445632003-01-06 21:35:55 +0000102 fread(buf,1,1,stream);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800103 if ((buf[0] != 'a') || (buf[1] != 0)) {
vapierfab8d742006-02-15 05:47:07 +0000104 tst_resm(TFAIL,"bad contents in %s", tempfile2);
robbiewc8445632003-01-06 21:35:55 +0000105 local_flag = FAILED;
106 }
107 fclose(stream);
108 }
Garrett Cooper53740502010-12-16 00:04:01 -0800109 if (local_flag == PASSED) {
vapierfab8d742006-02-15 05:47:07 +0000110 tst_resm(TPASS, "Test passed.");
Garrett Cooper53740502010-12-16 00:04:01 -0800111 } else {
vapierfab8d742006-02-15 05:47:07 +0000112 tst_resm(TFAIL, "Test failed.");
Garrett Cooper53740502010-12-16 00:04:01 -0800113 }
robbiewc8445632003-01-06 21:35:55 +0000114
Garrett Cooper53740502010-12-16 00:04:01 -0800115 local_flag = PASSED;
robbiewc8445632003-01-06 21:35:55 +0000116
117 /*--------------------------------------------------------------------*/
118 unlink(tempfile1);
119 unlink(tempfile2);
subrata_modakbdbaec52009-02-26 12:14:51 +0000120
robbiewc8445632003-01-06 21:35:55 +0000121 } /* end for */
122 tst_rmdir();
123 tst_exit();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700124}