1 /* $NetBSD: only.c,v 1.3 2017/09/07 04:04:13 nakayama Exp $ */
4 * Copyright (c) 2013 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of The NetBSD Foundation nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
34 #if HAVE_NBTOOL_CONFIG_H
35 #include "nbtool_config.h"
38 #include <sys/param.h>
56 static struct hentry *table[1024];
60 hash_str(const char *str)
62 const uint8_t *s = (const uint8_t *)str;
65 while ((c = *s++) != '\0')
66 hash = hash * 33 + c; /* "perl": k=33, r=r+r/32 */
67 return hash + (hash >> 5);
71 hash_find(const char *str, uint32_t *h)
74 *h = hash_str(str) % NELEM(table);
76 for (e = table[*h]; e; e = e->next)
77 if (e->hash == *h && strcmp(e->str, str) == 0)
83 hash_insert(char *str, uint32_t h)
88 if ((e = malloc(sizeof(*e))) == NULL)
89 mtree_err("memory allocation error");
90 if ((x = strdup(str)) == NULL)
91 mtree_err("memory allocation error");
103 char *ptr = strrchr(str, '/');
109 if (!hash_find(str, &h)) {
117 load_only(const char *fname)
123 if ((fp = fopen(fname, "r")) == NULL)
124 err(1, "Cannot open `%s'", fname);
126 while ((line = fparseln(fp, &len, &lineno, NULL, FPARSELN_UNESCALL))) {
128 if (hash_find(line, &h))
129 err(1, "Duplicate entry %s", line);
130 hash_insert(line, h);
140 find_only(const char *path)
146 return hash_find(path, &h);