Back to index

glibc  2.9
perror.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991-1993,1997,1998,2000-2005 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <errno.h>
00020 #include <stdio.h>
00021 #include <string.h>
00022 #include <unistd.h>
00023 #include <wchar.h>
00024 #include "libioP.h"
00025 
00026 static void
00027 perror_internal (FILE *fp, const char *s, int errnum)
00028 {
00029   char buf[1024];
00030   const char *colon;
00031   const char *errstring;
00032 
00033   if (s == NULL || *s == '\0')
00034     s = colon = "";
00035   else
00036     colon = ": ";
00037 
00038   errstring = __strerror_r (errnum, buf, sizeof buf);
00039 
00040   (void) __fxprintf (fp, "%s%s%s\n", s, colon, errstring);
00041 }
00042 
00043 
00044 /* Print a line on stderr consisting of the text in S, a colon, a space,
00045    a message describing the meaning of the contents of `errno' and a newline.
00046    If S is NULL or "", the colon and space are omitted.  */
00047 void
00048 perror (const char *s)
00049 {
00050   int errnum = errno;
00051   FILE *fp;
00052   int fd = -1;
00053 
00054 
00055   /* The standard says that 'perror' must not change the orientation
00056      of the stream.  What is supposed to happen when the stream isn't
00057      oriented yet?  In this case we'll create a new stream which is
00058      using the same underlying file descriptor.  */
00059   if (__builtin_expect (_IO_fwide (stderr, 0) != 0, 1)
00060       || (fd = fileno (stderr)) == -1
00061       || (fd = __dup (fd)) == -1
00062       || (fp = fdopen (fd, "w+")) == NULL)
00063     {
00064       if (__builtin_expect (fd != -1, 0))
00065        __close (fd);
00066 
00067       /* Use standard error as is.  */
00068       perror_internal (stderr, s, errnum);
00069     }
00070   else
00071     {
00072       /* We don't have to do any special hacks regarding the file
00073         position.  Since the stderr stream wasn't used so far we just
00074         write to the descriptor.  */
00075       perror_internal (fp, s, errnum);
00076       /* Close the stream.  */
00077       fclose (fp);
00078     }
00079 }
00080 libc_hidden_def (perror)