aboutsummaryrefslogtreecommitdiff
blob: f74c4078933da83245ded89d9cdb8910805b3cad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* Taken from the Li18nux base test suite.  */

#define _XOPEN_SOURCE 500
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <wchar.h>

int
main (void)
{
  FILE *fp;
  const char *str = "abcdef";
  wint_t ret, wc, ungetone = 0x00E4;	/* 0x00E4 means `a umlaut'. */
  char fname[] = "/tmp/tst-ungetwc1.out.XXXXXX";
  int fd;
  int result = 0;

  puts ("This program runs on de_DE.UTF-8 locale.");
  if (setlocale (LC_ALL, "de_DE.UTF-8") == NULL)
    {
      fprintf (stderr, "Err: Cannot run on the de_DE.UTF-8 locale");
      exit (EXIT_FAILURE);
    }

  fd = mkstemp (fname);
  if (fd == -1)
    {
      printf ("cannot open temp file: %m\n");
      exit (EXIT_FAILURE);
    }

  /* Write some characters to `testfile'. */
  if ((fp = fdopen (fd, "w")) == NULL)
    {
      fprintf (stderr, "Cannot open 'testfile'.");
      exit (EXIT_FAILURE);
    }
  fputs (str, fp);
  fclose (fp);

  /* Open `testfile'. */
  if ((fp = fopen (fname, "r")) == NULL)
    {
      fprintf (stderr, "Cannot open 'testfile'.");
      exit (EXIT_FAILURE);
    }

  /* Unget a character. */
  ret = ungetwc (ungetone, fp);
  printf ("Unget a character (0x%04x)\n", (unsigned int) ungetone);
  fflush (stdout);
  if (ret == WEOF)
    {
      puts ("ungetwc() returns NULL.");
      exit (EXIT_SUCCESS);
    }

  /* Reget a character. */
  wc = getwc (fp);
  printf ("Reget a character (0x%04x)\n", (unsigned int) wc);
  fflush (stdout);
  if (wc == ungetone)
    {
      puts ("The ungotten character is equal to the regotten character.");
      fflush (stdout);
    }
  else
    {
      puts ("The ungotten character is not equal to the regotten character.");
      printf ("ungotten one: %04x, regetone: %04x", ungetone, wc);
      fflush (stdout);
      result = 1;
    }
  fclose (fp);

  unlink (fname);

  return result;
}