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 the value of ftell is not cached when the stream handle is not
94 do_ftell_test (const char *filename
)
104 /* In w, w+ and r+ modes, the file position should be at the
105 beginning of the file. After the write, the offset should be
106 updated to data_len. */
107 {"w", O_WRONLY
, 0, data_len
},
108 {"w+", O_RDWR
, 0, data_len
},
109 {"r+", O_RDWR
, 0, data_len
},
110 /* For 'a' and 'a+' modes, the initial file position should be the
111 current end of file. After the write, the offset has data_len
112 added to the old value. */
113 {"a", O_WRONLY
, data_len
, 2 * data_len
},
114 {"a+", O_RDWR
, 2 * data_len
, 3 * data_len
},
116 for (int j
= 0; j
< 2; j
++)
118 for (int i
= 0; i
< sizeof (test_modes
) / sizeof (struct test
); i
++)
122 printf ("\tftell: %s (file, \"%s\"): ", j
== 0 ? "fdopen" : "fopen",
126 ret
= get_handles_fdopen (filename
, fd
, fp
, test_modes
[i
].fd_mode
,
129 ret
= get_handles_fopen (filename
, fd
, fp
, test_modes
[i
].mode
);
134 long off
= ftell (fp
);
135 if (off
!= test_modes
[i
].old_off
)
137 printf ("Incorrect old offset. Expected %zu but got %ld, ",
138 test_modes
[i
].old_off
, off
);
142 printf ("old offset = %ld, ", off
);
144 /* The effect of this write on the offset should be seen in the ftell
145 call that follows it. */
146 int ret
= write (fd
, data
, data_len
);
149 if (off
!= test_modes
[i
].new_off
)
151 printf ("Incorrect new offset. Expected %zu but got %ld\n",
152 test_modes
[i
].old_off
, off
);
156 printf ("new offset = %ld\n", off
);
165 /* This test opens the file for writing, moves the file offset of the
166 underlying file, writes out data and then checks if ftell trips on it. */
168 do_write_test (const char *filename
)
183 for (int j
= 0; j
< 2; j
++)
185 for (int i
= 0; i
< sizeof (test_modes
) / sizeof (struct test
); i
++)
187 printf ("\twrite: %s (file, \"%s\"): ", j
== 0 ? "fopen" : "fdopen",
191 ret
= get_handles_fopen (filename
, fd
, fp
, test_modes
[i
].mode
);
193 ret
= get_handles_fdopen (filename
, fd
, fp
, test_modes
[i
].fd_mode
,
199 /* Move offset to just before the end of the file. */
200 off_t ret
= lseek (fd
, file_len
- 1, SEEK_SET
);
203 printf ("lseek failed: %m\n");
207 /* Write some data. */
208 size_t written
= fputs_func (data
, fp
);
212 printf ("fputs[1] failed to write data\n");
216 /* Verify that the offset points to the end of the file. The length
217 of the file would be the original length + the length of data
218 written to it - the amount by which we moved the offset using
220 long offset
= ftell (fp
);
221 file_len
= file_len
- 1 + data_len
;
223 if (offset
!= file_len
)
225 printf ("Incorrect offset. Expected %zu, but got %ld\n",
231 printf ("offset = %ld\n", offset
);
239 /* This test opens a file in append mode, writes some data, and then verifies
240 that ftell does not trip over it. */
242 do_append_test (const char *filename
)
257 for (int j
= 0; j
< 2; j
++)
259 for (int i
= 0; i
< sizeof (test_modes
) / sizeof (struct test
); i
++)
261 printf ("\tappend: %s (file, \"%s\"): ", j
== 0 ? "fopen" : "fdopen",
265 ret
= get_handles_fopen (filename
, fd
, fp
, test_modes
[i
].mode
);
267 ret
= get_handles_fdopen (filename
, fd
, fp
, test_modes
[i
].fd_mode
,
273 /* Write some data. */
274 size_t written
= fputs_func (data
, fp
);
278 printf ("fputs[1] failed to write all data\n");
282 /* Verify that the offset points to the end of the file. The file
283 len is maintained by adding data_len each time to reflect the data
285 long offset
= ftell (fp
);
286 file_len
+= data_len
;
288 if (offset
!= file_len
)
290 printf ("Incorrect offset. Expected %zu, but got %ld\n",
296 printf ("offset = %ld\n", offset
);
305 do_one_test (const char *filename
)
309 ret
|= do_ftell_test (filename
);
310 ret
|= do_write_test (filename
);
311 ret
|= do_append_test (filename
);
316 /* Run a set of tests for ftell for regular files and wide mode files. */
324 int fd
= create_temp_file ("tst-active-handler-tmp.", &filename
);
328 printf ("create_temp_file: %m\n");
332 fp
= fdopen (fd
, "w");
335 printf ("fdopen[0]: %m\n");
341 data_len
= strlen (char_data
);
342 file_len
= strlen (char_data
);
343 written
= fputs (data
, fp
);
347 printf ("fputs[1] failed to write data\n");
355 /* Tests for regular files. */
356 puts ("Regular mode:");
357 fputs_func
= (fputs_func_t
) fputs
;
359 data_len
= strlen (char_data
);
360 ret
|= do_one_test (filename
);
362 /* Truncate the file before repeating the tests in wide mode. */
363 fp
= fopen (filename
, "w");
366 printf ("fopen failed %m\n");
371 /* Tests for wide files. */
373 if (setlocale (LC_ALL
, "en_US.UTF-8") == NULL
)
375 printf ("Cannot set en_US.UTF-8 locale.\n");
378 fputs_func
= (fputs_func_t
) fputws
;
380 data_len
= wcslen (wide_data
);
381 ret
|= do_one_test (filename
);