+ return result;
+}
+#endif
+
+static bool
+is_database(const char *path)
+{
+ bool result = FALSE;
+#if USE_DATABASE
+ if (_nc_is_dir_path(path) && access(path, R_OK | X_OK) == 0) {
+ result = TRUE;
+ }
+#endif
+#if USE_TERMCAP
+ if (_nc_is_file_path(path) && access(path, R_OK) == 0) {
+ result = TRUE;
+ }
+#endif
+#if USE_HASHED_DB
+ if (!result) {
+ char filename[PATH_MAX];
+ if (_nc_is_file_path(path) && access(path, R_OK) == 0) {
+ result = TRUE;
+ } else if (make_db_name(filename, path, sizeof(filename))) {
+ if (_nc_is_file_path(filename) && access(filename, R_OK) == 0) {
+ result = TRUE;
+ }
+ }
+ }
+#endif
+ return result;
+}
+
+static void
+deschook(const char *cn, TERMTYPE *tp)
+/* display a description for the type */
+{
+ const char *desc;
+
+ if ((desc = strrchr(tp->term_names, '|')) == 0 || *++desc == '\0')
+ desc = "(No description)";
+
+ (void) printf("%-10s\t%s\n", cn, desc);
+}
+
+#if USE_TERMCAP
+static void
+show_termcap(char *buffer,
+ void (*hook) (const char *, TERMTYPE *tp))
+{
+ TERMTYPE data;
+ char *next = strchr(buffer, ':');
+ char *last;
+ char *list = buffer;
+
+ if (next)
+ *next = '\0';
+
+ last = strrchr(buffer, '|');
+ if (last)
+ ++last;
+
+ data.term_names = strdup(buffer);
+ while ((next = strtok(list, "|")) != 0) {
+ if (next != last)
+ hook(next, &data);
+ list = 0;
+ }
+ free(data.term_names);
+}
+#endif
+
+static int
+typelist(int eargc, char *eargv[],
+ bool verbosity,
+ void (*hook) (const char *, TERMTYPE *tp))
+/* apply a function to each entry in given terminfo directories */
+{
+ int i;
+
+ for (i = 0; i < eargc; i++) {
+#if USE_DATABASE
+ if (_nc_is_dir_path(eargv[i])) {
+ char *cwd_buf = 0;
+ DIR *termdir;
+ DIRENT *subdir;
+
+ if ((termdir = opendir(eargv[i])) == 0) {
+ (void) fflush(stdout);
+ (void) fprintf(stderr,
+ "%s: can't open terminfo directory %s\n",
+ _nc_progname, eargv[i]);
+ return (EXIT_FAILURE);
+ } else if (verbosity)
+ (void) printf("#\n#%s:\n#\n", eargv[i]);
+
+ while ((subdir = readdir(termdir)) != 0) {
+ size_t len = NAMLEN(subdir);
+ size_t cwd_len = len + strlen(eargv[i]) + 3;
+ char name_1[PATH_MAX];
+ DIR *entrydir;
+ DIRENT *entry;
+
+ cwd_buf = typeRealloc(char, cwd_len, cwd_buf);
+ if (cwd_buf == 0) {
+ perror("realloc cwd_buf");
+ continue;
+ }
+
+ strncpy(name_1, subdir->d_name, len)[len] = '\0';
+ if (isDotname(name_1))
+ continue;
+
+ (void) sprintf(cwd_buf, "%s/%.*s/", eargv[i], (int) len, name_1);
+ if (chdir(cwd_buf) != 0)
+ continue;
+
+ entrydir = opendir(".");
+ if (entrydir == 0) {
+ perror(cwd_buf);
+ continue;
+ }
+ while ((entry = readdir(entrydir)) != 0) {
+ char name_2[PATH_MAX];
+ TERMTYPE lterm;
+ char *cn;
+ int status;
+
+ len = NAMLEN(entry);
+ strncpy(name_2, entry->d_name, len)[len] = '\0';
+ if (isDotname(name_2) || !_nc_is_file_path(name_2))
+ continue;
+
+ status = _nc_read_file_entry(name_2, <erm);
+ if (status <= 0) {
+ (void) fflush(stdout);
+ (void) fprintf(stderr,
+ "%s: couldn't open terminfo file %s.\n",
+ _nc_progname, name_2);
+ return (EXIT_FAILURE);
+ }
+
+ /* only visit things once, by primary name */
+ cn = _nc_first_name(lterm.term_names);
+ if (!strcmp(cn, name_2)) {
+ /* apply the selected hook function */
+ (*hook) (cn, <erm);
+ }
+ _nc_free_termtype(<erm);
+ }
+ closedir(entrydir);
+ }
+ closedir(termdir);
+ if (cwd_buf != 0)
+ free(cwd_buf);
+ }
+#if USE_HASHED_DB
+ else {
+ DB *capdbp;
+ char filename[PATH_MAX];
+
+ if (make_db_name(filename, eargv[i], sizeof(filename))) {
+ if ((capdbp = _nc_db_open(filename, FALSE)) != 0) {
+ DBT key, data;
+ int code;
+
+ code = _nc_db_first(capdbp, &key, &data);
+ while (code == 0) {
+ TERMTYPE lterm;
+ int used;
+ char *have;
+ char *cn;
+
+ if (_nc_db_have_data(&key, &data, &have, &used)) {
+ if (_nc_read_termtype(<erm, have, used) > 0) {
+ /* only visit things once, by primary name */
+ cn = _nc_first_name(lterm.term_names);
+ /* apply the selected hook function */
+ (*hook) (cn, <erm);
+ _nc_free_termtype(<erm);
+ }
+ }
+ code = _nc_db_next(capdbp, &key, &data);
+ }
+
+ _nc_db_close(capdbp);
+ }
+ }
+ }
+#endif
+#endif
+#if USE_TERMCAP
+#if HAVE_BSD_CGETENT
+ char *db_array[2];
+ char *buffer = 0;
+
+ if (verbosity)
+ (void) printf("#\n#%s:\n#\n", eargv[i]);
+
+ db_array[0] = eargv[i];
+ db_array[1] = 0;
+
+ if (cgetfirst(&buffer, db_array)) {
+ show_termcap(buffer, hook);
+ free(buffer);
+ while (cgetnext(&buffer, db_array)) {
+ show_termcap(buffer, hook);
+ free(buffer);
+ }
+ }
+ cgetclose();
+#else
+ /* scan termcap text-file only */
+ if (_nc_is_file_path(eargv[i])) {
+ char buffer[2048];
+ FILE *fp;
+
+ if ((fp = fopen(eargv[i], "r")) != 0) {
+ while (fgets(buffer, sizeof(buffer), fp) != 0) {
+ if (*buffer == '#')
+ continue;
+ if (isspace(*buffer))
+ continue;
+ show_termcap(buffer, hook);
+ }
+ fclose(fp);
+ }
+ }
+#endif
+#endif
+ }
+
+ return (EXIT_SUCCESS);
+}
+
+static void
+usage(void)
+{
+ (void) fprintf(stderr, "usage: %s [-ahuUV] [-v n] [file...]\n", _nc_progname);
+ ExitProgram(EXIT_FAILURE);