]>
git.saurik.com Git - apple/network_cmds.git/blob - revnetgroup.tproj/hash.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
24 /* $OpenBSD: hash.c,v 1.1 1997/04/15 22:06:11 maja Exp $ */
27 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by Bill Paul.
40 * 4. Neither the name of the author nor the names of any co-contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * $FreeBSD: hash.c,v 1.4 1997/02/22 14:22:01 peter Exp $
62 #include <sys/types.h>
66 static const char rcsid
[] = "$OpenBSD: hash.c,v 1.1 1997/04/15 22:06:11 maja Exp $";
70 * This hash function is stolen directly from the
71 * Berkeley DB package. It already exists inside libc, but
72 * it's declared static which prevents us from calling it
76 * OZ's original sdbm hash
83 register const u_char
*key
;
87 #define HASHC h = *key++ + 65599 * h
92 loop
= (len
+ 8 - 1) >> 3;
94 switch (len
& (8 - 1)) {
126 * Generate a hash value for a given key (character string).
127 * We mask off all but the lower 8 bits since our table array
128 * can only hold 256 elements.
130 u_int32_t
hashkey(key
)
136 return(hash((void *)key
, strlen(key
)) & HASH_MASK
);
139 /* Find an entry in the hash table (may be hanging off a linked list). */
140 char *lookup(table
, key
)
141 struct group_entry
*table
[];
144 struct group_entry
*cur
;
146 cur
= table
[hashkey(key
)];
149 if (!strcmp(cur
->key
, key
))
158 * Store an entry in the main netgroup hash table. Here's how this
159 * works: the table can only be so big when we initialize it (TABLESIZE)
160 * but the number of netgroups in the /etc/netgroup file could easily be
161 * much larger than the table. Since our hash values are adjusted to
162 * never be greater than TABLESIZE too, this means it won't be long before
163 * we find ourselves with two keys that hash to the same value.
165 * One way to deal with this is to malloc(2) a second table and start
166 * doing indirection, but this is a pain in the butt and it's not worth
167 * going to all that trouble for a dinky little program like this. Instead,
168 * we turn each table entry into a linked list and simply link keys
169 * with the same hash value together at the same index location within
172 * That's a lot of comment for such a small piece of code, isn't it.
174 void store (table
, key
, data
)
175 struct group_entry
*table
[];
178 struct group_entry
*new;
183 new = (struct group_entry
*)malloc(sizeof(struct group_entry
));
184 new->key
= strdup(key
);
185 new->data
= strdup(data
);
186 new->next
= table
[i
];
193 * Store a group member entry and/or update its grouplist. This is
194 * a bit more complicated than the previous function since we have to
195 * maintain not only the hash table of group members, each group member
196 * structure also has a linked list of groups hung off it. If handed
197 * a member name that we haven't encountered before, we have to do
198 * two things: add that member to the table (possibly hanging them
199 * off the end of a linked list, as above), and add a group name to
200 * the member's grouplist list. If we're handed a name that already has
201 * an entry in the table, then we just have to do one thing, which is
202 * to update its grouplist.
204 void mstore (table
, key
, data
, domain
)
205 struct member_entry
*table
[];
206 char *key
, *data
, *domain
;
208 struct member_entry
*cur
, *new;
209 struct grouplist
*tmp
,*p
;
215 tmp
= (struct grouplist
*)malloc(sizeof(struct grouplist
));
216 tmp
->groupname
= strdup(data
);
219 /* Check if all we have to do is insert a new groupname. */
221 if (!strcmp(cur
->key
, key
) && !strcmp(cur
->domain
,domain
)) {
224 if (!strcmp(p
->groupname
,data
))
228 tmp
->next
= cur
->groups
;
235 /* Didn't find a match -- add the whole mess to the table. */
236 new = (struct member_entry
*)malloc(sizeof(struct member_entry
));
237 new->key
= strdup(key
);
238 new->domain
= strdup(domain
);
240 new->next
= table
[i
];