chiark / gitweb /
update debian version
[inn-innduct.git] / lib / hstrerror.c
1 /*  $Id: hstrerror.c 5556 2002-08-11 22:23:14Z rra $
2 **
3 **  Replacement for a missing hstrerror.
4 **
5 **  Written by Russ Allbery <rra@stanford.edu>
6 **  This work is hereby placed in the public domain by its author.
7 **
8 **  Provides hstrerror (strerror, but for h_errno from the resolver
9 **  libraries) on those platforms that don't have it (most non-BSD).  This
10 **  function is thread-safe unless called with an unknown h_errno.
11 */
12
13 #include "config.h"
14 #include "clibrary.h"
15 #include <netdb.h>
16
17 static const char * const errors[] = {
18     "No resolver error",                /* 0 NETDB_SUCCESS */
19     "Unknown host",                     /* 1 HOST_NOT_FOUND */
20     "Host name lookup failure",         /* 2 TRY_AGAIN */
21     "Unknown server error",             /* 3 NO_RECOVERY */
22     "No address associated with name",  /* 4 NO_ADDRESS / NO_DATA */
23 };
24 static int nerrors = (sizeof errors / sizeof errors[0]);
25
26 /* If we're running the test suite, rename hstrerror to avoid conflicts with
27    the system version. */
28 #if TESTING
29 # define hstrerror test_hstrerror
30 const char *test_hstrerror(int);
31 #endif
32
33 const char *
34 hstrerror(int error)
35 {
36     static char buf[32];
37
38     if (error == -1)
39         return "Internal resolver error";
40     if (error >= 0 && error < nerrors)
41         return errors[error];
42     snprintf(buf, sizeof(buf), "Resolver error %d", error);
43     return buf;
44 }