herror.c revision 39f4382a972d5916a43aa588baee2635dbda4089
1/*
2 * Copyright (c) 1987, 1993
3 *  The Regents of the University of California.  All rights reserved.
4 *
5 * Portions copyright (c) 1999, 2000
6 * Intel Corporation.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this software
21 *    must display the following acknowledgement:
22 *
23 *    This product includes software developed by the University of
24 *    California, Berkeley, Intel Corporation, and its contributors.
25 *
26 * 4. Neither the name of University, Intel Corporation, or their respective
27 *    contributors may be used to endorse or promote products derived from
28 *    this software without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS, INTEL CORPORATION AND
31 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
32 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
33 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS,
34 * INTEL CORPORATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
35 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 *
42 */
43
44/*
45 * Portions Copyright (c) 1996 by Internet Software Consortium.
46 *
47 * Permission to use, copy, modify, and distribute this software for any
48 * purpose with or without fee is hereby granted, provided that the above
49 * copyright notice and this permission notice appear in all copies.
50 *
51 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
52 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
53 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
54 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
55 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
56 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
57 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
58 * SOFTWARE.
59
60  herror.c  8.1 (Berkeley) 6/4/93
61  herror.c,v 1.1.1.1 2003/11/19 01:51:28 kyu3 Exp
62 */
63
64#include <sys/types.h>
65#include <sys/uio.h>
66#include <netdb.h>
67#include <string.h>
68#include <stdio.h>
69#include <unistd.h>
70
71const char *h_errlist[] = {
72  "Resolver Error 0 (no error)",
73  "Unknown host",       /* 1 HOST_NOT_FOUND */
74  "Host name lookup failure",   /* 2 TRY_AGAIN */
75  "Unknown server error",     /* 3 NO_RECOVERY */
76  "No address associated with name",  /* 4 NO_ADDRESS */
77};
78int h_nerr = { sizeof h_errlist / sizeof h_errlist[0] };
79
80int h_errno;
81
82const char *
83hstrerror(
84  int err
85  );
86
87/*
88 * herror --
89 *  print the error indicated by the h_errno value.
90 */
91void
92herror(
93  const char *s
94  )
95{
96  struct iovec iov[4];
97  register struct iovec *v = iov;
98  int   i;
99
100  if (s && *s) {
101    v->iov_base = (char *)s;
102    v->iov_len = strlen(s);
103    v++;
104    v->iov_base = ": ";
105    v->iov_len = 2;
106    v++;
107  }
108  v->iov_base = (char *)hstrerror(h_errno);
109  v->iov_len = strlen(v->iov_base);
110  v++;
111  v->iov_base = "\n";
112  v->iov_len = 1;
113#ifdef _ORG_FREEBSD_
114  writev(STDERR_FILENO, iov, (v - iov) + 1);
115#else
116  for (i = 0; i < (v - iov) + 1; i++)
117    fprintf( stderr, iov[i].iov_base);
118#endif
119
120}
121
122const char *
123hstrerror(
124  int err
125  )
126{
127  if (err < 0)
128    return ("Resolver internal error");
129  else if (err < h_nerr)
130    return (h_errlist[err]);
131  return ("Unknown resolver error");
132}
133