Back to index

glibc  2.9
herror.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1987, 1993
00003  *    The Regents of the University of California.  All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 4. Neither the name of the University nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
00030 /*
00031  * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
00032  *
00033  * Permission to use, copy, modify, and distribute this software for any
00034  * purpose with or without fee is hereby granted, provided that the above
00035  * copyright notice and this permission notice appear in all copies.
00036  *
00037  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
00038  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
00039  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
00040  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
00041  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
00042  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
00043  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
00044  * SOFTWARE.
00045  */
00046 
00047 #if defined(LIBC_SCCS) && !defined(lint)
00048 static const char sccsid[] = "@(#)herror.c       8.1 (Berkeley) 6/4/93";
00049 static const char rcsid[] = "$BINDId: herror.c,v 8.11 1999/10/13 16:39:39 vixie Exp $";
00050 #endif /* LIBC_SCCS and not lint */
00051 
00052 #include <sys/types.h>
00053 #include <sys/param.h>
00054 #include <sys/uio.h>
00055 
00056 #include <netinet/in.h>
00057 #include <arpa/nameser.h>
00058 
00059 #include <netdb.h>
00060 #include <resolv.h>
00061 #include <string.h>
00062 #include <unistd.h>
00063 
00064 #include <libintl.h>
00065 #include <not-cancel.h>
00066 
00067 const char *const h_errlist[] = {
00068        N_("Resolver Error 0 (no error)"),
00069        N_("Unknown host"),                /* 1 HOST_NOT_FOUND */
00070        N_("Host name lookup failure"),           /* 2 TRY_AGAIN */
00071        N_("Unknown server error"),        /* 3 NO_RECOVERY */
00072        N_("No address associated with name"),    /* 4 NO_ADDRESS */
00073 };
00074 const int     h_nerr = { sizeof h_errlist / sizeof h_errlist[0] };
00075 
00076 /*
00077  * herror --
00078  *     print the error indicated by the h_errno value.
00079  */
00080 void
00081 herror(const char *s) {
00082        struct iovec iov[4], *v = iov;
00083 
00084        if (s != NULL && *s != '\0') {
00085               v->iov_base = (/*noconst*/ char *)s;
00086               v->iov_len = strlen(s);
00087               v++;
00088               v->iov_base = ": ";
00089               v->iov_len = 2;
00090               v++;
00091        }
00092        v->iov_base = (char *)hstrerror(h_errno);
00093        v->iov_len = strlen(v->iov_base);
00094        v++;
00095        v->iov_base = "\n";
00096        v->iov_len = 1;
00097        writev_not_cancel_no_status(STDERR_FILENO, iov, (v - iov) + 1);
00098 }
00099 
00100 /*
00101  * hstrerror --
00102  *     return the string associated with a given "host" errno value.
00103  */
00104 const char *
00105 hstrerror(int err) {
00106        if (err < 0)
00107               return _("Resolver internal error");
00108        else if (err < h_nerr)
00109               return _(h_errlist[err]);
00110        return _("Unknown resolver error");
00111 }
00112 libc_hidden_def (hstrerror)