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