+ char **result = typeCalloc(char *, count + 1);
+ if (result == 0)
+ failed("realloc eargv");
+
+ assert(result != 0);
+ return result;
+}
+
+static void
+freeArgv(char **argv)
+{
+ if (argv) {
+ int count = 0;
+ while (argv[count]) {
+ free(argv[count++]);
+ }
+ free(argv);
+ }
+}
+
+#if USE_HASHED_DB
+static bool
+make_db_name(char *dst, const char *src, unsigned limit)
+{
+ static const char suffix[] = DBM_SUFFIX;
+
+ bool result = FALSE;
+ size_t lens = sizeof(suffix) - 1;
+ size_t size = strlen(src);
+ size_t need = lens + size;
+
+ if (need <= limit) {
+ if (size >= lens
+ && !strcmp(src + size - lens, suffix)) {
+ _nc_STRCPY(dst, src, PATH_MAX);
+ } else {
+ _nc_SPRINTF(dst, _nc_SLIMIT(PATH_MAX) "%s%s", src, suffix);
+ }
+ result = TRUE;
+ }
+ return result;
+}
+#endif
+
+typedef void (DescHook) (int /* db_index */ ,
+ int /* db_limit */ ,
+ const char * /* term_name */ ,
+ TERMTYPE * /* term */ );
+
+static const char *
+term_description(TERMTYPE *tp)
+{
+ const char *desc;
+
+ if (tp->term_names == 0
+ || (desc = strrchr(tp->term_names, '|')) == 0
+ || (*++desc == '\0')) {
+ desc = "(No description)";
+ }
+
+ return desc;
+}
+
+/* display a description for the type */
+static void
+deschook(int db_index, int db_limit, const char *term_name, TERMTYPE *tp)
+{
+ (void) db_index;
+ (void) db_limit;
+ (void) printf("%-10s\t%s\n", term_name, term_description(tp));
+}
+
+static unsigned long
+string_sum(const char *value)
+{
+ unsigned long result = 0;
+
+ if ((intptr_t) value == (intptr_t) (-1)) {
+ result = ~result;
+ } else if (value) {
+ while (*value) {
+ result += UChar(*value);
+ ++value;
+ }
+ }
+ return result;
+}
+
+static unsigned long
+checksum_of(TERMTYPE *tp)
+{
+ unsigned long result = string_sum(tp->term_names);
+ unsigned i;
+
+ for (i = 0; i < NUM_BOOLEANS(tp); i++) {
+ result += (unsigned long) (tp->Booleans[i]);
+ }
+ for (i = 0; i < NUM_NUMBERS(tp); i++) {
+ result += (unsigned long) (tp->Numbers[i]);
+ }
+ for (i = 0; i < NUM_STRINGS(tp); i++) {
+ result += string_sum(tp->Strings[i]);
+ }
+ return result;
+}
+
+/* collect data, to sort before display */
+static void
+sorthook(int db_index, int db_limit, const char *term_name, TERMTYPE *tp)
+{
+ TERMDATA *data = new_termdata();
+
+ data->db_index = db_index;
+ data->checksum = ((db_limit > 1) ? checksum_of(tp) : 0);
+ data->term_name = strmalloc(term_name);
+ data->description = strmalloc(term_description(tp));
+}
+
+#if NCURSES_USE_TERMCAP
+static void
+show_termcap(int db_index, int db_limit, char *buffer, DescHook hook)
+{
+ TERMTYPE data;
+ char *next = strchr(buffer, ':');
+ char *last;
+ char *list = buffer;
+
+ if (next)
+ *next = '\0';
+
+ last = strrchr(buffer, '|');
+ if (last)
+ ++last;
+
+ memset(&data, 0, sizeof(data));
+ data.term_names = strmalloc(buffer);
+ while ((next = strtok(list, "|")) != 0) {
+ if (next != last)
+ hook(db_index, db_limit, next, &data);
+ list = 0;
+ }
+ free(data.term_names);
+}
+#endif
+
+#if NCURSES_USE_DATABASE
+static char *
+copy_entryname(DIRENT * src)
+{
+ size_t len = NAMLEN(src);
+ char *result = malloc(len + 1);
+ if (result == 0)
+ failed("copy entryname");
+ memcpy(result, src->d_name, len);
+ result[len] = '\0';
+
+ return result;