Changeset 4703

Show
Ignore:
Timestamp:
08/26/07 10:28:10 (7 years ago)
Author:
khali
Message:

Thanks to the new prototype of sensors_get_detected_chips, we no
longer need to build an array from the chip names passed on the
command line. Instead, we can process them one after the other
directly. This saves some memory and allows for minor code cleanups.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lm-sensors/branches/lm-sensors-3.0.0/prog/sensors/main.c

    r4701 r4703  
    4949static void do_a_print(const sensors_chip_name *name); 
    5050static int do_a_set(const sensors_chip_name *name); 
    51 static int do_the_real_work(int *error); 
     51static int do_the_real_work(const sensors_chip_name *chip, int *error); 
    5252static const char *sprintf_chip_name(const sensors_chip_name *name); 
    5353 
    54 #define CHIPS_MAX 20 
    55 sensors_chip_name chips[CHIPS_MAX]; 
    56 int chips_count=0; 
    5754int do_sets, do_raw, fahrenheit, hide_adapter; 
    5855 
     
    201198  } 
    202199 
    203   if (optind == argc) { 
    204     chips[0].prefix = SENSORS_CHIP_NAME_PREFIX_ANY; 
    205     chips[0].bus.type = SENSORS_BUS_TYPE_ANY; 
    206     chips[0].bus.nr = SENSORS_BUS_NR_ANY; 
    207     chips[0].addr = SENSORS_CHIP_NAME_ADDR_ANY; 
    208     chips_count = 1; 
    209   } else  
    210     for(i = optind; i < argc; i++)  
    211       if ((res = sensors_parse_chip_name(argv[i],chips+chips_count))) { 
    212         fprintf(stderr,"Parse error in chip name `%s'\n",argv[i]); 
    213         print_short_help(); 
    214         exit(1); 
    215       } else if (++chips_count == CHIPS_MAX) { 
    216         fprintf(stderr,"Too many chips on command line!\n"); 
    217         exit(1); 
    218       } 
    219  
    220200  open_config_file(config_file_name); 
    221201  if ((res = sensors_init(config_file))) { 
     
    228208  set_degstr(); 
    229209 
    230   if(do_the_real_work(&error)) { 
    231     sensors_cleanup(); 
    232     exit(error); 
     210  if (optind == argc) { /* No chip name on command line */ 
     211    if (!do_the_real_work(NULL, &error)) { 
     212      fprintf(stderr, 
     213              "No sensors found!\n" 
     214              "Make sure you loaded all the kernel drivers you need.\n" 
     215              "Try sensors-detect to find out which these are.\n"); 
     216      error = 1; 
     217    } 
    233218  } else { 
    234     if (optind == argc) /* No chip name on command line */ 
    235             fprintf(stderr, 
    236                     "No sensors found!\n" 
    237                     "Make sure you loaded all the kernel drivers you need.\n" 
    238                     "Try sensors-detect to find out which these are.\n"); 
    239     else 
    240             fprintf(stderr,"Specified sensor(s) not found!\n"); 
    241     sensors_cleanup(); 
    242     exit(1); 
    243   } 
     219    int cnt = 0; 
     220    sensors_chip_name chip; 
     221 
     222    for (i = optind; i < argc; i++) { 
     223      if (sensors_parse_chip_name(argv[i], &chip)) { 
     224        fprintf(stderr, "Parse error in chip name `%s'\n", argv[i]); 
     225        print_short_help(); 
     226        error = 1; 
     227        goto exit; 
     228      } 
     229      cnt += do_the_real_work(&chip, &error); 
     230    } 
     231 
     232    if (!cnt) { 
     233      fprintf(stderr, "Specified sensor(s) not found!\n"); 
     234      error = 1; 
     235    } 
     236  } 
     237 
     238exit: 
     239  sensors_cleanup(); 
     240  exit(res); 
    244241} 
    245242 
    246243/* returns number of chips found */ 
    247 int do_the_real_work(int *error) 
     244int do_the_real_work(const sensors_chip_name *match, int *error) 
    248245{ 
    249246  const sensors_chip_name *chip; 
    250   int chip_nr,i; 
     247  int chip_nr; 
    251248  int cnt = 0; 
    252249 
    253   *error = 0; 
    254   for (i = 0; i < chips_count; i++) { 
    255     chip_nr = 0; 
    256     while ((chip = sensors_get_detected_chips(&chips[i], &chip_nr))) { 
    257       if (do_sets) { 
    258         if (do_a_set(chip)) 
    259           *error = 1; 
    260       } else 
    261         do_a_print(chip); 
    262       cnt++; 
    263     } 
     250  chip_nr = 0; 
     251  while ((chip = sensors_get_detected_chips(match, &chip_nr))) { 
     252    if (do_sets) { 
     253      if (do_a_set(chip)) 
     254        *error = 1; 
     255    } else 
     256      do_a_print(chip); 
     257    cnt++; 
    264258  } 
    265259  return cnt;