Blame stdio-common/tst-perror.c

Packit 6c4009
/* Test of perror.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@redhat.com>, 2001.
Packit 6c4009
   To be used only for testing glibc.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <error.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <wchar.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
#define MB_EXP \
Packit 6c4009
  "null mode test 1: Invalid or incomplete multibyte or wide character\n" \
Packit 6c4009
  "multibyte string\n" \
Packit 6c4009
  "<0 mode test: Invalid argument\n"
Packit 6c4009
#define MB_EXP_LEN (sizeof (MB_EXP) - 1)
Packit 6c4009
Packit 6c4009
#define WC_EXP \
Packit 6c4009
  "null mode test 2: Invalid or incomplete multibyte or wide character\n" \
Packit 6c4009
  "wide string\n" \
Packit 6c4009
  ">0 mode test: Invalid argument\n"
Packit 6c4009
#define WC_EXP_LEN (sizeof (WC_EXP) - 1)
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  int fd;
Packit 6c4009
  char fname[] = "/tmp/tst-perror.XXXXXX";
Packit 6c4009
  int result = 0;
Packit 6c4009
  char buf[200];
Packit 6c4009
  ssize_t n;
Packit 6c4009
Packit 6c4009
  fd = mkstemp (fname);
Packit 6c4009
  if (fd == -1)
Packit 6c4009
    error (EXIT_FAILURE, errno, "cannot create temporary file");
Packit 6c4009
Packit 6c4009
  /* Make sure the file gets removed.  */
Packit 6c4009
  unlink (fname);
Packit 6c4009
Packit 6c4009
  fclose (stderr);
Packit 6c4009
Packit 6c4009
  if (dup2 (fd, 2) == -1)
Packit 6c4009
    {
Packit 6c4009
      printf ("cannot create file descriptor 2: %m\n");
Packit 6c4009
      exit (EXIT_FAILURE);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  stderr = fdopen (2, "w");
Packit 6c4009
  if (stderr == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("fdopen failed: %m\n");
Packit 6c4009
      exit (EXIT_FAILURE);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (fwide (stderr, 0) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("stderr not initially in mode 0\n");
Packit 6c4009
      exit (EXIT_FAILURE);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  errno = EILSEQ;
Packit 6c4009
  perror ("null mode test 1");
Packit 6c4009
Packit 6c4009
  if (fwide (stderr, 0) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("perror changed the mode from 0");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  fputs ("multibyte string\n", stderr);
Packit 6c4009
Packit 6c4009
  if (fwide (stderr, 0) >= 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("fputs didn't set orientation to narrow");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  errno = EINVAL;
Packit 6c4009
  perror ("<0 mode test");
Packit 6c4009
Packit 6c4009
  fclose (stderr);
Packit 6c4009
Packit 6c4009
  lseek (fd, 0, SEEK_SET);
Packit 6c4009
  n = read (fd, buf, sizeof (buf));
Packit 6c4009
  if (n != MB_EXP_LEN || memcmp (buf, MB_EXP, MB_EXP_LEN) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("multibyte test failed.  Expected:\n%s\nGot:\n%.*s\n",
Packit 6c4009
	      MB_EXP, (int) n, buf);
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    puts ("multibyte test succeeded");
Packit 6c4009
Packit 6c4009
  lseek (fd, 0, SEEK_SET);
Packit 6c4009
  ftruncate (fd, 0);
Packit 6c4009
Packit 6c4009
  if (dup2 (fd, 2) == -1)
Packit 6c4009
    {
Packit 6c4009
      printf ("cannot create file descriptor 2: %m\n");
Packit 6c4009
      exit (EXIT_FAILURE);
Packit 6c4009
    }
Packit 6c4009
  stderr = fdopen (2, "w");
Packit 6c4009
  if (stderr == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("fdopen failed: %m\n");
Packit 6c4009
      exit (EXIT_FAILURE);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (fwide (stderr, 0) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("stderr not initially in mode 0\n");
Packit 6c4009
      exit (EXIT_FAILURE);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  errno = EILSEQ;
Packit 6c4009
  perror ("null mode test 2");
Packit 6c4009
Packit 6c4009
  if (fwide (stderr, 0) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("perror changed the mode from 0");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  fputws (L"wide string\n", stderr);
Packit 6c4009
Packit 6c4009
  if (fwide (stderr, 0) <= 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("fputws didn't set orientation to wide");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  errno = EINVAL;
Packit 6c4009
  perror (">0 mode test");
Packit 6c4009
Packit 6c4009
  fclose (stderr);
Packit 6c4009
Packit 6c4009
  lseek (fd, 0, SEEK_SET);
Packit 6c4009
  n = read (fd, buf, sizeof (buf));
Packit 6c4009
  if (n != WC_EXP_LEN || memcmp (buf, WC_EXP, WC_EXP_LEN) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("wide test failed.  Expected:\n%s\nGot:\n%.*s\n",
Packit 6c4009
	      WC_EXP, (int) n, buf);
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    puts ("wide test succeeded");
Packit 6c4009
Packit 6c4009
  close (fd);
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"