1 /* Verify that ftell returns the correct value after a read and a write on a
2 file opened in a+ mode.
3 Copyright (C) 2014-2019 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/>. */
28 /* data points to either char_data or wide_data, depending on whether we're
29 testing regular file mode or wide mode respectively. Similarly,
30 fputs_func points to either fputs or fputws. data_len keeps track of the
31 length of the current data and file_len maintains the current file
35 static char char_buf
[BUF_LEN
];
36 static wchar_t wide_buf
[BUF_LEN
];
37 static const void *data
;
38 static const char *char_data
= "abcdefghijklmnopqrstuvwxyz";
39 static const wchar_t *wide_data
= L
"abcdefghijklmnopqrstuvwxyz";
40 static size_t data_len
;
41 static size_t file_len
;
43 typedef int (*fputs_func_t
) (const void *data
, FILE *fp
);
44 fputs_func_t fputs_func
;
46 typedef void *(*fgets_func_t
) (void *s
, int size
, FILE *stream
);
47 fgets_func_t fgets_func
;
49 static int do_test (void);
51 #define TEST_FUNCTION do_test ()
52 #include "../test-skeleton.c"
55 init_file (const char *filename
)
57 FILE *fp
= fopen (filename
, "w");
60 printf ("fopen: %m\n");
64 int written
= fputs_func (data
, fp
);
68 printf ("fputs failed to write data\n");
77 fp
= fopen (filename
, "a+");
80 printf ("fopen(a+): %m\n");
88 do_one_test (const char *filename
)
90 FILE *fp
= init_file (filename
);
95 void *ret
= fgets_func (buf
, BUF_LEN
, fp
);
99 printf ("read failed: %m\n");
104 int written
= fputs_func (data
, fp
);
108 printf ("fputs failed to write data\n");
113 file_len
+= data_len
;
115 long off
= ftell (fp
);
119 printf ("Incorrect offset %ld, expected %zu\n", off
, file_len
);
124 printf ("Correct offset %ld after write.\n", off
);
129 /* Run the tests for regular files and wide mode files. */
135 int fd
= create_temp_file ("tst-ftell-append-tmp.", &filename
);
139 printf ("create_temp_file: %m\n");
145 /* Tests for regular files. */
146 puts ("Regular mode:");
147 fputs_func
= (fputs_func_t
) fputs
;
148 fgets_func
= (fgets_func_t
) fgets
;
151 data_len
= strlen (char_data
);
152 ret
|= do_one_test (filename
);
154 /* Tests for wide files. */
156 if (setlocale (LC_ALL
, "en_US.UTF-8") == NULL
)
158 printf ("Cannot set en_US.UTF-8 locale.\n");
161 fputs_func
= (fputs_func_t
) fputws
;
162 fgets_func
= (fgets_func_t
) fgetws
;
165 data_len
= wcslen (wide_data
);
166 ret
|= do_one_test (filename
);