robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 1 | /* |
| 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_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame^] | 28 | >BUGS: < |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 29 | ======================================================================*/ |
| 30 | #define _XOPEN_SOURCE 500 |
| 31 | #include <stdio.h> |
robbiew | a70576c | 2003-03-04 18:33:41 +0000 | [diff] [blame] | 32 | #include <errno.h> |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 33 | #include <fcntl.h> |
| 34 | #include <sys/types.h> |
| 35 | #include <sys/stat.h> |
| 36 | #include "test.h" |
| 37 | #include "usctest.h" |
| 38 | |
| 39 | char *TCID = "stream03"; |
| 40 | int TST_TOTAL = 1; |
| 41 | extern int Tst_count; |
| 42 | int local_flag; |
| 43 | |
| 44 | #define PASSED 1 |
| 45 | #define FAILED 0 |
| 46 | |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 47 | char progname[] = "stream03()" ; |
| 48 | char tempfile1[40]=""; |
| 49 | long ftell(); |
| 50 | |
| 51 | /*--------------------------------------------------------------------*/ |
| 52 | int 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: |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 79 | if((stream=fopen(tempfile1,"a+")) == NULL) { |
vapier | fab8d74 | 2006-02-15 05:47:07 +0000 | [diff] [blame] | 80 | tst_resm(TBROK,"fopen(%s) a+ failed: %s", tempfile1, strerror(errno)); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 81 | 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) { |
vapier | fab8d74 | 2006-02-15 05:47:07 +0000 | [diff] [blame] | 91 | tst_resm(TFAIL,"fwrite failed: %s", strerror(errno)); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 92 | tst_exit(); |
| 93 | } |
| 94 | pos=ftell(stream); |
robbiew | 631dd65 | 2003-06-10 15:00:41 +0000 | [diff] [blame] | 95 | if ((size_t)pos != strlen(junk) ) { |
vapier | fab8d74 | 2006-02-15 05:47:07 +0000 | [diff] [blame] | 96 | tst_resm(TFAIL, "strlen(junk)=%zi: file pointer descrepancy 2 (pos=%zi)", strlen(junk), pos); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 97 | local_flag = FAILED; |
| 98 | } |
| 99 | /* rewind and check */ |
| 100 | rewind(stream); |
| 101 | pos=ftell(stream); |
| 102 | if ( pos != 0 ) { |
vapier | fab8d74 | 2006-02-15 05:47:07 +0000 | [diff] [blame] | 103 | tst_resm(TFAIL,0,"file pointer descrepancy 3 (pos=%zi, wanted pos=0)", pos); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 104 | local_flag = FAILED; |
| 105 | } |
| 106 | /* seek from current position and then check */ |
| 107 | if (fseek(stream,strlen(junk),1) != 0) { |
vapier | fab8d74 | 2006-02-15 05:47:07 +0000 | [diff] [blame] | 108 | tst_resm(TFAIL,"fseek failed: %s", strerror(errno)); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 109 | tst_exit(); |
| 110 | } |
| 111 | pos=ftell(stream); |
robbiew | 631dd65 | 2003-06-10 15:00:41 +0000 | [diff] [blame] | 112 | if ((size_t)pos != strlen(junk) ) { |
vapier | fab8d74 | 2006-02-15 05:47:07 +0000 | [diff] [blame] | 113 | tst_resm(TFAIL,"strlen(junk)=%zi: file pointer descrepancy 4 (pos=%zi)", strlen(junk), pos); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 114 | local_flag = FAILED; |
| 115 | } |
| 116 | /* seek from end of file and then check */ |
| 117 | if (fseek(stream,0,2) != 0) { |
vapier | fab8d74 | 2006-02-15 05:47:07 +0000 | [diff] [blame] | 118 | tst_resm(TFAIL,"fseek failed: %s", strerror(errno)); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 119 | tst_exit(); |
| 120 | } |
| 121 | pos=ftell(stream); |
robbiew | 631dd65 | 2003-06-10 15:00:41 +0000 | [diff] [blame] | 122 | if ((size_t)pos != strlen(junk) ) { |
vapier | fab8d74 | 2006-02-15 05:47:07 +0000 | [diff] [blame] | 123 | tst_resm(TFAIL,"strlen(junk)=%zi: file pointer descrepancy 5 (pos=%zi)", strlen(junk), pos); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 124 | local_flag = FAILED; |
| 125 | } |
| 126 | /* rewind with seek and then check */ |
| 127 | if (fseek(stream,0,0) != 0) { |
vapier | fab8d74 | 2006-02-15 05:47:07 +0000 | [diff] [blame] | 128 | tst_resm(TFAIL,"fseek failed: %s", strerror(errno)); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 129 | tst_exit(); |
| 130 | } |
| 131 | pos=ftell(stream); |
| 132 | if (pos != 0 ) { |
vapier | fab8d74 | 2006-02-15 05:47:07 +0000 | [diff] [blame] | 133 | tst_resm(TFAIL,"file pointer descrepancy 6 (pos=%zi, wanted pos=0)", pos); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 134 | local_flag = FAILED; |
| 135 | } |
| 136 | |
| 137 | /* read till EOF, do getc and then check ftell */ |
robbiew | 631dd65 | 2003-06-10 15:00:41 +0000 | [diff] [blame] | 138 | while (fgets (buf, sizeof(buf), stream)); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 139 | pos=ftell(stream); |
| 140 | (void) getc(stream); |
| 141 | pos=ftell(stream); |
robbiew | 631dd65 | 2003-06-10 15:00:41 +0000 | [diff] [blame] | 142 | if ((size_t)pos != strlen(junk) ) { |
vapier | fab8d74 | 2006-02-15 05:47:07 +0000 | [diff] [blame] | 143 | tst_resm(TFAIL,"strlen(junk)=%zi: file pointer descrepancy 7 (pos=%zi)", strlen(junk), pos); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 144 | local_flag = FAILED; |
| 145 | } |
| 146 | fclose(stream); |
| 147 | if (local_flag == PASSED) { |
vapier | fab8d74 | 2006-02-15 05:47:07 +0000 | [diff] [blame] | 148 | tst_resm(TPASS, "Test passed in block0."); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 149 | } else { |
vapier | fab8d74 | 2006-02-15 05:47:07 +0000 | [diff] [blame] | 150 | tst_resm(TFAIL, "Test failed in block0."); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | local_flag = PASSED; |
| 154 | |
| 155 | unlink(tempfile1); |
| 156 | /*--------------------------------------------------------------------*/ |
| 157 | //block1: |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 158 | if((stream=fopen(tempfile1,"a+")) == NULL) { |
vapier | fab8d74 | 2006-02-15 05:47:07 +0000 | [diff] [blame] | 159 | tst_resm(TFAIL,"fopen(%s) a+ failed: %s", tempfile1, strerror(errno)); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 160 | tst_exit(); |
| 161 | } |
| 162 | /* make sure offset of zero at start */ |
| 163 | opos=ftello(stream); |
| 164 | if ( opos != 0 ) { |
vapier | fab8d74 | 2006-02-15 05:47:07 +0000 | [diff] [blame] | 165 | tst_resm(TFAIL,"file pointer descrepancy 1 (opos=%zi, wanted opos=0)", opos); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 166 | local_flag = FAILED; |
| 167 | } |
| 168 | /* write something and check */ |
| 169 | if(fwrite(junk,sizeof(*junk),strlen(junk),stream) == 0) { |
vapier | fab8d74 | 2006-02-15 05:47:07 +0000 | [diff] [blame] | 170 | tst_resm(TFAIL,"fwrite failed: %s", strerror(errno)); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 171 | tst_exit(); |
| 172 | } |
| 173 | opos=ftello(stream); |
robbiew | 631dd65 | 2003-06-10 15:00:41 +0000 | [diff] [blame] | 174 | if ((size_t)opos != strlen(junk) ) { |
vapier | fab8d74 | 2006-02-15 05:47:07 +0000 | [diff] [blame] | 175 | tst_resm(TFAIL,"strlen(junk)=%zi: file pointer descrepancy 2 (opos=%zi)", strlen(junk), opos); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 176 | local_flag = FAILED; |
| 177 | } |
| 178 | /* rewind and check */ |
| 179 | rewind(stream); |
| 180 | opos=ftello(stream); |
| 181 | if ( opos != 0 ) { |
vapier | fab8d74 | 2006-02-15 05:47:07 +0000 | [diff] [blame] | 182 | tst_resm(TFAIL,"file pointer descrepancy 3 (opos=%zi, wanted opos=0)", opos); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 183 | local_flag = FAILED; |
| 184 | } |
| 185 | /* seek from current position and then check */ |
| 186 | if (fseeko(stream, (off_t)strlen(junk), 1) != 0) { |
vapier | fab8d74 | 2006-02-15 05:47:07 +0000 | [diff] [blame] | 187 | tst_resm(TFAIL,"fseeko failed: %s", strerror(errno)); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 188 | tst_exit(); |
| 189 | } |
| 190 | opos=ftello(stream); |
robbiew | 631dd65 | 2003-06-10 15:00:41 +0000 | [diff] [blame] | 191 | if ((size_t)opos != strlen(junk) ) { |
vapier | fab8d74 | 2006-02-15 05:47:07 +0000 | [diff] [blame] | 192 | tst_resm(TFAIL,"strlen(junk)=%zi: file pointer descrepancy 4 (opos=%zi)", strlen(junk), opos); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 193 | local_flag = FAILED; |
| 194 | } |
| 195 | /* seek from end of file and then check */ |
| 196 | if (fseeko(stream, (off_t)0, 2) != 0) { |
vapier | fab8d74 | 2006-02-15 05:47:07 +0000 | [diff] [blame] | 197 | tst_resm(TFAIL,"fseeko failed: %s", strerror(errno)); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 198 | tst_exit(); |
| 199 | } |
| 200 | opos=ftello(stream); |
robbiew | 631dd65 | 2003-06-10 15:00:41 +0000 | [diff] [blame] | 201 | if ((size_t)opos != strlen(junk) ) { |
vapier | fab8d74 | 2006-02-15 05:47:07 +0000 | [diff] [blame] | 202 | tst_resm(TFAIL,"strlen(junk)=%zi: file pointer descrepancy 5 (opos=%zi)", strlen(junk), opos); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 203 | local_flag = FAILED; |
| 204 | } |
| 205 | /* rewind with seek and then check */ |
| 206 | if (fseeko(stream, (off_t)0, 0) != 0) { |
vapier | fab8d74 | 2006-02-15 05:47:07 +0000 | [diff] [blame] | 207 | tst_resm(TFAIL,"fseeko failed: %s", strerror(errno)); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 208 | tst_exit(); |
| 209 | } |
| 210 | opos=ftello(stream); |
| 211 | if (opos != 0 ) { |
vapier | fab8d74 | 2006-02-15 05:47:07 +0000 | [diff] [blame] | 212 | tst_resm(TFAIL,"file pointer descrepancy 6 (opos=%zi, wanted opos=0)", opos); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 213 | local_flag = FAILED; |
| 214 | } |
| 215 | |
| 216 | /* read till EOF, do getc and then check ftello */ |
robbiew | 631dd65 | 2003-06-10 15:00:41 +0000 | [diff] [blame] | 217 | while (fgets (buf, sizeof(buf), stream)); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 218 | opos=ftello(stream); |
| 219 | (void) getc(stream); |
| 220 | opos=ftello(stream); |
robbiew | 631dd65 | 2003-06-10 15:00:41 +0000 | [diff] [blame] | 221 | if ((size_t)opos != strlen(junk) ) { |
vapier | fab8d74 | 2006-02-15 05:47:07 +0000 | [diff] [blame] | 222 | tst_resm(TFAIL,"strlen(junk)=%zi: file pointer descrepancy 7 (opos=%zi)", strlen(junk), opos); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 223 | local_flag = FAILED; |
| 224 | } |
| 225 | fclose(stream); |
| 226 | if (local_flag == PASSED) { |
vapier | fab8d74 | 2006-02-15 05:47:07 +0000 | [diff] [blame] | 227 | tst_resm(TPASS, "Test passed in block1."); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 228 | } else { |
vapier | fab8d74 | 2006-02-15 05:47:07 +0000 | [diff] [blame] | 229 | tst_resm(TFAIL, "Test failed in block1."); |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 230 | } |
| 231 | /*--------------------------------------------------------------------*/ |
| 232 | unlink(tempfile1); |
| 233 | } /* end for */ |
| 234 | tst_rmdir(); |
| 235 | tst_exit(); |
subrata_modak | 43337a3 | 2009-02-26 11:43:51 +0000 | [diff] [blame] | 236 | return 0; |
robbiew | c844563 | 2003-01-06 21:35:55 +0000 | [diff] [blame] | 237 | } |