From: Phil Sutter <phil@nwl.cc>
To: Stephen Hemminger <shemming@brocade.com>
Cc: netdev@vger.kernel.org
Subject: [iproute PATCH 1/5] lnstat: review lnstat_update()
Date: Sat, 28 Nov 2015 01:00:01 +0100 [thread overview]
Message-ID: <1448668805-28074-2-git-send-email-phil@nwl.cc> (raw)
In-Reply-To: <1448668805-28074-1-git-send-email-phil@nwl.cc>
Instead of calling rewind() and fgets() before every call to
scan_lines(), move them into scan_lines() itself.
This should also fix compat mode, as before the second call to
scan_lines() the first line was skipped unconditionally.
Signed-off-by: Phil Sutter <phil@nwl.cc>
---
misc/lnstat_util.c | 18 +++++++-----------
1 file changed, 7 insertions(+), 11 deletions(-)
diff --git a/misc/lnstat_util.c b/misc/lnstat_util.c
index 6dde7c4943271..433e992976eac 100644
--- a/misc/lnstat_util.c
+++ b/misc/lnstat_util.c
@@ -38,18 +38,22 @@
/* Read (and summarize for SMP) the different stats vars. */
static int scan_lines(struct lnstat_file *lf, int i)
{
+ char buf[FGETS_BUF_SIZE];
int j, num_lines = 0;
for (j = 0; j < lf->num_fields; j++)
lf->fields[j].values[i] = 0;
- while(!feof(lf->fp)) {
- char buf[FGETS_BUF_SIZE];
+ rewind(lf->fp);
+ /* skip first line */
+ if (!lf->compat && !fgets(buf, sizeof(buf)-1, lf->fp))
+ return -1;
+
+ while(!feof(lf->fp) && fgets(buf, sizeof(buf)-1, lf->fp)) {
char *ptr = buf;
num_lines++;
- fgets(buf, sizeof(buf)-1, lf->fp);
gettimeofday(&lf->last_read, NULL);
for (j = 0; j < lf->num_fields; j++) {
@@ -81,7 +85,6 @@ static int time_after(struct timeval *last,
int lnstat_update(struct lnstat_file *lnstat_files)
{
struct lnstat_file *lf;
- char buf[FGETS_BUF_SIZE];
struct timeval tv;
gettimeofday(&tv, NULL);
@@ -91,11 +94,6 @@ int lnstat_update(struct lnstat_file *lnstat_files)
int i;
struct lnstat_field *lfi;
- rewind(lf->fp);
- if (!lf->compat) {
- /* skip first line */
- fgets(buf, sizeof(buf)-1, lf->fp);
- }
scan_lines(lf, 1);
for (i = 0, lfi = &lf->fields[i];
@@ -107,8 +105,6 @@ int lnstat_update(struct lnstat_file *lnstat_files)
/ lf->interval.tv_sec;
}
- rewind(lf->fp);
- fgets(buf, sizeof(buf)-1, lf->fp);
scan_lines(lf, 0);
}
}
--
2.5.0
next prev parent reply other threads:[~2015-11-28 0:00 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-11-28 0:00 [iproute PATCH 0/5] warning-annoyance induced code-review Phil Sutter
2015-11-28 0:00 ` Phil Sutter [this message]
2015-11-28 0:00 ` [iproute PATCH 2/5] ss: reduce max indentation level in init_service_resolver() Phil Sutter
2015-11-28 0:00 ` [iproute PATCH 3/5] ss: review is_ephemeral() Phil Sutter
2015-11-28 0:00 ` [iproute PATCH 4/5] get rid of remaining -Wunused-result warnings Phil Sutter
2015-11-28 0:00 ` [iproute PATCH 5/5] get rid of unnecessary fgets() buffer size limitation Phil Sutter
2015-11-29 19:51 ` [iproute PATCH 0/5] warning-annoyance induced code-review Stephen Hemminger
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1448668805-28074-2-git-send-email-phil@nwl.cc \
--to=phil@nwl.cc \
--cc=netdev@vger.kernel.org \
--cc=shemming@brocade.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).