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 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 <http://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 fputs_func_t fputs_func;
91 /* Test that ftell output after a rewind is correct. */
93 do_rewind_test (const char *filename)
103 {"w", O_WRONLY, 0, data_len},
104 {"w+", O_RDWR, 0, data_len},
105 {"r+", O_RDWR, 0, data_len},
106 /* The new offsets for 'a' and 'a+' modes have to factor in the
107 previous writes since they always append to the end of the
109 {"a", O_WRONLY, 0, 3 * data_len},
110 {"a+", O_RDWR, 0, 4 * data_len},
113 /* Empty the file before the test so that our offsets are simple to
115 FILE *fp = fopen (filename, "w");
118 printf ("Failed to open file for emptying\n");
123 for (int j = 0; j < 2; j++)
125 for (int i = 0; i < sizeof (test_modes) / sizeof (struct test); i++)
131 printf ("\trewind: %s (file, \"%s\"): ", j == 0 ? "fdopen" : "fopen",
135 fileret = get_handles_fdopen (filename, fd, fp,
136 test_modes[i].fd_mode,
139 fileret = get_handles_fopen (filename, fd, fp, test_modes[i].mode);
144 /* Write some content to the file, rewind and ensure that the ftell
145 output after the rewind is 0. POSIX does not specify what the
146 behavior is when a file is rewound in 'a' mode, so we retain
147 current behavior, which is to keep the 0 offset. */
148 size_t written = fputs_func (data, fp);
152 printf ("fputs[1] failed to write data\n");
157 long offset = ftell (fp);
159 if (offset != test_modes[i].old_off)
161 printf ("Incorrect old offset. Expected %zu, but got %ld, ",
162 test_modes[i].old_off, offset);
166 printf ("old offset = %ld, ", offset);
168 written = fputs_func (data, fp);
172 printf ("fputs[1] failed to write data\n");
176 /* After this write, the offset in append modes should factor in the
177 implicit lseek to the end of file. */
179 if (offset != test_modes[i].new_off)
181 printf ("Incorrect new offset. Expected %zu, but got %ld\n",
182 test_modes[i].new_off, offset);
186 printf ("new offset = %ld\n", offset);
192 /* Test that the value of ftell is not cached when the stream handle is not
195 do_ftell_test (const char *filename)
205 /* In w, w+ and r+ modes, the file position should be at the
206 beginning of the file. After the write, the offset should be
207 updated to data_len. */
208 {"w", O_WRONLY, 0, data_len},
209 {"w+", O_RDWR, 0, data_len},
210 {"r+", O_RDWR, 0, data_len},
211 /* For the 'a' mode, the initial file position should be the
212 current end of file. After the write, the offset has data_len
213 added to the old value. For a+ mode however, the initial file
214 position is the file position of the underlying file descriptor,
215 since it is initially assumed to be in read mode. */
216 {"a", O_WRONLY, data_len, 2 * data_len},
217 {"a+", O_RDWR, 0, 3 * data_len},
219 for (int j = 0; j < 2; j++)
221 for (int i = 0; i < sizeof (test_modes) / sizeof (struct test); i++)
227 printf ("\tftell: %s (file, \"%s\"): ", j == 0 ? "fdopen" : "fopen",
231 fileret = get_handles_fdopen (filename, fd, fp,
232 test_modes[i].fd_mode,
235 fileret = get_handles_fopen (filename, fd, fp, test_modes[i].mode);
240 long off = ftell (fp);
241 if (off != test_modes[i].old_off)
243 printf ("Incorrect old offset. Expected %zu but got %ld, ",
244 test_modes[i].old_off, off);
248 printf ("old offset = %ld, ", off);
250 /* The effect of this write on the offset should be seen in the ftell
251 call that follows it. */
252 int write_ret = write (fd, data, data_len);
253 if (write_ret != data_len)
255 printf ("write failed (%m)\n");
260 if (off != test_modes[i].new_off)
262 printf ("Incorrect new offset. Expected %zu but got %ld\n",
263 test_modes[i].new_off, off);
267 printf ("new offset = %ld\n", off);
276 /* This test opens the file for writing, moves the file offset of the
277 underlying file, writes out data and then checks if ftell trips on it. */
279 do_write_test (const char *filename)
294 for (int j = 0; j < 2; j++)
296 for (int i = 0; i < sizeof (test_modes) / sizeof (struct test); i++)
299 printf ("\twrite: %s (file, \"%s\"): ", j == 0 ? "fopen" : "fdopen",
303 fileret = get_handles_fopen (filename, fd, fp, test_modes[i].mode);
305 fileret = get_handles_fdopen (filename, fd, fp,
306 test_modes[i].fd_mode,
312 /* Move offset to just before the end of the file. */
313 off_t seek_ret = lseek (fd, file_len - 1, SEEK_SET);
316 printf ("lseek failed: %m\n");
320 /* Write some data. */
321 size_t written = fputs_func (data, fp);
325 printf ("fputs[1] failed to write data\n");
329 /* Verify that the offset points to the end of the file. The length
330 of the file would be the original length + the length of data
331 written to it - the amount by which we moved the offset using
333 long offset = ftell (fp);
334 file_len = file_len - 1 + data_len;
336 if (offset != file_len)
338 printf ("Incorrect offset. Expected %zu, but got %ld\n",
344 printf ("offset = %ld\n", offset);
352 /* This test opens a file in append mode, writes some data, and then verifies
353 that ftell does not trip over it. */
355 do_append_test (const char *filename)
370 for (int j = 0; j < 2; j++)
372 for (int i = 0; i < sizeof (test_modes) / sizeof (struct test); i++)
376 printf ("\tappend: %s (file, \"%s\"): ", j == 0 ? "fopen" : "fdopen",
380 fileret = get_handles_fopen (filename, fd, fp, test_modes[i].mode);
382 fileret = get_handles_fdopen (filename, fd, fp,
383 test_modes[i].fd_mode,
389 /* Write some data. */
390 size_t written = fputs_func (data, fp);
394 printf ("fputs[1] failed to write all data\n");
398 /* Verify that the offset points to the end of the file. The file
399 len is maintained by adding data_len each time to reflect the data
401 long offset = ftell (fp);
402 file_len += data_len;
404 if (offset != file_len)
406 printf ("Incorrect offset. Expected %zu, but got %ld\n",
412 printf ("offset = %ld\n", offset);
417 /* For fdopen in 'a' mode, the file descriptor should not change if the file
418 is already open with the O_APPEND flag set. */
419 fd = open (filename, O_WRONLY | O_APPEND, 0);
422 printf ("open(O_APPEND) failed: %m\n");
426 off_t seek_ret = lseek (fd, file_len - 1, SEEK_SET);
429 printf ("lseek[O_APPEND][0] failed: %m\n");
433 fp = fdopen (fd, "a");
436 printf ("fdopen(O_APPEND) failed: %m\n");
441 off_t new_seek_ret = lseek (fd, 0, SEEK_CUR);
444 printf ("lseek[O_APPEND][1] failed: %m\n");
448 printf ("\tappend: fdopen (file, \"a\"): O_APPEND: ");
450 if (seek_ret != new_seek_ret)
452 printf ("incorrectly modified file offset to %ld, should be %ld",
453 new_seek_ret, seek_ret);
457 printf ("retained current file offset %ld", seek_ret);
459 new_seek_ret = ftello (fp);
461 if (seek_ret != new_seek_ret)
463 printf (", ftello reported incorrect offset %ld, should be %ld\n",
464 new_seek_ret, seek_ret);
468 printf (", ftello reported correct offset %ld\n", seek_ret);
476 do_one_test (const char *filename)
480 ret |= do_ftell_test (filename);
481 ret |= do_write_test (filename);
482 ret |= do_append_test (filename);
483 ret |= do_rewind_test (filename);
488 /* Run a set of tests for ftell for regular files and wide mode files. */
496 int fd = create_temp_file ("tst-active-handler-tmp.", &filename);
500 printf ("create_temp_file: %m\n");
504 fp = fdopen (fd, "w");
507 printf ("fdopen[0]: %m\n");
513 data_len = strlen (char_data);
514 file_len = strlen (char_data);
515 written = fputs (data, fp);
519 printf ("fputs[1] failed to write data\n");
527 /* Tests for regular files. */
528 puts ("Regular mode:");
529 fputs_func = (fputs_func_t) fputs;
531 data_len = strlen (char_data);
532 ret |= do_one_test (filename);
534 /* Truncate the file before repeating the tests in wide mode. */
535 fp = fopen (filename, "w");
538 printf ("fopen failed %m\n");
543 /* Tests for wide files. */
545 if (setlocale (LC_ALL, "en_US.UTF-8") == NULL)
547 printf ("Cannot set en_US.UTF-8 locale.\n");
550 fputs_func = (fputs_func_t) fputws;
552 data_len = wcslen (wide_data);
553 ret |= do_one_test (filename);