]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - ncurses/tinfo/write_entry.c
ncurses 5.9 - patch 20130824
[ncurses.git] / ncurses / tinfo / write_entry.c
index 3f6eebfcb33d9ae1f35fac5b5dfde2c3b591d776..f7ae96fb584f5ee7305c7a2904b7c795080717a6 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 1998-2010,2011 Free Software Foundation, Inc.              *
+ * Copyright (c) 1998-2012,2013 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            *
@@ -47,7 +47,7 @@
 #define TRACE_OUT(p)           /*nothing */
 #endif
 
-MODULE_ID("$Id: write_entry.c,v 1.81 2011/10/22 15:33:37 tom Exp $")
+MODULE_ID("$Id: write_entry.c,v 1.88 2013/07/27 15:52:18 tom Exp $")
 
 static int total_written;
 
@@ -99,7 +99,7 @@ check_writeable(int code)
     if (verified[s - dirnames])
        return;
 
-    sprintf(dir, LEAF_FMT, code);
+    _nc_SPRINTF(dir, _nc_SLIMIT(sizeof(dir)) LEAF_FMT, code);
     if (make_db_root(dir) < 0) {
        _nc_err_abort("%s/%s: permission denied", _nc_tic_dir(0), dir);
     }
@@ -116,29 +116,28 @@ make_db_path(char *dst, const char *src, size_t limit)
 
     if (src == top || _nc_is_abs_path(src)) {
        if (strlen(src) + 1 <= limit) {
-           (void) strcpy(dst, src);
+           _nc_STRCPY(dst, src, limit);
            rc = 0;
        }
     } else {
        if (strlen(top) + strlen(src) + 2 <= limit) {
-           (void) sprintf(dst, "%s/%s", top, src);
+           _nc_SPRINTF(dst, _nc_SLIMIT(limit) "%s/%s", top, src);
            rc = 0;
        }
     }
 #if USE_HASHED_DB
     if (rc == 0) {
-       if (_nc_is_dir_path(dst)) {
-           rc = -1;
-       } else {
-           static const char suffix[] = DBM_SUFFIX;
-           size_t have = strlen(dst);
-           size_t need = strlen(suffix);
-           if (have > need && strcmp(dst + (int) (have - need), suffix)) {
-               if (have + need <= limit)
-                   strcat(dst, suffix);
-               else
-                   rc = -1;
+       static const char suffix[] = DBM_SUFFIX;
+       size_t have = strlen(dst);
+       size_t need = strlen(suffix);
+       if (have > need && strcmp(dst + (int) (have - need), suffix)) {
+           if (have + need <= limit) {
+               _nc_STRCAT(dst, suffix, limit);
+           } else {
+               rc = -1;
            }
+       } else if (_nc_is_dir_path(dst)) {
+           rc = -1;
        }
     }
 #endif
@@ -158,10 +157,11 @@ make_db_root(const char *path)
 #if USE_HASHED_DB
        DB *capdbp;
 
-       if ((capdbp = _nc_db_open(fullpath, TRUE)) == NULL)
+       if ((capdbp = _nc_db_open(fullpath, TRUE)) == NULL) {
            rc = -1;
-       else if (_nc_db_close(capdbp) < 0)
+       } else if (_nc_db_close(capdbp) < 0) {
            rc = -1;
+       }
 #else
        struct stat statbuf;
 
@@ -273,16 +273,21 @@ _nc_write_entry(TERMTYPE *const tp)
     char name_list[MAX_TERMINFO_LENGTH];
     char *first_name, *other_names;
     char *ptr;
+    const char *term_names = tp->term_names;
+    size_t name_size = strlen(term_names);
 
-    assert(strlen(tp->term_names) != 0);
-    assert(strlen(tp->term_names) < sizeof(name_list));
+    if (name_size == 0) {
+       _nc_syserr_abort("no terminal name found.");
+    } else if (name_size >= sizeof(name_list) - 1) {
+       _nc_syserr_abort("terminal name too long: %s", term_names);
+    }
 
-    (void) strcpy(name_list, tp->term_names);
+    _nc_STRCPY(name_list, term_names, sizeof(name_list));
     DEBUG(7, ("Name list = '%s'", name_list));
 
     first_name = name_list;
 
-    ptr = &name_list[strlen(name_list) - 1];
+    ptr = &name_list[name_size - 1];
     other_names = ptr + 1;
 
     while (ptr > name_list && *ptr != '|')
@@ -316,8 +321,8 @@ _nc_write_entry(TERMTYPE *const tp)
            buffer[0] = 0;
 
            memset(&key, 0, sizeof(key));
-           key.data = tp->term_names;
-           key.size = strlen(tp->term_names);
+           key.data = term_names;
+           key.size = name_size;
 
            memset(&data, 0, sizeof(data));
            data.data = buffer;
@@ -330,8 +335,10 @@ _nc_write_entry(TERMTYPE *const tp)
            key.data = name_list;
            key.size = strlen(name_list);
 
-           strcpy(buffer + 1, tp->term_names);
-           data.size = strlen(tp->term_names) + 1;
+           _nc_STRCPY(buffer + 1,
+                      term_names,
+                      sizeof(buffer) - 1);
+           data.size = name_size + 1;
 
            _nc_db_put(capdb, &key, &data);
 
@@ -359,7 +366,8 @@ _nc_write_entry(TERMTYPE *const tp)
     if (strlen(first_name) >= sizeof(filename) - (2 + LEAF_LEN))
        _nc_warning("terminal name too long.");
 
-    sprintf(filename, LEAF_FMT "/%s", first_name[0], first_name);
+    _nc_SPRINTF(filename, _nc_SLIMIT(sizeof(filename))
+               LEAF_FMT "/%s", first_name[0], first_name);
 
     /*
      * Has this primary name been written since the first call to
@@ -369,7 +377,22 @@ _nc_write_entry(TERMTYPE *const tp)
     if (start_time > 0 &&
        stat(filename, &statbuf) >= 0
        && statbuf.st_mtime >= start_time) {
+#if HAVE_LINK && !USE_SYMLINKS
+       /*
+        * If the file has more than one link, the reason for the previous
+        * write could be that the current primary name used to be an alias for
+        * the previous entry.  In that case, unlink the file so that we will
+        * not modify the previous entry as we write this one.
+        */
+       if (statbuf.st_nlink > 1) {
+           _nc_warning("name redefined.");
+           unlink(filename);
+       } else {
+           _nc_warning("name multiply defined.");
+       }
+#else
        _nc_warning("name multiply defined.");
+#endif
     }
 
     check_writeable(first_name[0]);
@@ -400,7 +423,8 @@ _nc_write_entry(TERMTYPE *const tp)
        }
 
        check_writeable(ptr[0]);
-       sprintf(linkname, LEAF_FMT "/%s", ptr[0], ptr);
+       _nc_SPRINTF(linkname, _nc_SLIMIT(sizeof(linkname))
+                   LEAF_FMT "/%s", ptr[0], ptr);
 
        if (strcmp(filename, linkname) == 0) {
            _nc_warning("self-synonym ignored");
@@ -415,7 +439,7 @@ _nc_write_entry(TERMTYPE *const tp)
            if (first_name[0] == linkname[0])
                strncpy(symlinkname, first_name, sizeof(symlinkname) - 1);
            else {
-               strcpy(symlinkname, "../");
+               _nc_STRCPY(symlinkname, "../", sizeof(suymlinkname));
                strncat(symlinkname, filename, sizeof(symlinkname) - 4);
            }
            symlinkname[sizeof(symlinkname) - 1] = '\0';