diff --git a/cputree.c b/cputree.c index 370e426..670b1ba 100644 --- a/cputree.c +++ b/cputree.c @@ -142,7 +142,7 @@ static void do_one_cpu(char *path) file = fopen(new_path, "r"); if (file) { char *line = NULL; - size_t size=0; + size_t size = 0; if (getline(&line, &size, file)==0) return; fclose(file); @@ -176,8 +176,8 @@ static void do_one_cpu(char *path) file = fopen(new_path, "r"); cpu_set(cpu->number, cpu->package_mask); if (file) { - char *line; - size_t size; + char *line = NULL; + size_t size = 0; if (getline(&line, &size, file)) cpumask_parse_user(line, strlen(line), cpu->package_mask); fclose(file); @@ -190,8 +190,8 @@ static void do_one_cpu(char *path) snprintf(new_path, PATH_MAX, "%s/cache/index1/shared_cpu_map", path); file = fopen(new_path, "r"); if (file) { - char *line; - size_t size; + char *line = NULL; + size_t size = 0; if (getline(&line, &size, file)) cpumask_parse_user(line, strlen(line), cpu->cache_mask); fclose(file); @@ -200,8 +200,8 @@ static void do_one_cpu(char *path) snprintf(new_path, PATH_MAX, "%s/cache/index2/shared_cpu_map", path); file = fopen(new_path, "r"); if (file) { - char *line; - size_t size; + char *line = NULL; + size_t size = 0; if (getline(&line, &size, file)) cpumask_parse_user(line, strlen(line), cpu->cache_mask); fclose(file); diff --git a/network.c b/network.c index 7cb8439..5c16a40 100644 --- a/network.c +++ b/network.c @@ -24,6 +24,7 @@ * even though the amount of work is high; this file is there to compensate for this * by adding actual package counts to the calculated amount of work of interrupts */ +#define _GNU_SOURCE #include #include #include @@ -123,15 +124,20 @@ void account_for_nic_stats(void) { struct nic *nic; FILE *file; - char line[8192]; + char *line = NULL; + size_t size = 0; file = fopen("/proc/net/dev", "r"); if (!file) return; /* first two lines are headers */ - if (fgets(line, 8191, file)==NULL) + if (getline(&line, &size, file)==0) { + free(line); return; - if (fgets(line, 8191, file)==NULL) + } + if (getline(&line, &size, file)==0) { + free(line); return; + } while (!feof(file)) { uint64_t rxcount; @@ -139,7 +145,9 @@ void account_for_nic_stats(void) uint64_t delta; int dummy; char *c, *c2; - if (fgets(line, 8191, file)==NULL) + if (getline(&line, &size, file)==0) + break; + if (line==NULL) break; c = strchr(line, ':'); if (c==NULL) /* header line */ @@ -172,4 +180,5 @@ void account_for_nic_stats(void) } fclose(file); + free(line); } diff --git a/numa.c b/numa.c index 8c26e01..1efb5b3 100644 --- a/numa.c +++ b/numa.c @@ -24,6 +24,7 @@ * In addition the PCI class information is used to refine the classification * of interrupt sources */ +#define _GNU_SOURCE #include #include #include diff --git a/powermode.c b/powermode.c index acf8bb5..e0a10b7 100644 --- a/powermode.c +++ b/powermode.c @@ -18,6 +18,7 @@ * 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301 USA */ +#define _GNU_SOURCE #include #include #include @@ -36,7 +37,8 @@ static unsigned int hysteresis; void check_power_mode(void) { FILE *file; - char line[4096]; + char *line = NULL; + size_t size = 0; char *c; uint64_t dummy, irq, softirq; line[0]=0; @@ -44,9 +46,11 @@ void check_power_mode(void) file = fopen("/proc/stat", "r"); if (!file) return; - if (fgets(line, 4095, file)==NULL) - memset(line,0, 4096); + if (getline(&line, &size, file)==0) + size=0; fclose(file); + if (!line) + return; c=&line[4]; dummy = strtoull(c, &c, 10); /* user */ dummy = strtoull(c, &c, 10); /* nice */ @@ -71,5 +75,6 @@ void check_power_mode(void) hysteresis = 0; } previous = irq; + free(line); } diff --git a/procinterrupts.c b/procinterrupts.c index 3d84b01..7461c84 100644 --- a/procinterrupts.c +++ b/procinterrupts.c @@ -18,6 +18,7 @@ * 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301 USA */ +#define _GNU_SOURCE #include #include #include @@ -32,16 +33,18 @@ void parse_proc_interrupts(void) { FILE *file; - char line[LINESIZE+1]; + char *line = NULL; + size_t size = 0; - line[LINESIZE] = 0; file = fopen("/proc/interrupts", "r"); if (!file) return; /* first line is the header we don't need; nuke it */ - if (fgets(line, LINESIZE, file)==NULL) + if (getline(&line, &size, file)==0) { + free(line); return; + } while (!feof(file)) { cpumask_t present; @@ -50,7 +53,7 @@ void parse_proc_interrupts(void) uint64_t count; char *c, *c2; - if (fgets(line, LINESIZE, file)==NULL) + if (getline(&line, &size, file)==0) break; @@ -85,4 +88,5 @@ void parse_proc_interrupts(void) set_interrupt_count(number, count, &present); } fclose(file); + free(line); }