Changeset 5196

Show
Ignore:
Timestamp:
04/20/08 19:36:50 (7 months ago)
Author:
khali
Message:

Limit the stack usage in gather_i2c_busses:
* Reuse variables where possible.
* Don't assign things we don't need in sscanf.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • i2c-tools/trunk/tools/i2cbusses.c

    r5195 r5196  
    131131static struct i2c_adap *gather_i2c_busses(void) 
    132132{ 
    133         FILE *fptr; 
    134         char s[100]; 
     133        char s[120]; 
    135134        struct dirent *de, *dde; 
    136135        DIR *dir, *ddir; 
    137136        FILE *f; 
    138         char *border; 
    139         char dev[NAME_MAX], fstype[NAME_MAX], sysfs[NAME_MAX], n[NAME_MAX]; 
     137        char fstype[NAME_MAX], sysfs[NAME_MAX], n[NAME_MAX]; 
    140138        int foundsysfs = 0; 
    141139        int count=0; 
     
    147145 
    148146        /* look in /proc/bus/i2c */ 
    149         if((fptr = fopen("/proc/bus/i2c", "r"))) { 
    150                 while(fgets(s, 100, fptr)) { 
     147        if ((f = fopen("/proc/bus/i2c", "r"))) { 
     148                while (fgets(s, 120, f)) { 
    151149                        char *algo, *name, *type, *all; 
    152150                        int len_algo, len_name, len_type; 
     
    186184                        count++; 
    187185                } 
    188                 fclose(fptr); 
     186                fclose(f); 
    189187                goto done; 
    190188        } 
     
    196194        } 
    197195        while (fgets(n, NAME_MAX, f)) { 
    198                 sscanf(n, "%[^ ] %[^ ] %[^ ] %*s\n", dev, sysfs, fstype); 
     196                sscanf(n, "%*[^ ] %[^ ] %[^ ] %*s\n", sysfs, fstype); 
    199197                if (strcasecmp(fstype, "sysfs") == 0) { 
    200198                        foundsysfs++; 
     
    254252                        int i2cbus; 
    255253                        enum adt type; 
    256                         char    x[120]; 
    257254                        char *px; 
    258255 
    259                         px = fgets(x, 120, f); 
     256                        px = fgets(s, 120, f); 
    260257                        fclose(f); 
    261258                        if (!px) { 
     
    263260                                continue; 
    264261                        } 
    265                         if ((border = strchr(x, '\n')) != NULL) 
    266                                 *border = 0; 
     262                        if ((px = strchr(s, '\n')) != NULL) 
     263                                *px = 0; 
    267264                        if (!sscanf(de->d_name, "i2c-%d", &i2cbus)) 
    268265                                continue; 
    269                         if(!strncmp(x, "ISA ", 4)) { 
     266                        if (!strncmp(s, "ISA ", 4)) { 
    270267                                type = adt_isa; 
    271268                        } else { 
     
    282279 
    283280                        adapters[count].nr = i2cbus; 
    284                         adapters[count].name = strdup(x); 
     281                        adapters[count].name = strdup(s); 
    285282                        if (adapters[count].name == NULL) { 
    286283                                free_adapters(adapters);