blob: 72a54cf57fcc2c440ba9354f4427abe8b5708492 [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/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_modak4bb656a2009-02-26 12:02:09 +000030>BUGS: <
robbiewc8445632003-01-06 21:35:55 +000031======================================================================*/
32
33#include <stdio.h>
robbiewa70576c2003-03-04 18:33:41 +000034#include <errno.h>
robbiewc8445632003-01-06 21:35:55 +000035#include <fcntl.h>
36#include <sys/types.h>
37#include <sys/stat.h>
38#include "test.h"
robbiewc8445632003-01-06 21:35:55 +000039
40char *TCID = "stream04";
41int TST_TOTAL = 1;
Wanlong Gao354ebb42012-12-07 10:10:04 +080042int local_flag;
robbiewc8445632003-01-06 21:35:55 +000043
44#define PASSED 1
45#define FAILED 0
46
Wanlong Gao354ebb42012-12-07 10:10:04 +080047char progname[] = "stream04()";
48char tempfile1[40] = "";
robbiewc8445632003-01-06 21:35:55 +000049long ftell();
50
Garrett Cooper53740502010-12-16 00:04:01 -080051/* XXX: add setup and cleanup */
52
robbiewc8445632003-01-06 21:35:55 +000053/*--------------------------------------------------------------------*/
54int main(int ac, char *av[])
55{
56 FILE *stream;
Wanlong Gao354ebb42012-12-07 10:10:04 +080057 char *junk = "abcdefghijklmnopqrstuvwxyz";
robbiewc8445632003-01-06 21:35:55 +000058 char *inbuf;
59 int ret;
60
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 */
Garrett Cooperdb0ca4b2010-12-19 07:46:53 -080067 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();
70 for (lc = 0; TEST_LOOPING(lc); lc++) {
71
72 local_flag = PASSED;
73
74 sprintf(tempfile1, "stream04.%d", getpid());
75 /*--------------------------------------------------------------------*/
Wanlong Gao354ebb42012-12-07 10:10:04 +080076 //block0:
77 if ((stream = fopen(tempfile1, "a+")) == NULL) {
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +010078 tst_brkm(TFAIL | TERRNO, tst_rmdir, "fopen(%s) a+ failed",
Wanlong Gao354ebb42012-12-07 10:10:04 +080079 tempfile1);
robbiewc8445632003-01-06 21:35:55 +000080 }
81 /* write something and check */
Wanlong Gao354ebb42012-12-07 10:10:04 +080082 if ((ret =
83 fwrite(junk, sizeof(*junk), strlen(junk), stream)) == 0) {
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +010084 tst_brkm(TFAIL, tst_rmdir, "fwrite failed: %s",
85 strerror(errno));
robbiewc8445632003-01-06 21:35:55 +000086 }
87
Wanlong Gao354ebb42012-12-07 10:10:04 +080088 if ((size_t) ret != strlen(junk)) {
89 tst_resm(TFAIL,
90 "strlen(junk) = %zi != return value from fwrite = %zi",
91 strlen(junk), ret);
robbiewc8445632003-01-06 21:35:55 +000092 local_flag = FAILED;
93 }
94
95 fclose(stream);
Wanlong Gao354ebb42012-12-07 10:10:04 +080096 if ((stream = fopen(tempfile1, "r+")) == NULL) {
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +010097 tst_brkm(TFAIL, tst_rmdir, "fopen(%s) r+ failed: %s", tempfile1,
Wanlong Gao354ebb42012-12-07 10:10:04 +080098 strerror(errno));
robbiewc8445632003-01-06 21:35:55 +000099 }
Cyril Hrubisd218f342014-09-23 13:14:56 +0200100 if ((inbuf = malloc(strlen(junk))) == 0) {
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +0100101 tst_brkm(TBROK, tst_rmdir, "test failed because of malloc: %s",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800102 strerror(errno));
robbiewc8445632003-01-06 21:35:55 +0000103 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800104 if ((ret =
105 fread(inbuf, sizeof(*junk), strlen(junk), stream)) == 0) {
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +0100106 tst_brkm(TFAIL, tst_rmdir, "fread failed: %s",
107 strerror(errno));
robbiewc8445632003-01-06 21:35:55 +0000108 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800109 if ((size_t) ret != strlen(junk)) {
110 tst_resm(TFAIL,
111 "strlen(junk) = %zi != return value from fread = %zi",
112 strlen(junk), ret);
robbiewc8445632003-01-06 21:35:55 +0000113 local_flag = FAILED;
114 }
115 fclose(stream);
116 if (local_flag == PASSED) {
vapierfab8d742006-02-15 05:47:07 +0000117 tst_resm(TPASS, "Test passed.");
robbiewc8445632003-01-06 21:35:55 +0000118 } else {
vapierfab8d742006-02-15 05:47:07 +0000119 tst_resm(TFAIL, "Test failed.");
robbiewc8445632003-01-06 21:35:55 +0000120 }
121 /*--------------------------------------------------------------------*/
122 unlink(tempfile1);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800123 } /* end for */
robbiewc8445632003-01-06 21:35:55 +0000124 tst_rmdir();
125 tst_exit();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700126}