Update copyright dates with scripts/update-copyrights.
[glibc.git] / libio / tst-ftell-partial-wide.c
blob1129f1bbfc4b513293ec6c35b1ba6356be569967
1 /* Verify that ftell does not go into an infinite loop when a conversion fails
2 due to insufficient space in the buffer.
3 Copyright (C) 2014-2015 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/>. */
20 #include <wchar.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <locale.h>
25 #include <errno.h>
26 #include <unistd.h>
28 static int do_test (void);
29 #define TEST_FUNCTION do_test ()
30 #include "../test-skeleton.c"
32 /* Arbitrary number large enough so that the target buffer during conversion is
33 not large enough. */
34 #define STRING_SIZE (1400)
35 #define NSTRINGS (2)
37 static int
38 do_test (void)
40 FILE *fp = NULL;
41 wchar_t *inputs[NSTRINGS] = {NULL};
42 int ret = 1;
44 if (setlocale (LC_ALL, "en_US.UTF-8") == NULL)
46 printf ("Cannot set en_US.UTF-8 locale.\n");
47 goto out;
51 /* Generate input from one character, chosen because it has an odd number of
52 bytes in UTF-8, making it easier to reproduce the problem:
54 NAME Hiragana letter GO
55 CHAR ご
56 UTF-8 E38194
57 UCS 3054
58 MARC-8 692434 */
59 wchar_t seed = L'ご';
60 for (int i = 0; i < NSTRINGS; i++)
62 inputs[i] = malloc (STRING_SIZE * sizeof (wchar_t));
63 if (inputs[i] == NULL)
65 printf ("Failed to allocate memory for inputs: %m\n");
66 goto out;
68 wmemset (inputs[i], seed, STRING_SIZE - 1);
69 inputs[i][STRING_SIZE - 1] = L'\0';
72 char *filename;
73 int fd = create_temp_file ("tst-fseek-wide-partial.out", &filename);
75 if (fd == -1)
77 printf ("create_temp_file: %m\n");
78 goto out;
81 fp = fdopen (fd, "w+");
82 if (fp == NULL)
84 printf ("fopen: %m\n");
85 close (fd);
86 goto out;
89 for (int i = 0; i < NSTRINGS; i++)
91 printf ("offset: %ld\n", ftell (fp));
92 if (fputws (inputs[i], fp) == -1)
94 perror ("fputws");
95 goto out;
98 ret = 0;
100 out:
101 if (fp != NULL)
102 fclose (fp);
103 for (int i = 0; i < NSTRINGS; i++)
104 free (inputs[i]);
106 return ret;