(root)/
glibc-2.38/
libio/
tst-ungetwc1.c
       1  /* Taken from the Li18nux base test suite.  */
       2  
       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>
       9  
      10  static int
      11  do_test (void)
      12  {
      13    FILE *fp;
      14    const char *str = "abcdef";
      15    wint_t ret, wc, ungetone = 0x00E4;	/* 0x00E4 means `a umlaut'. */
      16    char fname[] = "/tmp/tst-ungetwc1.out.XXXXXX";
      17    int fd;
      18    int result = 0;
      19  
      20    puts ("This program runs on de_DE.UTF-8 locale.");
      21    if (setlocale (LC_ALL, "de_DE.UTF-8") == NULL)
      22      {
      23        fprintf (stderr, "Err: Cannot run on the de_DE.UTF-8 locale");
      24        exit (EXIT_FAILURE);
      25      }
      26  
      27    fd = mkstemp (fname);
      28    if (fd == -1)
      29      {
      30        printf ("cannot open temp file: %m\n");
      31        exit (EXIT_FAILURE);
      32      }
      33  
      34    /* Write some characters to `testfile'. */
      35    if ((fp = fdopen (fd, "w")) == NULL)
      36      {
      37        fprintf (stderr, "Cannot open 'testfile'.");
      38        exit (EXIT_FAILURE);
      39      }
      40    fputs (str, fp);
      41    fclose (fp);
      42  
      43    /* Open `testfile'. */
      44    if ((fp = fopen (fname, "r")) == NULL)
      45      {
      46        fprintf (stderr, "Cannot open 'testfile'.");
      47        exit (EXIT_FAILURE);
      48      }
      49  
      50    /* Unget a character. */
      51    ret = ungetwc (ungetone, fp);
      52    printf ("Unget a character (0x%04x)\n", (unsigned int) ungetone);
      53    fflush (stdout);
      54    if (ret == WEOF)
      55      {
      56        puts ("ungetwc() returns NULL.");
      57        exit (EXIT_SUCCESS);
      58      }
      59  
      60    /* Reget a character. */
      61    wc = getwc (fp);
      62    printf ("Reget a character (0x%04x)\n", (unsigned int) wc);
      63    fflush (stdout);
      64    if (wc == ungetone)
      65      {
      66        puts ("The ungotten character is equal to the regotten character.");
      67        fflush (stdout);
      68      }
      69    else
      70      {
      71        puts ("The ungotten character is not equal to the regotten character.");
      72        printf ("ungotten one: %04x, regetone: %04x", ungetone, wc);
      73        fflush (stdout);
      74        result = 1;
      75      }
      76    fclose (fp);
      77  
      78    unlink (fname);
      79  
      80    return result;
      81  }
      82  
      83  #define TEST_FUNCTION do_test ()
      84  #include "../test-skeleton.c"