blob: 4c63c1c6b4a6be38a134cc56995779b007d144d8 [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"
39#include "usctest.h"
40
41char *TCID = "stream04";
42int TST_TOTAL = 1;
Wanlong Gao354ebb42012-12-07 10:10:04 +080043int local_flag;
robbiewc8445632003-01-06 21:35:55 +000044
45#define PASSED 1
46#define FAILED 0
47
Wanlong Gao354ebb42012-12-07 10:10:04 +080048char progname[] = "stream04()";
49char tempfile1[40] = "";
robbiewc8445632003-01-06 21:35:55 +000050long ftell();
51
Garrett Cooper53740502010-12-16 00:04:01 -080052/* XXX: add setup and cleanup */
53
robbiewc8445632003-01-06 21:35:55 +000054/*--------------------------------------------------------------------*/
55int main(int ac, char *av[])
56{
57 FILE *stream;
Wanlong Gao354ebb42012-12-07 10:10:04 +080058 char *junk = "abcdefghijklmnopqrstuvwxyz";
robbiewc8445632003-01-06 21:35:55 +000059 char *inbuf;
60 int ret;
61
Cyril Hrubis89af32a2012-10-24 16:39:11 +020062 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020063 const char *msg;
robbiewc8445632003-01-06 21:35:55 +000064
Wanlong Gao354ebb42012-12-07 10:10:04 +080065 /*
66 * parse standard options
67 */
Garrett Cooperdb0ca4b2010-12-19 07:46:53 -080068 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
69 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiewc8445632003-01-06 21:35:55 +000070 tst_tmpdir();
71 for (lc = 0; TEST_LOOPING(lc); lc++) {
72
73 local_flag = PASSED;
74
75 sprintf(tempfile1, "stream04.%d", getpid());
76 /*--------------------------------------------------------------------*/
Wanlong Gao354ebb42012-12-07 10:10:04 +080077 //block0:
78 if ((stream = fopen(tempfile1, "a+")) == NULL) {
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +010079 tst_brkm(TFAIL | TERRNO, tst_rmdir, "fopen(%s) a+ failed",
Wanlong Gao354ebb42012-12-07 10:10:04 +080080 tempfile1);
robbiewc8445632003-01-06 21:35:55 +000081 }
82 /* write something and check */
Wanlong Gao354ebb42012-12-07 10:10:04 +080083 if ((ret =
84 fwrite(junk, sizeof(*junk), strlen(junk), stream)) == 0) {
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +010085 tst_brkm(TFAIL, tst_rmdir, "fwrite failed: %s",
86 strerror(errno));
robbiewc8445632003-01-06 21:35:55 +000087 }
88
Wanlong Gao354ebb42012-12-07 10:10:04 +080089 if ((size_t) ret != strlen(junk)) {
90 tst_resm(TFAIL,
91 "strlen(junk) = %zi != return value from fwrite = %zi",
92 strlen(junk), ret);
robbiewc8445632003-01-06 21:35:55 +000093 local_flag = FAILED;
94 }
95
96 fclose(stream);
Wanlong Gao354ebb42012-12-07 10:10:04 +080097 if ((stream = fopen(tempfile1, "r+")) == NULL) {
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +010098 tst_brkm(TFAIL, tst_rmdir, "fopen(%s) r+ failed: %s", tempfile1,
Wanlong Gao354ebb42012-12-07 10:10:04 +080099 strerror(errno));
robbiewc8445632003-01-06 21:35:55 +0000100 }
Cyril Hrubisd218f342014-09-23 13:14:56 +0200101 if ((inbuf = malloc(strlen(junk))) == 0) {
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +0100102 tst_brkm(TBROK, tst_rmdir, "test failed because of malloc: %s",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800103 strerror(errno));
robbiewc8445632003-01-06 21:35:55 +0000104 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800105 if ((ret =
106 fread(inbuf, sizeof(*junk), strlen(junk), stream)) == 0) {
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +0100107 tst_brkm(TFAIL, tst_rmdir, "fread failed: %s",
108 strerror(errno));
robbiewc8445632003-01-06 21:35:55 +0000109 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800110 if ((size_t) ret != strlen(junk)) {
111 tst_resm(TFAIL,
112 "strlen(junk) = %zi != return value from fread = %zi",
113 strlen(junk), ret);
robbiewc8445632003-01-06 21:35:55 +0000114 local_flag = FAILED;
115 }
116 fclose(stream);
117 if (local_flag == PASSED) {
vapierfab8d742006-02-15 05:47:07 +0000118 tst_resm(TPASS, "Test passed.");
robbiewc8445632003-01-06 21:35:55 +0000119 } else {
vapierfab8d742006-02-15 05:47:07 +0000120 tst_resm(TFAIL, "Test failed.");
robbiewc8445632003-01-06 21:35:55 +0000121 }
122 /*--------------------------------------------------------------------*/
123 unlink(tempfile1);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800124 } /* end for */
robbiewc8445632003-01-06 21:35:55 +0000125 tst_rmdir();
126 tst_exit();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700127}