1 /* Test of perror() function.
2 Copyright (C) 2011-2018 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)
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 <https://www.gnu.org/licenses/>. */
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
34 #define BASE "test-perror2"
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
)
44 ASSERT (freopen (BASE
".tmp", "w+", stderr
) == stderr
);
46 /* Test that perror does not clobber strerror buffer. */
57 msg1
= strerror (ENOENT
);
62 msg2
= strerror (ERANGE
);
72 msg4
= strerror (1729576);
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
));
93 /* Test that perror uses the same message as strerror. */
95 int errs
[] = { EACCES
, 0, -3, };
97 for (i
= 0; i
< SIZEOF (errs
); i
++)
100 char *err
= strerror (errs
[i
]);
103 ASSERT (strlen (err
) < sizeof buf
);
105 ASSERT (ftruncate (fileno (stderr
), 0) == 0);
108 ASSERT (!ferror (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);
120 ASSERT (!ferror (stderr
));
123 /* Commented out until cygwin behaves:
124 https://sourceware.org/ml/newlib/2011/msg00228.html */
126 /* Commented out until glibc behaves:
127 https://sourceware.org/bugzilla/show_bug.cgi?id=12792 */
128 ASSERT (ferror (stderr
));
132 ASSERT (fclose (stderr
) == 0);
133 ASSERT (remove (BASE
".tmp") == 0);