X-Git-Url: https://git.octo.it/?p=rrdtool.git;a=blobdiff_plain;f=src%2Frrd_cgi.c;h=27b4884b2e5b416c483f9ce598104d6911aa5203;hp=23567c5d2252fe0ca05988a6a4ac58d9c6c2923e;hb=75b8cf7a1eff3d24a8567f3209aa023ad4473e95;hpb=2a6a270edfda89b04722b42b57992907f871c671 diff --git a/src/rrd_cgi.c b/src/rrd_cgi.c index 23567c5..27b4884 100644 --- a/src/rrd_cgi.c +++ b/src/rrd_cgi.c @@ -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 +#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("\n", name, value); -# endif +#endif return varheap[i].value; } -#ifdef DEBUG_VARS +#ifdef DEBUG_VARS printf("\n", name, value, varheap[i].value); #endif @@ -372,6 +374,7 @@ static void calfree( if (calcpr) { free(calcpr); } + calcpr = NULL; } } @@ -389,6 +392,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 +458,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 +476,6 @@ int main( for (i=0;i 0) { time_t now; @@ -664,7 +724,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 +737,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(""); } @@ -894,7 +954,6 @@ char *drawgraph( DS_NAM_SIZE) * sizeof(char)); sprintf(err, "[ERROR: %s]", rrd_get_error()); rrd_clear_error(); - calfree(); return err; } } @@ -1328,7 +1387,8 @@ s_var **rrdcgiReadVariables( length = atoi(ip); if ((line = (char *) malloc(length + 2)) == NULL) return NULL; - fgets(line, length + 1, stdin); + if (fgets(line, length + 1, stdin) == NULL) + return NULL; } else return NULL; } else if (cp && !strcmp(cp, "GET")) { @@ -1428,8 +1488,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)