allow for input lines of arbitrary length in rrdtool pipe mode -- roger.meier terreac...
authoroetiker <oetiker@a5681a0c-68f1-0310-ab6d-d61299d08faa>
Tue, 4 Apr 2006 18:46:08 +0000 (18:46 +0000)
committeroetiker <oetiker@a5681a0c-68f1-0310-ab6d-d61299d08faa>
Tue, 4 Apr 2006 18:46:08 +0000 (18:46 +0000)
git-svn-id: svn://svn.oetiker.ch/rrdtool/branches/1.2/program@784 a5681a0c-68f1-0310-ab6d-d61299d08faa

CONTRIBUTORS
src/rrd_tool.c

index d7eb4b0..edfde57 100644 (file)
@@ -46,6 +46,7 @@ Radoslaw Karas <rkaras with tyndall.ie>
 Rainer Bawidamann <Rainer.Bawidamann with informatik.uni-ulm.de>
 Roman Hoogant <rhoogant with ee.ethz.ch>
 Ronan Mullally <ronan in 4L.ie>
+Roger J. Meier <roger.meier in terreactive.ch> (arbitrary linelength in rrdtool)
 Russ Wright <rwwright with home.com>
 Sean Summers <sean with Fenstermaker.com> (RPM .spec)
 Selena M Brewington <smbrewin with ichips.intel.com> add_ds
index dd3f0f9..8bd4ded 100644 (file)
@@ -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)