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