Back to index

glibc  2.9
gai_strerror.c
Go to the documentation of this file.
00001 /* Copyright (C) 1997, 2001, 2002, 2004, 2005 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Philip Blundell <pjb27@cam.ac.uk>, 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 <libintl.h>
00021 #include <netdb.h>
00022 #include <stdint.h>
00023 #include <stdio.h>
00024 
00025 
00026 #define MSGSTRFIELD(line) MSGSTRFIELD1 (line)
00027 #define MSGSTRFIELD1(line) str##line
00028 static const union msgstr_t
00029 {
00030   struct
00031   {
00032 #define _S(n, s) char MSGSTRFIELD(__LINE__)[sizeof (s)];
00033 #include "gai_strerror-strs.h"
00034 #undef _S
00035   };
00036   char str[0];
00037 } msgstr =
00038   {
00039     {
00040 #define _S(n, s) s,
00041 #include "gai_strerror-strs.h"
00042 #undef _S
00043     }
00044   };
00045 static const struct
00046 {
00047   int16_t code;
00048   uint16_t idx;
00049 } msgidx[] =
00050   {
00051 #define _S(n, s) { n, offsetof (union msgstr_t, MSGSTRFIELD (__LINE__)) },
00052 #include "gai_strerror-strs.h"
00053 #undef _S
00054   };
00055 
00056 
00057 const char *
00058 gai_strerror (int code)
00059 {
00060   const char *result = "Unknown error";
00061   for (size_t i = 0; i < sizeof (msgidx) / sizeof (msgidx[0]); ++i)
00062     if (msgidx[i].code == code)
00063       {
00064        result = msgstr.str + msgidx[i].idx;
00065        break;
00066       }
00067 
00068   return _(result);
00069 }
00070 libc_hidden_def (gai_strerror)