X-Git-Url: https://git.octo.it/?p=rrdtool.git;a=blobdiff_plain;f=src%2Frrd_cgi.c;h=f6313f869dc0cc725e39e9107283a275571b8a8b;hp=800f3a37b5327448ae1ce6e4e2d700dcc47f391c;hb=609630f90a3247ae0956eb4f6d860e8a7f1797f6;hpb=3d068765c6b6c8d096e0692f22e5b5e407948b54 diff --git a/src/rrd_cgi.c b/src/rrd_cgi.c index 800f3a3..f6313f8 100644 --- a/src/rrd_cgi.c +++ b/src/rrd_cgi.c @@ -1,5 +1,5 @@ /***************************************************************************** - * RRDtool 1.3rc2 Copyright by Tobi Oetiker, 1997-2008 + * RRDtool 1.4.2 Copyright by Tobi Oetiker, 1997-2009 ***************************************************************************** * rrd_cgi.c RRD Web Page Generator *****************************************************************************/ @@ -374,6 +374,7 @@ static void calfree( if (calcpr) { free(calcpr); } + calcpr = NULL; } } @@ -391,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[]) @@ -666,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; @@ -679,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(""); } @@ -896,7 +954,6 @@ char *drawgraph( DS_NAM_SIZE) * sizeof(char)); sprintf(err, "[ERROR: %s]", rrd_get_error()); rrd_clear_error(); - calfree(); return err; } } @@ -996,7 +1053,7 @@ char *scanargs( int curarg_contains_rrd_directives; /* local array of arguments while parsing */ - int argc = 0; + int argc = 1; char **argv; #ifdef DEBUG_PARSER @@ -1012,6 +1069,7 @@ char *scanargs( if (!argv) { return NULL; } + argv[0] = "rrdcgi"; /* skip leading blanks */ while (isspace((int) *line)) { @@ -1115,7 +1173,7 @@ char *scanargs( argv[argc - 1] = rrd_expand_vars(stralloc(argv[argc - 1])); } #ifdef DEBUG_PARSER - if (argc > 0) { + if (argc > 1) { int n; printf("<-- arguments found [%d]\n", argc); @@ -1129,8 +1187,17 @@ char *scanargs( #endif /* update caller's notion of the argument array and it's size */ - *arguments = argv; - *argument_count = argc; + + /* note this is a bit of a hack since the rrd_cgi code used to just put + its arguments into a normal array starting at 0 ... since the rrd_* + commands expect and argc/argv array we used to just shift everything + by -1 ... this in turn exploded when a rrd_* function tried to print + argv[0] ... hence we are now doing everything in argv style but hand + over seemingly the old array ... but doing argv-1 will actually end + up in a 'good' place now. */ + + *arguments = argv+1; + *argument_count = argc-1; if (Quote) { return NULL; @@ -1184,7 +1251,7 @@ int parse( if (end) { /* got arguments, call function for 'tag' with arguments */ val = func(argc, (const char **) args); - free(args); + free(args-1); } else { /* unable to parse arguments, undo 0-termination by scanargs */ for (; argc > 0; argc--) { @@ -1330,7 +1397,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")) {