blob: ce968353320484a75bbcbe6a54e32bd5fa2f2a3d [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
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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.
30>BUGS: <
31======================================================================*/
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;
43extern int Tst_count;
44int local_flag;
45
46#define PASSED 1
47#define FAILED 0
48
49
robbiewc8445632003-01-06 21:35:55 +000050char progname[] = "stream04()" ;
51char tempfile1[40]="";
52long ftell();
53
54/*--------------------------------------------------------------------*/
55int main(int ac, char *av[])
56{
57 FILE *stream;
58 char *junk="abcdefghijklmnopqrstuvwxyz";
59 char *inbuf;
60 int ret;
61
62 int lc; /* loop counter */
63 char *msg; /* message returned from parse_opts */
64
65 /*
66 * parse standard options
67 */
68 if ((msg = parse_opts(ac, av, (option_t *)NULL, NULL)) != (char *)NULL){
69 tst_resm(TBROK, "OPTION PARSING ERROR - %s", msg);
70 tst_exit();
71 /*NOTREACHED*/
72 }
73 tst_tmpdir();
74 for (lc = 0; TEST_LOOPING(lc); lc++) {
75
76 local_flag = PASSED;
77
78 sprintf(tempfile1, "stream04.%d", getpid());
79 /*--------------------------------------------------------------------*/
80 //block0:
81 if(creat(tempfile1,0666) < 0) {
82 tst_resm(TFAIL,"\tcreat failed\n");
83 tst_exit();
84 }
85 if((stream=fopen(tempfile1,"a+")) == NULL) {
86 tst_resm(TFAIL,"\tfopen a+ failed\n");
87 tst_exit();
88 }
89 /* write something and check */
90 if((ret=fwrite(junk,sizeof(*junk),strlen(junk),stream)) == 0) {
91 tst_resm(TFAIL,"\tfwrite failed\n");
92 tst_exit();
93 }
94
robbiew631dd652003-06-10 15:00:41 +000095 if((size_t)ret != strlen(junk)) {
robbiewc8445632003-01-06 21:35:55 +000096 tst_resm(TFAIL,"strlen(junk),return value from fwrite");
97 local_flag = FAILED;
98 }
99
100 fclose(stream);
101 if((stream=fopen(tempfile1,"r+")) == NULL) {
102 tst_resm(TFAIL,"\tfopen r+ failed\n");
103 tst_exit();
104 }
105 if ( (inbuf=(char *)malloc(strlen(junk))) == 0) {
106 tst_resm(TBROK, "test failed because of malloc\n");
107 tst_exit();
108 }
109 if((ret=fread(inbuf,sizeof(*junk),strlen(junk),stream)) == 0) {
110 tst_resm(TFAIL,"\tfread failed\n");
111 tst_exit();
112 }
robbiew631dd652003-06-10 15:00:41 +0000113 if((size_t)ret != strlen(junk)) {
robbiewc8445632003-01-06 21:35:55 +0000114 tst_resm(TFAIL,"strlen(junk),return value from fread");
115 local_flag = FAILED;
116 }
117 fclose(stream);
118 if (local_flag == PASSED) {
119 tst_resm(TPASS, "Test passed.\n");
120 } else {
121 tst_resm(TFAIL, "Test failed.\n");
122 }
123 /*--------------------------------------------------------------------*/
124 unlink(tempfile1);
125 } /* end for */
126 tst_rmdir();
127 tst_exit();
128 return(0);
129}