fix from alex
[rrdtool.git] / src / rrd_cgi.c
index 75dabf4..b535541 100644 (file)
@@ -1,11 +1,13 @@
 /*****************************************************************************
- * RRDtool 1.2.23  Copyright by Tobi Oetiker, 1997-2007
+ * RRDtool 1.3.2  Copyright by Tobi Oetiker, 1997-2008
  *****************************************************************************
  * rrd_cgi.c  RRD Web Page Generator
  *****************************************************************************/
 
 #include "rrd_tool.h"
-
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
 
 #define MEMBLK 1024
 /*#define DEBUG_PARSER
@@ -221,7 +223,7 @@ static size_t varheap_size = 0;
 
 /* allocate and initialize variable heap */
 static int initvar(
-    )
+    void)
 {
     varheap = (vardata *) malloc(sizeof(vardata) * INIT_VARSTORE_SIZE);
     if (varheap == NULL) {
@@ -235,7 +237,7 @@ static int initvar(
 
 /* cleanup: free allocated memory */
 static void donevar(
-    )
+    void)
 {
     int       i;
 
@@ -288,13 +290,13 @@ static const char *putvar(
         if (0 == strcmp(name, varheap[i].name)) {
             /* overwrite existing entry */
             if (varheap[i].is_const) {
-#ifdef                 DEBUG_VARS
+#ifdef DEBUG_VARS
                 printf("<!-- setver(%s, %s): not assigning: "
                        "const variable -->\n", name, value);
-#                              endif
+#endif
                 return varheap[i].value;
             }
-#ifdef         DEBUG_VARS
+#ifdef DEBUG_VARS
             printf("<!-- setvar(%s, %s): overwriting old value (%s) -->\n",
                    name, value, varheap[i].value);
 #endif
@@ -389,6 +391,63 @@ char     *stralloc(
     return (nstr);
 }
 
+static int readfile(
+    const char *file_name,
+    char **buffer,
+    int skipfirst)
+{
+    long      writecnt = 0, totalcnt = MEMBLK;
+    long      offset = 0;
+    FILE     *input = NULL;
+    char      c;
+
+    if ((strcmp("-", file_name) == 0)) {
+        input = stdin;
+    } else {
+        if ((input = fopen(file_name, "rb")) == NULL) {
+            rrd_set_error("opening '%s': %s", file_name, rrd_strerror(errno));
+            return (-1);
+        }
+    }
+    if (skipfirst) {
+        do {
+            c = getc(input);
+            offset++;
+        } while (c != '\n' && !feof(input));
+    }
+    if (strcmp("-", file_name)) {
+        fseek(input, 0, SEEK_END);
+        /* have extra space for detecting EOF without realloc */
+        totalcnt = (ftell(input) + 1) / sizeof(char) - offset;
+        if (totalcnt < MEMBLK)
+            totalcnt = MEMBLK;  /* sanitize */
+        fseek(input, offset * sizeof(char), SEEK_SET);
+    }
+    if (((*buffer) = (char *) malloc((totalcnt + 4) * sizeof(char))) == NULL) {
+        perror("Allocate Buffer:");
+        exit(1);
+    };
+    do {
+        writecnt +=
+            fread((*buffer) + writecnt, 1,
+                  (totalcnt - writecnt) * sizeof(char), input);
+        if (writecnt >= totalcnt) {
+            totalcnt += MEMBLK;
+            if (((*buffer) =
+                 rrd_realloc((*buffer),
+                             (totalcnt + 4) * sizeof(char))) == NULL) {
+                perror("Realloc Buffer:");
+                exit(1);
+            };
+        }
+    } while (!feof(input));
+    (*buffer)[writecnt] = '\0';
+    if (strcmp("-", file_name) != 0) {
+        fclose(input);
+    };
+    return writecnt;
+}
+
 int main(
     int argc,
     char *argv[])
@@ -398,6 +457,10 @@ int main(
     char     *server_url = NULL;
     long      i;
     long      filter = 0;
+    struct option long_options[] = {
+        {"filter", no_argument, 0, 'f'},
+        {0, 0, 0, 0}
+    };
 
 #ifdef MUST_DISABLE_SIGFPE
     signal(SIGFPE, SIG_IGN);
@@ -412,10 +475,6 @@ int main(
        for (i=0;i<argc;i++)
        printf("%d-'%s'\n",i,argv[i]); */
     while (1) {
-        static struct option long_options[] = {
-            {"filter", no_argument, 0, 'f'},
-            {0, 0, 0, 0}
-        };
         int       option_index = 0;
         int       opt;
 
@@ -664,7 +723,7 @@ char     *printstrftime(
     long argc,
     const char **args)
 {
-    struct rrd_time_value start_tv, end_tv;
+    rrd_time_value_t start_tv, end_tv;
     char     *parsetime_error = NULL;
     char      formatted[MAX_STRFTIME_SIZE];
     struct tm *the_tm;
@@ -677,19 +736,19 @@ char     *printstrftime(
     }
 
     /* Init start and end time */
-    parsetime("end-24h", &start_tv);
-    parsetime("now", &end_tv);
+    rrd_parsetime("end-24h", &start_tv);
+    rrd_parsetime("now", &end_tv);
 
     /* Parse the start and end times we were given */
-    if ((parsetime_error = parsetime(args[1], &start_tv))) {
+    if ((parsetime_error = rrd_parsetime(args[1], &start_tv))) {
         rrd_set_error("start time: %s", parsetime_error);
         return stralloc("");
     }
-    if ((parsetime_error = parsetime(args[2], &end_tv))) {
+    if ((parsetime_error = rrd_parsetime(args[2], &end_tv))) {
         rrd_set_error("end time: %s", parsetime_error);
         return stralloc("");
     }
-    if (proc_start_end(&start_tv, &end_tv, &start_tmp, &end_tmp) == -1) {
+    if (rrd_proc_start_end(&start_tv, &end_tv, &start_tmp, &end_tmp) == -1) {
         return stralloc("");
     }
 
@@ -1428,8 +1487,8 @@ s_var   **rrdcgiReadVariables(
 
             /* try to find out if there's already such a variable */
             for (k = 0; k < i && (strncmp(result[k]->name, cp, esp - cp)
-                                  || !(strlen(result[k]->name) == esp - cp));
-                 k++);
+                                  || !(strlen(result[k]->name) ==
+                                       (size_t) (esp - cp))); k++);
 
             if (k == i) {   /* No such variable yet */
                 if ((result[i] = (s_var *) malloc(sizeof(s_var))) == NULL)