blob: f9f3ccadad4741e60aee75a673227054b3aae501 [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/filesuite/stream3.c, by Airong Zhang */
21
22/*======================================================================
23 =================== TESTPLAN SEGMENT ===================
24>KEYS: < fseek() ftell()
25>WHAT: < 1) Ensure ftell reports the correct current byte offset.
26>HOW: < 1) Open a file, write to it, reposition the file pointer and
27 check it.
28>BUGS: <
29======================================================================*/
30#define _XOPEN_SOURCE 500
31#include <stdio.h>
robbiewa70576c2003-03-04 18:33:41 +000032#include <errno.h>
robbiewc8445632003-01-06 21:35:55 +000033#include <fcntl.h>
34#include <sys/types.h>
35#include <sys/stat.h>
36#include "test.h"
37#include "usctest.h"
38
39char *TCID = "stream03";
40int TST_TOTAL = 1;
41extern int Tst_count;
42int local_flag;
43
44#define PASSED 1
45#define FAILED 0
46
robbiewc8445632003-01-06 21:35:55 +000047char progname[] = "stream03()" ;
48char tempfile1[40]="";
49long ftell();
50
51/*--------------------------------------------------------------------*/
52int main(int ac, char *av[])
53{
54 FILE *stream;
55 char buf[30];
56 char *junk="abcdefghijklmnopqrstuvwxyz";
57 long pos;
58 off_t opos;
59 int lc; /* loop counter */
60 char *msg; /* message returned from parse_opts */
61
62 /*
63 * parse standard options
64 */
65 if ((msg = parse_opts(ac, av, (option_t *)NULL, NULL)) != (char *)NULL){
66 tst_resm(TBROK, "OPTION PARSING ERROR - %s", msg);
67 tst_exit();
68 /*NOTREACHED*/
69 }
70
71 local_flag = PASSED;
72 tst_tmpdir();
73
74 for (lc = 0; TEST_LOOPING(lc); lc++) {
75
76 sprintf(tempfile1, "stream03.%d", getpid());
77 /*--------------------------------------------------------------------*/
78 //block0:
79 if(creat(tempfile1,0666) < 0) {
80 tst_resm(TBROK,"\tcreat failed\n");
81 tst_exit();
82 }
83 if((stream=fopen(tempfile1,"a+")) == NULL) {
84 tst_resm(TBROK,"\tfopen a+ failed\n");
85 tst_exit();
86 }
87 /* make sure offset of zero at start */
88 pos=ftell(stream);
89 if ( pos != 0 ) {
90 tst_resm(TFAIL,"file pointer descrepancy 1");
91 local_flag = FAILED;
92 }
93 /* write something and check */
94 if(fwrite(junk,sizeof(*junk),strlen(junk),stream) == 0) {
95 tst_resm(TFAIL,"\tfwrite failed\n");
96 tst_exit();
97 }
98 pos=ftell(stream);
robbiew631dd652003-06-10 15:00:41 +000099 if ((size_t)pos != strlen(junk) ) {
robbiewc8445632003-01-06 21:35:55 +0000100 tst_resm(TFAIL, "strlen(junk):file pointer descrepancy 2");
101 local_flag = FAILED;
102 }
103 /* rewind and check */
104 rewind(stream);
105 pos=ftell(stream);
106 if ( pos != 0 ) {
107 tst_resm(TFAIL,0,"file pointer descrepancy 3");
108 local_flag = FAILED;
109 }
110 /* seek from current position and then check */
111 if (fseek(stream,strlen(junk),1) != 0) {
112 tst_resm(TFAIL,"\tfseek failed\n");
113 tst_exit();
114 }
115 pos=ftell(stream);
robbiew631dd652003-06-10 15:00:41 +0000116 if ((size_t)pos != strlen(junk) ) {
robbiewc8445632003-01-06 21:35:55 +0000117 tst_resm(TFAIL,"strlen(junk),file pointer descrepancy 4");
118 local_flag = FAILED;
119 }
120 /* seek from end of file and then check */
121 if (fseek(stream,0,2) != 0) {
122 tst_resm(TFAIL,"\tfseek failed\n");
123 tst_exit();
124 }
125 pos=ftell(stream);
robbiew631dd652003-06-10 15:00:41 +0000126 if ((size_t)pos != strlen(junk) ) {
robbiewc8445632003-01-06 21:35:55 +0000127 tst_resm(TFAIL,"strlen(junk),file pointer descrepancy 5");
128 local_flag = FAILED;
129 }
130 /* rewind with seek and then check */
131 if (fseek(stream,0,0) != 0) {
132 tst_resm(TFAIL,"\tfseek failed\n");
133 tst_exit();
134 }
135 pos=ftell(stream);
136 if (pos != 0 ) {
137 tst_resm(TFAIL,"file pointer descrepancy 6");
138 local_flag = FAILED;
139 }
140
141 /* read till EOF, do getc and then check ftell */
robbiew631dd652003-06-10 15:00:41 +0000142 while (fgets (buf, sizeof(buf), stream));
robbiewc8445632003-01-06 21:35:55 +0000143 pos=ftell(stream);
144 (void) getc(stream);
145 pos=ftell(stream);
robbiew631dd652003-06-10 15:00:41 +0000146 if ((size_t)pos != strlen(junk) ) {
robbiewc8445632003-01-06 21:35:55 +0000147 tst_resm(TFAIL,"strlen(junk),file pointer descrepancy 7");
148 local_flag = FAILED;
149 }
150 fclose(stream);
151 if (local_flag == PASSED) {
152 tst_resm(TPASS, "Test passed in block0.\n");
153 } else {
154 tst_resm(TFAIL, "Test failed in block0.\n");
155 }
156
157 local_flag = PASSED;
158
159 unlink(tempfile1);
160 /*--------------------------------------------------------------------*/
161 //block1:
162 if(creat(tempfile1,0666) < 0) {
163 tst_resm(TFAIL,"\tcreat failed\n");
164 tst_exit();
165 }
166 if((stream=fopen(tempfile1,"a+")) == NULL) {
167 tst_resm(TFAIL,"\tfopen a+ failed\n");
168 tst_exit();
169 }
170 /* make sure offset of zero at start */
171 opos=ftello(stream);
172 if ( opos != 0 ) {
173 tst_resm(TFAIL,"file pointer descrepancy 1");
174 local_flag = FAILED;
175 }
176 /* write something and check */
177 if(fwrite(junk,sizeof(*junk),strlen(junk),stream) == 0) {
178 tst_resm(TFAIL,"\tfwrite failed\n");
179 tst_exit();
180 }
181 opos=ftello(stream);
robbiew631dd652003-06-10 15:00:41 +0000182 if ((size_t)opos != strlen(junk) ) {
robbiewc8445632003-01-06 21:35:55 +0000183 tst_resm(TFAIL,"strlen(junk),file pointer descrepancy 2");
184 local_flag = FAILED;
185 }
186 /* rewind and check */
187 rewind(stream);
188 opos=ftello(stream);
189 if ( opos != 0 ) {
190 tst_resm(TFAIL,"file pointer descrepancy 3");
191 local_flag = FAILED;
192 }
193 /* seek from current position and then check */
194 if (fseeko(stream, (off_t)strlen(junk), 1) != 0) {
195 tst_resm(TFAIL,"\tfseeko failed\n");
196 tst_exit();
197 }
198 opos=ftello(stream);
robbiew631dd652003-06-10 15:00:41 +0000199 if ((size_t)opos != strlen(junk) ) {
robbiewc8445632003-01-06 21:35:55 +0000200 tst_resm(TFAIL,"strlen(junk),file pointer descrepancy 4");
201 local_flag = FAILED;
202 }
203 /* seek from end of file and then check */
204 if (fseeko(stream, (off_t)0, 2) != 0) {
205 tst_resm(TFAIL,"\tfseeko failed\n");
206 tst_exit();
207 }
208 opos=ftello(stream);
robbiew631dd652003-06-10 15:00:41 +0000209 if ((size_t)opos != strlen(junk) ) {
robbiewc8445632003-01-06 21:35:55 +0000210 tst_resm(TFAIL,"strlen(junk),file pointer descrepancy 5");
211 local_flag = FAILED;
212 }
213 /* rewind with seek and then check */
214 if (fseeko(stream, (off_t)0, 0) != 0) {
215 tst_resm(TFAIL,"\tfseeko failed\n");
216 tst_exit();
217 }
218 opos=ftello(stream);
219 if (opos != 0 ) {
220 tst_resm(TFAIL,"file pointer descrepancy 6");
221 local_flag = FAILED;
222 }
223
224 /* read till EOF, do getc and then check ftello */
robbiew631dd652003-06-10 15:00:41 +0000225 while (fgets (buf, sizeof(buf), stream));
robbiewc8445632003-01-06 21:35:55 +0000226 opos=ftello(stream);
227 (void) getc(stream);
228 opos=ftello(stream);
robbiew631dd652003-06-10 15:00:41 +0000229 if ((size_t)opos != strlen(junk) ) {
robbiewc8445632003-01-06 21:35:55 +0000230 tst_resm(TFAIL,"strlen(junk),file pointer descrepancy 7");
231 local_flag = FAILED;
232 }
233 fclose(stream);
234 if (local_flag == PASSED) {
235 tst_resm(TPASS, "Test passed in block1.\n");
236 } else {
237 tst_resm(TFAIL, "Test failed in block1.\n");
238 }
239 /*--------------------------------------------------------------------*/
240 unlink(tempfile1);
241 } /* end for */
242 tst_rmdir();
243 tst_exit();
244 return(0);
245}