blob: 47ba944adb03da1f139d365aee6455e9bf7c45cc [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/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>
subrata_modakda124b92009-10-13 14:00:45 +000036#include <inttypes.h>
robbiewc8445632003-01-06 21:35:55 +000037#include "test.h"
robbiewc8445632003-01-06 21:35:55 +000038
39char *TCID = "stream03";
40int TST_TOTAL = 1;
Wanlong Gao354ebb42012-12-07 10:10:04 +080041int local_flag;
robbiewc8445632003-01-06 21:35:55 +000042
43#define PASSED 1
44#define FAILED 0
45
Wanlong Gao354ebb42012-12-07 10:10:04 +080046char progname[] = "stream03()";
47char tempfile1[40] = "";
robbiewc8445632003-01-06 21:35:55 +000048
robbiewc8445632003-01-06 21:35:55 +000049int main(int ac, char *av[])
50{
51 FILE *stream;
52 char buf[30];
Wanlong Gao354ebb42012-12-07 10:10:04 +080053 char *junk = "abcdefghijklmnopqrstuvwxyz";
robbiewc8445632003-01-06 21:35:55 +000054 long pos;
55 off_t opos;
subrata_modakda124b92009-10-13 14:00:45 +000056 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020057 const char *msg;
robbiewc8445632003-01-06 21:35:55 +000058
subrata_modakda124b92009-10-13 14:00:45 +000059 /*
60 * parse standard options
61 */
62 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +010063 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
subrata_modakda124b92009-10-13 14:00:45 +000064 }
robbiewc8445632003-01-06 21:35:55 +000065
subrata_modakda124b92009-10-13 14:00:45 +000066 local_flag = PASSED;
robbiewc8445632003-01-06 21:35:55 +000067 tst_tmpdir();
68
69 for (lc = 0; TEST_LOOPING(lc); lc++) {
70
71 sprintf(tempfile1, "stream03.%d", getpid());
72 /*--------------------------------------------------------------------*/
Wanlong Gao354ebb42012-12-07 10:10:04 +080073 //block0:
subrata_modakda124b92009-10-13 14:00:45 +000074
Wanlong Gao354ebb42012-12-07 10:10:04 +080075 if ((stream = fopen(tempfile1, "a+")) == NULL) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +010076 tst_brkm(TBROK, NULL, "fopen(%s) a+ failed: %s",
77 tempfile1,
Wanlong Gao354ebb42012-12-07 10:10:04 +080078 strerror(errno));
robbiewc8445632003-01-06 21:35:55 +000079 }
subrata_modakda124b92009-10-13 14:00:45 +000080
robbiewc8445632003-01-06 21:35:55 +000081 /* make sure offset of zero at start */
Wanlong Gao354ebb42012-12-07 10:10:04 +080082 pos = ftell(stream);
subrata_modakda124b92009-10-13 14:00:45 +000083
84 if (pos != 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +080085 tst_resm(TFAIL, "file pointer descrepancy 1");
robbiewc8445632003-01-06 21:35:55 +000086 local_flag = FAILED;
87 }
subrata_modakda124b92009-10-13 14:00:45 +000088
robbiewc8445632003-01-06 21:35:55 +000089 /* write something and check */
subrata_modakda124b92009-10-13 14:00:45 +000090 if (fwrite(junk, sizeof(*junk), strlen(junk), stream) == 0) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +010091 tst_brkm(TFAIL, NULL, "fwrite failed: %s",
92 strerror(errno));
robbiewc8445632003-01-06 21:35:55 +000093 }
subrata_modakda124b92009-10-13 14:00:45 +000094
Wanlong Gao354ebb42012-12-07 10:10:04 +080095 pos = ftell(stream);
subrata_modakda124b92009-10-13 14:00:45 +000096
97 if (pos != strlen(junk)) {
Wanlong Gao354ebb42012-12-07 10:10:04 +080098 tst_resm(TFAIL,
99 "strlen(junk)=%zi: file pointer descrepancy 2 (pos=%li)",
100 strlen(junk), pos);
robbiewc8445632003-01-06 21:35:55 +0000101 local_flag = FAILED;
102 }
subrata_modakda124b92009-10-13 14:00:45 +0000103
robbiewc8445632003-01-06 21:35:55 +0000104 /* rewind and check */
105 rewind(stream);
subrata_modakda124b92009-10-13 14:00:45 +0000106 pos = ftell(stream);
107
108 if (pos != 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800109 tst_resm(TFAIL,
110 "file pointer descrepancy 3 (pos=%li, wanted pos=0)",
111 pos);
robbiewc8445632003-01-06 21:35:55 +0000112 local_flag = FAILED;
113 }
subrata_modakda124b92009-10-13 14:00:45 +0000114
robbiewc8445632003-01-06 21:35:55 +0000115 /* seek from current position and then check */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800116 if (fseek(stream, strlen(junk), 1) != 0) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100117 tst_brkm(TFAIL, NULL, "fseek failed: %s",
118 strerror(errno));
robbiewc8445632003-01-06 21:35:55 +0000119 }
subrata_modakda124b92009-10-13 14:00:45 +0000120
121 pos = ftell(stream);
122
123 if (pos != strlen(junk)) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800124 tst_resm(TFAIL,
125 "strlen(junk)=%zi: file pointer descrepancy 4 (pos=%li)",
126 strlen(junk), pos);
robbiewc8445632003-01-06 21:35:55 +0000127 local_flag = FAILED;
128 }
subrata_modakda124b92009-10-13 14:00:45 +0000129
robbiewc8445632003-01-06 21:35:55 +0000130 /* seek from end of file and then check */
subrata_modakda124b92009-10-13 14:00:45 +0000131 if (fseek(stream, 0, 2) != 0) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100132 tst_brkm(TFAIL, NULL, "fseek failed: %s",
133 strerror(errno));
robbiewc8445632003-01-06 21:35:55 +0000134 }
subrata_modakda124b92009-10-13 14:00:45 +0000135
136 pos = ftell(stream);
137
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800138 if (pos != strlen(junk)) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800139 tst_resm(TFAIL,
140 "strlen(junk)=%zi: file pointer descrepancy 5 (pos=%li)",
141 strlen(junk), pos);
robbiewc8445632003-01-06 21:35:55 +0000142 local_flag = FAILED;
143 }
subrata_modakda124b92009-10-13 14:00:45 +0000144
robbiewc8445632003-01-06 21:35:55 +0000145 /* rewind with seek and then check */
subrata_modakda124b92009-10-13 14:00:45 +0000146 if (fseek(stream, 0, 0) != 0) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100147 tst_brkm(TFAIL, NULL, "fseek failed: %s",
148 strerror(errno));
robbiewc8445632003-01-06 21:35:55 +0000149 }
subrata_modakda124b92009-10-13 14:00:45 +0000150
151 pos = ftell(stream);
152
153 if (pos != 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800154 tst_resm(TFAIL,
155 "file pointer descrepancy 6 (pos=%li, wanted pos=0)",
156 pos);
robbiewc8445632003-01-06 21:35:55 +0000157 local_flag = FAILED;
158 }
159
160 /* read till EOF, do getc and then check ftell */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800161 while (fgets(buf, sizeof(buf), stream)) ;
162 pos = ftell(stream);
subrata_modakda124b92009-10-13 14:00:45 +0000163 getc(stream);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800164 pos = ftell(stream);
subrata_modakda124b92009-10-13 14:00:45 +0000165
166 if (pos != strlen(junk)) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800167 tst_resm(TFAIL,
168 "strlen(junk)=%zi: file pointer descrepancy 7 (pos=%li)",
169 strlen(junk), pos);
robbiewc8445632003-01-06 21:35:55 +0000170 local_flag = FAILED;
171 }
subrata_modakda124b92009-10-13 14:00:45 +0000172
robbiewc8445632003-01-06 21:35:55 +0000173 fclose(stream);
subrata_modakda124b92009-10-13 14:00:45 +0000174
robbiewc8445632003-01-06 21:35:55 +0000175 if (local_flag == PASSED) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800176 tst_resm(TPASS, "Test passed in block0.");
177 } else {
178 tst_resm(TFAIL, "Test failed in block0.");
179 }
robbiewc8445632003-01-06 21:35:55 +0000180
Wanlong Gao354ebb42012-12-07 10:10:04 +0800181 local_flag = PASSED;
robbiewc8445632003-01-06 21:35:55 +0000182
183 unlink(tempfile1);
184 /*--------------------------------------------------------------------*/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800185 //block1:
subrata_modakda124b92009-10-13 14:00:45 +0000186 if ((stream = fopen(tempfile1, "a+")) == NULL) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100187 tst_brkm(TFAIL, NULL, "fopen(%s) a+ failed: %s",
188 tempfile1,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800189 strerror(errno));
robbiewc8445632003-01-06 21:35:55 +0000190 }
subrata_modakda124b92009-10-13 14:00:45 +0000191
robbiewc8445632003-01-06 21:35:55 +0000192 /* make sure offset of zero at start */
subrata_modakda124b92009-10-13 14:00:45 +0000193 opos = ftello(stream);
194
195 if (opos != 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800196 tst_resm(TFAIL,
197 "file pointer descrepancy 1 (opos=%" PRId64
198 ", wanted opos=0)", (int64_t) opos);
robbiewc8445632003-01-06 21:35:55 +0000199 local_flag = FAILED;
200 }
subrata_modakda124b92009-10-13 14:00:45 +0000201
robbiewc8445632003-01-06 21:35:55 +0000202 /* write something and check */
subrata_modakda124b92009-10-13 14:00:45 +0000203 if (fwrite(junk, sizeof(*junk), strlen(junk), stream) == 0) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100204 tst_brkm(TFAIL, NULL, "fwrite failed: %s",
205 strerror(errno));
robbiewc8445632003-01-06 21:35:55 +0000206 }
subrata_modakda124b92009-10-13 14:00:45 +0000207
208 opos = ftello(stream);
209
210 if (opos != strlen(junk)) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800211 tst_resm(TFAIL,
212 "strlen(junk)=%zi: file pointer descrepancy 2 (opos=%"
213 PRId64 ")", strlen(junk), (int64_t) opos);
robbiewc8445632003-01-06 21:35:55 +0000214 local_flag = FAILED;
215 }
subrata_modakda124b92009-10-13 14:00:45 +0000216
robbiewc8445632003-01-06 21:35:55 +0000217 /* rewind and check */
218 rewind(stream);
subrata_modakda124b92009-10-13 14:00:45 +0000219 opos = ftello(stream);
220
221 if (opos != 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800222 tst_resm(TFAIL,
223 "file pointer descrepancy 3 (opos=%" PRId64
224 ", wanted opos=0)", (int64_t) opos);
robbiewc8445632003-01-06 21:35:55 +0000225 local_flag = FAILED;
226 }
subrata_modakda124b92009-10-13 14:00:45 +0000227
robbiewc8445632003-01-06 21:35:55 +0000228 /* seek from current position and then check */
subrata_modakda124b92009-10-13 14:00:45 +0000229 if (fseeko(stream, strlen(junk), 1) != 0) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100230 tst_brkm(TFAIL, NULL, "fseeko failed: %s",
231 strerror(errno));
robbiewc8445632003-01-06 21:35:55 +0000232 }
subrata_modakda124b92009-10-13 14:00:45 +0000233
234 opos = ftello(stream);
235
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800236 if (opos != strlen(junk)) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800237 tst_resm(TFAIL,
238 "strlen(junk)=%zi: file pointer descrepancy 4 (opos=%"
239 PRId64 ")", strlen(junk), (int64_t) opos);
robbiewc8445632003-01-06 21:35:55 +0000240 local_flag = FAILED;
241 }
subrata_modakda124b92009-10-13 14:00:45 +0000242
robbiewc8445632003-01-06 21:35:55 +0000243 /* seek from end of file and then check */
subrata_modakda124b92009-10-13 14:00:45 +0000244 if (fseeko(stream, 0, 2) != 0) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100245 tst_brkm(TFAIL, NULL, "fseeko failed: %s",
246 strerror(errno));
robbiewc8445632003-01-06 21:35:55 +0000247 }
subrata_modakda124b92009-10-13 14:00:45 +0000248
249 opos = ftello(stream);
250
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800251 if (opos != strlen(junk)) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800252 tst_resm(TFAIL,
253 "strlen(junk)=%zi: file pointer descrepancy 5 (opos=%"
254 PRId64 ")", strlen(junk), (int64_t) opos);
robbiewc8445632003-01-06 21:35:55 +0000255 local_flag = FAILED;
256 }
subrata_modakda124b92009-10-13 14:00:45 +0000257
robbiewc8445632003-01-06 21:35:55 +0000258 /* rewind with seek and then check */
subrata_modakda124b92009-10-13 14:00:45 +0000259 if (fseeko(stream, 0, 0) != 0) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100260 tst_brkm(TFAIL, NULL, "fseeko failed: %s",
261 strerror(errno));
robbiewc8445632003-01-06 21:35:55 +0000262 }
subrata_modakda124b92009-10-13 14:00:45 +0000263
264 opos = ftello(stream);
265
266 if (opos != 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800267 tst_resm(TFAIL,
268 "file pointer descrepancy 6 (opos=%" PRId64
269 ", wanted opos=0)", (int64_t) opos);
robbiewc8445632003-01-06 21:35:55 +0000270 local_flag = FAILED;
271 }
272
273 /* read till EOF, do getc and then check ftello */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800274 while (fgets(buf, sizeof(buf), stream)) ;
subrata_modakda124b92009-10-13 14:00:45 +0000275
Wanlong Gao354ebb42012-12-07 10:10:04 +0800276 opos = ftello(stream);
subrata_modakda124b92009-10-13 14:00:45 +0000277 getc(stream);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800278 opos = ftello(stream);
subrata_modakda124b92009-10-13 14:00:45 +0000279
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800280 if (opos != strlen(junk)) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800281 tst_resm(TFAIL,
282 "strlen(junk)=%zi: file pointer descrepancy 7 (opos=%li)",
283 strlen(junk), opos);
robbiewc8445632003-01-06 21:35:55 +0000284 local_flag = FAILED;
285 }
subrata_modakda124b92009-10-13 14:00:45 +0000286
robbiewc8445632003-01-06 21:35:55 +0000287 fclose(stream);
subrata_modakda124b92009-10-13 14:00:45 +0000288
robbiewc8445632003-01-06 21:35:55 +0000289 if (local_flag == PASSED) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800290 tst_resm(TPASS, "Test passed in block1.");
291 } else {
292 tst_resm(TFAIL, "Test failed in block1.");
293 }
subrata_modakda124b92009-10-13 14:00:45 +0000294
robbiewc8445632003-01-06 21:35:55 +0000295 unlink(tempfile1);
subrata_modakda124b92009-10-13 14:00:45 +0000296 }
297
robbiewc8445632003-01-06 21:35:55 +0000298 tst_rmdir();
299 tst_exit();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700300}