Restore ChangeLog
[glibc.git] / stdio-common / tst-perror.c
blobb809c2fc405a57410bcfc388fa9ae65562ec6450
1 /* Test of perror.
2 Contributed by Ulrich Drepper <drepper@redhat.com>, 2001.
3 To be used only for testing glibc. */
5 #include <errno.h>
6 #include <error.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <wchar.h>
14 #define MB_EXP \
15 "null mode test 1: Invalid or incomplete multibyte or wide character\n" \
16 "multibyte string\n" \
17 "<0 mode test: Invalid argument\n"
18 #define MB_EXP_LEN (sizeof (MB_EXP) - 1)
20 #define WC_EXP \
21 "null mode test 2: Invalid or incomplete multibyte or wide character\n" \
22 "wide string\n" \
23 ">0 mode test: Invalid argument\n"
24 #define WC_EXP_LEN (sizeof (WC_EXP) - 1)
27 int
28 main (void)
30 int fd;
31 char fname[] = "/tmp/tst-perror.XXXXXX";
32 int result = 0;
33 char buf[200];
34 ssize_t n;
36 fd = mkstemp (fname);
37 if (fd == -1)
38 error (EXIT_FAILURE, errno, "cannot create temporary file");
40 /* Make sure the file gets removed. */
41 unlink (fname);
43 fclose (stderr);
45 if (dup2 (fd, 2) == -1)
47 printf ("cannot create file descriptor 2: %m\n");
48 exit (EXIT_FAILURE);
51 stderr = fdopen (2, "w");
52 if (stderr == NULL)
54 printf ("fdopen failed: %m\n");
55 exit (EXIT_FAILURE);
58 if (fwide (stderr, 0) != 0)
60 printf ("stderr not initially in mode 0\n");
61 exit (EXIT_FAILURE);
64 errno = EILSEQ;
65 perror ("null mode test 1");
67 if (fwide (stderr, 0) != 0)
69 puts ("perror changed the mode from 0");
70 result = 1;
73 fputs ("multibyte string\n", stderr);
75 if (fwide (stderr, 0) >= 0)
77 puts ("fputs didn't set orientation to narrow");
78 result = 1;
81 errno = EINVAL;
82 perror ("<0 mode test");
84 fclose (stderr);
86 lseek (fd, 0, SEEK_SET);
87 n = read (fd, buf, sizeof (buf));
88 if (n != MB_EXP_LEN || memcmp (buf, MB_EXP, MB_EXP_LEN) != 0)
90 printf ("multibyte test failed. Expected:\n%s\nGot:\n%.*s\n",
91 MB_EXP, (int) n, buf);
92 result = 1;
94 else
95 puts ("multibyte test succeeded");
97 lseek (fd, 0, SEEK_SET);
98 ftruncate (fd, 0);
100 if (dup2 (fd, 2) == -1)
102 printf ("cannot create file descriptor 2: %m\n");
103 exit (EXIT_FAILURE);
105 stderr = fdopen (2, "w");
106 if (stderr == NULL)
108 printf ("fdopen failed: %m\n");
109 exit (EXIT_FAILURE);
112 if (fwide (stderr, 0) != 0)
114 printf ("stderr not initially in mode 0\n");
115 exit (EXIT_FAILURE);
118 errno = EILSEQ;
119 perror ("null mode test 2");
121 if (fwide (stderr, 0) != 0)
123 puts ("perror changed the mode from 0");
124 result = 1;
127 fputws (L"wide string\n", stderr);
129 if (fwide (stderr, 0) <= 0)
131 puts ("fputws didn't set orientation to wide");
132 result = 1;
135 errno = EINVAL;
136 perror (">0 mode test");
138 fclose (stderr);
140 lseek (fd, 0, SEEK_SET);
141 n = read (fd, buf, sizeof (buf));
142 if (n != WC_EXP_LEN || memcmp (buf, WC_EXP, WC_EXP_LEN) != 0)
144 printf ("wide test failed. Expected:\n%s\nGot:\n%.*s\n",
145 WC_EXP, (int) n, buf);
146 result = 1;
148 else
149 puts ("wide test succeeded");
151 close (fd);
153 return result;