prepare for the release of rrdtool-1.4.2
[rrdtool.git] / src / rrd_error.c
index 1d7ae6b..5408d6c 100644 (file)
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * RRDtool 1.3rc6  Copyright by Tobi Oetiker, 1997-2008
+ * RRDtool 1.4.2  Copyright by Tobi Oetiker, 1997-2009
  *****************************************************************************
  * rrd_error.c   Common Header File
  *****************************************************************************
  *
  *************************************************************************** */
 
-#include "rrd_tool.h"
+#include <stdlib.h>
 #include <stdarg.h>
 
+#include "rrd_tool.h"
+
 #define MAXLEN 4096
 #define ERRBUFLEN 256
 #define CTX (rrd_get_context())
@@ -46,7 +48,7 @@ void rrd_set_error(
     rrd_clear_error();
     va_start(argp, fmt);
 #ifdef HAVE_VSNPRINTF
-    vsnprintf(CTX->rrd_error, CTX->len, fmt, argp);
+    vsnprintf(CTX->rrd_error, sizeof(CTX->rrd_error), fmt, argp);
 #else
     vsprintf(CTX->rrd_error, fmt, argp);
 #endif
@@ -78,7 +80,7 @@ char     *rrd_get_error(
    context. Using these functions would require to change each and
    every function containing any of the non _r versions... */
 void rrd_set_error_r(
-    struct rrd_context *rrd_ctx,
+    rrd_context_t * rrd_ctx,
     char *fmt,
     ...)
 {
@@ -87,75 +89,60 @@ void rrd_set_error_r(
     rrd_clear_error_r(rrd_ctx);
     va_start(argp, fmt);
 #ifdef HAVE_VSNPRINTF
-    vsnprintf((char *) rrd_ctx->rrd_error, rrd_ctx->len, fmt, argp);
-    rrd_ctx->rrd_error[rrd_ctx->len] = '\0';
+    vsnprintf(rrd_ctx->rrd_error, sizeof(rrd_ctx->rrd_error), fmt, argp);
+    rrd_ctx->rrd_error[sizeof(rrd_ctx->rrd_error) - 1] = '\0';
 #else
-    vsprintf((char *) rrd_ctx->rrd_error, fmt, argp);
+    vsprintf(rrd_ctx->rrd_error, fmt, argp);
 #endif
     va_end(argp);
 }
 
 int rrd_test_error_r(
-    struct rrd_context *rrd_ctx)
+    rrd_context_t * rrd_ctx)
 {
     return rrd_ctx->rrd_error[0] != '\0';
 }
 
 void rrd_clear_error_r(
-    struct rrd_context *rrd_ctx)
+    rrd_context_t * rrd_ctx)
 {
     rrd_ctx->rrd_error[0] = '\0';
 }
 
 char     *rrd_get_error_r(
-    struct rrd_context *rrd_ctx)
+    rrd_context_t * rrd_ctx)
 {
-    return (char *) rrd_ctx->rrd_error;
+    return rrd_ctx->rrd_error;
 }
 #endif
 
 /* PS: Should we move this to some other file? It is not really error
    related. */
-struct rrd_context *rrd_new_context(
+rrd_context_t *rrd_new_context(
     void)
 {
-    struct rrd_context *rrd_ctx =
-        (struct rrd_context *) malloc(sizeof(struct rrd_context));
+    rrd_context_t *rrd_ctx = (rrd_context_t *) malloc(sizeof(rrd_context_t));
 
-    if (rrd_ctx) {
-        rrd_ctx->rrd_error = malloc(MAXLEN + 10);
-        rrd_ctx->lib_errstr = malloc(ERRBUFLEN + 10);
-        if (rrd_ctx->rrd_error && rrd_ctx->lib_errstr) {
-            *rrd_ctx->rrd_error = 0;
-            *rrd_ctx->lib_errstr = 0;
-            rrd_ctx->len = MAXLEN;
-            rrd_ctx->errlen = ERRBUFLEN;
-            return rrd_ctx;
-        }
-        if (rrd_ctx->rrd_error)
-            free(rrd_ctx->rrd_error);
-        if (rrd_ctx->lib_errstr)
-            free(rrd_ctx->lib_errstr);
-        free(rrd_ctx);
+    if (!rrd_ctx) {
+        return NULL;
     }
-    return NULL;
+
+    rrd_ctx->rrd_error[0] = '\0';
+    rrd_ctx->lib_errstr[0] = '\0';
+    return rrd_ctx;
 }
 
 void rrd_free_context(
-    struct rrd_context *rrd_ctx)
+    rrd_context_t * rrd_ctx)
 {
     if (rrd_ctx) {
-        if (rrd_ctx->rrd_error)
-            free(rrd_ctx->rrd_error);
-        if (rrd_ctx->lib_errstr)
-            free(rrd_ctx->lib_errstr);
         free(rrd_ctx);
     }
 }
 
 #if 0
 void rrd_globalize_error(
-    struct rrd_context *rrd_ctx)
+    rrd_context_t * rrd_ctx)
 {
     if (rrd_ctx) {
         rrd_set_error(rrd_ctx->rrd_error);