int main(int argc, char *argv[]) { struct rooted_tree *tree; struct hash *rename_map; struct parameters params; params = get_params(argc, argv); rename_map = read_map(params.map_filename); while (NULL != (tree = parse_tree())) { process_tree(tree, rename_map, params); destroy_tree(tree, DONT_FREE_NODE_DATA); } struct llist *keys = hash_keys(rename_map); if (NULL == keys) { perror(NULL); exit(EXIT_FAILURE); } struct list_elem *e; for (e = keys->head; NULL != e; e = e->next) { char *key = (char *) e->data; char *val = hash_get(rename_map, key); free(val); } destroy_llist(keys); destroy_hash(rename_map); return 0; }
static errno_t sbus_server_bus_list_names(TALLOC_CTX *mem_ctx, struct sbus_request *sbus_req, struct sbus_server *server, const char ***_names) { hash_key_t *keys; const char **names; unsigned long count; unsigned long i; int hret; hret = hash_keys(server->names, &count, &keys); if (hret != HASH_SUCCESS) { return ENOMEM; } names = talloc_zero_array(mem_ctx, const char *, count + 2); if (names == NULL) { talloc_free(keys); return ENOMEM; } names[0] = DBUS_SERVICE_DBUS; for (i = 1; i < count + 1; i++) { names[i] = keys[i - 1].str; } *_names = names; talloc_free(keys); return EOK; }
errno_t autofs_orphan_maps(struct autofs_ctx *actx) { int hret; unsigned long mcount; unsigned long i; hash_key_t *maps; if (!actx || !actx->maps) { return EINVAL; } hret = hash_keys(actx->maps, &mcount, &maps); if (hret != HASH_SUCCESS) { return EIO; } for (i = 0; i < mcount; i++) { hret = hash_delete(actx->maps, &maps[i]); if (hret != HASH_SUCCESS) { DEBUG(SSSDBG_MINOR_FAILURE, "Could not delete key from hash\n"); continue; } } return EOK; }
static void sbus_signal_name_owner_changed(struct sbus_incoming_signal *a_signal, void *handler_data) { hash_table_t *table = a_signal->conn->clients; hash_key_t *keys; unsigned long count; unsigned long i; int hret; DEBUG(SSSDBG_TRACE_FUNC, "Clearing UIDs cache\n"); hret = hash_keys(table, &count, &keys); if (hret != HASH_SUCCESS) { DEBUG(SSSDBG_OP_FAILURE, "Unable to get hash keys\n"); return; } for (i = 0; i < count; i++) { hret = hash_delete(table, &keys[i]); if (hret != HASH_SUCCESS) { DEBUG(SSSDBG_MINOR_FAILURE, "Could not delete key from hash\n"); return; } } return; }
const char * sky_find(Hash_table *hp, const char **keys) { const char **keysp = keys; if (ids) { free(ids); ids = NULL; } while (*keysp) { struct reftab *rp = _sky_find(hp,*keysp); if (!rp) return NULL; if (rp->type == rt_singleton) return rp->c.id; if (ids) { const char **destid,**nextid; for (destid = nextid = ids; *nextid; ++nextid) { const char *ok = hash_find(rp->c.hash,(unsigned char *)*nextid); if (ok) *destid++ = *nextid; } if (destid > ids) *destid = NULL; else return NULL; } else ids = hash_keys(rp->c.hash); ++keysp; } if (ids) { if (ids[0] && !ids[1]) return ids[0]; else return (const char *)1; } else return NULL; }
static errno_t sbus_router_reset_listeners(struct sbus_connection *conn) { TALLOC_CTX *tmp_ctx; hash_key_t *keys; char *interface; char *name; unsigned long count; unsigned long i; errno_t ret; int hret; tmp_ctx = talloc_new(NULL); if (tmp_ctx == NULL) { DEBUG(SSSDBG_FATAL_FAILURE, "Out of memory!\n"); return ENOMEM; } hret = hash_keys(conn->router->listeners, &count, &keys); if (hret != HASH_SUCCESS) { ret = ENOMEM; goto done; } talloc_steal(tmp_ctx, keys); for (i = 0; i < count; i++) { ret = sbus_router_signal_parse(tmp_ctx, keys[i].str, &interface, &name); if (ret != EOK) { goto done; } sbus_router_signal_match(conn->router, conn->connection, interface, name); } ret = EOK; done: talloc_free(tmp_ctx); return ret; }
Hash *frame_create(List *args, List *values){ Hash *frame = create_empty_frame(); while (args != NULL && values != NULL){ frame_add(frame, args->car, values->car); args = args->cdr; values = values->cdr; } die_unless(args == NULL && values == NULL, "Arg names and values have different lengths"); return frame; } Hash *copy_frame(Hash *frame){ Hash *copy = create_empty_frame(); void add_key_value(void *key){ void *value = hash_value(frame, key); frame_add(copy, key, value); } List *keys = hash_keys(frame); list_for_each(keys, add_key_value); free_list(keys, nop_free_fn); return copy; } void free_frame(Hash *frame){ free_hash(frame, free_key_value_pair); }
static char * build_filter(TALLOC_CTX *mem_ctx, struct sysdb_ctx *sysdb, hash_table_t *table, struct sdap_attr_map *map, ipa_sudo_conv_rdn_fn rdn_fn) { TALLOC_CTX *tmp_ctx; hash_key_t *keys; unsigned long int count; unsigned long int i; char *filter; char *rdn_val; const char *rdn_attr; char *safe_rdn; errno_t ret; int hret; tmp_ctx = talloc_new(NULL); if (tmp_ctx == NULL) { return NULL; } hret = hash_keys(table, &count, &keys); if (hret != HASH_SUCCESS) { ret = ENOMEM; goto done; } talloc_steal(tmp_ctx, keys); filter = talloc_strdup(tmp_ctx, ""); if (filter == NULL) { ret = ENOMEM; goto done; } for (i = 0; i < count; i++) { ret = rdn_fn(tmp_ctx, map, sysdb, keys[i].str, &rdn_val, &rdn_attr); if (ret != EOK) { DEBUG(SSSDBG_CRIT_FAILURE, "Unable to get member %s [%d]: %s\n", keys[i].str, ret, sss_strerror(ret)); goto done; } ret = sss_filter_sanitize(tmp_ctx, rdn_val, &safe_rdn); if (ret != EOK) { DEBUG(SSSDBG_CRIT_FAILURE, "Unable to sanitize DN " "[%d]: %s\n", ret, sss_strerror(ret)); goto done; } filter = talloc_asprintf_append(filter, "(%s=%s)", rdn_attr, safe_rdn); if (filter == NULL) { ret = ENOMEM; goto done; } } /* objectClass is always first */ filter = talloc_asprintf(filter, "(&(objectClass=%s)(|%s))", map[0].name, filter); if (filter == NULL) { ret = ENOMEM; goto done; } talloc_steal(mem_ctx, filter); ret = EOK; done: talloc_free(tmp_ctx); if (ret != EOK) { return NULL; } return filter; }
int main(int argc, char **argv) { static hash_table_t *table = NULL; hash_key_t key, *keys; hash_value_t value; struct hash_iter_context_t *iter; hash_entry_t *entry; unsigned long i, n_entries; int error; struct my_data_t *my_data = new_data(1024, "Hello World!"); unsigned long count; /* Create a hash table */ error = hash_create(10, &table, delete_callback, NULL); if (error != HASH_SUCCESS) { fprintf(stderr, "cannot create hash table (%s)\n", hash_error_string(error)); return error; } /* Enter a key named "My Data" and specify it's value as a pointer to my_data */ key.type = HASH_KEY_STRING; key.str = strdup("My Data"); value.type = HASH_VALUE_PTR; value.ptr = my_data; if ((error = hash_enter(table, &key, &value)) != HASH_SUCCESS) { fprintf(stderr, "cannot add to table \"%s\" (%s)\n", key.str, hash_error_string(error)); return error; } free(key.str); /* Get a list of keys and print them out, free the list when we're done */ if ((error = hash_keys(table, &count, &keys)) != HASH_SUCCESS) { fprintf(stderr, "cannot get key list (%s)\n", hash_error_string(error)); return error; } for (i = 0; i < count; i++) printf("key: %s\n", keys[i].str); free(keys); /* Lookup the key named "My Data" */ key.type = HASH_KEY_STRING; key.str = strdup("My Data"); if ((error = hash_lookup(table, &key, &value)) != HASH_SUCCESS) { fprintf(stderr, "cannot find key \"%s\" (%s)\n", key.str, hash_error_string(error)); } free(key.str); /* Visit each entry in the table, callback will increment count on each visit */ printf("Iterate using callback\n"); count = 0; hash_iterate(table, visit_callback, &count); /* Assure number of visits equal the table size */ assert(count == hash_count(table)); /* Visit each entry using iterator object */ printf("Iterate using iterator\n"); n_entries = 0; iter = new_hash_iter_context(table); while ((entry = iter->next(iter)) != NULL) { struct my_data_t *data = (struct my_data_t *) entry->value.ptr; printf("%s = [foo=%d bar=%s]\n", entry->key.str, data->foo, data->bar); n_entries++; } free(iter); /* Assure number of visits equal the table size */ assert(n_entries == hash_count(table)); /* Remove the entry, deletion callback will be invoked */ key.type = HASH_KEY_STRING; key.str = strdup("My Data"); if ((error = hash_delete(table, &key)) != HASH_SUCCESS) { fprintf(stderr, "cannot delete from table (%s)\n", hash_error_string(error)); } /* Assure key is no longer in table */ assert (!hash_has_key(table, &key)); free(key.str); /* Free the table */ hash_destroy(table); return 0; }