]> git.proxmox.com Git - mirror_iproute2.git/blobdiff - misc/lnstat_util.c
ll_map: Add function to remove link cache entry by index
[mirror_iproute2.git] / misc / lnstat_util.c
index 433e992976eac630c1858f7c1040aaa8c7cee633..c2dc42ec1ff12a4848691d2e1ea2b642f0aa9631 100644 (file)
@@ -49,7 +49,7 @@ static int scan_lines(struct lnstat_file *lf, int i)
        if (!lf->compat && !fgets(buf, sizeof(buf)-1, lf->fp))
                return -1;
 
-       while(!feof(lf->fp) && fgets(buf, sizeof(buf)-1, lf->fp)) {
+       while (!feof(lf->fp) && fgets(buf, sizeof(buf)-1, lf->fp)) {
                char *ptr = buf;
 
                num_lines++;
@@ -58,6 +58,7 @@ static int scan_lines(struct lnstat_file *lf, int i)
 
                for (j = 0; j < lf->num_fields; j++) {
                        unsigned long f = strtoul(ptr, &ptr, 16);
+
                        if (j == 0)
                                lf->fields[j].values[i] = f;
                        else
@@ -102,7 +103,7 @@ int lnstat_update(struct lnstat_file *lnstat_files)
                                        lfi->result = lfi->values[1];
                                else
                                        lfi->result = (lfi->values[1]-lfi->values[0])
-                                                       / lf->interval.tv_sec;
+                                                       / lf->interval.tv_sec;
                        }
 
                        scan_lines(lf, 0);
@@ -138,7 +139,8 @@ static int lnstat_scan_fields(struct lnstat_file *lf)
        char buf[FGETS_BUF_SIZE];
 
        rewind(lf->fp);
-       fgets(buf, sizeof(buf)-1, lf->fp);
+       if (!fgets(buf, sizeof(buf)-1, lf->fp))
+               return -1;
 
        return __lnstat_scan_fields(lf, buf);
 }
@@ -148,7 +150,8 @@ static int lnstat_scan_compat_rtstat_fields(struct lnstat_file *lf)
 {
        char buf[FGETS_BUF_SIZE];
 
-       strncpy(buf, RTSTAT_COMPAT_LINE, sizeof(buf)-1);
+       strncpy(buf, RTSTAT_COMPAT_LINE, sizeof(buf) - 1);
+       buf[sizeof(buf) - 1] = '\0';
 
        return __lnstat_scan_fields(lf, buf);
 }
@@ -157,6 +160,7 @@ static int lnstat_scan_compat_rtstat_fields(struct lnstat_file *lf)
 static int name_in_array(const int num, const char **arr, const char *name)
 {
        int i;
+
        for (i = 0; i < num; i++) {
                if (!strcmp(arr[i], name))
                        return 1;
@@ -170,18 +174,15 @@ static struct lnstat_file *alloc_and_open(const char *path, const char *file)
        struct lnstat_file *lf;
 
        /* allocate */
-       lf = malloc(sizeof(*lf));
-       if (!lf)
+       lf = calloc(1, sizeof(*lf));
+       if (!lf) {
+               fprintf(stderr, "out of memory\n");
                return NULL;
+       }
 
        /* initialize */
-       memset(lf, 0, sizeof(*lf));
-
-       /* de->d_name is guaranteed to be <= NAME_MAX */
-       strcpy(lf->basename, file);
-       strcpy(lf->path, path);
-       strcat(lf->path, "/");
-       strcat(lf->path, lf->basename);
+       snprintf(lf->basename, sizeof(lf->basename), "%s", file);
+       snprintf(lf->path, sizeof(lf->path), "%s/%s", path, file);
 
        /* initialize to default */
        lf->interval.tv_sec = 1;
@@ -189,6 +190,7 @@ static struct lnstat_file *alloc_and_open(const char *path, const char *file)
        /* open */
        lf->fp = fopen(lf->path, "r");
        if (!lf->fp) {
+               perror(lf->path);
                free(lf);
                return NULL;
        }
@@ -255,12 +257,16 @@ struct lnstat_file *lnstat_scan_dir(const char *path, const int num_req_files,
                        continue;
 
                lf = alloc_and_open(path, de->d_name);
-               if (!lf)
+               if (!lf) {
+                       closedir(dir);
                        return NULL;
+               }
 
                /* fill in field structure */
-               if (lnstat_scan_fields(lf) < 0)
+               if (lnstat_scan_fields(lf) < 0) {
+                       closedir(dir);
                        return NULL;
+               }
 
                /* prepend to global list */
                lf->next = lnstat_files;