1 /* Test of perror() function.
2 Copyright (C) 2011-2020 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 (STREQ (msg4
, str4
));
90 /* Test that perror uses the same message as strerror. */
92 int errs
[] = { EACCES
, 0, -3, };
94 for (i
= 0; i
< SIZEOF (errs
); i
++)
97 char *err
= strerror (errs
[i
]);
100 ASSERT (strlen (err
) < sizeof buf
);
102 ASSERT (ftruncate (fileno (stderr
), 0) == 0);
105 ASSERT (!ferror (stderr
));
107 ASSERT (fgets (buf
, sizeof buf
, stderr
) == buf
);
108 ASSERT (strstr (buf
, err
));
112 /* Test that perror reports write failure. */
114 ASSERT (freopen (BASE
".tmp", "r", stderr
) == stderr
);
115 ASSERT (setvbuf (stderr
, NULL
, _IONBF
, BUFSIZ
) == 0);
117 ASSERT (!ferror (stderr
));
120 /* Commented out until cygwin behaves:
121 https://sourceware.org/ml/newlib/2011/msg00228.html */
123 /* Commented out until glibc behaves:
124 https://sourceware.org/bugzilla/show_bug.cgi?id=12792 */
125 ASSERT (ferror (stderr
));
129 ASSERT (fclose (stderr
) == 0);
130 ASSERT (remove (BASE
".tmp") == 0);