]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - ncurses/tinfo/write_entry.c
ncurses 5.9 - patch 20111217
[ncurses.git] / ncurses / tinfo / write_entry.c
index 2552eea56df9bcfc5f93de186ff21887c2debf3e..b1daf1649e2086389b45c263538c68e4e3224d42 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 1998-2008,2009 Free Software Foundation, Inc.              *
+ * Copyright (c) 1998-2010,2011 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            *
 #include <curses.priv.h>
 #include <hashed_db.h>
 
-#include <sys/stat.h>
-
 #include <tic.h>
-#include <term_entry.h>
-
-#ifndef S_ISDIR
-#define S_ISDIR(mode) ((mode & S_IFMT) == S_IFDIR)
-#endif
 
 #if 1
 #define TRACE_OUT(p) DEBUG(2, p)
@@ -54,7 +47,7 @@
 #define TRACE_OUT(p)           /*nothing */
 #endif
 
-MODULE_ID("$Id: write_entry.c,v 1.75 2009/12/05 21:25:01 tom Exp $")
+MODULE_ID("$Id: write_entry.c,v 1.82 2011/10/30 14:33:13 tom Exp $")
 
 static int total_written;
 
@@ -77,7 +70,7 @@ write_file(char *filename, TERMTYPE *tp)
     DEBUG(1, ("Created %s", filename));
 
     if (write_object(tp, buffer, &offset, limit) == ERR
-       || fwrite(buffer, sizeof(char), offset, fp) != offset) {
+       || fwrite(buffer, sizeof(char), (size_t) offset, fp) != offset) {
        _nc_syserr_abort("error writing %s/%s", _nc_tic_dir(0), filename);
     }
 
@@ -116,7 +109,7 @@ check_writeable(int code)
 #endif /* !USE_HASHED_DB */
 
 static int
-make_db_path(char *dst, const char *src, unsigned limit)
+make_db_path(char *dst, const char *src, size_t limit)
 {
     int rc = -1;
     const char *top = _nc_tic_dir(0);
@@ -134,18 +127,17 @@ make_db_path(char *dst, const char *src, unsigned limit)
     }
 #if USE_HASHED_DB
     if (rc == 0) {
-       if (_nc_is_dir_path(dst)) {
-           rc = -1;
-       } else {
-           static const char suffix[] = DBM_SUFFIX;
-           unsigned have = strlen(dst);
-           unsigned need = strlen(suffix);
-           if (have > need && strcmp(dst + 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) {
+               strcat(dst, suffix);
+           } else {
+               rc = -1;
            }
+       } else if (_nc_is_dir_path(dst)) {
+           rc = -1;
        }
     }
 #endif
@@ -165,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;
 
@@ -356,7 +349,6 @@ _nc_write_entry(TERMTYPE *const tp)
 
                _nc_db_put(capdb, &key, &data);
            }
-           _nc_db_close(capdb);
        }
     }
 #else /* !USE_HASHED_DB */
@@ -470,29 +462,29 @@ _nc_write_entry(TERMTYPE *const tp)
 #endif /* USE_HASHED_DB */
 }
 
-static unsigned
+static size_t
 fake_write(char *dst,
           unsigned *offset,
-          unsigned limit,
+          size_t limit,
           char *src,
-          unsigned want,
-          unsigned size)
+          size_t want,
+          size_t size)
 {
-    unsigned have = (limit - *offset);
+    size_t have = (limit - *offset);
 
     want *= size;
     if (have > 0) {
        if (want > have)
            want = have;
        memcpy(dst + *offset, src, want);
-       *offset += want;
+       *offset += (unsigned) want;
     } else {
        want = 0;
     }
     return (want / size);
 }
 
-#define Write(buf, size, count) fake_write(buffer, offset, limit, (char *) buf, count, size)
+#define Write(buf, size, count) fake_write(buffer, offset, (size_t) limit, (char *) buf, (size_t) count, (size_t) size)
 
 #undef LITTLE_ENDIAN           /* BSD/OS defines this as a feature macro */
 #define HI(x)                  ((x) / 256)
@@ -503,10 +495,10 @@ fake_write(char *dst,
 #define WRITE_STRING(str) (Write(str, sizeof(char), strlen(str) + 1) == strlen(str) + 1)
 
 static int
-compute_offsets(char **Strings, unsigned strmax, short *offsets)
+compute_offsets(char **Strings, size_t strmax, short *offsets)
 {
     int nextfree = 0;
-    unsigned i;
+    size_t i;
 
     for (i = 0; i < strmax; i++) {
        if (Strings[i] == ABSENT_STRING) {
@@ -524,9 +516,9 @@ compute_offsets(char **Strings, unsigned strmax, short *offsets)
 }
 
 static void
-convert_shorts(unsigned char *buf, short *Numbers, unsigned count)
+convert_shorts(unsigned char *buf, short *Numbers, size_t count)
 {
-    unsigned i;
+    size_t i;
     for (i = 0; i < count; i++) {
        if (Numbers[i] == ABSENT_NUMERIC) {     /* HI/LO won't work */
            buf[2 * i] = buf[2 * i + 1] = 0377;
@@ -535,7 +527,7 @@ convert_shorts(unsigned char *buf, short *Numbers, unsigned count)
            buf[2 * i + 1] = 0377;
        } else {
            LITTLE_ENDIAN(buf + 2 * i, Numbers[i]);
-           TRACE_OUT(("put Numbers[%d]=%d", i, Numbers[i]));
+           TRACE_OUT(("put Numbers[%u]=%d", (unsigned) i, Numbers[i]));
        }
     }
 }
@@ -578,7 +570,7 @@ extended_Strings(TERMTYPE *tp)
 
     for (i = 0; i < tp->ext_Strings; ++i) {
        if (tp->Strings[STRCOUNT + i] != ABSENT_STRING)
-           result = (i + 1);
+           result = (unsigned short) (i + 1);
     }
     return result;
 }
@@ -707,7 +699,7 @@ write_object(TERMTYPE *tp, char *buffer, unsigned *offset, unsigned limit)
            return (ERR);
 
        nextfree = compute_offsets(tp->Strings + STRCOUNT,
-                                  tp->ext_Strings,
+                                  (size_t) tp->ext_Strings,
                                   offsets);
        TRACE_OUT(("after extended string capabilities, nextfree=%d", nextfree));
 
@@ -715,7 +707,7 @@ write_object(TERMTYPE *tp, char *buffer, unsigned *offset, unsigned limit)
            return (ERR);
 
        nextfree += compute_offsets(tp->ext_Names,
-                                   extcnt,
+                                   (size_t) extcnt,
                                    offsets + tp->ext_Strings);
        TRACE_OUT(("after extended capnames, nextfree=%d", nextfree));
        strmax = tp->ext_Strings + extcnt;
@@ -743,7 +735,7 @@ write_object(TERMTYPE *tp, char *buffer, unsigned *offset, unsigned limit)
 
        TRACE_OUT(("WRITE %d numbers @%d", tp->ext_Numbers, *offset));
        if (tp->ext_Numbers) {
-           convert_shorts(buf, tp->Numbers + NUMCOUNT, tp->ext_Numbers);
+           convert_shorts(buf, tp->Numbers + NUMCOUNT, (size_t) tp->ext_Numbers);
            if (Write(buf, 2, tp->ext_Numbers) != tp->ext_Numbers)
                return (ERR);
        }