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/iosuite/stream4.c, by Airong Zhang */ |
| 21 | |
| 22 | /*====================================================================== |
| 23 | =================== TESTPLAN SEGMENT =================== |
| 24 | >KEYS: < fwrite() fread() |
| 25 | >WHAT: < 1) Ensure fwrite appends data to stream. |
| 26 | < 2) Ensure fread and fwrite return values are valid. |
| 27 | >HOW: < 1) Open a file, write to it, and then check it. |
| 28 | < 2) Fwrite a know quanity, check return value. |
| 29 | < Fread a know quanity, check return value. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 30 | >BUGS: < |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 31 | ======================================================================*/ |
| 32 | |
| 33 | #include <stdio.h> |
robbiew | a70576c | 2003-03-04 18:33:41 +0000 | [diff] [blame] | 34 | #include <errno.h> |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 35 | #include <fcntl.h> |
| 36 | #include <sys/types.h> |
| 37 | #include <sys/stat.h> |
| 38 | #include "test.h" |
| 39 | #include "usctest.h" |
| 40 | |
| 41 | char *TCID = "stream04"; |
| 42 | int TST_TOTAL = 1; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 43 | int local_flag; |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 44 | |
| 45 | #define PASSED 1 |
| 46 | #define FAILED 0 |
| 47 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 48 | char progname[] = "stream04()"; |
| 49 | char tempfile1[40] = ""; |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 50 | long ftell(); |
| 51 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 52 | /* XXX: add setup and cleanup */ |
| 53 | |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 54 | /*--------------------------------------------------------------------*/ |
| 55 | int main(int ac, char *av[]) |
| 56 | { |
| 57 | FILE *stream; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 58 | char *junk = "abcdefghijklmnopqrstuvwxyz"; |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 59 | char *inbuf; |
| 60 | int ret; |
| 61 | |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 62 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 63 | const char *msg; |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 64 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 65 | /* |
| 66 | * parse standard options |
| 67 | */ |
Garrett Cooper | db0ca4b | 2010-12-19 07:46:53 -0800 | [diff] [blame] | 68 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
| 69 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 70 | tst_tmpdir(); |
| 71 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 72 | |
| 73 | local_flag = PASSED; |
| 74 | |
| 75 | sprintf(tempfile1, "stream04.%d", getpid()); |
| 76 | /*--------------------------------------------------------------------*/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 77 | //block0: |
| 78 | if ((stream = fopen(tempfile1, "a+")) == NULL) { |
Cyril Hrubis | 9fa8ad0 | 2014-12-16 13:20:49 +0100 | [diff] [blame^] | 79 | tst_brkm(TFAIL | TERRNO, tst_rmdir, "fopen(%s) a+ failed", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 80 | tempfile1); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 81 | } |
| 82 | /* write something and check */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 83 | if ((ret = |
| 84 | fwrite(junk, sizeof(*junk), strlen(junk), stream)) == 0) { |
Cyril Hrubis | 9fa8ad0 | 2014-12-16 13:20:49 +0100 | [diff] [blame^] | 85 | tst_brkm(TFAIL, tst_rmdir, "fwrite failed: %s", |
| 86 | strerror(errno)); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 89 | if ((size_t) ret != strlen(junk)) { |
| 90 | tst_resm(TFAIL, |
| 91 | "strlen(junk) = %zi != return value from fwrite = %zi", |
| 92 | strlen(junk), ret); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 93 | local_flag = FAILED; |
| 94 | } |
| 95 | |
| 96 | fclose(stream); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 97 | if ((stream = fopen(tempfile1, "r+")) == NULL) { |
Cyril Hrubis | 9fa8ad0 | 2014-12-16 13:20:49 +0100 | [diff] [blame^] | 98 | tst_brkm(TFAIL, tst_rmdir, "fopen(%s) r+ failed: %s", tempfile1, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 99 | strerror(errno)); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 100 | } |
Cyril Hrubis | d218f34 | 2014-09-23 13:14:56 +0200 | [diff] [blame] | 101 | if ((inbuf = malloc(strlen(junk))) == 0) { |
Cyril Hrubis | 9fa8ad0 | 2014-12-16 13:20:49 +0100 | [diff] [blame^] | 102 | tst_brkm(TBROK, tst_rmdir, "test failed because of malloc: %s", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 103 | strerror(errno)); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 104 | } |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 105 | if ((ret = |
| 106 | fread(inbuf, sizeof(*junk), strlen(junk), stream)) == 0) { |
Cyril Hrubis | 9fa8ad0 | 2014-12-16 13:20:49 +0100 | [diff] [blame^] | 107 | tst_brkm(TFAIL, tst_rmdir, "fread failed: %s", |
| 108 | strerror(errno)); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 109 | } |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 110 | if ((size_t) ret != strlen(junk)) { |
| 111 | tst_resm(TFAIL, |
| 112 | "strlen(junk) = %zi != return value from fread = %zi", |
| 113 | strlen(junk), ret); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 114 | local_flag = FAILED; |
| 115 | } |
| 116 | fclose(stream); |
| 117 | if (local_flag == PASSED) { |
vapier | fab8d74 | 2006-02-15 05:47:07 +0000 | [diff] [blame] | 118 | tst_resm(TPASS, "Test passed."); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 119 | } else { |
vapier | fab8d74 | 2006-02-15 05:47:07 +0000 | [diff] [blame] | 120 | tst_resm(TFAIL, "Test failed."); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 121 | } |
| 122 | /*--------------------------------------------------------------------*/ |
| 123 | unlink(tempfile1); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 124 | } /* end for */ |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 125 | tst_rmdir(); |
| 126 | tst_exit(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 127 | } |