Back to index

glibc  2.9
err.c
Go to the documentation of this file.
00001 /* 4.4BSD utility functions for error messages.
00002    Copyright (C) 1995,96,98,2001,02 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <stdarg.h>
00021 #include <err.h>
00022 #include <stdlib.h>
00023 #include <errno.h>
00024 #include <string.h>
00025 #include <stdio.h>
00026 
00027 #ifdef USE_IN_LIBIO
00028 # include <wchar.h>
00029 # define flockfile(s) _IO_flockfile (s)
00030 # define funlockfile(s) _IO_funlockfile (s)
00031 #endif
00032 
00033 extern char *__progname;
00034 
00035 #define VA(call)                                                     \
00036 {                                                                    \
00037   va_list ap;                                                        \
00038   va_start (ap, format);                                             \
00039   call;                                                                     \
00040   va_end (ap);                                                              \
00041 }
00042 
00043 #ifdef USE_IN_LIBIO
00044 static void
00045 convert_and_print (const char *format, __gnuc_va_list ap)
00046 {
00047 # define ALLOCA_LIMIT       2000
00048   size_t len;
00049   wchar_t *wformat = NULL;
00050   mbstate_t st;
00051   size_t res;
00052   const char *tmp;
00053 
00054   if (format == NULL)
00055     return;
00056 
00057   len = strlen (format) + 1;
00058 
00059   do
00060     {
00061       if (len < ALLOCA_LIMIT)
00062        wformat = (wchar_t *) alloca (len * sizeof (wchar_t));
00063       else
00064        {
00065          if (wformat != NULL && len / 2 < ALLOCA_LIMIT)
00066            wformat = NULL;
00067 
00068          wformat = (wchar_t *) realloc (wformat, len * sizeof (wchar_t));
00069 
00070          if (wformat == NULL)
00071            {
00072              fputws_unlocked (L"out of memory\n", stderr);
00073              return;
00074            }
00075        }
00076 
00077       memset (&st, '\0', sizeof (st));
00078       tmp =format;
00079     }
00080   while ((res = __mbsrtowcs (wformat, &tmp, len, &st)) == len);
00081 
00082   if (res == (size_t) -1)
00083     /* The string cannot be converted.  */
00084     wformat = (wchar_t *) L"???";
00085 
00086   __vfwprintf (stderr, wformat, ap);
00087 }
00088 #endif
00089 
00090 void
00091 vwarnx (const char *format, __gnuc_va_list ap)
00092 {
00093   flockfile (stderr);
00094 #ifdef USE_IN_LIBIO
00095   if (_IO_fwide (stderr, 0) > 0)
00096     {
00097       __fwprintf (stderr, L"%s: ", __progname);
00098       convert_and_print (format, ap);
00099       putwc_unlocked (L'\n', stderr);
00100     }
00101   else
00102 #endif
00103     {
00104       fprintf (stderr, "%s: ", __progname);
00105       if (format)
00106        vfprintf (stderr, format, ap);
00107       putc_unlocked ('\n', stderr);
00108     }
00109   funlockfile (stderr);
00110 }
00111 libc_hidden_def (vwarnx)
00112 
00113 void
00114 vwarn (const char *format, __gnuc_va_list ap)
00115 {
00116   int error = errno;
00117 
00118   flockfile (stderr);
00119 #ifdef USE_IN_LIBIO
00120   if (_IO_fwide (stderr, 0) > 0)
00121     {
00122       __fwprintf (stderr, L"%s: ", __progname);
00123       if (format)
00124        {
00125          convert_and_print (format, ap);
00126          fputws_unlocked (L": ", stderr);
00127        }
00128       __set_errno (error);
00129       __fwprintf (stderr, L"%m\n");
00130     }
00131   else
00132 #endif
00133     {
00134       fprintf (stderr, "%s: ", __progname);
00135       if (format)
00136        {
00137          vfprintf (stderr, format, ap);
00138          fputs_unlocked (": ", stderr);
00139        }
00140       __set_errno (error);
00141       fprintf (stderr, "%m\n");
00142     }
00143   funlockfile (stderr);
00144 }
00145 libc_hidden_def (vwarn)
00146 
00147 
00148 void
00149 warn (const char *format, ...)
00150 {
00151   VA (vwarn (format, ap))
00152 }
00153 libc_hidden_def (warn)
00154 
00155 void
00156 warnx (const char *format, ...)
00157 {
00158   VA (vwarnx (format, ap))
00159 }
00160 libc_hidden_def (warnx)
00161 
00162 void
00163 verr (int status, const char *format, __gnuc_va_list ap)
00164 {
00165   vwarn (format, ap);
00166   exit (status);
00167 }
00168 libc_hidden_def (verr)
00169 
00170 void
00171 verrx (int status, const char *format, __gnuc_va_list ap)
00172 {
00173   vwarnx (format, ap);
00174   exit (status);
00175 }
00176 libc_hidden_def (verrx)
00177 
00178 void
00179 err (int status, const char *format, ...)
00180 {
00181   VA (verr (status, format, ap))
00182 }
00183 
00184 void
00185 errx (int status, const char *format, ...)
00186 {
00187   VA (verrx (status, format, ap))
00188 }