1 /* Copyright (C) 1991-2019 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library 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 GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
26 perror_internal (FILE *fp
, const char *s
, int errnum
)
30 const char *errstring
;
32 if (s
== NULL
|| *s
== '\0')
37 errstring
= __strerror_r (errnum
, buf
, sizeof buf
);
39 (void) __fxprintf (fp
, "%s%s%s\n", s
, colon
, errstring
);
43 /* Print a line on stderr consisting of the text in S, a colon, a space,
44 a message describing the meaning of the contents of `errno' and a newline.
45 If S is NULL or "", the colon and space are omitted. */
47 perror (const char *s
)
54 /* The standard says that 'perror' must not change the orientation
55 of the stream. What is supposed to happen when the stream isn't
56 oriented yet? In this case we'll create a new stream which is
57 using the same underlying file descriptor. */
58 if (__builtin_expect (_IO_fwide (stderr
, 0) != 0, 1)
59 || (fd
= __fileno (stderr
)) == -1
60 || (fd
= __dup (fd
)) == -1
61 || (fp
= fdopen (fd
, "w+")) == NULL
)
63 if (__glibc_unlikely (fd
!= -1))
66 /* Use standard error as is. */
67 perror_internal (stderr
, s
, errnum
);
71 /* We don't have to do any special hacks regarding the file
72 position. Since the stderr stream wasn't used so far we just
73 write to the descriptor. */
74 perror_internal (fp
, s
, errnum
);
76 if (_IO_ferror_unlocked (fp
))
77 stderr
->_flags
|= _IO_ERR_SEEN
;
79 /* Close the stream. */
83 libc_hidden_def (perror
)