Code

openvpn plugin: multi1_read: Replace a hard to read for-loop with a while-loop.
authorFlorian Forster <octo@leeloo.lan.home.verplant.org>
Fri, 25 Sep 2009 09:49:45 +0000 (11:49 +0200)
committerFlorian Forster <octo@leeloo.lan.home.verplant.org>
Tue, 1 Dec 2009 10:35:43 +0000 (11:35 +0100)
src/openvpn.c

index 62b022a52c1ecb82c383d0fe9cf9cfeeea785820..647c9e134d1fb456a19bef375fb3faeec6f78dc0 100644 (file)
@@ -231,30 +231,35 @@ static int multi1_read (char *name, FILE *fh)
 {
        char buffer[1024];
        char *fields[10];
-       const int max_fields = STATIC_ARRAY_SIZE (fields);
-       int  fields_num, read = 0, skip = 1;
+       int  fields_num, read = 0, found_header = 0;
 
        /* read the file until the "ROUTING TABLE" line is found (no more info after) */
-       for ( ; strcmp (buffer, "ROUTING TABLE\n"); fgets (buffer, sizeof (buffer), fh))
+       while (fgets (buffer, sizeof (buffer), fh) != NULL)
        {
-               if (skip) /* skip the first lines until the client list section is found */
+               if (strcmp (buffer, "ROUTING TABLE\n") == 0)
+                       break;
+
+               if (strcmp (buffer, V1STRING) == 0)
                {
+                       found_header = 1;
+                       continue;
+               }
+
+               /* skip the first lines until the client list section is found */
+               if (found_header == 0)
                        /* we can't start reading data until this string is found */
-                       if (strcmp (buffer, V1STRING) == 0)
-                               skip = 0;
+                       continue;
 
+               fields_num = openvpn_strsplit (buffer,
+                               fields, STATIC_ARRAY_SIZE (fields));
+               if (fields_num < 4)
                        continue;
-               }
-               else
-               {
-                       fields_num = openvpn_strsplit (buffer, fields, max_fields);
 
-                       iostats_submit (name,                   /* vpn instance */
-                                       fields[0],              /* "Common Name" */
-                                       atoll (fields[2]),      /* "Bytes Received" */
-                                       atoll (fields[3]));     /* "Bytes Sent" */
-                       read = 1;
-               }
+               iostats_submit (name,                   /* vpn instance */
+                               fields[0],              /* "Common Name" */
+                               atoll (fields[2]),      /* "Bytes Received" */
+                               atoll (fields[3]));     /* "Bytes Sent" */
+               read = 1;
        }
 
        return (read);