ncurses 5.9 - patch 20120721
[ncurses.git] / progs / toe.c
index 34216e954b8b8ec7fe1f2873a791943386798bd0..4924d2c1cccf32da6266f958d49c028aae7965a8 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 1998-2010,2011 Free Software Foundation, Inc.              *
+ * Copyright (c) 1998-2011,2012 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.68 2012/07/21 22:55:59 tom Exp $")
 
 #define isDotname(name) (!strcmp(name, ".") || !strcmp(name, ".."))
 
@@ -236,10 +236,11 @@ make_db_name(char *dst, const char *src, unsigned limit)
 
     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;
@@ -294,10 +295,10 @@ checksum_of(TERMTYPE *tp)
     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]);
@@ -344,6 +345,21 @@ show_termcap(int db_index, int db_limit, char *buffer, DescHook hook)
 }
 #endif
 
+#if 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,
@@ -371,23 +387,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 +418,16 @@ typelist(int eargc, char *eargv[],
                    continue;
                }
                while ((entry = readdir(entrydir)) != 0) {
-                   char name_2[PATH_MAX];
+                   char *name_2;
                    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))
+                   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,6 +435,7 @@ typelist(int eargc, char *eargv[],
                        (void) fprintf(stderr,
                                       "%s: couldn't open terminfo file %s.\n",
                                       _nc_progname, name_2);
+                       free(name_2);
                        return (EXIT_FAILURE);
                    }
 
@@ -423,6 +446,7 @@ typelist(int eargc, char *eargv[],
                        hook(i, eargc, cn, &lterm);
                    }
                    _nc_free_termtype(&lterm);
+                   free(name_2);
                }
                closedir(entrydir);
            }