+/*
+ * Build a terminfo pathname and try to read the data. Returns TGETENT_YES on
+ * success, TGETENT_NO on failure.
+ */
+static int
+_nc_read_tic_entry(char *filename,
+ unsigned limit,
+ const char *const path,
+ const char *name,
+ TERMTYPE *const tp)
+{
+ int code = TGETENT_NO;
+#if USE_HASHED_DB
+ DB *capdbp;
+#endif
+ char buffer[MAX_ENTRY_SIZE + 1];
+ int used;
+
+ TR(TRACE_DATABASE,
+ (T_CALLED("_nc_read_tic_entry(file=%p, path=%s, name=%s)"),
+ filename, path, name));
+
+ if ((used = decode_quickdump(buffer, path)) != 0
+ && (code = _nc_read_termtype(tp, buffer, used)) == TGETENT_YES
+ && _nc_name_match(tp->term_names, name, "|")) {
+ TR(TRACE_DATABASE, ("loaded quick-dump for %s", name));
+ } else
+#if USE_HASHED_DB
+ if (make_db_filename(filename, limit, path)
+ && (capdbp = _nc_db_open(filename, FALSE)) != 0) {
+
+ DBT key, data;
+ int reccnt = 0;
+ char *save = strdup(name);
+
+ memset(&key, 0, sizeof(key));
+ key.data = save;
+ key.size = strlen(save);
+
+ /*
+ * This lookup could return termcap data, which we do not want. We are
+ * looking for compiled (binary) terminfo data.
+ *
+ * cgetent uses a two-level lookup. On the first it uses the given
+ * name to return a record containing only the aliases for an entry.
+ * On the second (using that list of aliases as a key), it returns the
+ * content of the terminal description. We expect second lookup to
+ * return data beginning with the same set of aliases.
+ *
+ * For compiled terminfo, the list of aliases in the second case will
+ * be null-terminated. A termcap entry will not be, and will run on
+ * into the description. So we can easily distinguish between the two
+ * (source/binary) by checking the lengths.
+ */
+ while (_nc_db_get(capdbp, &key, &data) == 0) {
+ char *have = (char *) data.data;
+ used = (int) data.size - 1;
+
+ if (*have++ == 0) {
+ if (data.size > key.size
+ && IS_TIC_MAGIC(have)) {
+ code = _nc_read_termtype(tp, have, used);
+ if (code == TGETENT_NO) {
+ _nc_free_termtype(tp);
+ }
+ }
+ break;
+ }
+
+ /*
+ * Just in case we have a corrupt database, do not waste time with
+ * it.
+ */
+ if (++reccnt >= 3)
+ break;
+
+ /*
+ * Prepare for the second level.
+ */
+ key.data = have;
+ key.size = used;
+ }
+
+ free(save);
+ } else /* may be either filesystem or flat file */
+#endif
+ if (make_dir_filename(filename, limit, path, name)) {
+ code = _nc_read_file_entry(filename, tp);
+ }
+#if NCURSES_USE_TERMCAP
+ else if (code != TGETENT_YES) {
+ code = _nc_read_termcap_entry(name, tp);
+ _nc_SPRINTF(filename, _nc_SLIMIT(PATH_MAX)
+ "%.*s", PATH_MAX - 1, _nc_get_source());
+ }
+#endif
+ returnDB(code);