]> git.proxmox.com Git - mirror_iproute2.git/blobdiff - misc/nstat.c
nstat: print useful error messages in abort() cases
[mirror_iproute2.git] / misc / nstat.c
index a4dd405d43a93e86e712475eb3b9e3e733bee839..425e75ef461ec33146b761c29d63681c4e0fa7cd 100644 (file)
@@ -37,7 +37,6 @@ int reset_history;
 int ignore_history;
 int no_output;
 int json_output;
-int pretty;
 int no_update;
 int scan_interval;
 int time_constant;
@@ -143,14 +142,19 @@ static void load_good_table(FILE *fp)
                }
                /* idbuf is as big as buf, so this is safe */
                nr = sscanf(buf, "%s%llu%lg", idbuf, &val, &rate);
-               if (nr < 2)
-                       abort();
+               if (nr < 2) {
+                       fprintf(stderr, "%s:%d: error parsing history file\n",
+                               __FILE__, __LINE__);
+                       exit(-2);
+               }
                if (nr < 3)
                        rate = 0;
                if (useless_number(idbuf))
                        continue;
-               if ((n = malloc(sizeof(*n))) == NULL)
-                       abort();
+               if ((n = malloc(sizeof(*n))) == NULL) {
+                       perror("nstat: malloc");
+                       exit(-1);
+               }
                n->id = strdup(idbuf);
                n->val = val;
                n->rate = rate;
@@ -178,19 +182,24 @@ static int count_spaces(const char *line)
 
 static void load_ugly_table(FILE *fp)
 {
-       char buf[4096];
+       char *buf = NULL;
+       size_t buflen = 0;
+       ssize_t nread;
        struct nstat_ent *db = NULL;
        struct nstat_ent *n;
 
-       while (fgets(buf, sizeof(buf), fp) != NULL) {
-               char idbuf[sizeof(buf)];
+       while ((nread = getline(&buf, &buflen, fp)) != -1) {
+               char idbuf[4096];
                int  off;
                char *p;
                int count1, count2, skip = 0;
 
                p = strchr(buf, ':');
-               if (!p)
-                       abort();
+               if (!p) {
+                       fprintf(stderr, "%s:%d: error parsing history file\n",
+                               __FILE__, __LINE__);
+                       exit(-2);
+               }
                count1 = count_spaces(buf);
                *p = 0;
                idbuf[0] = 0;
@@ -210,8 +219,10 @@ static void load_ugly_table(FILE *fp)
                                strncat(idbuf, p, sizeof(idbuf) - off - 1);
                        }
                        n = malloc(sizeof(*n));
-                       if (!n)
-                               abort();
+                       if (!n) {
+                               perror("nstat: malloc");
+                               exit(-1);
+                       }
                        n->id = strdup(idbuf);
                        n->rate = 0;
                        n->next = db;
@@ -219,18 +230,28 @@ static void load_ugly_table(FILE *fp)
                        p = next;
                }
                n = db;
-               if (fgets(buf, sizeof(buf), fp) == NULL)
-                       abort();
+               nread = getline(&buf, &buflen, fp);
+               if (nread == -1) {
+                       fprintf(stderr, "%s:%d: error parsing history file\n",
+                               __FILE__, __LINE__);
+                       exit(-2);
+               }
                count2 = count_spaces(buf);
                if (count2 > count1)
                        skip = count2 - count1;
                do {
                        p = strrchr(buf, ' ');
-                       if (!p)
-                               abort();
+                       if (!p) {
+                               fprintf(stderr, "%s:%d: error parsing history file\n",
+                                       __FILE__, __LINE__);
+                               exit(-2);
+                       }
                        *p = 0;
-                       if (sscanf(p+1, "%llu", &n->val) != 1)
-                               abort();
+                       if (sscanf(p+1, "%llu", &n->val) != 1) {
+                               fprintf(stderr, "%s:%d: error parsing history file\n",
+                                       __FILE__, __LINE__);
+                               exit(-2);
+                       }
                        /* Trick to skip "dummy" trailing ICMP MIB in 2.4 */
                        if (skip)
                                skip--;
@@ -238,6 +259,7 @@ static void load_ugly_table(FILE *fp)
                                n = n->next;
                } while (p > buf + off + 2);
        }
+       free(buf);
 
        while (db) {
                n = db;
@@ -525,18 +547,18 @@ static void usage(void) __attribute__((noreturn));
 static void usage(void)
 {
        fprintf(stderr,
-"Usage: nstat [OPTION] [ PATTERN [ PATTERN ] ]\n"
-"   -h, --help           this message\n"
-"   -a, --ignore         ignore history\n"
-"   -d, --scan=SECS      sample every statistics every SECS\n"
-"   -j, --json           format output in JSON\n"
-"   -n, --nooutput       do history only\n"
-"   -p, --pretty         pretty print\n"
-"   -r, --reset          reset history\n"
-"   -s, --noupdate       don't update history\n"
-"   -t, --interval=SECS  report average over the last SECS\n"
-"   -V, --version        output version information\n"
-"   -z, --zeros          show entries with zero activity\n");
+               "Usage: nstat [OPTION] [ PATTERN [ PATTERN ] ]\n"
+               "   -h, --help          this message\n"
+               "   -a, --ignore        ignore history\n"
+               "   -d, --scan=SECS     sample every statistics every SECS\n"
+               "   -j, --json          format output in JSON\n"
+               "   -n, --nooutput      do history only\n"
+               "   -p, --pretty        pretty print\n"
+               "   -r, --reset         reset history\n"
+               "   -s, --noupdate      don't update history\n"
+               "   -t, --interval=SECS report average over the last SECS\n"
+               "   -V, --version       output version information\n"
+               "   -z, --zeros         show entries with zero activity\n");
        exit(-1);
 }