Browse Source

Limit the stack usage in gather_i2c_busses:

* Reuse variables where possible.
* Don't assign things we don't need in sscanf.


git-svn-id: http://lm-sensors.org/svn/i2c-tools/trunk@5196 7894878c-1315-0410-8ee3-d5d059ff63e0
tags/v3.0.2
Jean Delvare 18 years ago
parent
commit
e34ebe15e7
  1. 25
      tools/i2cbusses.c

25
tools/i2cbusses.c

@ -130,13 +130,11 @@ static struct i2c_adap *more_adapters(struct i2c_adap *adapters, int n)
static struct i2c_adap *gather_i2c_busses(void) static struct i2c_adap *gather_i2c_busses(void)
{ {
FILE *fptr;
char s[100];
char s[120];
struct dirent *de, *dde; struct dirent *de, *dde;
DIR *dir, *ddir; DIR *dir, *ddir;
FILE *f; FILE *f;
char *border;
char dev[NAME_MAX], fstype[NAME_MAX], sysfs[NAME_MAX], n[NAME_MAX];
char fstype[NAME_MAX], sysfs[NAME_MAX], n[NAME_MAX];
int foundsysfs = 0; int foundsysfs = 0;
int count=0; int count=0;
struct i2c_adap *adapters; struct i2c_adap *adapters;
@ -146,8 +144,8 @@ static struct i2c_adap *gather_i2c_busses(void)
return NULL; return NULL;
/* look in /proc/bus/i2c */ /* look in /proc/bus/i2c */
if((fptr = fopen("/proc/bus/i2c", "r"))) {
while(fgets(s, 100, fptr)) {
if ((f = fopen("/proc/bus/i2c", "r"))) {
while (fgets(s, 120, f)) {
char *algo, *name, *type, *all; char *algo, *name, *type, *all;
int len_algo, len_name, len_type; int len_algo, len_name, len_type;
int i2cbus; int i2cbus;
@ -185,7 +183,7 @@ static struct i2c_adap *gather_i2c_busses(void)
algo); algo);
count++; count++;
} }
fclose(fptr);
fclose(f);
goto done; goto done;
} }
@ -195,7 +193,7 @@ static struct i2c_adap *gather_i2c_busses(void)
goto done; goto done;
} }
while (fgets(n, NAME_MAX, f)) { while (fgets(n, NAME_MAX, f)) {
sscanf(n, "%[^ ] %[^ ] %[^ ] %*s\n", dev, sysfs, fstype);
sscanf(n, "%*[^ ] %[^ ] %[^ ] %*s\n", sysfs, fstype);
if (strcasecmp(fstype, "sysfs") == 0) { if (strcasecmp(fstype, "sysfs") == 0) {
foundsysfs++; foundsysfs++;
break; break;
@ -253,20 +251,19 @@ found:
if (f != NULL) { if (f != NULL) {
int i2cbus; int i2cbus;
enum adt type; enum adt type;
char x[120];
char *px; char *px;
px = fgets(x, 120, f);
px = fgets(s, 120, f);
fclose(f); fclose(f);
if (!px) { if (!px) {
fprintf(stderr, "%s: read error\n", n); fprintf(stderr, "%s: read error\n", n);
continue; continue;
} }
if ((border = strchr(x, '\n')) != NULL)
*border = 0;
if ((px = strchr(s, '\n')) != NULL)
*px = 0;
if (!sscanf(de->d_name, "i2c-%d", &i2cbus)) if (!sscanf(de->d_name, "i2c-%d", &i2cbus))
continue; continue;
if(!strncmp(x, "ISA ", 4)) {
if (!strncmp(s, "ISA ", 4)) {
type = adt_isa; type = adt_isa;
} else { } else {
/* Attempt to probe for adapter capabilities */ /* Attempt to probe for adapter capabilities */
@ -281,7 +278,7 @@ found:
} }
adapters[count].nr = i2cbus; adapters[count].nr = i2cbus;
adapters[count].name = strdup(x);
adapters[count].name = strdup(s);
if (adapters[count].name == NULL) { if (adapters[count].name == NULL) {
free_adapters(adapters); free_adapters(adapters);
return NULL; return NULL;

Loading…
Cancel
Save