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