Theodore Ts'o | d035509 | 2003-05-05 23:07:32 -0400 | [diff] [blame] | 1 | /* |
| 2 | * com_right.c -- provide Heimdall / Kerberos4kth com_err interfaces |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 3 | * for backwards compatbility |
| 4 | * |
Theodore Ts'o | d035509 | 2003-05-05 23:07:32 -0400 | [diff] [blame] | 5 | * Copyright (c) 2003 by Theodore Ts'o |
| 6 | * |
| 7 | * Taken from lib/com_err/error.c from Kerberos4kth distribution. |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 8 | * |
Theodore Ts'o | d035509 | 2003-05-05 23:07:32 -0400 | [diff] [blame] | 9 | * Copyright (c) 1997, 1998, 2001 Kungliga Tekniska Högskolan |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 10 | * (Royal Institute of Technology, Stockholm, Sweden). |
| 11 | * All rights reserved. |
Theodore Ts'o | d035509 | 2003-05-05 23:07:32 -0400 | [diff] [blame] | 12 | * |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 13 | * Redistribution and use in source and binary forms, with or without |
| 14 | * modification, are permitted provided that the following conditions |
| 15 | * are met: |
Theodore Ts'o | d035509 | 2003-05-05 23:07:32 -0400 | [diff] [blame] | 16 | * |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 17 | * 1. Redistributions of source code must retain the above copyright |
| 18 | * notice, this list of conditions and the following disclaimer. |
Theodore Ts'o | d035509 | 2003-05-05 23:07:32 -0400 | [diff] [blame] | 19 | * |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 20 | * 2. Redistributions in binary form must reproduce the above copyright |
| 21 | * notice, this list of conditions and the following disclaimer in the |
| 22 | * documentation and/or other materials provided with the distribution. |
Theodore Ts'o | d035509 | 2003-05-05 23:07:32 -0400 | [diff] [blame] | 23 | * |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 24 | * 3. Neither the name of the Institute nor the names of its contributors |
| 25 | * may be used to endorse or promote products derived from this software |
| 26 | * without specific prior written permission. |
Theodore Ts'o | d035509 | 2003-05-05 23:07:32 -0400 | [diff] [blame] | 27 | * |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 28 | * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND |
| 29 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 30 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 31 | * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE |
| 32 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 33 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 34 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 35 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 37 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 38 | * SUCH DAMAGE. |
Theodore Ts'o | d035509 | 2003-05-05 23:07:32 -0400 | [diff] [blame] | 39 | */ |
| 40 | |
Theodore Ts'o | d1154eb | 2011-09-18 17:34:37 -0400 | [diff] [blame] | 41 | #include "config.h" |
Theodore Ts'o | d035509 | 2003-05-05 23:07:32 -0400 | [diff] [blame] | 42 | #include <stdio.h> |
| 43 | #include <stdlib.h> |
| 44 | #include <string.h> |
| 45 | #include "com_err.h" |
| 46 | #include "error_table.h" |
| 47 | |
| 48 | const char * |
| 49 | com_right(struct et_list *list, long code) |
| 50 | { |
| 51 | struct et_list *p; |
| 52 | for (p = list; p; p = p->next) { |
| 53 | if (code >= p->table->base && code < p->table->base + p->table->n_msgs) |
| 54 | return p->table->msgs[code - p->table->base]; |
| 55 | } |
| 56 | return NULL; |
| 57 | } |
| 58 | |
Theodore Ts'o | 1601b10 | 2010-03-14 19:34:53 -0400 | [diff] [blame] | 59 | const char * |
| 60 | com_right_r(struct et_list *list, long code, char *str, size_t len) |
| 61 | { |
| 62 | struct et_list *p; |
| 63 | for (p = list; p; p = p->next) { |
| 64 | if ((code >= p->table->base) && |
| 65 | (code < p->table->base + p->table->n_msgs)) { |
| 66 | strncpy(str, p->table->msgs[code - p->table->base], len); |
| 67 | str[len-1] = '\0'; |
| 68 | return str; |
| 69 | } |
| 70 | } |
| 71 | return NULL; |
| 72 | } |
| 73 | |
Theodore Ts'o | d035509 | 2003-05-05 23:07:32 -0400 | [diff] [blame] | 74 | struct foobar { |
| 75 | struct et_list etl; |
| 76 | struct error_table tab; |
| 77 | }; |
| 78 | |
| 79 | /* |
| 80 | * We provide this routine for compatibility with Heimdall generated |
| 81 | * foo_err.c files, but we don't use this ourselves for foo_err.c |
| 82 | * files generated by our compile_et. This is so our foo_err.c |
| 83 | * files can be used with older com_err libraries without running |
| 84 | * afoul of dependencies. |
| 85 | */ |
| 86 | void |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 87 | initialize_error_table_r(struct et_list **list, |
| 88 | const char **messages, |
Theodore Ts'o | d035509 | 2003-05-05 23:07:32 -0400 | [diff] [blame] | 89 | int num_errors, |
| 90 | long base) |
| 91 | { |
| 92 | struct et_list *et, **end; |
| 93 | struct error_table *tab; |
| 94 | struct foobar *f; |
| 95 | |
| 96 | for (end = list, et = *list; et; end = &et->next, et = et->next) |
| 97 | if (et->table->msgs == messages) |
| 98 | return; |
| 99 | f = malloc(sizeof(*f)); |
| 100 | if (f == NULL) |
| 101 | return; |
| 102 | et = &f->etl; |
| 103 | et->table = tab = &f->tab; |
| 104 | tab->msgs = messages; |
| 105 | tab->n_msgs = num_errors; |
| 106 | tab->base = base; |
| 107 | et->next = NULL; |
| 108 | *end = et; |
| 109 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 110 | |
Theodore Ts'o | d035509 | 2003-05-05 23:07:32 -0400 | [diff] [blame] | 111 | |
| 112 | void |
| 113 | free_error_table(struct et_list *et) |
| 114 | { |
| 115 | while(et){ |
| 116 | struct et_list *p = et; |
| 117 | et = et->next; |
| 118 | free(p); |
| 119 | } |
| 120 | } |