/* 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++)
- lf->fields[j].values[i] = strtoul(ptr, &ptr, 16);
+ 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
+ lf->fields[j].values[i] += f;
+ }
}
return num_lines;
}
-static int time_after(struct timeval *last,
- struct timeval *tout,
+static int time_after(struct timeval *last,
+ struct timeval *tout,
struct timeval *now)
{
if (now->tv_sec > last->tv_sec + tout->tv_sec)
int lnstat_update(struct lnstat_file *lnstat_files)
{
struct lnstat_file *lf;
- char buf[FGETS_BUF_SIZE];
struct timeval tv;
gettimeofday(&tv, NULL);
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];
+ for (i = 0, lfi = &lf->fields[i];
i < lf->num_fields; i++, lfi = &lf->fields[i]) {
if (i == 0)
lfi->result = lfi->values[1];
else
lfi->result = (lfi->values[1]-lfi->values[0])
- / lf->interval.tv_sec;
+ / lf->interval.tv_sec;
}
- rewind(lf->fp);
- fgets(buf, sizeof(buf)-1, lf->fp);
scan_lines(lf, 0);
}
}
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);
}
{
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);
}
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;
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;
/* open */
lf->fp = fopen(lf->path, "r");
if (!lf->fp) {
+ perror(lf->path);
free(lf);
return NULL;
}
if (!path)
path = PROC_NET_STAT;
-
+
dir = opendir(path);
if (!dir) {
struct lnstat_file *lf;
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;
}
struct lnstat_field *lnstat_find_field(struct lnstat_file *lnstat_files,
- char *name)
+ const char *name)
{
struct lnstat_file *lf;
struct lnstat_field *ret = NULL;
- char *colon = strchr(name, ':');
- char *file, *field;
+ const char *colon = strchr(name, ':');
+ char *file;
+ const char *field;
if (colon) {
file = strndup(name, colon-name);
file = NULL;
field = name;
}
-
+
for (lf = lnstat_files; lf; lf = lf->next) {
int i;
}
}
out:
- if (file)
- free(file);
+ free(file);
return ret;
}