1/* $NetBSD$ */ 2 3/* 4 * Copyright (c) 1997-2014 Erez Zadok 5 * Copyright (c) 1989 Jan-Simon Pendry 6 * Copyright (c) 1989 Imperial College of Science, Technology & Medicine 7 * Copyright (c) 1989 The Regents of the University of California. 8 * All rights reserved. 9 * 10 * This code is derived from software contributed to Berkeley by 11 * Jan-Simon Pendry at Imperial College, London. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * 38 * File: am-utils/fsinfo/fsi_dict.c 39 * 40 */ 41 42/* 43 * Dictionary support 44 */ 45 46#ifdef HAVE_CONFIG_H 47# include <config.h> 48#endif /* HAVE_CONFIG_H */ 49#include <am_defs.h> 50#include <fsi_data.h> 51#include <fsinfo.h> 52 53 54static int 55dict_hash(char *k) 56{ 57 u_int h; 58 59 for (h = 0; *k; h += *k++) ; 60 return h % DICTHASH; 61} 62 63 64dict * 65new_dict(void) 66{ 67 dict *dp = CALLOC(struct dict); 68 69 return dp; 70} 71 72 73static void 74dict_add_data(dict_ent *de, char *v) 75{ 76 dict_data *dd = CALLOC(struct dict_data); 77 78 dd->dd_data = v; 79 ins_que(&dd->dd_q, de->de_q.q_back); 80 de->de_count++; 81} 82 83 84static dict_ent * 85new_dict_ent(char *k) 86{ 87 dict_ent *de = CALLOC(struct dict_ent); 88 89 de->de_key = k; 90 init_que(&de->de_q); 91 return de; 92} 93 94 95dict_ent * 96dict_locate(dict *dp, char *k) 97{ 98 dict_ent *de = dp->de[dict_hash(k)]; 99 100 while (de && !STREQ(de->de_key, k)) 101 de = de->de_next; 102 return de; 103} 104 105 106void 107dict_add(dict *dp, char *k, char *v) 108{ 109 dict_ent *de = dict_locate(dp, k); 110 111 if (!de) { 112 dict_ent **dep = &dp->de[dict_hash(k)]; 113 de = new_dict_ent(k); 114 de->de_next = *dep; 115 *dep = de; 116 } 117 dict_add_data(de, v); 118} 119 120 121int 122dict_iter(dict *dp, int (*fn) (qelem *)) 123{ 124 int i; 125 int errors = 0; 126 127 for (i = 0; i < DICTHASH; i++) { 128 dict_ent *de = dp->de[i]; 129 while (de) { 130 errors += (*fn) (&de->de_q); 131 de = de->de_next; 132 } 133 } 134 return errors; 135} 136