1 /* $NetBSD: only.c,v 1.2 2013/02/05 00:59:03 christos 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/cdefs.h>
40 #if defined(__RCSID) && !defined(lint)
41 __RCSID("$NetBSD: only.c,v 1.2 2013/02/05 00:59:03 christos Exp $");
44 #include <sys/param.h>
62 static struct hentry
*table
[1024];
66 hash_str(const char *str
)
68 const uint8_t *s
= (const uint8_t *)str
;
71 while ((c
= *s
++) != '\0')
72 hash
= hash
* 33 + c
; /* "perl": k=33, r=r+r/32 */
73 return hash
+ (hash
>> 5);
77 hash_find(const char *str
, uint32_t *h
)
80 *h
= hash_str(str
) % __arraycount(table
);
82 for (e
= table
[*h
]; e
; e
= e
->next
)
83 if (e
->hash
== *h
&& strcmp(e
->str
, str
) == 0)
89 hash_insert(char *str
, uint32_t h
)
93 if ((e
= malloc(sizeof(*e
))) == NULL
)
94 mtree_err("memory allocation error");
106 char *ptr
= strrchr(str
, '/');
112 if (!hash_find(str
, &h
)) {
113 char *x
= strdup(str
);
115 mtree_err("memory allocation error");
123 load_only(const char *fname
)
129 if ((fp
= fopen(fname
, "r")) == NULL
)
130 err(1, "Cannot open `%s'", fname
);
132 while ((line
= fparseln(fp
, &len
, &lineno
, NULL
, FPARSELN_UNESCALL
))) {
134 if (hash_find(line
, &h
))
135 err(1, "Duplicate entry %s", line
);
136 hash_insert(line
, h
);
145 find_only(const char *path
)
151 return hash_find(path
, &h
);