void write_directory_file (void) { FILE *fp = listed_incremental_stream; char buf[UINTMAX_STRSIZE_BOUND]; char *s; if (! fp) return; if (fseeko (fp, 0L, SEEK_SET) != 0) seek_error (listed_incremental_option); if (sys_truncate (fileno (fp)) != 0) truncate_error (listed_incremental_option); fprintf (fp, "%s-%s-%d\n", PACKAGE_NAME, PACKAGE_VERSION, TAR_INCREMENTAL_VERSION); s = (TYPE_SIGNED (time_t) ? imaxtostr (start_time.tv_sec, buf) : umaxtostr (start_time.tv_sec, buf)); fwrite (s, strlen (s) + 1, 1, fp); s = umaxtostr (start_time.tv_nsec, buf); fwrite (s, strlen (s) + 1, 1, fp); if (! ferror (fp) && directory_table) hash_do_for_each (directory_table, write_directory_file_entry, fp); if (ferror (fp)) write_error (listed_incremental_option); if (fclose (fp) != 0) close_error (listed_incremental_option); }
int run_list_events (const char *cmd, size_t argc, char *argv[]) { if (argc != 0) { fprintf (stderr, _("use 'list-events' to list event handlers\n")); return -1; } hash_do_for_each (event_handlers, list_event, NULL); return 0; }
static void gen_remove_all_expired (Hash_table *ht, Hash_data_freer freer, time_t now) { struct gen_remove_data data; data.now = now; data.ht = ht; data.freer = freer; /* Careful reading of the documentation to hash _seems_ to indicate * that this is safe, _provided_ we use the default thresholds (in * particular, no shrink threshold). */ hash_do_for_each (ht, gen_remove_if_expired, &data); }
int main (int argc, char **argv) { unsigned int i; unsigned int k; unsigned int table_size[] = {1, 2, 3, 4, 5, 23, 53}; Hash_table *ht; Hash_tuning tuning; hash_reset_tuning (&tuning); tuning.shrink_threshold = 0.3; tuning.shrink_factor = 0.707; tuning.growth_threshold = 1.5; tuning.growth_factor = 2.0; tuning.is_n_buckets = true; if (1 < argc) { unsigned int seed; if (get_seed (argv[1], &seed) != 0) { fprintf (stderr, "invalid seed: %s\n", argv[1]); exit (EXIT_FAILURE); } srand (seed); } for (i = 0; i < ARRAY_CARDINALITY (table_size); i++) { size_t sz = table_size[i]; ht = hash_initialize (sz, NULL, hash_pjw, hash_compare_strings, NULL); ASSERT (ht); insert_new (ht, "a"); { char *str1 = strdup ("a"); char *str2; ASSERT (str1); str2 = hash_insert (ht, str1); ASSERT (str1 != str2); ASSERT (STREQ (str1, str2)); free (str1); } insert_new (ht, "b"); insert_new (ht, "c"); i = 0; ASSERT (hash_do_for_each (ht, walk, &i) == 3); ASSERT (i == 7); { void *buf[5] = { NULL }; ASSERT (hash_get_entries (ht, NULL, 0) == 0); ASSERT (hash_get_entries (ht, buf, 5) == 3); ASSERT (STREQ (buf[0], "a") || STREQ (buf[0], "b") || STREQ (buf[0], "c")); } ASSERT (hash_delete (ht, "a")); ASSERT (hash_delete (ht, "a") == NULL); ASSERT (hash_delete (ht, "b")); ASSERT (hash_delete (ht, "c")); ASSERT (hash_rehash (ht, 47)); ASSERT (hash_rehash (ht, 467)); /* Free an empty table. */ hash_clear (ht); hash_free (ht); ht = hash_initialize (sz, NULL, hash_pjw, hash_compare_strings, NULL); ASSERT (ht); insert_new (ht, "z"); insert_new (ht, "y"); insert_new (ht, "x"); insert_new (ht, "w"); insert_new (ht, "v"); insert_new (ht, "u"); hash_clear (ht); ASSERT (hash_get_n_entries (ht) == 0); hash_free (ht); /* Test pointer hashing. */ ht = hash_initialize (sz, NULL, NULL, NULL, NULL); ASSERT (ht); { char *str = strdup ("a"); ASSERT (str); insert_new (ht, "a"); insert_new (ht, str); ASSERT (hash_lookup (ht, str) == str); free (str); } hash_free (ht); } hash_reset_tuning (&tuning); tuning.shrink_threshold = 0.3; tuning.shrink_factor = 0.707; tuning.growth_threshold = 1.5; tuning.growth_factor = 2.0; tuning.is_n_buckets = true; /* Invalid tuning. */ ht = hash_initialize (4651, &tuning, hash_pjw, hash_compare_strings, hash_freer); ASSERT (!ht); /* Alternate tuning. */ tuning.growth_threshold = 0.89; /* Run with default tuning, then with custom tuning settings. */ for (k = 0; k < 2; k++) { Hash_tuning const *tune = (k == 0 ? NULL : &tuning); /* Now, each entry is malloc'd. */ ht = hash_initialize (4651, tune, hash_pjw, hash_compare_strings, hash_freer); ASSERT (ht); for (i = 0; i < 10000; i++) { unsigned int op = rand () % 10; switch (op) { case 0: case 1: case 2: case 3: case 4: case 5: { char buf[50]; char const *p = uinttostr (i, buf); char *p_dup = strdup (p); ASSERT (p_dup); insert_new (ht, p_dup); } break; case 6: { size_t n = hash_get_n_entries (ht); ASSERT (hash_rehash (ht, n + rand () % 20)); } break; case 7: { size_t n = hash_get_n_entries (ht); size_t delta = rand () % 20; if (delta < n) ASSERT (hash_rehash (ht, n - delta)); } break; case 8: case 9: { /* Delete a random entry. */ size_t n = hash_get_n_entries (ht); if (n) { size_t kk = rand () % n; void const *p; void *v; for (p = hash_get_first (ht); kk; --kk, p = hash_get_next (ht, p)) { /* empty */ } ASSERT (p); v = hash_delete (ht, p); ASSERT (v); free (v); } break; } } ASSERT (hash_table_ok (ht)); } hash_free (ht); } return 0; }
void muscles_m4_output (FILE *out) { hash_do_for_each (muscle_table, muscle_m4_output_processor, out); }