about summary refs log tree commit diff
path: root/stdio-common/tst-perror.c
blob: b4ab58346229e037e0b896f8d278d9f8bc6fbd8f (plain) (blame)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* Test of perror.
   To be used only for testing glibc.  */

#include <errno.h>
#include <error.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wchar.h>

#include <support/xunistd.h>

#define MB_EXP \
  "null mode test 1: Invalid or incomplete multibyte or wide character\n" \
  "multibyte string\n" \
  "<0 mode test: Invalid argument\n"
#define MB_EXP_LEN (sizeof (MB_EXP) - 1)

#define WC_EXP \
  "null mode test 2: Invalid or incomplete multibyte or wide character\n" \
  "wide string\n" \
  ">0 mode test: Invalid argument\n"
#define WC_EXP_LEN (sizeof (WC_EXP) - 1)


static int
do_test (void)
{
  int fd;
  char fname[] = "/tmp/tst-perror.XXXXXX";
  int result = 0;
  char buf[200];
  ssize_t n;

  fd = mkstemp (fname);
  if (fd == -1)
    error (EXIT_FAILURE, errno, "cannot create temporary file");

  /* Make sure the file gets removed.  */
  unlink (fname);

  fclose (stderr);

  if (dup2 (fd, 2) == -1)
    {
      printf ("cannot create file descriptor 2: %m\n");
      exit (EXIT_FAILURE);
    }

  stderr = fdopen (2, "w");
  if (stderr == NULL)
    {
      printf ("fdopen failed: %m\n");
      exit (EXIT_FAILURE);
    }

  if (fwide (stderr, 0) != 0)
    {
      printf ("stderr not initially in mode 0\n");
      exit (EXIT_FAILURE);
    }

  errno = EILSEQ;
  perror ("null mode test 1");

  if (fwide (stderr, 0) != 0)
    {
      puts ("perror changed the mode from 0");
      result = 1;
    }

  fputs ("multibyte string\n", stderr);

  if (fwide (stderr, 0) >= 0)
    {
      puts ("fputs didn't set orientation to narrow");
      result = 1;
    }

  errno = EINVAL;
  perror ("<0 mode test");

  fclose (stderr);

  lseek (fd, 0, SEEK_SET);
  n = read (fd, buf, sizeof (buf));
  if (n != MB_EXP_LEN || memcmp (buf, MB_EXP, MB_EXP_LEN) != 0)
    {
      printf ("multibyte test failed.  Expected:\n%s\nGot:\n%.*s\n",
	      MB_EXP, (int) n, buf);
      result = 1;
    }
  else
    puts ("multibyte test succeeded");

  lseek (fd, 0, SEEK_SET);
  xftruncate (fd, 0);

  if (dup2 (fd, 2) == -1)
    {
      printf ("cannot create file descriptor 2: %m\n");
      exit (EXIT_FAILURE);
    }
  stderr = fdopen (2, "w");
  if (stderr == NULL)
    {
      printf ("fdopen failed: %m\n");
      exit (EXIT_FAILURE);
    }

  if (fwide (stderr, 0) != 0)
    {
      printf ("stderr not initially in mode 0\n");
      exit (EXIT_FAILURE);
    }

  errno = EILSEQ;
  perror ("null mode test 2");

  if (fwide (stderr, 0) != 0)
    {
      puts ("perror changed the mode from 0");
      result = 1;
    }

  fputws (L"wide string\n", stderr);

  if (fwide (stderr, 0) <= 0)
    {
      puts ("fputws didn't set orientation to wide");
      result = 1;
    }

  errno = EINVAL;
  perror (">0 mode test");

  fclose (stderr);

  lseek (fd, 0, SEEK_SET);
  n = read (fd, buf, sizeof (buf));
  if (n != WC_EXP_LEN || memcmp (buf, WC_EXP, WC_EXP_LEN) != 0)
    {
      printf ("wide test failed.  Expected:\n%s\nGot:\n%.*s\n",
	      WC_EXP, (int) n, buf);
      result = 1;
    }
  else
    puts ("wide test succeeded");

  close (fd);

  return result;
}

#define TEST_FUNCTION do_test ()
#include "../test-skeleton.c"