re_search_internal: Avoid overflow in computing re_malloc buffer size
[glibc.git] / libio / bug-ungetwc2.c
blobb901fc01e3e39b854e8f9d5075d6c671e1dc23b2
1 #define _XOPEN_SOURCE 500
2 #include <errno.h>
3 #include <error.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <locale.h>
7 #include <wchar.h>
9 const char test_locale[] = "de_DE.UTF-8";
10 const wchar_t write_wchars[] = {L'A', 0x00C4, L'B', L'\0'};
11 /* `0x00C4' is A with diaeresis. */
12 size_t last_pos = 2; /* Which character is last one to read. */
13 wint_t unget_wchar = L'C'; /* Ungotten ide characters. */
15 char *fname;
18 static int do_test (void);
19 #define TEST_FUNCTION do_test ()
21 #include "../test-skeleton.c"
24 static int
25 do_test (void)
27 size_t i;
28 wint_t wc;
29 FILE *fp;
30 int fd;
32 fname = (char *) malloc (strlen (test_dir) + sizeof "/bug-ungetwc2.XXXXXX");
33 if (fname == NULL)
35 puts ("no memory");
36 return 1;
38 strcpy (stpcpy (fname, test_dir), "/bug-ungetwc2.XXXXXX");
39 fd = mkstemp (fname);
40 if (fd == -1)
42 printf ("cannot open temporary file: %m\n");
43 return 1;
45 add_temp_file (fname);
47 printf ("\nNote: This program runs on %s locale.\n\n", test_locale);
49 if (setlocale (LC_ALL, test_locale) == NULL)
51 fprintf (stderr, "Cannot use `%s' locale.\n", test_locale);
52 exit (EXIT_FAILURE);
55 /* Output to the file. */
56 if ((fp = fdopen (fd, "w")) == NULL)
58 setlocale (LC_ALL, "C");
59 fprintf (stderr, "Cannot make `%s' file.\n", fname);
60 exit (EXIT_FAILURE);
62 fprintf (fp, "%ls", write_wchars);
63 fclose (fp);
65 /* Read from the file. */
66 fp = fopen (fname, "r");
67 if (fp == NULL)
69 setlocale (LC_ALL, "C");
70 error (EXIT_FAILURE, errno, "cannot open %s", fname);
73 printf ("%s is opened.\n", fname);
75 for (i = 0; i < last_pos; i++)
77 wc = getwc (fp);
78 printf ("> `%lc' is gotten.\n", wc);
81 /* Unget a wide character. */
82 ungetwc (unget_wchar, fp);
83 printf ("< `%lc' is ungotten.\n", unget_wchar);
85 /* Reget the wide character. */
86 wc = getwc (fp);
87 printf ("> `%lc' is regotten.\n", wc);
89 fflush (stdout);
90 fclose (fp);
92 return 0;