From 02f15efd404a828391c966322986457805f386a6 Mon Sep 17 00:00:00 2001 From: oetiker Date: Tue, 4 Apr 2006 18:46:08 +0000 Subject: [PATCH] allow for input lines of arbitrary length in rrdtool pipe mode -- roger.meier terreactive.ch git-svn-id: svn://svn.oetiker.ch/rrdtool/branches/1.2/program@784 a5681a0c-68f1-0310-ab6d-d61299d08faa --- CONTRIBUTORS | 1 + src/rrd_tool.c | 28 ++++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index d7eb4b0..edfde57 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -46,6 +46,7 @@ Radoslaw Karas Rainer Bawidamann Roman Hoogant Ronan Mullally +Roger J. Meier (arbitrary linelength in rrdtool) Russ Wright Sean Summers (RPM .spec) Selena M Brewington add_ds diff --git a/src/rrd_tool.c b/src/rrd_tool.c index dd3f0f9..8bd4ded 100644 --- a/src/rrd_tool.c +++ b/src/rrd_tool.c @@ -295,11 +295,34 @@ void PrintUsage(char *cmd) fputs(help_lic, stdout); } +static char *fgetslong(char **aLinePtr, FILE *stream) +{ + char *linebuf; + size_t bufsize = MAX_LENGTH; + int eolpos = 0; + + if (feof(stream)) return *aLinePtr = 0; + if (!(linebuf = malloc(bufsize))) { + perror("fgetslong: malloc"); + exit(1); + } + linebuf[0] = '\0'; + while (fgets(linebuf + eolpos, MAX_LENGTH, stream)) { + eolpos += strlen(linebuf + eolpos); + if (linebuf[eolpos - 1] == '\n') return *aLinePtr = linebuf; + bufsize += MAX_LENGTH; + if (!(linebuf = realloc(linebuf, bufsize))) { + perror("fgetslong: realloc"); + exit(1); + } + } + return *aLinePtr = linebuf[0] ? linebuf : 0; +} int main(int argc, char *argv[]) { char **myargv; - char aLine[MAX_LENGTH]; + char *aLine; char *firstdir=""; #ifdef MUST_DISABLE_SIGFPE signal(SIGFPE,SIG_IGN); @@ -362,7 +385,7 @@ int main(int argc, char *argv[]) } } - while (fgets(aLine, sizeof(aLine)-1, stdin)){ + while (fgetslong(&aLine, stdin)){ if ((argc = CountArgs(aLine)) == 0) { printf("ERROR: not enough arguments\n"); } @@ -397,6 +420,7 @@ int main(int argc, char *argv[]) } } fflush(stdout); /* this is important for pipes to work */ + free(aLine); } } else if (argc == 2) -- 2.11.0