1 /* Test of perror() function.
2 Copyright (C) 2011-2024 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 /* Tell GCC not to warn about myerr being leaked. */
26 #if _GL_GNUC_PREREQ (13, 0)
27 # pragma GCC diagnostic ignored "-Wanalyzer-fd-leak"
30 /* This test intentionally parses stderr. So, we arrange to have fd 10
31 (outside the range of interesting fd's during the test) set up to
32 duplicate the original stderr. */
33 #define BACKUP_STDERR_FILENO 10
34 #define ASSERT_STREAM myerr
39 #define BASE "test-perror2"
44 /* We change fd 2 later, so save it in fd 10. */
45 if (dup2 (STDERR_FILENO
, BACKUP_STDERR_FILENO
) != BACKUP_STDERR_FILENO
46 || (myerr
= fdopen (BACKUP_STDERR_FILENO
, "w")) == NULL
)
49 ASSERT (freopen (BASE
".tmp", "w+", stderr
) == stderr
);
51 /* Test that perror does not clobber strerror buffer. */
62 msg1
= strerror (ENOENT
);
67 msg2
= strerror (ERANGE
);
77 msg4
= strerror (1729576);
86 ASSERT (!ferror (stderr
));
87 ASSERT (STREQ (msg4
, str4
));
95 /* Test that perror uses the same message as strerror. */
97 int errs
[] = { EACCES
, 0, -3, };
99 for (i
= 0; i
< SIZEOF (errs
); i
++)
102 const char *err
= strerror (errs
[i
]);
105 ASSERT (strlen (err
) < sizeof buf
);
107 ASSERT (ftruncate (fileno (stderr
), 0) == 0);
110 ASSERT (!ferror (stderr
));
112 ASSERT (fgets (buf
, sizeof buf
, stderr
) == buf
);
113 ASSERT (strstr (buf
, err
));
117 /* Test that perror reports write failure. */
119 ASSERT (freopen (BASE
".tmp", "r", stderr
) == stderr
);
120 ASSERT (setvbuf (stderr
, NULL
, _IONBF
, BUFSIZ
) == 0);
122 ASSERT (!ferror (stderr
));
125 /* Commented out until cygwin behaves:
126 https://sourceware.org/ml/newlib/2011/msg00228.html */
128 /* Commented out until glibc behaves:
129 https://sourceware.org/bugzilla/show_bug.cgi?id=12792 */
130 ASSERT (ferror (stderr
));
134 ASSERT (fclose (stderr
) == 0);
135 ASSERT (remove (BASE
".tmp") == 0);
137 return test_exit_status
;