]> git.ipfire.org Git - thirdparty/glibc.git/blame - stdio-common/perror.c
Prepare vfscanf to use __strtof128_internal
[thirdparty/glibc.git] / stdio-common / perror.c
CommitLineData
688903eb 1/* Copyright (C) 1991-2018 Free Software Foundation, Inc.
c84142e8 2 This file is part of the GNU C Library.
28f540f4 3
c84142e8 4 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
28f540f4 8
c84142e8
UD
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
41bdb6e2 12 Lesser General Public License for more details.
28f540f4 13
41bdb6e2 14 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
28f540f4 17
62ab42d6 18#include <errno.h>
28f540f4 19#include <stdio.h>
479e9b3f 20#include <string.h>
1fc0e331 21#include <unistd.h>
62ab42d6 22#include <wchar.h>
14c35863 23#include "libioP.h"
28f540f4 24
1fc0e331 25static void
eaad82e0 26perror_internal (FILE *fp, const char *s, int errnum)
28f540f4
RM
27{
28 char buf[1024];
c84142e8 29 const char *colon;
62ab42d6 30 const char *errstring;
28f540f4
RM
31
32 if (s == NULL || *s == '\0')
33 s = colon = "";
34 else
35 colon = ": ";
36
62ab42d6
UD
37 errstring = __strerror_r (errnum, buf, sizeof buf);
38
8a259a23 39 (void) __fxprintf (fp, "%s%s%s\n", s, colon, errstring);
1fc0e331
UD
40}
41
42
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. */
46void
47perror (const char *s)
48{
eaad82e0 49 int errnum = errno;
1fc0e331
UD
50 FILE *fp;
51 int fd = -1;
52
55183f24 53
1fc0e331
UD
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)
9744496f 59 || (fd = __fileno (stderr)) == -1
3ba06713 60 || (fd = __dup (fd)) == -1
1fc0e331
UD
61 || (fp = fdopen (fd, "w+")) == NULL)
62 {
a1ffb40e 63 if (__glibc_unlikely (fd != -1))
6293b803 64 __close (fd);
1fc0e331
UD
65
66 /* Use standard error as is. */
eaad82e0 67 perror_internal (stderr, s, errnum);
1fc0e331
UD
68 }
69 else
70 {
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. */
eaad82e0 74 perror_internal (fp, s, errnum);
aec84f53
UD
75
76 if (_IO_ferror_unlocked (fp))
77 stderr->_flags |= _IO_ERR_SEEN;
78
1fc0e331
UD
79 /* Close the stream. */
80 fclose (fp);
1fc0e331 81 }
28f540f4 82}
b9b91868 83libc_hidden_def (perror)