|
Packit |
6c4009 |
/* Copyright (C) 1991-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit |
6c4009 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License as published by the Free Software Foundation; either
|
|
Packit |
6c4009 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6c4009 |
Lesser General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License along with the GNU C Library; if not, see
|
|
Packit |
6c4009 |
<http://www.gnu.org/licenses/>. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <errno.h>
|
|
Packit |
6c4009 |
#include <stdio.h>
|
|
Packit |
6c4009 |
#include <string.h>
|
|
Packit |
6c4009 |
#include <unistd.h>
|
|
Packit |
6c4009 |
#include <wchar.h>
|
|
Packit |
6c4009 |
#include "libioP.h"
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static void
|
|
Packit |
6c4009 |
perror_internal (FILE *fp, const char *s, int errnum)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
char buf[1024];
|
|
Packit |
6c4009 |
const char *colon;
|
|
Packit |
6c4009 |
const char *errstring;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (s == NULL || *s == '\0')
|
|
Packit |
6c4009 |
s = colon = "";
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
colon = ": ";
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
errstring = __strerror_r (errnum, buf, sizeof buf);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
(void) __fxprintf (fp, "%s%s%s\n", s, colon, errstring);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Print a line on stderr consisting of the text in S, a colon, a space,
|
|
Packit |
6c4009 |
a message describing the meaning of the contents of `errno' and a newline.
|
|
Packit |
6c4009 |
If S is NULL or "", the colon and space are omitted. */
|
|
Packit |
6c4009 |
void
|
|
Packit |
6c4009 |
perror (const char *s)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
int errnum = errno;
|
|
Packit |
6c4009 |
FILE *fp;
|
|
Packit |
6c4009 |
int fd = -1;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* The standard says that 'perror' must not change the orientation
|
|
Packit |
6c4009 |
of the stream. What is supposed to happen when the stream isn't
|
|
Packit |
6c4009 |
oriented yet? In this case we'll create a new stream which is
|
|
Packit |
6c4009 |
using the same underlying file descriptor. */
|
|
Packit |
6c4009 |
if (__builtin_expect (_IO_fwide (stderr, 0) != 0, 1)
|
|
Packit |
6c4009 |
|| (fd = __fileno (stderr)) == -1
|
|
Packit |
6c4009 |
|| (fd = __dup (fd)) == -1
|
|
Packit |
6c4009 |
|| (fp = fdopen (fd, "w+")) == NULL)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (__glibc_unlikely (fd != -1))
|
|
Packit |
6c4009 |
__close (fd);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Use standard error as is. */
|
|
Packit |
6c4009 |
perror_internal (stderr, s, errnum);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* We don't have to do any special hacks regarding the file
|
|
Packit |
6c4009 |
position. Since the stderr stream wasn't used so far we just
|
|
Packit |
6c4009 |
write to the descriptor. */
|
|
Packit |
6c4009 |
perror_internal (fp, s, errnum);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (_IO_ferror_unlocked (fp))
|
|
Packit |
6c4009 |
stderr->_flags |= _IO_ERR_SEEN;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Close the stream. */
|
|
Packit |
6c4009 |
fclose (fp);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
libc_hidden_def (perror)
|