static void list_keys(const char *ring_name, key_serial_t ring_id) { key_serial_t *key; void *keylist; int count; count = keyctl_read_alloc(ring_id, &keylist); if (count < 0) { xlog_err("Failed to read keyring %s: %m", ring_name); return; } count /= (int)sizeof(*key); switch (count) { case 0: printf("No %s keys found.\n", ring_name); break; case 1: printf("1 %s key found:\n", ring_name); break; default: printf("%u %s keys found:\n", count, ring_name); } for (key = keylist; count--; key++) list_key(*key); free(keylist); }
//find and print all items between k1 and k2, inclusive void find2(CHTbl *htbl, void *k1, void *k2) { ListElmt **fulllist = malloc(sizeof(ListElmt)*(htbl->size)); ListElmt *element; int i = 0; int j = 0; for(i = 0; i < htbl->buckets; ++i) { for(element = list_head(&htbl->table[i]); element != NULL; element = list_next(element)) { if((htbl->match(list_key(element), k1) > -1) && htbl->match(list_key(element), k2) < 1) fulllist[j++] = element; } } qsort(fulllist, j, sizeof(ListElmt*), elemcompare); for(i = 0; i < j; ++i) printf("%s\n%s\n\n", (char *)fulllist[i]->key, (char *)fulllist[i]->data); free(fulllist); }
//function to remove an item with the key "key" from the hash table int htbl_remove(CHTbl *htbl, void *key) { List *clist; ListElmt *element, *parent = NULL; int bucket; void *nkey, *data; bucket = htbl->h(key)%htbl->buckets; clist = &htbl->table[bucket]; for(element = list_head(clist); element != NULL; element = list_next(element)) { if(!htbl->match(key, list_key(element))) { list_rem_next(clist, parent, (const void **) &nkey, (const void **)&data); clist->destroy(nkey, data); htbl->size--; return 0; } parent = element; } return -1; }