xref: /freebsd/contrib/com_err/error.c (revision 6a068746777241722b2b32c5d0bc443a2a64d80b)
1847a7462SMark Murray /*
2*ae771770SStanislav Sedov  * Copyright (c) 1997, 1998, 2001 Kungliga Tekniska Högskolan
3847a7462SMark Murray  * (Royal Institute of Technology, Stockholm, Sweden).
4847a7462SMark Murray  * All rights reserved.
5847a7462SMark Murray  *
6847a7462SMark Murray  * Redistribution and use in source and binary forms, with or without
7847a7462SMark Murray  * modification, are permitted provided that the following conditions
8847a7462SMark Murray  * are met:
9847a7462SMark Murray  *
10847a7462SMark Murray  * 1. Redistributions of source code must retain the above copyright
11847a7462SMark Murray  *    notice, this list of conditions and the following disclaimer.
12847a7462SMark Murray  *
13847a7462SMark Murray  * 2. Redistributions in binary form must reproduce the above copyright
14847a7462SMark Murray  *    notice, this list of conditions and the following disclaimer in the
15847a7462SMark Murray  *    documentation and/or other materials provided with the distribution.
16847a7462SMark Murray  *
17f054c1dfSJacques Vidrine  * 3. Neither the name of the Institute nor the names of its contributors
18847a7462SMark Murray  *    may be used to endorse or promote products derived from this software
19847a7462SMark Murray  *    without specific prior written permission.
20847a7462SMark Murray  *
21847a7462SMark Murray  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22847a7462SMark Murray  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23847a7462SMark Murray  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24847a7462SMark Murray  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25847a7462SMark Murray  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26847a7462SMark Murray  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27847a7462SMark Murray  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28847a7462SMark Murray  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29847a7462SMark Murray  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30847a7462SMark Murray  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31847a7462SMark Murray  * SUCH DAMAGE.
32847a7462SMark Murray  */
33847a7462SMark Murray 
34*ae771770SStanislav Sedov 
35847a7462SMark Murray #include <stdio.h>
36847a7462SMark Murray #include <stdlib.h>
37847a7462SMark Murray #include <string.h>
38847a7462SMark Murray #include <com_right.h>
39847a7462SMark Murray 
40*ae771770SStanislav Sedov #ifdef LIBINTL
41*ae771770SStanislav Sedov #include <libintl.h>
42*ae771770SStanislav Sedov #else
43*ae771770SStanislav Sedov #define dgettext(d,s) (s)
44*ae771770SStanislav Sedov #endif
45*ae771770SStanislav Sedov 
46*ae771770SStanislav Sedov KRB5_LIB_FUNCTION const char * KRB5_LIB_CALL
com_right(struct et_list * list,long code)47847a7462SMark Murray com_right(struct et_list *list, long code)
48847a7462SMark Murray {
49847a7462SMark Murray     struct et_list *p;
50*ae771770SStanislav Sedov     for (p = list; p; p = p->next)
51847a7462SMark Murray 	if (code >= p->table->base && code < p->table->base + p->table->n_msgs)
52847a7462SMark Murray 	    return p->table->msgs[code - p->table->base];
53*ae771770SStanislav Sedov     return NULL;
54*ae771770SStanislav Sedov }
55*ae771770SStanislav Sedov 
56*ae771770SStanislav Sedov KRB5_LIB_FUNCTION const char * KRB5_LIB_CALL
com_right_r(struct et_list * list,long code,char * str,size_t len)57*ae771770SStanislav Sedov com_right_r(struct et_list *list, long code, char *str, size_t len)
58*ae771770SStanislav Sedov {
59*ae771770SStanislav Sedov     struct et_list *p;
60*ae771770SStanislav Sedov     for (p = list; p; p = p->next) {
61*ae771770SStanislav Sedov 	if (code >= p->table->base && code < p->table->base + p->table->n_msgs) {
62*ae771770SStanislav Sedov 	    const char *msg = p->table->msgs[code - p->table->base];
63*ae771770SStanislav Sedov #ifdef LIBINTL
64*ae771770SStanislav Sedov 	    char domain[12 + 20];
65*ae771770SStanislav Sedov 	    snprintf(domain, sizeof(domain), "heim_com_err%d", p->table->base);
66*ae771770SStanislav Sedov #endif
67*ae771770SStanislav Sedov 	    strlcpy(str, dgettext(domain, msg), len);
68*ae771770SStanislav Sedov 	    return str;
69*ae771770SStanislav Sedov 	}
70847a7462SMark Murray     }
71847a7462SMark Murray     return NULL;
72847a7462SMark Murray }
73847a7462SMark Murray 
74847a7462SMark Murray struct foobar {
75847a7462SMark Murray     struct et_list etl;
76847a7462SMark Murray     struct error_table et;
77847a7462SMark Murray };
78847a7462SMark Murray 
79*ae771770SStanislav Sedov KRB5_LIB_FUNCTION void KRB5_LIB_CALL
initialize_error_table_r(struct et_list ** list,const char ** messages,int num_errors,long base)80847a7462SMark Murray initialize_error_table_r(struct et_list **list,
81847a7462SMark Murray 			 const char **messages,
82847a7462SMark Murray 			 int num_errors,
83847a7462SMark Murray 			 long base)
84847a7462SMark Murray {
85f054c1dfSJacques Vidrine     struct et_list *et, **end;
86847a7462SMark Murray     struct foobar *f;
87f054c1dfSJacques Vidrine     for (end = list, et = *list; et; end = &et->next, et = et->next)
88847a7462SMark Murray         if (et->table->msgs == messages)
89847a7462SMark Murray             return;
90847a7462SMark Murray     f = malloc(sizeof(*f));
91847a7462SMark Murray     if (f == NULL)
92847a7462SMark Murray         return;
93847a7462SMark Murray     et = &f->etl;
94847a7462SMark Murray     et->table = &f->et;
95847a7462SMark Murray     et->table->msgs = messages;
96847a7462SMark Murray     et->table->n_msgs = num_errors;
97847a7462SMark Murray     et->table->base = base;
98f054c1dfSJacques Vidrine     et->next = NULL;
99f054c1dfSJacques Vidrine     *end = et;
100847a7462SMark Murray }
101847a7462SMark Murray 
102847a7462SMark Murray 
103*ae771770SStanislav Sedov KRB5_LIB_FUNCTION void KRB5_LIB_CALL
free_error_table(struct et_list * et)104847a7462SMark Murray free_error_table(struct et_list *et)
105847a7462SMark Murray {
106847a7462SMark Murray     while(et){
107847a7462SMark Murray 	struct et_list *p = et;
108847a7462SMark Murray 	et = et->next;
109847a7462SMark Murray 	free(p);
110847a7462SMark Murray     }
111847a7462SMark Murray }
112