xref: /freebsd/crypto/heimdal/lib/com_err/com_err.c (revision 6a068746777241722b2b32c5d0bc443a2a64d80b)
15e9cd1aeSAssar Westerlund /*
2*ae771770SStanislav Sedov  * Copyright (c) 1997 - 2002 Kungliga Tekniska Högskolan
35e9cd1aeSAssar Westerlund  * (Royal Institute of Technology, Stockholm, Sweden).
45e9cd1aeSAssar Westerlund  * All rights reserved.
55e9cd1aeSAssar Westerlund  *
65e9cd1aeSAssar Westerlund  * Redistribution and use in source and binary forms, with or without
75e9cd1aeSAssar Westerlund  * modification, are permitted provided that the following conditions
85e9cd1aeSAssar Westerlund  * are met:
95e9cd1aeSAssar Westerlund  *
105e9cd1aeSAssar Westerlund  * 1. Redistributions of source code must retain the above copyright
115e9cd1aeSAssar Westerlund  *    notice, this list of conditions and the following disclaimer.
125e9cd1aeSAssar Westerlund  *
135e9cd1aeSAssar Westerlund  * 2. Redistributions in binary form must reproduce the above copyright
145e9cd1aeSAssar Westerlund  *    notice, this list of conditions and the following disclaimer in the
155e9cd1aeSAssar Westerlund  *    documentation and/or other materials provided with the distribution.
165e9cd1aeSAssar Westerlund  *
175e9cd1aeSAssar Westerlund  * 3. Neither the name of the Institute nor the names of its contributors
185e9cd1aeSAssar Westerlund  *    may be used to endorse or promote products derived from this software
195e9cd1aeSAssar Westerlund  *    without specific prior written permission.
205e9cd1aeSAssar Westerlund  *
215e9cd1aeSAssar Westerlund  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
225e9cd1aeSAssar Westerlund  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
235e9cd1aeSAssar Westerlund  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
245e9cd1aeSAssar Westerlund  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
255e9cd1aeSAssar Westerlund  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
265e9cd1aeSAssar Westerlund  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
275e9cd1aeSAssar Westerlund  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
285e9cd1aeSAssar Westerlund  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
295e9cd1aeSAssar Westerlund  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
305e9cd1aeSAssar Westerlund  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
315e9cd1aeSAssar Westerlund  * SUCH DAMAGE.
325e9cd1aeSAssar Westerlund  */
335e9cd1aeSAssar Westerlund 
34*ae771770SStanislav Sedov 
355e9cd1aeSAssar Westerlund #include <config.h>
36*ae771770SStanislav Sedov 
375e9cd1aeSAssar Westerlund #include <stdio.h>
385e9cd1aeSAssar Westerlund #include <stdlib.h>
395e9cd1aeSAssar Westerlund #include <string.h>
405e9cd1aeSAssar Westerlund #include <roken.h>
415e9cd1aeSAssar Westerlund #include "com_err.h"
425e9cd1aeSAssar Westerlund 
435e9cd1aeSAssar Westerlund struct et_list *_et_list = NULL;
445e9cd1aeSAssar Westerlund 
455e9cd1aeSAssar Westerlund 
46*ae771770SStanislav Sedov KRB5_LIB_FUNCTION const char * KRB5_LIB_CALL
error_message(long code)475e9cd1aeSAssar Westerlund error_message (long code)
485e9cd1aeSAssar Westerlund {
495e9cd1aeSAssar Westerlund     static char msg[128];
505e9cd1aeSAssar Westerlund     const char *p = com_right(_et_list, code);
518373020dSJacques Vidrine     if (p == NULL) {
528373020dSJacques Vidrine 	if (code < 0)
53c19800e8SDoug Rabson 	    snprintf(msg, sizeof(msg), "Unknown error %ld", code);
548373020dSJacques Vidrine 	else
555e9cd1aeSAssar Westerlund 	    p = strerror(code);
568373020dSJacques Vidrine     }
575e9cd1aeSAssar Westerlund     if (p != NULL && *p != '\0') {
58c19800e8SDoug Rabson 	strlcpy(msg, p, sizeof(msg));
595e9cd1aeSAssar Westerlund     } else
60c19800e8SDoug Rabson 	snprintf(msg, sizeof(msg), "Unknown error %ld", code);
615e9cd1aeSAssar Westerlund     return msg;
625e9cd1aeSAssar Westerlund }
635e9cd1aeSAssar Westerlund 
64*ae771770SStanislav Sedov KRB5_LIB_FUNCTION int KRB5_LIB_CALL
init_error_table(const char ** msgs,long base,int count)655e9cd1aeSAssar Westerlund init_error_table(const char **msgs, long base, int count)
665e9cd1aeSAssar Westerlund {
675e9cd1aeSAssar Westerlund     initialize_error_table_r(&_et_list, msgs, count, base);
685e9cd1aeSAssar Westerlund     return 0;
695e9cd1aeSAssar Westerlund }
705e9cd1aeSAssar Westerlund 
71*ae771770SStanislav Sedov static void KRB5_CALLCONV
725e9cd1aeSAssar Westerlund default_proc (const char *whoami, long code, const char *fmt, va_list args)
73adb0ddaeSAssar Westerlund     __attribute__((__format__(__printf__, 3, 0)));
74adb0ddaeSAssar Westerlund 
75*ae771770SStanislav Sedov static void KRB5_CALLCONV
default_proc(const char * whoami,long code,const char * fmt,va_list args)76adb0ddaeSAssar Westerlund default_proc (const char *whoami, long code, const char *fmt, va_list args)
775e9cd1aeSAssar Westerlund {
785e9cd1aeSAssar Westerlund     if (whoami)
795e9cd1aeSAssar Westerlund       fprintf(stderr, "%s: ", whoami);
805e9cd1aeSAssar Westerlund     if (code)
815e9cd1aeSAssar Westerlund       fprintf(stderr, "%s ", error_message(code));
825e9cd1aeSAssar Westerlund     if (fmt)
835e9cd1aeSAssar Westerlund       vfprintf(stderr, fmt, args);
845e9cd1aeSAssar Westerlund     fprintf(stderr, "\r\n");	/* ??? */
855e9cd1aeSAssar Westerlund }
865e9cd1aeSAssar Westerlund 
875e9cd1aeSAssar Westerlund static errf com_err_hook = default_proc;
885e9cd1aeSAssar Westerlund 
89*ae771770SStanislav Sedov KRB5_LIB_FUNCTION void KRB5_LIB_CALL
com_err_va(const char * whoami,long code,const char * fmt,va_list args)905e9cd1aeSAssar Westerlund com_err_va (const char *whoami,
915e9cd1aeSAssar Westerlund 	    long code,
925e9cd1aeSAssar Westerlund 	    const char *fmt,
935e9cd1aeSAssar Westerlund 	    va_list args)
945e9cd1aeSAssar Westerlund {
955e9cd1aeSAssar Westerlund     (*com_err_hook) (whoami, code, fmt, args);
965e9cd1aeSAssar Westerlund }
975e9cd1aeSAssar Westerlund 
98*ae771770SStanislav Sedov KRB5_LIB_FUNCTION void KRB5_LIB_CALL
com_err(const char * whoami,long code,const char * fmt,...)995e9cd1aeSAssar Westerlund com_err (const char *whoami,
1005e9cd1aeSAssar Westerlund 	 long code,
1015e9cd1aeSAssar Westerlund 	 const char *fmt,
1025e9cd1aeSAssar Westerlund 	 ...)
1035e9cd1aeSAssar Westerlund {
1045e9cd1aeSAssar Westerlund     va_list ap;
1055e9cd1aeSAssar Westerlund     va_start(ap, fmt);
1065e9cd1aeSAssar Westerlund     com_err_va (whoami, code, fmt, ap);
1075e9cd1aeSAssar Westerlund     va_end(ap);
1085e9cd1aeSAssar Westerlund }
1095e9cd1aeSAssar Westerlund 
110*ae771770SStanislav Sedov KRB5_LIB_FUNCTION errf KRB5_LIB_CALL
set_com_err_hook(errf new)1115e9cd1aeSAssar Westerlund set_com_err_hook (errf new)
1125e9cd1aeSAssar Westerlund {
1135e9cd1aeSAssar Westerlund     errf old = com_err_hook;
1145e9cd1aeSAssar Westerlund 
1155e9cd1aeSAssar Westerlund     if (new)
1165e9cd1aeSAssar Westerlund 	com_err_hook = new;
1175e9cd1aeSAssar Westerlund     else
1185e9cd1aeSAssar Westerlund 	com_err_hook = default_proc;
1195e9cd1aeSAssar Westerlund 
1205e9cd1aeSAssar Westerlund     return old;
1215e9cd1aeSAssar Westerlund }
1225e9cd1aeSAssar Westerlund 
123*ae771770SStanislav Sedov KRB5_LIB_FUNCTION errf KRB5_LIB_CALL
reset_com_err_hook(void)1245e9cd1aeSAssar Westerlund reset_com_err_hook (void)
1255e9cd1aeSAssar Westerlund {
1265e9cd1aeSAssar Westerlund     return set_com_err_hook(NULL);
1275e9cd1aeSAssar Westerlund }
1285e9cd1aeSAssar Westerlund 
1295e9cd1aeSAssar Westerlund #define ERRCODE_RANGE   8       /* # of bits to shift table number */
1305e9cd1aeSAssar Westerlund #define BITS_PER_CHAR   6       /* # bits to shift per character in name */
1315e9cd1aeSAssar Westerlund 
1325e9cd1aeSAssar Westerlund static const char char_set[] =
1335e9cd1aeSAssar Westerlund         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
1345e9cd1aeSAssar Westerlund 
1355e9cd1aeSAssar Westerlund static char buf[6];
1365e9cd1aeSAssar Westerlund 
137*ae771770SStanislav Sedov KRB5_LIB_FUNCTION const char * KRB5_LIB_CALL
error_table_name(int num)1385e9cd1aeSAssar Westerlund error_table_name(int num)
1395e9cd1aeSAssar Westerlund {
1405e9cd1aeSAssar Westerlund     int ch;
1415e9cd1aeSAssar Westerlund     int i;
1425e9cd1aeSAssar Westerlund     char *p;
1435e9cd1aeSAssar Westerlund 
1445e9cd1aeSAssar Westerlund     /* num = aa aaa abb bbb bcc ccc cdd ddd d?? ??? ??? */
1455e9cd1aeSAssar Westerlund     p = buf;
1465e9cd1aeSAssar Westerlund     num >>= ERRCODE_RANGE;
1475e9cd1aeSAssar Westerlund     /* num = ?? ??? ??? aaa aaa bbb bbb ccc ccc ddd ddd */
1485e9cd1aeSAssar Westerlund     num &= 077777777;
1495e9cd1aeSAssar Westerlund     /* num = 00 000 000 aaa aaa bbb bbb ccc ccc ddd ddd */
1505e9cd1aeSAssar Westerlund     for (i = 4; i >= 0; i--) {
1515e9cd1aeSAssar Westerlund         ch = (num >> BITS_PER_CHAR * i) & ((1 << BITS_PER_CHAR) - 1);
1525e9cd1aeSAssar Westerlund         if (ch != 0)
1535e9cd1aeSAssar Westerlund             *p++ = char_set[ch-1];
1545e9cd1aeSAssar Westerlund     }
1555e9cd1aeSAssar Westerlund     *p = '\0';
1565e9cd1aeSAssar Westerlund     return(buf);
1575e9cd1aeSAssar Westerlund }
158adb0ddaeSAssar Westerlund 
159*ae771770SStanislav Sedov KRB5_LIB_FUNCTION void KRB5_LIB_CALL
add_to_error_table(struct et_list * new_table)160adb0ddaeSAssar Westerlund add_to_error_table(struct et_list *new_table)
161adb0ddaeSAssar Westerlund {
162adb0ddaeSAssar Westerlund     struct et_list *et;
163adb0ddaeSAssar Westerlund 
164adb0ddaeSAssar Westerlund     for (et = _et_list; et; et = et->next) {
165adb0ddaeSAssar Westerlund 	if (et->table->base == new_table->table->base)
166adb0ddaeSAssar Westerlund 	    return;
167adb0ddaeSAssar Westerlund     }
168adb0ddaeSAssar Westerlund 
169adb0ddaeSAssar Westerlund     new_table->next = _et_list;
170adb0ddaeSAssar Westerlund     _et_list = new_table;
171adb0ddaeSAssar Westerlund }
172