Back to index

glibc  2.9
nis_error.c
Go to the documentation of this file.
00001 /* Copyright (c) 1997,1998,1999,2004,2005,2006 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997.
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 <errno.h>
00021 #include <syslog.h>
00022 #include <string.h>
00023 #include <libintl.h>
00024 #include <rpcsvc/nis.h>
00025 
00026 
00027 #define MF(line) MF1 (line)
00028 #define MF1(line) str##line
00029 static const union msgstr_t
00030 {
00031   struct
00032   {
00033 #define S(s) char MF(__LINE__)[sizeof (s)];
00034 #include "nis_error.h"
00035 #undef S
00036   };
00037   char str[0];
00038 } msgstr =
00039   {
00040     {
00041 #define S(s) s,
00042 #include "nis_error.h"
00043 #undef S
00044     }
00045   };
00046 
00047 static const unsigned short int msgidx[] =
00048   {
00049 #define S(s) offsetof (union msgstr_t, MF (__LINE__)),
00050 #include "nis_error.h"
00051 #undef S
00052   };
00053 
00054 
00055 const char *
00056 nis_sperrno (const nis_error status)
00057 {
00058   if (status >= sizeof (msgidx) / sizeof (msgidx[0]))
00059     return "???";
00060   else
00061     return gettext (msgstr.str + msgidx[status]);
00062 }
00063 libnsl_hidden_def (nis_sperrno)
00064 
00065 void
00066 nis_perror (const nis_error status, const char *label)
00067 {
00068   fprintf (stderr, "%s: %s\n", label, nis_sperrno (status));
00069 }
00070 
00071 void
00072 nis_lerror (const nis_error status, const char *label)
00073 {
00074   syslog (LOG_ERR, "%s: %s", label, nis_sperrno (status));
00075 }
00076 
00077 char *
00078 nis_sperror_r (const nis_error status, const char *label,
00079               char *buffer, size_t buflen)
00080 {
00081   if (snprintf (buffer, buflen, "%s: %s", label, nis_sperrno (status))
00082       >= buflen)
00083     {
00084       __set_errno (ERANGE);
00085       return NULL;
00086     }
00087 
00088   return buffer;
00089 }
00090 libnsl_hidden_def (nis_sperror_r)
00091 
00092 char *
00093 nis_sperror (const nis_error status, const char *label)
00094 {
00095   static char buffer[NIS_MAXNAMELEN + 1];
00096 
00097   return nis_sperror_r (status, label, buffer, sizeof (buffer));
00098 }