Blame libio/tst-ungetwc1.c

Packit 6c4009
/* Taken from the Li18nux base test suite.  */
Packit 6c4009
Packit 6c4009
#define _XOPEN_SOURCE 500
Packit 6c4009
#include <locale.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <wchar.h>
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  FILE *fp;
Packit 6c4009
  const char *str = "abcdef";
Packit 6c4009
  wint_t ret, wc, ungetone = 0x00E4;	/* 0x00E4 means `a umlaut'. */
Packit 6c4009
  char fname[] = "/tmp/tst-ungetwc1.out.XXXXXX";
Packit 6c4009
  int fd;
Packit 6c4009
  int result = 0;
Packit 6c4009
Packit 6c4009
  puts ("This program runs on de_DE.UTF-8 locale.");
Packit 6c4009
  if (setlocale (LC_ALL, "de_DE.UTF-8") == NULL)
Packit 6c4009
    {
Packit 6c4009
      fprintf (stderr, "Err: Cannot run on the de_DE.UTF-8 locale");
Packit 6c4009
      exit (EXIT_FAILURE);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  fd = mkstemp (fname);
Packit 6c4009
  if (fd == -1)
Packit 6c4009
    {
Packit 6c4009
      printf ("cannot open temp file: %m\n");
Packit 6c4009
      exit (EXIT_FAILURE);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Write some characters to `testfile'. */
Packit 6c4009
  if ((fp = fdopen (fd, "w")) == NULL)
Packit 6c4009
    {
Packit 6c4009
      fprintf (stderr, "Cannot open 'testfile'.");
Packit 6c4009
      exit (EXIT_FAILURE);
Packit 6c4009
    }
Packit 6c4009
  fputs (str, fp);
Packit 6c4009
  fclose (fp);
Packit 6c4009
Packit 6c4009
  /* Open `testfile'. */
Packit 6c4009
  if ((fp = fopen (fname, "r")) == NULL)
Packit 6c4009
    {
Packit 6c4009
      fprintf (stderr, "Cannot open 'testfile'.");
Packit 6c4009
      exit (EXIT_FAILURE);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Unget a character. */
Packit 6c4009
  ret = ungetwc (ungetone, fp);
Packit 6c4009
  printf ("Unget a character (0x%04x)\n", (unsigned int) ungetone);
Packit 6c4009
  fflush (stdout);
Packit 6c4009
  if (ret == WEOF)
Packit 6c4009
    {
Packit 6c4009
      puts ("ungetwc() returns NULL.");
Packit 6c4009
      exit (EXIT_SUCCESS);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Reget a character. */
Packit 6c4009
  wc = getwc (fp);
Packit 6c4009
  printf ("Reget a character (0x%04x)\n", (unsigned int) wc);
Packit 6c4009
  fflush (stdout);
Packit 6c4009
  if (wc == ungetone)
Packit 6c4009
    {
Packit 6c4009
      puts ("The ungotten character is equal to the regotten character.");
Packit 6c4009
      fflush (stdout);
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      puts ("The ungotten character is not equal to the regotten character.");
Packit 6c4009
      printf ("ungotten one: %04x, regetone: %04x", ungetone, wc);
Packit 6c4009
      fflush (stdout);
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  fclose (fp);
Packit 6c4009
Packit 6c4009
  unlink (fname);
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"