[BZ #6771]
[glibc.git] / libio / tst-ungetwc2.c
blobd15e287c05451ea290d48a1c76904dda092b2e89
1 /* Taken from the Li18nux base test suite. */
3 #define _XOPEN_SOURCE 500
4 #include <locale.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <wchar.h>
10 int
11 main (void)
13 FILE *fp;
14 const char *str = "abcdef";
15 wint_t ret, wc;
16 char fname[] = "/tmp/tst-ungetwc2.out.XXXXXX";
17 int fd;
18 long int pos;
19 int result = 0;
21 puts ("This program runs on de_DE.UTF-8 locale.");
22 if (setlocale (LC_ALL, "de_DE.UTF-8") == NULL)
24 fprintf (stderr, "Err: Cannot run on the de_DE.UTF-8 locale\n");
25 exit (EXIT_FAILURE);
28 /* Write some characters to `testfile'. */
29 fd = mkstemp (fname);
30 if (fd == -1)
32 printf ("cannot open temp file: %m\n");
33 exit (EXIT_FAILURE);
35 if ((fp = fdopen (fd, "w")) == NULL)
37 fprintf (stderr, "Cannot open 'testfile'.\n");
38 exit (EXIT_FAILURE);
40 fputs (str, fp);
41 fclose (fp);
43 /* Open `testfile'. */
44 if ((fp = fopen (fname, "r")) == NULL)
46 fprintf (stderr, "Cannot open 'testfile'.");
47 exit (EXIT_FAILURE);
50 /* Get a character. */
51 wc = getwc (fp);
52 pos = ftell (fp);
53 printf ("After get a character: %ld\n", pos);
54 if (pos != 1)
55 result = 1;
57 /* Unget a character. */
58 ret = ungetwc (wc, fp);
59 if (ret == WEOF)
61 fprintf (stderr, "ungetwc() returns NULL.");
62 exit (EXIT_FAILURE);
64 pos = ftell (fp);
65 printf ("After unget a character: %ld\n", pos);
66 if (pos != 0)
67 result = 1;
69 /* Reget a character. */
70 wc = getwc (fp);
71 pos = ftell (fp);
72 printf ("After reget a character: %ld\n", pos);
73 if (pos != 1)
74 result = 1;
76 fclose (fp);
78 unlink (fname);
80 return result;