1 /* Verify that ftell returns the correct value at various points before and
2 after the handler on which it is called becomes active.
3 Copyright (C) 2014-2024 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
29 static int do_test (void);
31 #define TEST_FUNCTION do_test ()
32 #include "../test-skeleton.c"
34 #define get_handles_fdopen(filename, fd, fp, fd_mode, mode) \
37 (fd) = open ((filename), (fd_mode), 0); \
40 printf ("open failed: %m\n"); \
45 (fp) = fdopen ((fd), (mode)); \
48 printf ("fdopen failed: %m\n"); \
56 #define get_handles_fopen(filename, fd, fp, mode) \
59 (fp) = fopen ((filename), (mode)); \
62 printf ("fopen failed: %m\n"); \
70 printf ("fileno failed: %m\n"); \
77 /* data points to either char_data or wide_data, depending on whether we're
78 testing regular file mode or wide mode respectively. Similarly,
79 fputs_func points to either fputs or fputws. data_len keeps track of the
80 length of the current data and file_len maintains the current file
82 static const void *data
;
83 static const char *char_data
= "abcdef";
84 static const wchar_t *wide_data
= L
"abcdef";
85 static size_t data_len
;
86 static size_t file_len
;
88 typedef int (*fputs_func_t
) (const void *data
, FILE *fp
);
89 typedef void *(*fgets_func_t
) (void *ws
, int n
, FILE *fp
);
90 fputs_func_t fputs_func
;
91 fgets_func_t fgets_func
;
93 /* This test verifies that the offset reported by ftell is correct after the
94 file is truncated using ftruncate. ftruncate does not change the file
95 offset on truncation and hence, SEEK_CUR should continue to point to the
96 old offset and not be changed to the new offset. */
98 do_ftruncate_test (const char *filename
)
109 {"w", O_WRONLY
| O_TRUNC
},
110 {"w+", O_RDWR
| O_TRUNC
},
115 for (int j
= 0; j
< 2; j
++)
117 for (int i
= 0; i
< sizeof (test_modes
) / sizeof (struct test
); i
++)
120 printf ("\tftruncate: %s (file, \"%s\"): ",
121 j
== 0 ? "fopen" : "fdopen",
125 fileret
= get_handles_fopen (filename
, fd
, fp
, test_modes
[i
].mode
);
127 fileret
= get_handles_fdopen (filename
, fd
, fp
,
128 test_modes
[i
].fd_mode
,
134 /* Write some data. */
135 size_t written
= fputs_func (data
, fp
);
139 printf ("fputs[1] failed to write data\n");
143 /* Record the offset. */
144 long offset
= ftell (fp
);
146 /* Flush data to allow switching active handles. */
149 printf ("Flush failed: %m\n");
153 /* Now truncate the file. */
154 if (ftruncate (fd
, 0) != 0)
156 printf ("Failed to truncate file: %m\n");
160 /* ftruncate does not change the offset, so there is no need to call
161 anything to be able to switch active handles. */
162 long new_offset
= ftell (fp
);
164 /* The offset should remain unchanged since ftruncate does not update
166 if (offset
!= new_offset
)
168 printf ("Incorrect offset. Expected %ld, but got %ld\n",
174 printf ("offset = %ld\n", offset
);
182 /* Test that ftell output after a rewind is correct. */
184 do_rewind_test (const char *filename
)
194 {"w", O_WRONLY
| O_TRUNC
, 0, data_len
},
195 {"w+", O_RDWR
| O_TRUNC
, 0, data_len
},
196 {"r+", O_RDWR
, 0, data_len
},
197 /* The new offsets for 'a' and 'a+' modes have to factor in the
198 previous writes since they always append to the end of the
200 {"a", O_WRONLY
, 0, 3 * data_len
},
201 {"a+", O_RDWR
, 0, 4 * data_len
},
204 /* Empty the file before the test so that our offsets are simple to
206 FILE *fp
= fopen (filename
, "w");
209 printf ("Failed to open file for emptying\n");
214 for (int j
= 0; j
< 2; j
++)
216 for (int i
= 0; i
< sizeof (test_modes
) / sizeof (struct test
); i
++)
222 printf ("\trewind: %s (file, \"%s\"): ", j
== 0 ? "fdopen" : "fopen",
226 fileret
= get_handles_fdopen (filename
, fd
, fp
,
227 test_modes
[i
].fd_mode
,
230 fileret
= get_handles_fopen (filename
, fd
, fp
, test_modes
[i
].mode
);
235 /* Write some content to the file, rewind and ensure that the ftell
236 output after the rewind is 0. POSIX does not specify what the
237 behavior is when a file is rewound in 'a' mode, so we retain
238 current behavior, which is to keep the 0 offset. */
239 size_t written
= fputs_func (data
, fp
);
243 printf ("fputs[1] failed to write data\n");
248 long offset
= ftell (fp
);
250 if (offset
!= test_modes
[i
].old_off
)
252 printf ("Incorrect old offset. Expected %zu, but got %ld, ",
253 test_modes
[i
].old_off
, offset
);
257 printf ("old offset = %ld, ", offset
);
259 written
= fputs_func (data
, fp
);
263 printf ("fputs[1] failed to write data\n");
267 /* After this write, the offset in append modes should factor in the
268 implicit lseek to the end of file. */
270 if (offset
!= test_modes
[i
].new_off
)
272 printf ("Incorrect new offset. Expected %zu, but got %ld\n",
273 test_modes
[i
].new_off
, offset
);
277 printf ("new offset = %ld\n", offset
);
283 /* Test that the value of ftell is not cached when the stream handle is not
286 do_ftell_test (const char *filename
)
297 /* In w, w+ and r+ modes, the file position should be at the
298 beginning of the file. After the write, the offset should be
299 updated to data_len. We don't use eof_off in w and a modes since
300 they don't allow reading. */
301 {"w", O_WRONLY
| O_TRUNC
, 0, data_len
, 0},
302 {"w+", O_RDWR
| O_TRUNC
, 0, data_len
, 2 * data_len
},
303 {"r+", O_RDWR
, 0, data_len
, 3 * data_len
},
304 /* For the 'a' mode, the initial file position should be the
305 current end of file. After the write, the offset has data_len
306 added to the old value. For a+ mode however, the initial file
307 position is the file position of the underlying file descriptor,
308 since it is initially assumed to be in read mode. */
309 {"a", O_WRONLY
, 3 * data_len
, 4 * data_len
, 5 * data_len
},
310 {"a+", O_RDWR
, 0, 5 * data_len
, 6 * data_len
},
312 for (int j
= 0; j
< 2; j
++)
314 for (int i
= 0; i
< sizeof (test_modes
) / sizeof (struct test
); i
++)
320 printf ("\tftell: %s (file, \"%s\"): ", j
== 0 ? "fdopen" : "fopen",
324 fileret
= get_handles_fdopen (filename
, fd
, fp
,
325 test_modes
[i
].fd_mode
,
328 fileret
= get_handles_fopen (filename
, fd
, fp
, test_modes
[i
].mode
);
333 long off
= ftell (fp
);
334 if (off
!= test_modes
[i
].old_off
)
336 printf ("Incorrect old offset. Expected %zu but got %ld, ",
337 test_modes
[i
].old_off
, off
);
341 printf ("old offset = %ld, ", off
);
343 /* The effect of this write on the offset should be seen in the ftell
344 call that follows it. */
345 int write_ret
= write (fd
, data
, data_len
);
346 if (write_ret
!= data_len
)
348 printf ("write failed (%m)\n");
353 if (off
!= test_modes
[i
].new_off
)
355 printf ("Incorrect new offset. Expected %zu but got %ld",
356 test_modes
[i
].new_off
, off
);
360 printf ("new offset = %ld", off
);
362 /* Read to the end, write some data to the fd and check if ftell can
363 see the new ofset. Do this test only for files that allow
365 if (test_modes
[i
].fd_mode
!= O_WRONLY
)
367 wchar_t tmpbuf
[data_len
];
371 while (fgets_func (tmpbuf
, data_len
, fp
) && !feof (fp
));
373 write_ret
= write (fd
, data
, data_len
);
374 if (write_ret
!= data_len
)
376 printf ("write failed (%m)\n");
381 if (off
!= test_modes
[i
].eof_off
)
383 printf (", Incorrect offset after read EOF. "
384 "Expected %zu but got %ld\n",
385 test_modes
[i
].eof_off
, off
);
389 printf (", offset after EOF = %ld\n", off
);
401 /* This test opens the file for writing, moves the file offset of the
402 underlying file, writes out data and then checks if ftell trips on it. */
404 do_write_test (const char *filename
)
414 {"w", O_WRONLY
| O_TRUNC
},
415 {"w+", O_RDWR
| O_TRUNC
},
419 for (int j
= 0; j
< 2; j
++)
421 for (int i
= 0; i
< sizeof (test_modes
) / sizeof (struct test
); i
++)
424 printf ("\twrite: %s (file, \"%s\"): ", j
== 0 ? "fopen" : "fdopen",
428 fileret
= get_handles_fopen (filename
, fd
, fp
, test_modes
[i
].mode
);
430 fileret
= get_handles_fdopen (filename
, fd
, fp
,
431 test_modes
[i
].fd_mode
,
437 /* Move offset to just before the end of the file. */
438 off_t seek_ret
= lseek (fd
, file_len
- 1, SEEK_SET
);
441 printf ("lseek failed: %m\n");
445 /* Write some data. */
446 size_t written
= fputs_func (data
, fp
);
450 printf ("fputs[1] failed to write data\n");
454 /* Verify that the offset points to the end of the file. The length
455 of the file would be the original length + the length of data
456 written to it - the amount by which we moved the offset using
458 long offset
= ftell (fp
);
459 file_len
= file_len
- 1 + data_len
;
461 if (offset
!= file_len
)
463 printf ("Incorrect offset. Expected %zu, but got %ld\n",
469 printf ("offset = %ld\n", offset
);
477 /* This test opens a file in append mode, writes some data, and then verifies
478 that ftell does not trip over it. */
480 do_append_test (const char *filename
)
495 for (int j
= 0; j
< 2; j
++)
497 for (int i
= 0; i
< sizeof (test_modes
) / sizeof (struct test
); i
++)
501 printf ("\tappend: %s (file, \"%s\"): ", j
== 0 ? "fopen" : "fdopen",
505 fileret
= get_handles_fopen (filename
, fd
, fp
, test_modes
[i
].mode
);
507 fileret
= get_handles_fdopen (filename
, fd
, fp
,
508 test_modes
[i
].fd_mode
,
514 /* Write some data. */
515 size_t written
= fputs_func (data
, fp
);
519 printf ("fputs[1] failed to write all data\n");
523 /* Verify that the offset points to the end of the file. The file
524 len is maintained by adding data_len each time to reflect the data
526 long offset
= ftell (fp
);
527 file_len
+= data_len
;
529 if (offset
!= file_len
)
531 printf ("Incorrect offset. Expected %zu, but got %ld\n",
537 printf ("offset = %ld\n", offset
);
542 /* For fdopen in 'a' mode, the file descriptor should not change if the file
543 is already open with the O_APPEND flag set. */
544 fd
= open (filename
, O_WRONLY
| O_APPEND
, 0);
547 printf ("open(O_APPEND) failed: %m\n");
551 off_t seek_ret
= lseek (fd
, file_len
- 1, SEEK_SET
);
554 printf ("lseek[O_APPEND][0] failed: %m\n");
558 fp
= fdopen (fd
, "a");
561 printf ("fdopen(O_APPEND) failed: %m\n");
566 off_t new_seek_ret
= lseek (fd
, 0, SEEK_CUR
);
569 printf ("lseek[O_APPEND][1] failed: %m\n");
573 printf ("\tappend: fdopen (file, \"a\"): O_APPEND: ");
575 if (seek_ret
!= new_seek_ret
)
577 printf ("incorrectly modified file offset to %jd, should be %jd",
578 (intmax_t) new_seek_ret
, (intmax_t) seek_ret
);
582 printf ("retained current file offset %jd", (intmax_t) seek_ret
);
584 new_seek_ret
= ftello (fp
);
586 if (seek_ret
!= new_seek_ret
)
588 printf (", ftello reported incorrect offset %jd, should be %jd\n",
589 (intmax_t) new_seek_ret
, (intmax_t) seek_ret
);
593 printf (", ftello reported correct offset %jd\n", (intmax_t) seek_ret
);
601 do_one_test (const char *filename
)
605 ret
|= do_ftell_test (filename
);
606 ret
|= do_write_test (filename
);
607 ret
|= do_append_test (filename
);
608 ret
|= do_rewind_test (filename
);
609 ret
|= do_ftruncate_test (filename
);
614 /* Run a set of tests for ftell for regular files and wide mode files. */
622 int fd
= create_temp_file ("tst-active-handler-tmp.", &filename
);
626 printf ("create_temp_file: %m\n");
630 fp
= fdopen (fd
, "w");
633 printf ("fdopen[0]: %m\n");
639 data_len
= strlen (char_data
);
640 file_len
= strlen (char_data
);
641 written
= fputs (data
, fp
);
645 printf ("fputs[1] failed to write data\n");
653 /* Tests for regular files. */
654 puts ("Regular mode:");
655 fputs_func
= (fputs_func_t
) fputs
;
656 fgets_func
= (fgets_func_t
) fgets
;
658 data_len
= strlen (char_data
);
659 ret
|= do_one_test (filename
);
661 /* Truncate the file before repeating the tests in wide mode. */
662 fp
= fopen (filename
, "w");
665 printf ("fopen failed %m\n");
670 /* Tests for wide files. */
672 if (setlocale (LC_ALL
, "en_US.UTF-8") == NULL
)
674 printf ("Cannot set en_US.UTF-8 locale.\n");
677 fputs_func
= (fputs_func_t
) fputws
;
678 fgets_func
= (fgets_func_t
) fgetws
;
680 data_len
= wcslen (wide_data
);
681 ret
|= do_one_test (filename
);