blob: 548618530add2aabbfce73637c1950af4e2463ec [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.
subrata_modak4bb656a2009-02-26 12:02:09 +000028>BUGS: <
robbiewc8445632003-01-06 21:35:55 +000029======================================================================*/
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:
robbiewc8445632003-01-06 21:35:55 +000079 if((stream=fopen(tempfile1,"a+")) == NULL) {
vapierfab8d742006-02-15 05:47:07 +000080 tst_resm(TBROK,"fopen(%s) a+ failed: %s", tempfile1, strerror(errno));
robbiewc8445632003-01-06 21:35:55 +000081 tst_exit();
82 }
83 /* make sure offset of zero at start */
84 pos=ftell(stream);
85 if ( pos != 0 ) {
86 tst_resm(TFAIL,"file pointer descrepancy 1");
87 local_flag = FAILED;
88 }
89 /* write something and check */
90 if(fwrite(junk,sizeof(*junk),strlen(junk),stream) == 0) {
vapierfab8d742006-02-15 05:47:07 +000091 tst_resm(TFAIL,"fwrite failed: %s", strerror(errno));
robbiewc8445632003-01-06 21:35:55 +000092 tst_exit();
93 }
94 pos=ftell(stream);
robbiew631dd652003-06-10 15:00:41 +000095 if ((size_t)pos != strlen(junk) ) {
vapierfab8d742006-02-15 05:47:07 +000096 tst_resm(TFAIL, "strlen(junk)=%zi: file pointer descrepancy 2 (pos=%zi)", strlen(junk), pos);
robbiewc8445632003-01-06 21:35:55 +000097 local_flag = FAILED;
98 }
99 /* rewind and check */
100 rewind(stream);
101 pos=ftell(stream);
102 if ( pos != 0 ) {
vapierfab8d742006-02-15 05:47:07 +0000103 tst_resm(TFAIL,0,"file pointer descrepancy 3 (pos=%zi, wanted pos=0)", pos);
robbiewc8445632003-01-06 21:35:55 +0000104 local_flag = FAILED;
105 }
106 /* seek from current position and then check */
107 if (fseek(stream,strlen(junk),1) != 0) {
vapierfab8d742006-02-15 05:47:07 +0000108 tst_resm(TFAIL,"fseek failed: %s", strerror(errno));
robbiewc8445632003-01-06 21:35:55 +0000109 tst_exit();
110 }
111 pos=ftell(stream);
robbiew631dd652003-06-10 15:00:41 +0000112 if ((size_t)pos != strlen(junk) ) {
vapierfab8d742006-02-15 05:47:07 +0000113 tst_resm(TFAIL,"strlen(junk)=%zi: file pointer descrepancy 4 (pos=%zi)", strlen(junk), pos);
robbiewc8445632003-01-06 21:35:55 +0000114 local_flag = FAILED;
115 }
116 /* seek from end of file and then check */
117 if (fseek(stream,0,2) != 0) {
vapierfab8d742006-02-15 05:47:07 +0000118 tst_resm(TFAIL,"fseek failed: %s", strerror(errno));
robbiewc8445632003-01-06 21:35:55 +0000119 tst_exit();
120 }
121 pos=ftell(stream);
robbiew631dd652003-06-10 15:00:41 +0000122 if ((size_t)pos != strlen(junk) ) {
vapierfab8d742006-02-15 05:47:07 +0000123 tst_resm(TFAIL,"strlen(junk)=%zi: file pointer descrepancy 5 (pos=%zi)", strlen(junk), pos);
robbiewc8445632003-01-06 21:35:55 +0000124 local_flag = FAILED;
125 }
126 /* rewind with seek and then check */
127 if (fseek(stream,0,0) != 0) {
vapierfab8d742006-02-15 05:47:07 +0000128 tst_resm(TFAIL,"fseek failed: %s", strerror(errno));
robbiewc8445632003-01-06 21:35:55 +0000129 tst_exit();
130 }
131 pos=ftell(stream);
132 if (pos != 0 ) {
vapierfab8d742006-02-15 05:47:07 +0000133 tst_resm(TFAIL,"file pointer descrepancy 6 (pos=%zi, wanted pos=0)", pos);
robbiewc8445632003-01-06 21:35:55 +0000134 local_flag = FAILED;
135 }
136
137 /* read till EOF, do getc and then check ftell */
robbiew631dd652003-06-10 15:00:41 +0000138 while (fgets (buf, sizeof(buf), stream));
robbiewc8445632003-01-06 21:35:55 +0000139 pos=ftell(stream);
140 (void) getc(stream);
141 pos=ftell(stream);
robbiew631dd652003-06-10 15:00:41 +0000142 if ((size_t)pos != strlen(junk) ) {
vapierfab8d742006-02-15 05:47:07 +0000143 tst_resm(TFAIL,"strlen(junk)=%zi: file pointer descrepancy 7 (pos=%zi)", strlen(junk), pos);
robbiewc8445632003-01-06 21:35:55 +0000144 local_flag = FAILED;
145 }
146 fclose(stream);
147 if (local_flag == PASSED) {
vapierfab8d742006-02-15 05:47:07 +0000148 tst_resm(TPASS, "Test passed in block0.");
robbiewc8445632003-01-06 21:35:55 +0000149 } else {
vapierfab8d742006-02-15 05:47:07 +0000150 tst_resm(TFAIL, "Test failed in block0.");
robbiewc8445632003-01-06 21:35:55 +0000151 }
152
153 local_flag = PASSED;
154
155 unlink(tempfile1);
156 /*--------------------------------------------------------------------*/
157 //block1:
robbiewc8445632003-01-06 21:35:55 +0000158 if((stream=fopen(tempfile1,"a+")) == NULL) {
vapierfab8d742006-02-15 05:47:07 +0000159 tst_resm(TFAIL,"fopen(%s) a+ failed: %s", tempfile1, strerror(errno));
robbiewc8445632003-01-06 21:35:55 +0000160 tst_exit();
161 }
162 /* make sure offset of zero at start */
163 opos=ftello(stream);
164 if ( opos != 0 ) {
vapierfab8d742006-02-15 05:47:07 +0000165 tst_resm(TFAIL,"file pointer descrepancy 1 (opos=%zi, wanted opos=0)", opos);
robbiewc8445632003-01-06 21:35:55 +0000166 local_flag = FAILED;
167 }
168 /* write something and check */
169 if(fwrite(junk,sizeof(*junk),strlen(junk),stream) == 0) {
vapierfab8d742006-02-15 05:47:07 +0000170 tst_resm(TFAIL,"fwrite failed: %s", strerror(errno));
robbiewc8445632003-01-06 21:35:55 +0000171 tst_exit();
172 }
173 opos=ftello(stream);
robbiew631dd652003-06-10 15:00:41 +0000174 if ((size_t)opos != strlen(junk) ) {
vapierfab8d742006-02-15 05:47:07 +0000175 tst_resm(TFAIL,"strlen(junk)=%zi: file pointer descrepancy 2 (opos=%zi)", strlen(junk), opos);
robbiewc8445632003-01-06 21:35:55 +0000176 local_flag = FAILED;
177 }
178 /* rewind and check */
179 rewind(stream);
180 opos=ftello(stream);
181 if ( opos != 0 ) {
vapierfab8d742006-02-15 05:47:07 +0000182 tst_resm(TFAIL,"file pointer descrepancy 3 (opos=%zi, wanted opos=0)", opos);
robbiewc8445632003-01-06 21:35:55 +0000183 local_flag = FAILED;
184 }
185 /* seek from current position and then check */
186 if (fseeko(stream, (off_t)strlen(junk), 1) != 0) {
vapierfab8d742006-02-15 05:47:07 +0000187 tst_resm(TFAIL,"fseeko failed: %s", strerror(errno));
robbiewc8445632003-01-06 21:35:55 +0000188 tst_exit();
189 }
190 opos=ftello(stream);
robbiew631dd652003-06-10 15:00:41 +0000191 if ((size_t)opos != strlen(junk) ) {
vapierfab8d742006-02-15 05:47:07 +0000192 tst_resm(TFAIL,"strlen(junk)=%zi: file pointer descrepancy 4 (opos=%zi)", strlen(junk), opos);
robbiewc8445632003-01-06 21:35:55 +0000193 local_flag = FAILED;
194 }
195 /* seek from end of file and then check */
196 if (fseeko(stream, (off_t)0, 2) != 0) {
vapierfab8d742006-02-15 05:47:07 +0000197 tst_resm(TFAIL,"fseeko failed: %s", strerror(errno));
robbiewc8445632003-01-06 21:35:55 +0000198 tst_exit();
199 }
200 opos=ftello(stream);
robbiew631dd652003-06-10 15:00:41 +0000201 if ((size_t)opos != strlen(junk) ) {
vapierfab8d742006-02-15 05:47:07 +0000202 tst_resm(TFAIL,"strlen(junk)=%zi: file pointer descrepancy 5 (opos=%zi)", strlen(junk), opos);
robbiewc8445632003-01-06 21:35:55 +0000203 local_flag = FAILED;
204 }
205 /* rewind with seek and then check */
206 if (fseeko(stream, (off_t)0, 0) != 0) {
vapierfab8d742006-02-15 05:47:07 +0000207 tst_resm(TFAIL,"fseeko failed: %s", strerror(errno));
robbiewc8445632003-01-06 21:35:55 +0000208 tst_exit();
209 }
210 opos=ftello(stream);
211 if (opos != 0 ) {
vapierfab8d742006-02-15 05:47:07 +0000212 tst_resm(TFAIL,"file pointer descrepancy 6 (opos=%zi, wanted opos=0)", opos);
robbiewc8445632003-01-06 21:35:55 +0000213 local_flag = FAILED;
214 }
215
216 /* read till EOF, do getc and then check ftello */
robbiew631dd652003-06-10 15:00:41 +0000217 while (fgets (buf, sizeof(buf), stream));
robbiewc8445632003-01-06 21:35:55 +0000218 opos=ftello(stream);
219 (void) getc(stream);
220 opos=ftello(stream);
robbiew631dd652003-06-10 15:00:41 +0000221 if ((size_t)opos != strlen(junk) ) {
vapierfab8d742006-02-15 05:47:07 +0000222 tst_resm(TFAIL,"strlen(junk)=%zi: file pointer descrepancy 7 (opos=%zi)", strlen(junk), opos);
robbiewc8445632003-01-06 21:35:55 +0000223 local_flag = FAILED;
224 }
225 fclose(stream);
226 if (local_flag == PASSED) {
vapierfab8d742006-02-15 05:47:07 +0000227 tst_resm(TPASS, "Test passed in block1.");
robbiewc8445632003-01-06 21:35:55 +0000228 } else {
vapierfab8d742006-02-15 05:47:07 +0000229 tst_resm(TFAIL, "Test failed in block1.");
robbiewc8445632003-01-06 21:35:55 +0000230 }
231 /*--------------------------------------------------------------------*/
232 unlink(tempfile1);
233 } /* end for */
234 tst_rmdir();
235 tst_exit();
subrata_modak43337a32009-02-26 11:43:51 +0000236 return 0;
robbiewc8445632003-01-06 21:35:55 +0000237}