Commit c4527d7b authored by Phil Sutter's avatar Phil Sutter Committed by Stephen Hemminger

ip{,6}tunnel: align do_tunnels_list() a bit

In iptunnel, declare loop variables inside the loop as done in
ip6tunnel.

Fix and simplify goto logic in ip6tunnel:
- Failure to read over header lines would have left fp opened.
- By returning directly upon fopen() failure, fp can be closed
  unconditionally in the end.

Use the same goto logic in iptunnel, as well.
Signed-off-by: default avatarPhil Sutter <phil@nwl.cc>
parent 4b3cb962
...@@ -326,14 +326,14 @@ static int do_tunnels_list(struct ip6_tnl_parm2 *p) ...@@ -326,14 +326,14 @@ static int do_tunnels_list(struct ip6_tnl_parm2 *p)
FILE *fp = fopen("/proc/net/dev", "r"); FILE *fp = fopen("/proc/net/dev", "r");
if (fp == NULL) { if (fp == NULL) {
perror("fopen"); perror("fopen");
goto end; return -1;
} }
/* skip two lines at the begenning of the file */ /* skip two lines at the begenning of the file */
if (!fgets(buf, sizeof(buf), fp) || if (!fgets(buf, sizeof(buf), fp) ||
!fgets(buf, sizeof(buf), fp)) { !fgets(buf, sizeof(buf), fp)) {
fprintf(stderr, "/proc/net/dev read error\n"); fprintf(stderr, "/proc/net/dev read error\n");
return -1; goto end;
} }
while (fgets(buf, sizeof(buf), fp) != NULL) { while (fgets(buf, sizeof(buf), fp) != NULL) {
...@@ -395,10 +395,8 @@ static int do_tunnels_list(struct ip6_tnl_parm2 *p) ...@@ -395,10 +395,8 @@ static int do_tunnels_list(struct ip6_tnl_parm2 *p)
printf("\n"); printf("\n");
} }
err = 0; err = 0;
end: end:
if (fp) fclose(fp);
fclose(fp);
return err; return err;
} }
......
...@@ -396,14 +396,8 @@ static void print_tunnel(struct ip_tunnel_parm *p) ...@@ -396,14 +396,8 @@ static void print_tunnel(struct ip_tunnel_parm *p)
static int do_tunnels_list(struct ip_tunnel_parm *p) static int do_tunnels_list(struct ip_tunnel_parm *p)
{ {
char name[IFNAMSIZ];
unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
rx_fifo, rx_frame,
tx_bytes, tx_packets, tx_errs, tx_drops,
tx_fifo, tx_colls, tx_carrier, rx_multi;
struct ip_tunnel_parm p1;
char buf[512]; char buf[512];
int err = -1;
FILE *fp = fopen("/proc/net/dev", "r"); FILE *fp = fopen("/proc/net/dev", "r");
if (fp == NULL) { if (fp == NULL) {
perror("fopen"); perror("fopen");
...@@ -414,19 +408,24 @@ static int do_tunnels_list(struct ip_tunnel_parm *p) ...@@ -414,19 +408,24 @@ static int do_tunnels_list(struct ip_tunnel_parm *p)
if (!fgets(buf, sizeof(buf), fp) || if (!fgets(buf, sizeof(buf), fp) ||
!fgets(buf, sizeof(buf), fp)) { !fgets(buf, sizeof(buf), fp)) {
fprintf(stderr, "/proc/net/dev read error\n"); fprintf(stderr, "/proc/net/dev read error\n");
fclose(fp); goto end;
return -1;
} }
while (fgets(buf, sizeof(buf), fp) != NULL) { while (fgets(buf, sizeof(buf), fp) != NULL) {
char name[IFNAMSIZ];
int index, type; int index, type;
unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
rx_fifo, rx_frame,
tx_bytes, tx_packets, tx_errs, tx_drops,
tx_fifo, tx_colls, tx_carrier, rx_multi;
struct ip_tunnel_parm p1;
char *ptr; char *ptr;
buf[sizeof(buf) - 1] = 0; buf[sizeof(buf) - 1] = 0;
if ((ptr = strchr(buf, ':')) == NULL || if ((ptr = strchr(buf, ':')) == NULL ||
(*ptr++ = 0, sscanf(buf, "%s", name) != 1)) { (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
fprintf(stderr, "Wrong format for /proc/net/dev. Giving up.\n"); fprintf(stderr, "Wrong format for /proc/net/dev. Giving up.\n");
fclose(fp); goto end;
return -1;
} }
if (sscanf(ptr, "%ld%ld%ld%ld%ld%ld%ld%*d%ld%ld%ld%ld%ld%ld%ld", if (sscanf(ptr, "%ld%ld%ld%ld%ld%ld%ld%*d%ld%ld%ld%ld%ld%ld%ld",
&rx_bytes, &rx_packets, &rx_errs, &rx_drops, &rx_bytes, &rx_packets, &rx_errs, &rx_drops,
...@@ -467,8 +466,10 @@ static int do_tunnels_list(struct ip_tunnel_parm *p) ...@@ -467,8 +466,10 @@ static int do_tunnels_list(struct ip_tunnel_parm *p)
} }
printf("\n"); printf("\n");
} }
err = 0;
end:
fclose(fp); fclose(fp);
return 0; return err;
} }
static int do_show(int argc, char **argv) static int do_show(int argc, char **argv)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment