error: Avoid "function declaration isn't a prototype" warning.
[gnulib.git] / tests / test-perror2.c
blob125961e9988b9708e97bcb8de5c44759c2c192dd
1 /* Test of perror() function.
2 Copyright (C) 2011-2017 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <http://www.gnu.org/licenses/>. */
17 #include <config.h>
19 #include <stdio.h>
21 #include <errno.h>
22 #include <string.h>
23 #include <unistd.h>
25 /* This test intentionally parses stderr. So, we arrange to have fd 10
26 (outside the range of interesting fd's during the test) set up to
27 duplicate the original stderr. */
28 #define BACKUP_STDERR_FILENO 10
29 #define ASSERT_STREAM myerr
30 #include "macros.h"
32 static FILE *myerr;
34 #define BASE "test-perror2"
36 int
37 main (void)
39 /* We change fd 2 later, so save it in fd 10. */
40 if (dup2 (STDERR_FILENO, BACKUP_STDERR_FILENO) != BACKUP_STDERR_FILENO
41 || (myerr = fdopen (BACKUP_STDERR_FILENO, "w")) == NULL)
42 return 2;
44 ASSERT (freopen (BASE ".tmp", "w+", stderr) == stderr);
46 /* Test that perror does not clobber strerror buffer. */
48 const char *msg1;
49 const char *msg2;
50 const char *msg3;
51 const char *msg4;
52 char *str1;
53 char *str2;
54 char *str3;
55 char *str4;
57 msg1 = strerror (ENOENT);
58 ASSERT (msg1);
59 str1 = strdup (msg1);
60 ASSERT (str1);
62 msg2 = strerror (ERANGE);
63 ASSERT (msg2);
64 str2 = strdup (msg2);
65 ASSERT (str2);
67 msg3 = strerror (-4);
68 ASSERT (msg3);
69 str3 = strdup (msg3);
70 ASSERT (str3);
72 msg4 = strerror (1729576);
73 ASSERT (msg4);
74 str4 = strdup (msg4);
75 ASSERT (str4);
77 errno = EACCES;
78 perror ("");
79 errno = -5;
80 perror ("");
81 ASSERT (!ferror (stderr));
82 ASSERT (msg1 == msg2 || msg1 == msg4 || STREQ (msg1, str1));
83 ASSERT (msg2 == msg4 || STREQ (msg2, str2));
84 ASSERT (msg3 == msg4 || STREQ (msg3, str3));
85 ASSERT (STREQ (msg4, str4));
87 free (str1);
88 free (str2);
89 free (str3);
90 free (str4);
93 /* Test that perror uses the same message as strerror. */
95 int errs[] = { EACCES, 0, -3, };
96 int i;
97 for (i = 0; i < SIZEOF (errs); i++)
99 char buf[256];
100 char *err = strerror (errs[i]);
102 ASSERT (err);
103 ASSERT (strlen (err) < sizeof buf);
104 rewind (stderr);
105 ASSERT (ftruncate (fileno (stderr), 0) == 0);
106 errno = errs[i];
107 perror (NULL);
108 ASSERT (!ferror (stderr));
109 rewind (stderr);
110 ASSERT (fgets (buf, sizeof buf, stderr) == buf);
111 ASSERT (strstr (buf, err));
115 /* Test that perror reports write failure. */
117 ASSERT (freopen (BASE ".tmp", "r", stderr) == stderr);
118 ASSERT (setvbuf (stderr, NULL, _IONBF, BUFSIZ) == 0);
119 errno = -1;
120 ASSERT (!ferror (stderr));
121 perror (NULL);
122 #if 0
123 /* Commented out until cygwin behaves:
124 http://sourceware.org/ml/newlib/2011/msg00228.html */
125 ASSERT (errno > 0);
126 /* Commented out until glibc behaves:
127 http://sourceware.org/bugzilla/show_bug.cgi?id=12792 */
128 ASSERT (ferror (stderr));
129 #endif
132 ASSERT (fclose (stderr) == 0);
133 ASSERT (remove (BASE ".tmp") == 0);
135 return 0;