xref: /netbsd-src/crypto/external/bsd/heimdal/dist/lib/com_err/com_err.c (revision 241bea01a19bbb306af27777a870b86d41cb3fda)
1*241bea01Schristos /*	$NetBSD: com_err.c,v 1.3 2019/12/15 22:50:47 christos Exp $	*/
2ca1c9b0cSelric 
3ca1c9b0cSelric /*
4ca1c9b0cSelric  * Copyright (c) 1997 - 2002 Kungliga Tekniska Högskolan
5ca1c9b0cSelric  * (Royal Institute of Technology, Stockholm, Sweden).
6ca1c9b0cSelric  * All rights reserved.
7ca1c9b0cSelric  *
8ca1c9b0cSelric  * Redistribution and use in source and binary forms, with or without
9ca1c9b0cSelric  * modification, are permitted provided that the following conditions
10ca1c9b0cSelric  * are met:
11ca1c9b0cSelric  *
12ca1c9b0cSelric  * 1. Redistributions of source code must retain the above copyright
13ca1c9b0cSelric  *    notice, this list of conditions and the following disclaimer.
14ca1c9b0cSelric  *
15ca1c9b0cSelric  * 2. Redistributions in binary form must reproduce the above copyright
16ca1c9b0cSelric  *    notice, this list of conditions and the following disclaimer in the
17ca1c9b0cSelric  *    documentation and/or other materials provided with the distribution.
18ca1c9b0cSelric  *
19ca1c9b0cSelric  * 3. Neither the name of the Institute nor the names of its contributors
20ca1c9b0cSelric  *    may be used to endorse or promote products derived from this software
21ca1c9b0cSelric  *    without specific prior written permission.
22ca1c9b0cSelric  *
23ca1c9b0cSelric  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ca1c9b0cSelric  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ca1c9b0cSelric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ca1c9b0cSelric  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ca1c9b0cSelric  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ca1c9b0cSelric  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ca1c9b0cSelric  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ca1c9b0cSelric  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ca1c9b0cSelric  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ca1c9b0cSelric  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ca1c9b0cSelric  * SUCH DAMAGE.
34ca1c9b0cSelric  */
35ca1c9b0cSelric 
36ca1c9b0cSelric 
37ca1c9b0cSelric #include <config.h>
38ca1c9b0cSelric 
39ca1c9b0cSelric #include <stdio.h>
40ca1c9b0cSelric #include <stdlib.h>
41ca1c9b0cSelric #include <string.h>
42ca1c9b0cSelric #include <krb5/roken.h>
43ca1c9b0cSelric #include <krb5/com_err.h>
44ca1c9b0cSelric 
45ca1c9b0cSelric struct et_list *_et_list = NULL;
46ca1c9b0cSelric 
47ca1c9b0cSelric 
48ca1c9b0cSelric KRB5_LIB_FUNCTION const char * KRB5_LIB_CALL
error_message(long code)49ca1c9b0cSelric error_message (long code)
50ca1c9b0cSelric {
51ca1c9b0cSelric     static char msg[128];
52ca1c9b0cSelric     const char *p = com_right(_et_list, code);
53ca1c9b0cSelric     if (p == NULL) {
54ca1c9b0cSelric 	if (code < 0)
55ca1c9b0cSelric 	    snprintf(msg, sizeof(msg), "Unknown error %ld", code);
56ca1c9b0cSelric 	else
57ca1c9b0cSelric 	    p = strerror(code);
58ca1c9b0cSelric     }
59ca1c9b0cSelric     if (p != NULL && *p != '\0') {
60ca1c9b0cSelric 	strlcpy(msg, p, sizeof(msg));
61ca1c9b0cSelric     } else
62ca1c9b0cSelric 	snprintf(msg, sizeof(msg), "Unknown error %ld", code);
63ca1c9b0cSelric     return msg;
64ca1c9b0cSelric }
65ca1c9b0cSelric 
66ca1c9b0cSelric KRB5_LIB_FUNCTION int KRB5_LIB_CALL
init_error_table(const char ** msgs,long base,int count)67ca1c9b0cSelric init_error_table(const char **msgs, long base, int count)
68ca1c9b0cSelric {
69ca1c9b0cSelric     initialize_error_table_r(&_et_list, msgs, count, base);
70ca1c9b0cSelric     return 0;
71ca1c9b0cSelric }
72ca1c9b0cSelric 
73ca1c9b0cSelric static void KRB5_CALLCONV
74ca1c9b0cSelric default_proc (const char *whoami, long code, const char *fmt, va_list args)
75ca1c9b0cSelric     __attribute__ ((__format__ (__printf__, 3, 0)));
76ca1c9b0cSelric 
77ca1c9b0cSelric static void KRB5_CALLCONV
default_proc(const char * whoami,long code,const char * fmt,va_list args)78ca1c9b0cSelric default_proc (const char *whoami, long code, const char *fmt, va_list args)
79ca1c9b0cSelric {
80ca1c9b0cSelric     if (whoami)
81ca1c9b0cSelric       fprintf(stderr, "%s: ", whoami);
82ca1c9b0cSelric     if (code)
83ca1c9b0cSelric       fprintf(stderr, "%s ", error_message(code));
84ca1c9b0cSelric     if (fmt)
85ca1c9b0cSelric       vfprintf(stderr, fmt, args);
86ca1c9b0cSelric     fprintf(stderr, "\r\n");	/* ??? */
87ca1c9b0cSelric }
88ca1c9b0cSelric 
89ca1c9b0cSelric static errf com_err_hook = default_proc;
90ca1c9b0cSelric 
91ca1c9b0cSelric KRB5_LIB_FUNCTION void KRB5_LIB_CALL
com_err_va(const char * whoami,long code,const char * fmt,va_list args)92ca1c9b0cSelric com_err_va (const char *whoami,
93ca1c9b0cSelric 	    long code,
94ca1c9b0cSelric 	    const char *fmt,
95ca1c9b0cSelric 	    va_list args)
96ca1c9b0cSelric {
97ca1c9b0cSelric     (*com_err_hook) (whoami, code, fmt, args);
98ca1c9b0cSelric }
99ca1c9b0cSelric 
100ca1c9b0cSelric KRB5_LIB_FUNCTION void KRB5_LIB_CALL
com_err(const char * whoami,long code,const char * fmt,...)101ca1c9b0cSelric com_err (const char *whoami,
102ca1c9b0cSelric 	 long code,
103ca1c9b0cSelric 	 const char *fmt,
104ca1c9b0cSelric 	 ...)
105ca1c9b0cSelric {
106ca1c9b0cSelric     va_list ap;
107ca1c9b0cSelric     va_start(ap, fmt);
108ca1c9b0cSelric     com_err_va (whoami, code, fmt, ap);
109ca1c9b0cSelric     va_end(ap);
110ca1c9b0cSelric }
111ca1c9b0cSelric 
112ca1c9b0cSelric KRB5_LIB_FUNCTION errf KRB5_LIB_CALL
set_com_err_hook(errf new)113ca1c9b0cSelric set_com_err_hook (errf new)
114ca1c9b0cSelric {
115ca1c9b0cSelric     errf old = com_err_hook;
116ca1c9b0cSelric 
117ca1c9b0cSelric     if (new)
118ca1c9b0cSelric 	com_err_hook = new;
119ca1c9b0cSelric     else
120ca1c9b0cSelric 	com_err_hook = default_proc;
121ca1c9b0cSelric 
122ca1c9b0cSelric     return old;
123ca1c9b0cSelric }
124ca1c9b0cSelric 
125ca1c9b0cSelric KRB5_LIB_FUNCTION errf KRB5_LIB_CALL
reset_com_err_hook(void)126ca1c9b0cSelric reset_com_err_hook (void)
127ca1c9b0cSelric {
128ca1c9b0cSelric     return set_com_err_hook(NULL);
129ca1c9b0cSelric }
130ca1c9b0cSelric 
131ca1c9b0cSelric #define ERRCODE_RANGE   8       /* # of bits to shift table number */
132ca1c9b0cSelric #define BITS_PER_CHAR   6       /* # bits to shift per character in name */
133ca1c9b0cSelric 
134ca1c9b0cSelric static const char char_set[] =
135ca1c9b0cSelric         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
136ca1c9b0cSelric 
137ca1c9b0cSelric static char buf[6];
138ca1c9b0cSelric 
139ca1c9b0cSelric KRB5_LIB_FUNCTION const char * KRB5_LIB_CALL
error_table_name(int num)140ca1c9b0cSelric error_table_name(int num)
141ca1c9b0cSelric {
142ca1c9b0cSelric     int ch;
143ca1c9b0cSelric     int i;
144ca1c9b0cSelric     char *p;
145ca1c9b0cSelric 
146ca1c9b0cSelric     /* num = aa aaa abb bbb bcc ccc cdd ddd d?? ??? ??? */
147ca1c9b0cSelric     p = buf;
148ca1c9b0cSelric     num >>= ERRCODE_RANGE;
149ca1c9b0cSelric     /* num = ?? ??? ??? aaa aaa bbb bbb ccc ccc ddd ddd */
150ca1c9b0cSelric     num &= 077777777;
151ca1c9b0cSelric     /* num = 00 000 000 aaa aaa bbb bbb ccc ccc ddd ddd */
152ca1c9b0cSelric     for (i = 4; i >= 0; i--) {
153ca1c9b0cSelric         ch = (num >> BITS_PER_CHAR * i) & ((1 << BITS_PER_CHAR) - 1);
154ca1c9b0cSelric         if (ch != 0)
155ca1c9b0cSelric             *p++ = char_set[ch-1];
156ca1c9b0cSelric     }
157ca1c9b0cSelric     *p = '\0';
158ca1c9b0cSelric     return(buf);
159ca1c9b0cSelric }
160ca1c9b0cSelric 
161ca1c9b0cSelric KRB5_LIB_FUNCTION void KRB5_LIB_CALL
add_to_error_table(struct et_list * new_table)162ca1c9b0cSelric add_to_error_table(struct et_list *new_table)
163ca1c9b0cSelric {
164ca1c9b0cSelric     struct et_list *et;
165ca1c9b0cSelric 
166ca1c9b0cSelric     for (et = _et_list; et; et = et->next) {
167ca1c9b0cSelric 	if (et->table->base == new_table->table->base)
168ca1c9b0cSelric 	    return;
169ca1c9b0cSelric     }
170ca1c9b0cSelric 
171ca1c9b0cSelric     new_table->next = _et_list;
172ca1c9b0cSelric     _et_list = new_table;
173ca1c9b0cSelric }
174