robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 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 |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 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. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 29 | >BUGS: < |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 30 | ======================================================================*/ |
| 31 | |
| 32 | #include <stdio.h> |
robbiew | a70576c | 2003-03-04 18:33:41 +0000 | [diff] [blame] | 33 | #include <errno.h> |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 34 | #include "test.h" |
| 35 | #include "usctest.h" |
| 36 | |
| 37 | char *TCID = "stream01"; |
| 38 | int TST_TOTAL = 1; |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 39 | int local_flag; |
| 40 | |
| 41 | #define PASSED 1 |
| 42 | #define FAILED 0 |
| 43 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 44 | /* XXX: add setup and cleanup. */ |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 45 | |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 46 | char progname[] = "stream01()" ; |
| 47 | char tempfile1[40]=""; |
| 48 | char tempfile2[40]=""; |
| 49 | |
| 50 | /*--------------------------------------------------------------------*/ |
| 51 | int main(int ac, char *av[]) |
| 52 | { |
| 53 | FILE *stream; |
| 54 | char buf[10]; |
| 55 | int i; |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 56 | int lc; |
| 57 | char *msg; |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 58 | |
| 59 | /* |
| 60 | * parse standard options |
| 61 | */ |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 62 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
| 63 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 64 | |
| 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_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 72 | //block0: |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 73 | if ((stream=fopen(tempfile1,"a+")) == NULL) { |
vapier | fab8d74 | 2006-02-15 05:47:07 +0000 | [diff] [blame] | 74 | tst_resm(TFAIL,"fopen(%s) a+ failed: %s", tempfile1, strerror(errno)); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 75 | tst_exit(); |
| 76 | } |
| 77 | fwrite("a",1,1,stream); |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 78 | if ((stream=freopen(tempfile2,"a+",stream)) == NULL) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 79 | tst_brkm(TFAIL|TERRNO, NULL, "freopen(%s) a+ failed", tempfile2); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 80 | } |
| 81 | fwrite("a",1,1,stream); |
| 82 | fclose(stream); |
| 83 | |
| 84 | /* now check that a single "a" is in each file */ |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 85 | if ((stream=fopen(tempfile1,"r")) == NULL) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 86 | tst_brkm(TFAIL|TERRNO, NULL, "fopen(%s) r failed", tempfile1); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 87 | } |
| 88 | else { |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 89 | for (i=0; i<10; i++) buf[i]=0; |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 90 | fread(buf,1,1,stream); |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 91 | if ((buf[0] != 'a') || (buf[1] != 0)) { |
vapier | fab8d74 | 2006-02-15 05:47:07 +0000 | [diff] [blame] | 92 | tst_resm(TFAIL,"bad contents in %s", tempfile1); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 93 | local_flag = FAILED; |
| 94 | } |
| 95 | fclose(stream); |
| 96 | } |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 97 | if ((stream=fopen(tempfile2,"r")) == NULL) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 98 | tst_brkm(TFAIL|TERRNO, NULL, "fopen(%s) r failed", tempfile2); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 99 | } |
| 100 | else { |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 101 | for (i=0; i<10; i++) buf[i]=0; |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 102 | fread(buf,1,1,stream); |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 103 | if ((buf[0] != 'a') || (buf[1] != 0)) { |
vapier | fab8d74 | 2006-02-15 05:47:07 +0000 | [diff] [blame] | 104 | tst_resm(TFAIL,"bad contents in %s", tempfile2); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 105 | local_flag = FAILED; |
| 106 | } |
| 107 | fclose(stream); |
| 108 | } |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 109 | if (local_flag == PASSED) { |
vapier | fab8d74 | 2006-02-15 05:47:07 +0000 | [diff] [blame] | 110 | tst_resm(TPASS, "Test passed."); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 111 | } else { |
vapier | fab8d74 | 2006-02-15 05:47:07 +0000 | [diff] [blame] | 112 | tst_resm(TFAIL, "Test failed."); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 113 | } |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 114 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 115 | local_flag = PASSED; |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 116 | |
| 117 | /*--------------------------------------------------------------------*/ |
| 118 | unlink(tempfile1); |
| 119 | unlink(tempfile2); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 120 | |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 121 | } /* end for */ |
| 122 | tst_rmdir(); |
| 123 | tst_exit(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 124 | } |