ncurses 6.1 - patch 20180825
[ncurses.git] / progs / toe.c
index 34216e954b8b8ec7fe1f2873a791943386798bd0..5abb46de1ea90875b9256a63e1ab79b298309933 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 1998-2010,2011 Free Software Foundation, Inc.              *
+ * Copyright (c) 1998-2013,2017 Free Software Foundation, Inc.              *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
  * copy of this software and associated documentation files (the            *
@@ -44,7 +44,7 @@
 #include <hashed_db.h>
 #endif
 
-MODULE_ID("$Id: toe.c,v 1.64 2012/01/01 02:56:17 tom Exp $")
+MODULE_ID("$Id: toe.c,v 1.77 2017/12/23 19:23:40 tom Exp $")
 
 #define isDotname(name) (!strcmp(name, ".") || !strcmp(name, ".."))
 
@@ -99,6 +99,8 @@ new_termdata(void)
     if (want >= len_termdata) {
        len_termdata = (2 * want) + 10;
        ptr_termdata = typeRealloc(TERMDATA, len_termdata, ptr_termdata);
+       if (ptr_termdata == 0)
+           failed("ptr_termdata");
     }
 
     return ptr_termdata + use_termdata++;
@@ -230,16 +232,17 @@ make_db_name(char *dst, const char *src, unsigned limit)
     static const char suffix[] = DBM_SUFFIX;
 
     bool result = FALSE;
-    unsigned lens = sizeof(suffix) - 1;
-    unsigned size = strlen(src);
-    unsigned need = lens + size;
+    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))
-           (void) strcpy(dst, src);
-       else
-           (void) sprintf(dst, "%s%s", src, suffix);
+           && !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;
@@ -249,22 +252,25 @@ make_db_name(char *dst, const char *src, unsigned limit)
 typedef void (DescHook) (int /* db_index */ ,
                         int /* db_limit */ ,
                         const char * /* term_name */ ,
-                        TERMTYPE * /* term */ );
+                        TERMTYPE2 * /* term */ );
 
 static const char *
-term_description(TERMTYPE *tp)
+term_description(TERMTYPE2 *tp)
 {
     const char *desc;
 
-    if ((desc = strrchr(tp->term_names, '|')) == 0 || *++desc == '\0')
+    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)
+deschook(int db_index, int db_limit, const char *term_name, TERMTYPE2 *tp)
 {
     (void) db_index;
     (void) db_limit;
@@ -288,16 +294,16 @@ string_sum(const char *value)
 }
 
 static unsigned long
-checksum_of(TERMTYPE *tp)
+checksum_of(TERMTYPE2 *tp)
 {
     unsigned long result = string_sum(tp->term_names);
     unsigned i;
 
     for (i = 0; i < NUM_BOOLEANS(tp); i++) {
-       result += (tp->Booleans[i]);
+       result += (unsigned long) (tp->Booleans[i]);
     }
     for (i = 0; i < NUM_NUMBERS(tp); i++) {
-       result += (tp->Numbers[i]);
+       result += (unsigned long) (tp->Numbers[i]);
     }
     for (i = 0; i < NUM_STRINGS(tp); i++) {
        result += string_sum(tp->Strings[i]);
@@ -307,7 +313,7 @@ checksum_of(TERMTYPE *tp)
 
 /* collect data, to sort before display */
 static void
-sorthook(int db_index, int db_limit, const char *term_name, TERMTYPE *tp)
+sorthook(int db_index, int db_limit, const char *term_name, TERMTYPE2 *tp)
 {
     TERMDATA *data = new_termdata();
 
@@ -317,11 +323,11 @@ sorthook(int db_index, int db_limit, const char *term_name, TERMTYPE *tp)
     data->description = strmalloc(term_description(tp));
 }
 
-#if USE_TERMCAP
+#if NCURSES_USE_TERMCAP
 static void
 show_termcap(int db_index, int db_limit, char *buffer, DescHook hook)
 {
-    TERMTYPE data;
+    TERMTYPE2 data;
     char *next = strchr(buffer, ':');
     char *last;
     char *list = buffer;
@@ -344,16 +350,31 @@ show_termcap(int db_index, int db_limit, char *buffer, DescHook hook)
 }
 #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;
+}
+#endif
+
 static int
 typelist(int eargc, char *eargv[],
-        bool verbosity,
+        int verbosity,
         DescHook hook)
 /* apply a function to each entry in given terminfo directories */
 {
     int i;
 
     for (i = 0; i < eargc; i++) {
-#if USE_DATABASE
+#if NCURSES_USE_DATABASE
        if (_nc_is_dir_path(eargv[i])) {
            char *cwd_buf = 0;
            DIR *termdir;
@@ -371,23 +392,28 @@ typelist(int eargc, char *eargv[],
                (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];
+               size_t cwd_len;
+               char *name_1;
                DIR *entrydir;
                DIRENT *entry;
 
+               name_1 = copy_entryname(subdir);
+               if (isDotname(name_1)) {
+                   free(name_1);
+                   continue;
+               }
+
+               cwd_len = NAMLEN(subdir) + strlen(eargv[i]) + 3;
                cwd_buf = typeRealloc(char, cwd_len, cwd_buf);
                if (cwd_buf == 0)
                    failed("realloc cwd_buf");
 
                assert(cwd_buf != 0);
 
-               strncpy(name_1, subdir->d_name, len)[len] = '\0';
-               if (isDotname(name_1))
-                   continue;
+               _nc_SPRINTF(cwd_buf, _nc_SLIMIT(cwd_len)
+                           "%s/%s/", eargv[i], name_1);
+               free(name_1);
 
-               (void) sprintf(cwd_buf, "%s/%.*s/", eargv[i], (int) len, name_1);
                if (chdir(cwd_buf) != 0)
                    continue;
 
@@ -397,15 +423,16 @@ typelist(int eargc, char *eargv[],
                    continue;
                }
                while ((entry = readdir(entrydir)) != 0) {
-                   char name_2[PATH_MAX];
-                   TERMTYPE lterm;
+                   char *name_2;
+                   TERMTYPE2 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))
+                   name_2 = copy_entryname(entry);
+                   if (isDotname(name_2) || !_nc_is_file_path(name_2)) {
+                       free(name_2);
                        continue;
+                   }
 
                    status = _nc_read_file_entry(name_2, &lterm);
                    if (status <= 0) {
@@ -413,7 +440,8 @@ typelist(int eargc, char *eargv[],
                        (void) fprintf(stderr,
                                       "%s: couldn't open terminfo file %s.\n",
                                       _nc_progname, name_2);
-                       return (EXIT_FAILURE);
+                       free(name_2);
+                       continue;
                    }
 
                    /* only visit things once, by primary name */
@@ -422,7 +450,8 @@ typelist(int eargc, char *eargv[],
                        /* apply the selected hook function */
                        hook(i, eargc, cn, &lterm);
                    }
-                   _nc_free_termtype(&lterm);
+                   _nc_free_termtype2(&lterm);
+                   free(name_2);
                }
                closedir(entrydir);
            }
@@ -446,7 +475,7 @@ typelist(int eargc, char *eargv[],
 
                    code = _nc_db_first(capdbp, &key, &data);
                    while (code == 0) {
-                       TERMTYPE lterm;
+                       TERMTYPE2 lterm;
                        int used;
                        char *have;
                        char *cn;
@@ -457,7 +486,7 @@ typelist(int eargc, char *eargv[],
                                cn = _nc_first_name(lterm.term_names);
                                /* apply the selected hook function */
                                hook(i, eargc, cn, &lterm);
-                               _nc_free_termtype(&lterm);
+                               _nc_free_termtype2(&lterm);
                            }
                        }
                        code = _nc_db_next(capdbp, &key, &data);
@@ -470,7 +499,7 @@ typelist(int eargc, char *eargv[],
        }
 #endif
 #endif
-#if USE_TERMCAP
+#if NCURSES_USE_TERMCAP
 #if HAVE_BSD_CGETENT
        {
            CGETENT_CONST char *db_array[2];
@@ -678,6 +707,8 @@ main(int argc, char *argv[])
            }
            if (!pass) {
                eargv = allocArgv(count);
+               if (eargv == 0)
+                   failed("eargv");
            } else {
                code = typelist((int) count, eargv, header, hook);
                freeArgv(eargv);
@@ -687,9 +718,11 @@ main(int argc, char *argv[])
        DBDIRS state;
        int offset;
        const char *path;
-       char **eargv = allocArgv(2);
+       char **eargv = allocArgv((size_t) 2);
        size_t count = 0;
 
+       if (eargv == 0)
+           failed("eargv");
        _nc_first_db(&state, &offset);
        if ((path = _nc_next_db(&state, &offset)) != 0) {
            eargv[count++] = strmalloc(path);