#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct nlist {
struct nlist *next;
char *name;
char *defn;
};
#define HASHSIZE 101
static struct nlist *hashtab[HASHSIZE];
unsigned hash (char *s)
{
unsigned hashval;
for (hashval = 0; *s != '\0'; s++)
hashval += *s + 31 * hashval;
return hashval % HASHSIZE;
}
struct nlist * lookup (char *s)
{
struct nlist *np;
for (np = hashtab[hash(s)]; np != NULL; np = np->next)
return np;
return NULL;
}
struct nlist * install (char *name, char *defn)
{
struct nlist *np;
unsigned hashval;
if ((np = lookup(name)) == NULL)
{
np
= (struct nlist
*) malloc(sizeof(*np
)); if (np == NULL || (np->name = strdup(name)) == NULL)
return NULL;
hashval = hash(name);
np->next = hashtab[hashval];
hashtab[hashval] = np;
}
else
free ((void *) np
->defn
); if ((np->defn = strdup(defn)) == NULL)
return NULL;
return np;
}
void undef (char *name)
{
struct nlist *curr, *last;
unsigned hashval;
last = curr = hashtab[hash(name)];
while (curr
!= NULL
&& strcmp(name
, curr
->name
)!= 0) {
last = curr;
curr = curr->next;
printf("last = %x, curr = %x\n", last
, curr
); }
if (curr)
{
last->next = curr->next;
// !!!!!!!!
free ((void *) curr
->name
); free ((void *) curr
->defn
); }
}
int main (int argc, char *argv[])
{
printf ("%u\n", hash
("abcdef")); install ("H", "127");
printf ("%s\n", lookup
("H")->defn
);
undef ("H");
if (lookup("H") != NULL)
printf ("%s\n", lookup
("H")->defn
); }
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKCnN0cnVjdCBubGlzdCB7CiAgICAgICAgc3RydWN0IG5saXN0ICpuZXh0OwogICAgICAgIGNoYXIgKm5hbWU7CiAgICAgICAgY2hhciAqZGVmbjsKfTsKCiNkZWZpbmUgSEFTSFNJWkUgMTAxCnN0YXRpYyBzdHJ1Y3Qgbmxpc3QgKmhhc2h0YWJbSEFTSFNJWkVdOwoKdW5zaWduZWQgaGFzaCAoY2hhciAqcykKewogICAgICAgIHVuc2lnbmVkIGhhc2h2YWw7CgogICAgICAgIGZvciAoaGFzaHZhbCA9IDA7ICpzICE9ICdcMCc7IHMrKykKICAgICAgICAgICAgICAgIGhhc2h2YWwgKz0gKnMgKyAzMSAqIGhhc2h2YWw7CiAgICAgICAgcmV0dXJuIGhhc2h2YWwgJSBIQVNIU0laRTsKfQoKc3RydWN0IG5saXN0ICogbG9va3VwIChjaGFyICpzKQp7CiAgICAgICAgc3RydWN0IG5saXN0ICpucDsKCiAgICAgICAgZm9yIChucCA9IGhhc2h0YWJbaGFzaChzKV07IG5wICE9IE5VTEw7IG5wID0gbnAtPm5leHQpCiAgICAgICAgICAgICAgICBpZiAoc3RyY21wKHMsIG5wLT5uYW1lKSA9PSAwKQogICAgICAgICAgICAgICAgICAgICAgICByZXR1cm4gbnA7CiAgICAgICAgcmV0dXJuIE5VTEw7Cn0KCnN0cnVjdCBubGlzdCAqIGluc3RhbGwgKGNoYXIgKm5hbWUsIGNoYXIgKmRlZm4pCnsKICAgICAgICBzdHJ1Y3Qgbmxpc3QgKm5wOwogICAgICAgIHVuc2lnbmVkIGhhc2h2YWw7CgogICAgICAgIGlmICgobnAgPSBsb29rdXAobmFtZSkpID09IE5VTEwpCiAgICAgICAgewogICAgICAgICAgICAgICAgbnAgPSAoc3RydWN0IG5saXN0ICopIG1hbGxvYyhzaXplb2YoKm5wKSk7CiAgICAgICAgICAgICAgICBpZiAobnAgPT0gTlVMTCB8fCAobnAtPm5hbWUgPSBzdHJkdXAobmFtZSkpID09IE5VTEwpCiAgICAgICAgICAgICAgICAgICAgICAgIHJldHVybiBOVUxMOwogICAgICAgICAgICAgICAgaGFzaHZhbCA9IGhhc2gobmFtZSk7CiAgICAgICAgICAgICAgICBucC0+bmV4dCA9IGhhc2h0YWJbaGFzaHZhbF07CiAgICAgICAgICAgICAgICBoYXNodGFiW2hhc2h2YWxdID0gbnA7CiAgICAgICAgfQogICAgICAgIGVsc2UKICAgICAgICAgICAgICAgIGZyZWUgKCh2b2lkICopIG5wLT5kZWZuKTsKICAgICAgICAgICAgICAgIGlmICgobnAtPmRlZm4gPSBzdHJkdXAoZGVmbikpID09IE5VTEwpCiAgICAgICAgICAgICAgICAgICAgICAgIHJldHVybiBOVUxMOwogICAgICAgICAgICAgICAgcmV0dXJuIG5wOwp9Cgp2b2lkIHVuZGVmIChjaGFyICpuYW1lKQp7CiAgICAgICAgc3RydWN0IG5saXN0ICpjdXJyLCAqbGFzdDsKICAgICAgICB1bnNpZ25lZCBoYXNodmFsOwoKICAgICAgICBsYXN0ID0gY3VyciA9IGhhc2h0YWJbaGFzaChuYW1lKV07CiAgICAgICAgd2hpbGUgKGN1cnIgIT0gTlVMTCAmJiBzdHJjbXAobmFtZSwgY3Vyci0+bmFtZSkhPSAwKQogICAgICAgIHsKICAgICAgICAgICAgICAgIHByaW50Zigid2hpbGUgbG9vcFxuIik7CiAgICAgICAgICAgICAgICBsYXN0ID0gY3VycjsKICAgICAgICAgICAgICAgIGN1cnIgPSBjdXJyLT5uZXh0OwogICAgICAgICAgICAgICAgcHJpbnRmKCJsYXN0ID0gJXgsIGN1cnIgPSAleFxuIiwgbGFzdCwgY3Vycik7CiAgICAgICAgfQoKICAgICAgICBwcmludGYoImFmdGVyIGxvb3BcbiIpOwogICAgICAgIGlmIChjdXJyKQogICAgICAgIHsKICAgICAgICAgICAgICAgIGxhc3QtPm5leHQgPSBjdXJyLT5uZXh0OwogICAgICAgICAgICAgICAgLy8gISEhISEhISEKCQkJCWZyZWUgKCh2b2lkICopIGN1cnItPm5hbWUpOwoJCQkJZnJlZSAoKHZvaWQgKikgY3Vyci0+ZGVmbik7CgkJCQlmcmVlIChjdXJyKTsKICAgICAgICB9Cn0KCgppbnQgbWFpbiAoaW50IGFyZ2MsIGNoYXIgKmFyZ3ZbXSkKewogICAgICAgIHByaW50ZiAoIiV1XG4iLCBoYXNoKCJhYmNkZWYiKSk7CiAgICAgICAgaW5zdGFsbCAoIkgiLCAiMTI3Iik7CiAgICAgICAgcHJpbnRmICgiJXNcbiIsIGxvb2t1cCgiSCIpLT5kZWZuKTsKCiAgICAgICAgdW5kZWYgKCJIIik7CiAgICAgIGlmIChsb29rdXAoIkgiKSAhPSBOVUxMKQogICAgICAgICAgICAgIHByaW50ZiAoIiVzXG4iLCBsb29rdXAoIkgiKS0+ZGVmbik7Cn0K