Changeset 4708

Show
Ignore:
Timestamp:
08/26/07 12:57:16 (6 years ago)
Author:
khali
Message:

Mass reindent and coding style cleanups.

Location:
lm-sensors/branches/lm-sensors-3.0.0/prog/sensors
Files:
4 modified

Legend:

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

    r4698 r4708  
    3333static inline float deg_ctof(float cel) 
    3434{ 
    35    return ( cel * ( 9.0F / 5.0F ) + 32.0F ); 
     35        return cel * (9.0F / 5.0F) + 32.0F; 
    3636} 
    3737 
     
    4141   note: symbolic constants defined in chips.h */ 
    4242void print_temp_info(float n_cur, float n_over, float n_hyst, 
    43                      int minmax, int curprec, int limitprec) 
     43                     int minmax, int curprec, int limitprec) 
    4444{ 
    45    /* note: deg_ctof() will preserve HUGEVAL */ 
    46    if (fahrenheit) { 
    47       n_cur = deg_ctof(n_cur); 
    48       n_over = deg_ctof(n_over); 
    49       n_hyst = deg_ctof(n_hyst); 
    50    } 
     45        /* note: deg_ctof() will preserve HUGEVAL */ 
     46        if (fahrenheit) { 
     47                n_cur = deg_ctof(n_cur); 
     48                n_over = deg_ctof(n_over); 
     49                n_hyst = deg_ctof(n_hyst); 
     50        } 
    5151 
    52    /* use %* to pass precision as an argument */ 
    53    if (n_cur != HUGE_VAL) 
    54       printf("%+6.*f%s  ", curprec, n_cur, degstr); 
    55    else 
    56       printf("   FAULT  "); 
     52        /* use %* to pass precision as an argument */ 
     53        if (n_cur != HUGE_VAL) 
     54                printf("%+6.*f%s  ", curprec, n_cur, degstr); 
     55        else 
     56                printf("   FAULT  "); 
    5757 
    58    if(minmax == MINMAX) 
    59         printf("(low  = %+5.*f%s, high = %+5.*f%s)  ", 
    60             limitprec, n_hyst, degstr, 
    61             limitprec, n_over, degstr); 
    62    else if(minmax == MAXONLY) 
    63         printf("(high = %+5.*f%s)                  ", 
    64             limitprec, n_over, degstr); 
    65    else if(minmax == CRIT) 
    66         printf("(high = %+5.*f%s, crit = %+5.*f%s)  ", 
    67             limitprec, n_over, degstr, 
    68             limitprec, n_hyst, degstr); 
    69    else if(minmax == HYST) 
    70         printf("(high = %+5.*f%s, hyst = %+5.*f%s)  ", 
    71             limitprec, n_over, degstr, 
    72             limitprec, n_hyst, degstr); 
    73    else if(minmax == HYSTONLY) 
    74         printf("(hyst = %+5.*f%s)                  ", 
    75             limitprec, n_over, degstr); 
    76    else if(minmax != SINGLE) 
    77         printf("Unknown temperature mode!"); 
     58        if (minmax == MINMAX) 
     59                printf("(low  = %+5.*f%s, high = %+5.*f%s)  ", 
     60                       limitprec, n_hyst, degstr, 
     61                       limitprec, n_over, degstr); 
     62        else if (minmax == MAXONLY) 
     63                printf("(high = %+5.*f%s)                  ", 
     64                       limitprec, n_over, degstr); 
     65        else if (minmax == CRIT) 
     66                printf("(high = %+5.*f%s, crit = %+5.*f%s)  ", 
     67                       limitprec, n_over, degstr, 
     68                       limitprec, n_hyst, degstr); 
     69        else if (minmax == HYST) 
     70                printf("(high = %+5.*f%s, hyst = %+5.*f%s)  ", 
     71                       limitprec, n_over, degstr, 
     72                       limitprec, n_hyst, degstr); 
     73        else if (minmax == HYSTONLY) 
     74                printf("(hyst = %+5.*f%s)                  ", 
     75                       limitprec, n_over, degstr); 
     76        else if (minmax != SINGLE) 
     77                printf("Unknown temperature mode!"); 
    7878} 
    7979 
    8080void print_label(const char *label, int space) 
    8181{ 
    82   int len=strlen(label)+1; 
    83   if (len > space) 
    84     printf("%s:\n%*s", label, space, ""); 
    85   else 
    86     printf("%s:%*s", label, space - len, ""); 
     82        int len = strlen(label)+1; 
     83        if (len > space) 
     84                printf("%s:\n%*s", label, space, ""); 
     85        else 
     86                printf("%s:%*s", label, space - len, ""); 
    8787} 
    8888 
    8989void print_vid_info(const sensors_chip_name *name, int f_vid, int label_size) 
    9090{ 
    91   char *label; 
    92   double vid; 
     91        char *label; 
     92        double vid; 
    9393 
    94   if ((label = sensors_get_label(name, f_vid)) 
    95       && !sensors_get_value(name, f_vid, &vid)) { 
    96     print_label(label, label_size); 
    97     printf("%+6.3f V\n", vid); 
    98   } 
    99   free(label); 
     94        if ((label = sensors_get_label(name, f_vid)) 
     95        && !sensors_get_value(name, f_vid, &vid)) { 
     96                print_label(label, label_size); 
     97                printf("%+6.3f V\n", vid); 
     98        } 
     99        free(label); 
    100100} 
    101101 
    102102void print_chip_raw(const sensors_chip_name *name) 
    103103{ 
    104   int a; 
    105   const sensors_feature_data *data; 
    106   char *label; 
    107   double val; 
    108   
    109   a = 0; 
    110   while((data=sensors_get_all_features(name, &a))) { 
    111     if (!(label = sensors_get_label(name, data->number))) { 
    112       printf("ERROR: Can't get feature `%s' data!\n",data->name); 
    113       continue; 
    114     } 
    115     if (data->mode & SENSORS_MODE_R) { 
    116       if (sensors_get_value(name, data->number, &val)) { 
    117         printf("ERROR: Can't get feature `%s' data!\n",data->name); 
    118         continue; 
    119       } 
    120       if (data->mapping != SENSORS_NO_MAPPING) 
    121         printf("  %s: %.2f (%s)\n",label,val,data->name); 
    122       else 
    123         printf("%s: %.2f (%s)\n",label,val,data->name); 
    124     } else  
    125       printf("(%s)\n",label); 
    126     free(label); 
    127   } 
     104        int a; 
     105        const sensors_feature_data *data; 
     106        char *label; 
     107        double val; 
     108 
     109        a = 0; 
     110        while ((data = sensors_get_all_features(name, &a))) { 
     111                if (!(label = sensors_get_label(name, data->number))) { 
     112                        printf("ERROR: Can't get feature `%s' data!\n", 
     113                               data->name); 
     114                        continue; 
     115                } 
     116                if (data->mode & SENSORS_MODE_R) { 
     117                        if (sensors_get_value(name, data->number, &val)) { 
     118                                printf("ERROR: Can't get feature `%s' data!\n", 
     119                                       data->name); 
     120                                continue; 
     121                        } 
     122                        if (data->mapping != SENSORS_NO_MAPPING) 
     123                                printf("  %s: %.2f (%s)\n", label, val, 
     124                                       data->name); 
     125                        else 
     126                                printf("%s: %.2f (%s)\n", label, val, 
     127                                       data->name); 
     128                } else 
     129                        printf("(%s)\n", label); 
     130                free(label); 
     131        } 
    128132} 
    129  
  • lm-sensors/branches/lm-sensors-3.0.0/prog/sensors/chips.h

    r4698 r4708  
    3333#define HYSTONLY 5 
    3434void print_temp_info(float n_cur, float n_over, float n_hyst, 
    35                      int minmax, int curprec, int limitprec); 
    36                       
     35                     int minmax, int curprec, int limitprec); 
     36 
    3737void print_vid_info(const sensors_chip_name *name, int f_vid, int label_size); 
    3838 
  • lm-sensors/branches/lm-sensors-3.0.0/prog/sensors/chips_generic.c

    r4695 r4708  
    2727#include "chips.h" 
    2828 
    29 static int get_feature_value(const sensors_chip_name *name,  
    30                              const sensors_feature_data *feature,  
    31                              double *val) 
    32 { 
    33   return sensors_get_value(name, feature->number, val); 
    34 } 
    35  
    36 static void sensors_get_available_features(const sensors_chip_name *name,  
    37                                            const sensors_feature_data *feature,  
    38                                            int i, 
    39                                            short *has_features,  
    40                                            double *feature_vals,  
    41                                            int size,  
    42                                            int first_val) 
    43 { 
    44   const sensors_feature_data *iter; 
    45    
    46   while((iter = sensors_get_all_features(name, &i)) && 
    47       iter->mapping == feature->number) { 
    48     int indx; 
    49      
    50     indx = iter->type - first_val - 1; 
    51     if (indx < 0 || indx >= size) { 
    52       printf("ERROR: Bug in sensors: index out of bound"); 
    53       return; 
    54     } 
    55      
    56     if (get_feature_value(name, iter, &feature_vals[indx])) 
    57       printf("ERROR: Can't get %s data!\n", iter->name); 
    58      
    59     has_features[indx] = 1; 
    60   } 
     29static int get_feature_value(const sensors_chip_name *name, 
     30                             const sensors_feature_data *feature, 
     31                             double *val) 
     32{ 
     33        return sensors_get_value(name, feature->number, val); 
     34} 
     35 
     36static void sensors_get_available_features(const sensors_chip_name *name, 
     37                                           const sensors_feature_data *feature, 
     38                                           int i, short *has_features, 
     39                                           double *feature_vals, int size, 
     40                                           int first_val) 
     41{ 
     42        const sensors_feature_data *iter; 
     43 
     44        while ((iter = sensors_get_all_features(name, &i)) && 
     45               iter->mapping == feature->number) { 
     46                int indx; 
     47 
     48                indx = iter->type - first_val - 1; 
     49                if (indx < 0 || indx >= size) { 
     50                        printf("ERROR: Bug in sensors: index out of bound"); 
     51                        return; 
     52                } 
     53 
     54                if (get_feature_value(name, iter, &feature_vals[indx])) 
     55                        printf("ERROR: Can't get %s data!\n", iter->name); 
     56 
     57                has_features[indx] = 1; 
     58        } 
    6159} 
    6260 
    6361static int sensors_get_label_size(const sensors_chip_name *name) 
    6462{ 
    65   int i; 
    66   const sensors_feature_data *iter; 
    67   char *label; 
    68   unsigned int max_size = 11; /* Initialised to 11 as minumum label-width */ 
    69  
    70   i = 0; 
    71   while((iter = sensors_get_all_features(name, &i))) { 
    72     if ((label = sensors_get_label(name, iter->number)) && 
    73         strlen(label) > max_size) 
    74       max_size = strlen(label); 
    75     free(label); 
    76   } 
    77   return max_size + 1; 
     63        int i; 
     64        const sensors_feature_data *iter; 
     65        char *label; 
     66        unsigned int max_size = 11;     /* 11 as minumum label width */ 
     67 
     68        i = 0; 
     69        while ((iter = sensors_get_all_features(name, &i))) { 
     70                if ((label = sensors_get_label(name, iter->number)) && 
     71                    strlen(label) > max_size) 
     72                        max_size = strlen(label); 
     73                free(label); 
     74        } 
     75        return max_size + 1; 
    7876} 
    7977 
     
    8381static inline float deg_ctof(float cel) 
    8482{ 
    85    return ( cel * ( 9.0F / 5.0F ) + 32.0F ); 
    86 } 
    87  
    88 #define TEMP_FEATURE(x) has_features[x - SENSORS_FEATURE_TEMP - 1] 
    89 #define TEMP_FEATURE_VAL(x) feature_vals[x - SENSORS_FEATURE_TEMP - 1] 
    90 static void print_generic_chip_temp(const sensors_chip_name *name,  
    91                                     const sensors_feature_data *feature, 
    92                                     int i, int label_size) 
    93 { 
    94   double val, max, min; 
    95   char *label; 
    96   int type; 
    97   const int size = SENSORS_FEATURE_TEMP_SENS - SENSORS_FEATURE_TEMP; 
    98   short has_features[SENSORS_FEATURE_TEMP_SENS - SENSORS_FEATURE_TEMP] = {0, }; 
    99   double feature_vals[SENSORS_FEATURE_TEMP_SENS - SENSORS_FEATURE_TEMP] = {0.0, }; 
    100    
    101   if (!(label = sensors_get_label(name, feature->number))) { 
    102     printf("ERROR: Can't get temperature label!\n"); 
    103     return; 
    104   } 
    105    
    106   if (get_feature_value(name, feature, &val)) { 
    107     printf("ERROR: Can't get %s data!\n", label); 
    108     free(label); 
    109     return; 
    110   } 
    111    
    112   sensors_get_available_features(name, feature, i, has_features, 
    113       feature_vals, size, SENSORS_FEATURE_TEMP); 
    114    
    115   if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_MAX)) { 
    116     max = TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_MAX); 
    117      
    118     if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_MIN)) { 
    119       min = TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_MIN); 
    120       type = MINMAX; 
    121     } else if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_MAX_HYST)) { 
    122       min = TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_MAX_HYST); 
    123       type = HYST; 
    124     } else if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_CRIT)) { 
    125       min = TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT); 
    126       type = CRIT; 
    127     } else { 
    128       min = 0; 
    129       type = MAXONLY; 
    130     } 
    131   } else { 
    132     min = max = 0; 
    133     type = SINGLE; 
    134   } 
    135    
    136   if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_FAULT) && 
    137       TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_FAULT)) 
    138     val = HUGE_VAL; 
    139    
    140   print_label(label, label_size); 
    141   free(label); 
    142    
    143   print_temp_info(val, max, min, type, 1, 1); 
    144    
    145   /* ALARM features */ 
    146   if ((TEMP_FEATURE(SENSORS_FEATURE_TEMP_ALARM) &&  
    147        TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_ALARM) > 0.5) 
    148    || (type == MINMAX && 
    149        TEMP_FEATURE(SENSORS_FEATURE_TEMP_MIN_ALARM) &&  
    150        TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_MIN_ALARM) > 0.5) 
    151    || (type == MINMAX && 
    152        TEMP_FEATURE(SENSORS_FEATURE_TEMP_MAX_ALARM) &&  
    153        TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_MAX_ALARM) > 0.5) 
    154    || (type == CRIT && 
    155        TEMP_FEATURE(SENSORS_FEATURE_TEMP_CRIT_ALARM) &&  
    156        TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT_ALARM) > 0.5)) { 
    157     printf("ALARM  "); 
    158   } 
    159    
    160   if (type != CRIT && TEMP_FEATURE(SENSORS_FEATURE_TEMP_CRIT)) { 
    161     if (fahrenheit) { 
    162       TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT) = deg_ctof( 
    163         TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT)); 
    164       TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT_HYST) = deg_ctof( 
    165         TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT_HYST)); 
    166     } 
    167      
    168     if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_CRIT_HYST)) 
    169       printf("\n%*s(crit = %+5.1f%s, hyst = %+5.1f%s)  ", label_size + 10, "",  
    170         TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT), degstr, 
    171         TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT_HYST), degstr); 
    172     else 
    173       printf("\n%*s(crit = %+5.1f%s)  ", label_size + 10, "",  
    174         TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT), degstr); 
    175  
    176     if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_CRIT_ALARM) && 
    177         TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT_ALARM)) { 
    178       printf("ALARM  "); 
    179     } 
    180   }        
    181  
    182   /* print out temperature sensor info */ 
    183   if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_SENS)) { 
    184     int sens = (int)TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_SENS); 
    185      
    186     /* older kernels / drivers sometimes report a beta value for thermistors */ 
    187     if (sens > 1000)  
    188       sens = 4; 
    189      
    190     printf("sensor = %s", sens == 0 ? "disabled" : 
    191                           sens == 1 ? "diode" : 
    192                           sens == 2 ? "transistor" : 
    193                           sens == 3 ? "thermal diode" : 
    194                           sens == 4 ? "thermistor" : 
    195                           sens == 5 ? "AMD AMDSI" : 
    196                           sens == 6 ? "Intel PECI" : 
    197                           "unknown"); 
    198   } 
    199   printf("\n"); 
    200 } 
    201  
    202 #define IN_FEATURE(x) has_features[x - SENSORS_FEATURE_IN - 1] 
    203 #define IN_FEATURE_VAL(x) feature_vals[x - SENSORS_FEATURE_IN - 1] 
    204 static void print_generic_chip_in(const sensors_chip_name *name,  
    205                                   const sensors_feature_data *feature, 
    206                                   int i, int label_size) 
    207 { 
    208   const int size = SENSORS_FEATURE_IN_MAX_ALARM - SENSORS_FEATURE_IN; 
    209   short has_features[SENSORS_FEATURE_IN_MAX_ALARM - SENSORS_FEATURE_IN] = {0, }; 
    210   double feature_vals[SENSORS_FEATURE_IN_MAX_ALARM - SENSORS_FEATURE_IN] = {0.0, }; 
    211   double val, alarm_max, alarm_min; 
    212   char *label; 
    213    
    214   if (!(label = sensors_get_label(name, feature->number))) { 
    215     printf("ERROR: Can't get in label!\n"); 
    216     return; 
    217   } 
    218    
    219   if (get_feature_value(name, feature, &val)) { 
    220     printf("ERROR: Can't get %s data!\n", label); 
    221     free(label); 
    222     return; 
    223   } 
    224    
    225   sensors_get_available_features(name, feature, i, has_features, feature_vals, 
    226       size, SENSORS_FEATURE_IN); 
    227    
    228   print_label(label, label_size); 
    229   free(label); 
    230   printf("%+6.2f V", val); 
    231    
    232   if (IN_FEATURE(SENSORS_FEATURE_IN_MIN) && IN_FEATURE(SENSORS_FEATURE_IN_MAX)) 
    233     printf("  (min = %+6.2f V, max = %+6.2f V)", 
    234       IN_FEATURE_VAL(SENSORS_FEATURE_IN_MIN), 
    235       IN_FEATURE_VAL(SENSORS_FEATURE_IN_MAX)); 
    236   else if (IN_FEATURE(SENSORS_FEATURE_IN_MIN)) 
    237     printf("  (min = %+6.2f V)", IN_FEATURE_VAL(SENSORS_FEATURE_IN_MIN)); 
    238   else if (IN_FEATURE(SENSORS_FEATURE_IN_MAX)) 
    239     printf("  (max = %+6.2f V)", IN_FEATURE_VAL(SENSORS_FEATURE_IN_MAX)); 
    240    
    241   if (IN_FEATURE(SENSORS_FEATURE_IN_MAX_ALARM) || 
    242       IN_FEATURE(SENSORS_FEATURE_IN_MIN_ALARM)) { 
    243     alarm_max = IN_FEATURE_VAL(SENSORS_FEATURE_IN_MAX_ALARM); 
    244     alarm_min = IN_FEATURE_VAL(SENSORS_FEATURE_IN_MIN_ALARM); 
    245      
    246     if (alarm_min || alarm_max) { 
    247       printf(" ALARM ("); 
    248        
    249       if (alarm_min) 
    250         printf("MIN"); 
    251       if (alarm_max) 
    252         printf("%sMAX", (alarm_min) ? ", " : ""); 
    253        
    254       printf(")"); 
    255     } 
    256   } else if (IN_FEATURE(SENSORS_FEATURE_IN_ALARM)) { 
    257     printf("   %s",  
    258     IN_FEATURE_VAL(SENSORS_FEATURE_IN_ALARM) ? "ALARM" : ""); 
    259   } 
    260    
    261   printf("\n"); 
    262 } 
    263  
    264 #define FAN_FEATURE(x) has_features[x - SENSORS_FEATURE_FAN - 1] 
    265 #define FAN_FEATURE_VAL(x) feature_vals[x - SENSORS_FEATURE_FAN - 1] 
    266 static void print_generic_chip_fan(const sensors_chip_name *name,  
    267                                    const sensors_feature_data *feature, 
    268                                    int i, int label_size) 
    269 { 
    270   char *label; 
    271   const int size = SENSORS_FEATURE_FAN_DIV - SENSORS_FEATURE_FAN; 
    272   short has_features[SENSORS_FEATURE_FAN_DIV - SENSORS_FEATURE_FAN] = {0, }; 
    273   double feature_vals[SENSORS_FEATURE_FAN_DIV - SENSORS_FEATURE_FAN] = {0.0, }; 
    274   double val; 
    275    
    276   if (!(label = sensors_get_label(name, feature->number))) { 
    277     printf("ERROR: Can't get fan label!\n"); 
    278     return; 
    279   } 
    280    
    281   if (get_feature_value(name, feature, &val)) 
    282   { 
    283     printf("ERROR: Can't get %s data!\n", label); 
    284     free(label); 
    285     return; 
    286   } 
    287    
    288   print_label(label, label_size); 
    289   free(label); 
    290  
    291   if (FAN_FEATURE(SENSORS_FEATURE_FAN_FAULT) && 
    292       FAN_FEATURE_VAL(SENSORS_FEATURE_FAN_FAULT)) 
    293     printf("   FAULT"); 
    294   else 
    295     printf("%4.0f RPM", val); 
    296    
    297   sensors_get_available_features(name, feature, i, has_features, feature_vals, 
    298       size, SENSORS_FEATURE_FAN); 
    299    
    300   if (FAN_FEATURE(SENSORS_FEATURE_FAN_MIN) && 
    301       FAN_FEATURE(SENSORS_FEATURE_FAN_DIV)) 
    302     printf("  (min = %4.0f RPM, div = %1.0f)", 
    303       FAN_FEATURE_VAL(SENSORS_FEATURE_FAN_MIN), 
    304       FAN_FEATURE_VAL(SENSORS_FEATURE_FAN_DIV)); 
    305   else if (FAN_FEATURE(SENSORS_FEATURE_FAN_MIN)) 
    306     printf("  (min = %4.0f RPM)", FAN_FEATURE_VAL(SENSORS_FEATURE_FAN_MIN)); 
    307   else if (FAN_FEATURE(SENSORS_FEATURE_FAN_DIV)) 
    308     printf("  (div = %1.0f)", FAN_FEATURE_VAL(SENSORS_FEATURE_FAN_DIV)); 
    309    
    310   if (FAN_FEATURE(SENSORS_FEATURE_FAN_ALARM) &&  
    311       FAN_FEATURE_VAL(SENSORS_FEATURE_FAN_ALARM)) { 
    312     printf("  ALARM"); 
    313   }        
    314    
    315   printf("\n"); 
     83        return cel * (9.0F / 5.0F) + 32.0F; 
     84} 
     85 
     86#define TEMP_FEATURE(x)         has_features[x - SENSORS_FEATURE_TEMP - 1] 
     87#define TEMP_FEATURE_VAL(x)     feature_vals[x - SENSORS_FEATURE_TEMP - 1] 
     88static void print_generic_chip_temp(const sensors_chip_name *name, 
     89                                    const sensors_feature_data *feature, 
     90                                    int i, int label_size) 
     91{ 
     92        double val, max, min; 
     93        char *label; 
     94        int type; 
     95        const int size = SENSORS_FEATURE_TEMP_SENS - SENSORS_FEATURE_TEMP; 
     96        short has_features[SENSORS_FEATURE_TEMP_SENS - SENSORS_FEATURE_TEMP] = { 0, }; 
     97        double feature_vals[SENSORS_FEATURE_TEMP_SENS - SENSORS_FEATURE_TEMP] = { 0.0, }; 
     98 
     99        if (!(label = sensors_get_label(name, feature->number))) { 
     100                printf("ERROR: Can't get temperature label!\n"); 
     101                return; 
     102        } 
     103 
     104        if (get_feature_value(name, feature, &val)) { 
     105                printf("ERROR: Can't get %s data!\n", label); 
     106                free(label); 
     107                return; 
     108        } 
     109 
     110        sensors_get_available_features(name, feature, i, has_features, 
     111                                       feature_vals, size, 
     112                                       SENSORS_FEATURE_TEMP); 
     113 
     114        if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_MAX)) { 
     115                max = TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_MAX); 
     116 
     117                if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_MIN)) { 
     118                        min = TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_MIN); 
     119                        type = MINMAX; 
     120                } else if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_MAX_HYST)) { 
     121                        min = TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_MAX_HYST); 
     122                        type = HYST; 
     123                } else if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_CRIT)) { 
     124                        min = TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT); 
     125                        type = CRIT; 
     126                } else { 
     127                        min = 0; 
     128                        type = MAXONLY; 
     129                } 
     130        } else { 
     131                min = max = 0; 
     132                type = SINGLE; 
     133        } 
     134 
     135        if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_FAULT) && 
     136            TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_FAULT)) 
     137                val = HUGE_VAL; 
     138 
     139        print_label(label, label_size); 
     140        free(label); 
     141 
     142        print_temp_info(val, max, min, type, 1, 1); 
     143 
     144        /* ALARM features */ 
     145        if ((TEMP_FEATURE(SENSORS_FEATURE_TEMP_ALARM) && 
     146             TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_ALARM) > 0.5) 
     147         || (type == MINMAX && 
     148             TEMP_FEATURE(SENSORS_FEATURE_TEMP_MIN_ALARM) && 
     149             TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_MIN_ALARM) > 0.5) 
     150         || (type == MINMAX && 
     151             TEMP_FEATURE(SENSORS_FEATURE_TEMP_MAX_ALARM) && 
     152             TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_MAX_ALARM) > 0.5) 
     153         || (type == CRIT && 
     154             TEMP_FEATURE(SENSORS_FEATURE_TEMP_CRIT_ALARM) && 
     155             TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT_ALARM) > 0.5)) { 
     156                printf("ALARM  "); 
     157        } 
     158 
     159        if (type != CRIT && TEMP_FEATURE(SENSORS_FEATURE_TEMP_CRIT)) { 
     160                if (fahrenheit) { 
     161                        TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT) = deg_ctof( 
     162                                TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT)); 
     163                        TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT_HYST) = deg_ctof( 
     164                                TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT_HYST)); 
     165                } 
     166 
     167                if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_CRIT_HYST)) 
     168                        printf("\n%*s(crit = %+5.1f%s, hyst = %+5.1f%s)  ", 
     169                               label_size + 10, "", 
     170                               TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT), 
     171                               degstr, 
     172                               TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT_HYST), 
     173                               degstr); 
     174                else 
     175                        printf("\n%*s(crit = %+5.1f%s)  ", 
     176                               label_size + 10, "", 
     177                               TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT), 
     178                               degstr); 
     179 
     180                if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_CRIT_ALARM) && 
     181                    TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT_ALARM)) { 
     182                        printf("ALARM  "); 
     183                } 
     184        } 
     185 
     186        /* print out temperature sensor info */ 
     187        if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_SENS)) { 
     188                int sens = (int)TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_SENS); 
     189 
     190                /* older kernels / drivers sometimes report a beta value for 
     191                   thermistors */ 
     192                if (sens > 1000) 
     193                        sens = 4; 
     194 
     195                printf("sensor = %s", sens == 0 ? "disabled" : 
     196                       sens == 1 ? "diode" : 
     197                       sens == 2 ? "transistor" : 
     198                       sens == 3 ? "thermal diode" : 
     199                       sens == 4 ? "thermistor" : 
     200                       sens == 5 ? "AMD AMDSI" : 
     201                       sens == 6 ? "Intel PECI" : "unknown"); 
     202        } 
     203        printf("\n"); 
     204} 
     205 
     206#define IN_FEATURE(x)           has_features[x - SENSORS_FEATURE_IN - 1] 
     207#define IN_FEATURE_VAL(x)       feature_vals[x - SENSORS_FEATURE_IN - 1] 
     208static void print_generic_chip_in(const sensors_chip_name *name, 
     209                                  const sensors_feature_data *feature, 
     210                                  int i, int label_size) 
     211{ 
     212        const int size = SENSORS_FEATURE_IN_MAX_ALARM - SENSORS_FEATURE_IN; 
     213        short has_features[SENSORS_FEATURE_IN_MAX_ALARM - SENSORS_FEATURE_IN] = { 0, }; 
     214        double feature_vals[SENSORS_FEATURE_IN_MAX_ALARM - SENSORS_FEATURE_IN] = { 0.0, }; 
     215        double val, alarm_max, alarm_min; 
     216        char *label; 
     217 
     218        if (!(label = sensors_get_label(name, feature->number))) { 
     219                printf("ERROR: Can't get in label!\n"); 
     220                return; 
     221        } 
     222 
     223        if (get_feature_value(name, feature, &val)) { 
     224                printf("ERROR: Can't get %s data!\n", label); 
     225                free(label); 
     226                return; 
     227        } 
     228 
     229        sensors_get_available_features(name, feature, i, has_features, 
     230                                       feature_vals, size, SENSORS_FEATURE_IN); 
     231 
     232        print_label(label, label_size); 
     233        free(label); 
     234        printf("%+6.2f V", val); 
     235 
     236        if (IN_FEATURE(SENSORS_FEATURE_IN_MIN) && 
     237            IN_FEATURE(SENSORS_FEATURE_IN_MAX)) 
     238                printf("  (min = %+6.2f V, max = %+6.2f V)", 
     239                       IN_FEATURE_VAL(SENSORS_FEATURE_IN_MIN), 
     240                       IN_FEATURE_VAL(SENSORS_FEATURE_IN_MAX)); 
     241        else if (IN_FEATURE(SENSORS_FEATURE_IN_MIN)) 
     242                printf("  (min = %+6.2f V)", 
     243                       IN_FEATURE_VAL(SENSORS_FEATURE_IN_MIN)); 
     244        else if (IN_FEATURE(SENSORS_FEATURE_IN_MAX)) 
     245                printf("  (max = %+6.2f V)", 
     246                       IN_FEATURE_VAL(SENSORS_FEATURE_IN_MAX)); 
     247 
     248        if (IN_FEATURE(SENSORS_FEATURE_IN_MAX_ALARM) || 
     249            IN_FEATURE(SENSORS_FEATURE_IN_MIN_ALARM)) { 
     250                alarm_max = IN_FEATURE_VAL(SENSORS_FEATURE_IN_MAX_ALARM); 
     251                alarm_min = IN_FEATURE_VAL(SENSORS_FEATURE_IN_MIN_ALARM); 
     252 
     253                if (alarm_min || alarm_max) { 
     254                        printf(" ALARM ("); 
     255 
     256                        if (alarm_min) 
     257                                printf("MIN"); 
     258                        if (alarm_max) 
     259                                printf("%sMAX", (alarm_min) ? ", " : ""); 
     260 
     261                        printf(")"); 
     262                } 
     263        } else if (IN_FEATURE(SENSORS_FEATURE_IN_ALARM)) { 
     264                printf("   %s", 
     265                IN_FEATURE_VAL(SENSORS_FEATURE_IN_ALARM) ? "ALARM" : ""); 
     266        } 
     267 
     268        printf("\n"); 
     269} 
     270 
     271#define FAN_FEATURE(x)          has_features[x - SENSORS_FEATURE_FAN - 1] 
     272#define FAN_FEATURE_VAL(x)      feature_vals[x - SENSORS_FEATURE_FAN - 1] 
     273static void print_generic_chip_fan(const sensors_chip_name *name, 
     274                                   const sensors_feature_data *feature, 
     275                                   int i, int label_size) 
     276{ 
     277        char *label; 
     278        const int size = SENSORS_FEATURE_FAN_DIV - SENSORS_FEATURE_FAN; 
     279        short has_features[SENSORS_FEATURE_FAN_DIV - SENSORS_FEATURE_FAN] = { 0, }; 
     280        double feature_vals[SENSORS_FEATURE_FAN_DIV - SENSORS_FEATURE_FAN] = { 0.0, }; 
     281        double val; 
     282 
     283        if (!(label = sensors_get_label(name, feature->number))) { 
     284                printf("ERROR: Can't get fan label!\n"); 
     285                return; 
     286        } 
     287 
     288        if (get_feature_value(name, feature, &val)) { 
     289                printf("ERROR: Can't get %s data!\n", label); 
     290                free(label); 
     291                return; 
     292        } 
     293 
     294        print_label(label, label_size); 
     295        free(label); 
     296 
     297        if (FAN_FEATURE(SENSORS_FEATURE_FAN_FAULT) && 
     298            FAN_FEATURE_VAL(SENSORS_FEATURE_FAN_FAULT)) 
     299                printf("   FAULT"); 
     300        else 
     301                printf("%4.0f RPM", val); 
     302 
     303        sensors_get_available_features(name, feature, i, has_features, 
     304                                       feature_vals, size, SENSORS_FEATURE_FAN); 
     305 
     306        if (FAN_FEATURE(SENSORS_FEATURE_FAN_MIN) && 
     307            FAN_FEATURE(SENSORS_FEATURE_FAN_DIV)) 
     308                printf("  (min = %4.0f RPM, div = %1.0f)", 
     309                       FAN_FEATURE_VAL(SENSORS_FEATURE_FAN_MIN), 
     310                       FAN_FEATURE_VAL(SENSORS_FEATURE_FAN_DIV)); 
     311        else if (FAN_FEATURE(SENSORS_FEATURE_FAN_MIN)) 
     312                printf("  (min = %4.0f RPM)", 
     313                       FAN_FEATURE_VAL(SENSORS_FEATURE_FAN_MIN)); 
     314        else if (FAN_FEATURE(SENSORS_FEATURE_FAN_DIV)) 
     315                printf("  (div = %1.0f)", 
     316                       FAN_FEATURE_VAL(SENSORS_FEATURE_FAN_DIV)); 
     317 
     318        if (FAN_FEATURE(SENSORS_FEATURE_FAN_ALARM) && 
     319            FAN_FEATURE_VAL(SENSORS_FEATURE_FAN_ALARM)) { 
     320                printf("  ALARM"); 
     321        } 
     322 
     323        printf("\n"); 
    316324} 
    317325 
    318326void print_generic_chip(const sensors_chip_name *name) 
    319327{ 
    320   const sensors_feature_data *feature; 
    321   int i, label_size; 
    322    
    323   label_size = sensors_get_label_size(name); 
    324    
    325   i = 0; 
    326   while((feature = sensors_get_all_features(name, &i))) { 
    327     if (feature->mapping != SENSORS_NO_MAPPING) 
    328       continue; 
    329      
    330     switch (feature->type) { 
    331       case SENSORS_FEATURE_TEMP: 
    332         print_generic_chip_temp(name, feature, i, label_size); break; 
    333       case SENSORS_FEATURE_IN: 
    334         print_generic_chip_in(name, feature, i, label_size); break; 
    335       case SENSORS_FEATURE_FAN: 
    336         print_generic_chip_fan(name, feature, i, label_size); break; 
    337       case SENSORS_FEATURE_VID: 
    338         print_vid_info(name, feature->number, label_size); break; 
    339       default: continue; 
    340     } 
    341   } 
    342 } 
     328        const sensors_feature_data *feature; 
     329        int i, label_size; 
     330 
     331        label_size = sensors_get_label_size(name); 
     332 
     333        i = 0; 
     334        while ((feature = sensors_get_all_features(name, &i))) { 
     335                if (feature->mapping != SENSORS_NO_MAPPING) 
     336                        continue; 
     337 
     338                switch (feature->type) { 
     339                case SENSORS_FEATURE_TEMP: 
     340                        print_generic_chip_temp(name, feature, i, label_size); 
     341                        break; 
     342                case SENSORS_FEATURE_IN: 
     343                        print_generic_chip_in(name, feature, i, label_size); 
     344                        break; 
     345                case SENSORS_FEATURE_FAN: 
     346                        print_generic_chip_fan(name, feature, i, label_size); 
     347                        break; 
     348                case SENSORS_FEATURE_VID: 
     349                        print_vid_info(name, feature->number, label_size); 
     350                        break; 
     351                default: 
     352                        continue; 
     353                } 
     354        } 
     355} 
  • lm-sensors/branches/lm-sensors-3.0.0/prog/sensors/main.c

    r4706 r4708  
    3131#endif 
    3232 
    33 #include "lib/sensors.h"  
     33#include "lib/sensors.h" 
    3434#include "lib/error.h" 
    3535#include "chips.h" 
     
    3737#include "chips_generic.h" 
    3838 
    39 #define PROGRAM "sensors" 
    40 #define VERSION LM_VERSION 
     39#define PROGRAM                 "sensors" 
     40#define VERSION                 LM_VERSION 
    4141#define DEFAULT_CONFIG_FILE     ETCDIR "/sensors.conf" 
    4242 
     
    5858void print_short_help(void) 
    5959{ 
    60   printf("Try `%s -h' for more information\n",PROGRAM); 
     60        printf("Try `%s -h' for more information\n", PROGRAM); 
    6161} 
    6262 
    6363void print_long_help(void) 
    6464{ 
    65   printf("Usage: %s [OPTION]... [CHIP]...\n",PROGRAM); 
    66   printf("  -c, --config-file     Specify a config file (default: %s)\n", 
    67          DEFAULT_CONFIG_FILE); 
    68   puts("  -h, --help            Display this help text\n" 
    69        "  -s, --set             Execute `set' statements (root only)\n" 
    70        "  -f, --fahrenheit      Show temperatures in degrees fahrenheit\n" 
    71        "  -A, --no-adapter      Do not show adapter for each chip\n" 
    72        "  -u                    Raw output (debugging only)\n" 
    73        "  -v, --version         Display the program version\n" 
    74        "\n" 
    75        "Use `-' after `-c' to read the config file from stdin.\n" 
    76        "If no chips are specified, all chip info will be printed.\n" 
    77        "Example chip names:\n" 
    78        "\tlm78-i2c-0-2d\t*-i2c-0-2d\n" 
    79        "\tlm78-i2c-0-*\t*-i2c-0-*\n" 
    80        "\tlm78-i2c-*-2d\t*-i2c-*-2d\n" 
    81        "\tlm78-i2c-*-*\t*-i2c-*-*\n" 
    82        "\tlm78-isa-0290\t*-isa-0290\n" 
    83        "\tlm78-isa-*\t*-isa-*\n" 
    84        "\tlm78-*"); 
     65        printf("Usage: %s [OPTION]... [CHIP]...\n", PROGRAM); 
     66        printf("  -c, --config-file     Specify a config file (default: %s)\n", 
     67               DEFAULT_CONFIG_FILE); 
     68        puts("  -h, --help            Display this help text\n" 
     69             "  -s, --set             Execute `set' statements (root only)\n" 
     70             "  -f, --fahrenheit      Show temperatures in degrees fahrenheit\n" 
     71             "  -A, --no-adapter      Do not show adapter for each chip\n" 
     72             "  -u                    Raw output (debugging only)\n" 
     73             "  -v, --version         Display the program version\n" 
     74             "\n" 
     75             "Use `-' after `-c' to read the config file from stdin.\n" 
     76             "If no chips are specified, all chip info will be printed.\n" 
     77             "Example chip names:\n" 
     78             "\tlm78-i2c-0-2d\t*-i2c-0-2d\n" 
     79             "\tlm78-i2c-0-*\t*-i2c-0-*\n" 
     80             "\tlm78-i2c-*-2d\t*-i2c-*-2d\n" 
     81             "\tlm78-i2c-*-*\t*-i2c-*-*\n" 
     82             "\tlm78-isa-0290\t*-isa-0290\n" 
     83             "\tlm78-isa-*\t*-isa-*\n" 
     84             "\tlm78-*"); 
    8585} 
    8686 
    8787void print_version(void) 
    8888{ 
    89   printf("%s version %s with libsensors version %s\n", PROGRAM, VERSION, libsensors_version); 
    90 } 
    91  
    92 /* This examines global var config_file, and leaves the name there too.  
     89        printf("%s version %s with libsensors version %s\n", PROGRAM, VERSION, 
     90               libsensors_version); 
     91} 
     92 
     93/* This examines global var config_file, and leaves the name there too. 
    9394   It also opens config_file. */ 
    9495static void open_config_file(const char* config_file_name) 
    9596{ 
    96   if (!strcmp(config_file_name,"-")) { 
    97     config_file = stdin; 
    98     return; 
    99   } 
    100  
    101   config_file = fopen(config_file_name, "r"); 
    102   if (!config_file) { 
    103     fprintf(stderr, "Could not open config file\n"); 
    104     perror(config_file_name); 
    105     exit(1); 
    106   } 
    107 } 
    108      
     97        if (!strcmp(config_file_name, "-")) { 
     98                config_file = stdin; 
     99                return; 
     100        } 
     101 
     102        config_file = fopen(config_file_name, "r"); 
     103        if (!config_file) { 
     104                fprintf(stderr, "Could not open config file\n"); 
     105                perror(config_file_name); 
     106                exit(1); 
     107        } 
     108} 
     109 
    109110static void close_config_file(const char* config_file_name) 
    110111{ 
    111   if (fclose(config_file) == EOF) { 
    112     fprintf(stderr,"Could not close config file\n"); 
    113     perror(config_file_name); 
    114   } 
     112        if (fclose(config_file) == EOF) { 
     113                fprintf(stderr, "Could not close config file\n"); 
     114                perror(config_file_name); 
     115        } 
    115116} 
    116117 
    117118static void set_degstr(void) 
    118119{ 
    119   const char *deg_default_text[2] = {" C", " F"}; 
     120        const char *deg_default_text[2] = { " C", " F" }; 
    120121 
    121122#ifdef HAVE_ICONV 
    122   /* Size hardcoded for better performance. 
    123      Don't forget to count the trailing \0! */ 
    124   size_t deg_latin1_size = 3; 
    125   char *deg_latin1_text[2] = {"\260C", "\260F"}; 
    126   size_t nconv; 
    127   size_t degstr_size = sizeof(degstr); 
    128   char *degstr_ptr = degstr; 
    129  
    130   iconv_t cd = iconv_open(nl_langinfo(CODESET), "ISO-8859-1"); 
    131   if (cd != (iconv_t) -1) { 
    132     nconv = iconv(cd, &(deg_latin1_text[fahrenheit]), &deg_latin1_size, 
    133                   &degstr_ptr, &degstr_size); 
    134     iconv_close(cd); 
    135      
    136     if (nconv != (size_t) -1) 
    137       return;       
    138   } 
     123        /* Size hardcoded for better performance. 
     124           Don't forget to count the trailing \0! */ 
     125        size_t deg_latin1_size = 3; 
     126        char *deg_latin1_text[2] = { "\260C", "\260F" }; 
     127        size_t nconv; 
     128        size_t degstr_size = sizeof(degstr); 
     129        char *degstr_ptr = degstr; 
     130 
     131        iconv_t cd = iconv_open(nl_langinfo(CODESET), "ISO-8859-1"); 
     132        if (cd != (iconv_t) -1) { 
     133                nconv = iconv(cd, &(deg_latin1_text[fahrenheit]), 
     134                              &deg_latin1_size, &degstr_ptr, &degstr_size); 
     135                iconv_close(cd); 
     136 
     137                if (nconv != (size_t) -1) 
     138                        return; 
     139        } 
    139140#endif /* HAVE_ICONV */ 
    140141 
    141   /* There was an error during the conversion, use the default text */ 
    142   strcpy(degstr, deg_default_text[fahrenheit]); 
     142        /* There was an error during the conversion, use the default text */ 
     143        strcpy(degstr, deg_default_text[fahrenheit]); 
    143144} 
    144145 
    145146int main (int argc, char *argv[]) 
    146147{ 
    147   int c,res,i,error; 
    148   const char *config_file_name = DEFAULT_CONFIG_FILE; 
    149  
    150   struct option long_opts[] =  { 
    151     { "help", no_argument, NULL, 'h' }, 
    152     { "set", no_argument, NULL, 's' }, 
    153     { "version", no_argument, NULL, 'v'}, 
    154     { "fahrenheit", no_argument, NULL, 'f' }, 
    155     { "no-adapter", no_argument, NULL, 'A' }, 
    156     { "config-file", required_argument, NULL, 'c' }, 
    157     { 0,0,0,0 } 
    158   }; 
    159  
    160   setlocale(LC_CTYPE, ""); 
    161  
    162   do_raw = 0; 
    163   do_sets = 0; 
    164   hide_adapter = 0; 
    165   while (1) { 
    166     c = getopt_long(argc, argv, "hsvfAc:u", long_opts, NULL); 
    167     if (c == EOF) 
    168       break; 
    169     switch(c) { 
    170     case ':': 
    171     case '?': 
    172       print_short_help(); 
    173       exit(1); 
    174     case 'h': 
    175       print_long_help(); 
    176       exit(0); 
    177     case 'v': 
    178       print_version(); 
    179       exit(0); 
    180     case 'c': 
    181       config_file_name = optarg; 
    182       break; 
    183     case 's': 
    184       do_sets = 1; 
    185       break; 
    186     case 'f': 
    187       fahrenheit = 1; 
    188       break; 
    189     case 'A': 
    190       hide_adapter = 1; 
    191       break; 
    192     case 'u': 
    193       do_raw = 1; 
    194       break; 
    195     default: 
    196       fprintf(stderr,"Internal error while parsing options!\n"); 
    197       exit(1); 
    198     } 
    199   } 
    200  
    201   open_config_file(config_file_name); 
    202   if ((res = sensors_init(config_file))) { 
    203     fprintf(stderr, "sensors_init: %s\n", sensors_strerror(res)); 
    204     exit(1); 
    205   } 
    206   close_config_file(config_file_name); 
    207  
    208   /* build the degrees string */ 
    209   set_degstr(); 
    210  
    211   if (optind == argc) { /* No chip name on command line */ 
    212     if (!do_the_real_work(NULL, &error)) { 
    213       fprintf(stderr, 
    214               "No sensors found!\n" 
    215               "Make sure you loaded all the kernel drivers you need.\n" 
    216               "Try sensors-detect to find out which these are.\n"); 
    217       error = 1; 
    218     } 
    219   } else { 
    220     int cnt = 0; 
    221     sensors_chip_name chip; 
    222  
    223     for (i = optind; i < argc; i++) { 
    224       if (sensors_parse_chip_name(argv[i], &chip)) { 
    225         fprintf(stderr, "Parse error in chip name `%s'\n", argv[i]); 
    226         print_short_help(); 
    227         error = 1; 
    228         goto exit; 
    229       } 
    230       cnt += do_the_real_work(&chip, &error); 
    231     } 
    232  
    233     if (!cnt) { 
    234       fprintf(stderr, "Specified sensor(s) not found!\n"); 
    235       error = 1; 
    236     } 
    237   } 
     148        int c, res, i, error; 
     149        const char *config_file_name = DEFAULT_CONFIG_FILE; 
     150 
     151        struct option long_opts[] =  { 
     152                { "help", no_argument, NULL, 'h' }, 
     153                { "set", no_argument, NULL, 's' }, 
     154                { "version", no_argument, NULL, 'v'}, 
     155                { "fahrenheit", no_argument, NULL, 'f' }, 
     156                { "no-adapter", no_argument, NULL, 'A' }, 
     157                { "config-file", required_argument, NULL, 'c' }, 
     158                { 0, 0, 0, 0 } 
     159        }; 
     160 
     161        setlocale(LC_CTYPE, ""); 
     162 
     163        do_raw = 0; 
     164        do_sets = 0; 
     165        hide_adapter = 0; 
     166        while (1) { 
     167                c = getopt_long(argc, argv, "hsvfAc:u", long_opts, NULL); 
     168                if (c == EOF) 
     169                        break; 
     170                switch(c) { 
     171                case ':': 
     172                case '?': 
     173                        print_short_help(); 
     174                        exit(1); 
     175                case 'h': 
     176                        print_long_help(); 
     177                        exit(0); 
     178                case 'v': 
     179                        print_version(); 
     180                        exit(0); 
     181                case 'c': 
     182                        config_file_name = optarg; 
     183                        break; 
     184                case 's': 
     185                        do_sets = 1; 
     186                        break; 
     187                case 'f': 
     188                        fahrenheit = 1; 
     189                        break; 
     190                case 'A': 
     191                        hide_adapter = 1; 
     192                        break; 
     193                case 'u': 
     194                        do_raw = 1; 
     195                        break; 
     196                default: 
     197                        fprintf(stderr, 
     198                                "Internal error while parsing options!\n"); 
     199                        exit(1); 
     200                } 
     201        } 
     202 
     203        open_config_file(config_file_name); 
     204        if ((res = sensors_init(config_file))) { 
     205                fprintf(stderr, "sensors_init: %s\n", sensors_strerror(res)); 
     206                exit(1); 
     207        } 
     208        close_config_file(config_file_name); 
     209 
     210        /* build the degrees string */ 
     211        set_degstr(); 
     212 
     213        if (optind == argc) { /* No chip name on command line */ 
     214                if (!do_the_real_work(NULL, &error)) { 
     215                        fprintf(stderr, 
     216                                "No sensors found!\n" 
     217                                "Make sure you loaded all the kernel drivers you need.\n" 
     218                                "Try sensors-detect to find out which these are.\n"); 
     219                        error = 1; 
     220                } 
     221        } else { 
     222                int cnt = 0; 
     223                sensors_chip_name chip; 
     224 
     225                for (i = optind; i < argc; i++) { 
     226                        if (sensors_parse_chip_name(argv[i], &chip)) { 
     227                                fprintf(stderr, 
     228                                        "Parse error in chip name `%s'\n", 
     229                                        argv[i]); 
     230                                print_short_help(); 
     231                                error = 1; 
     232                                goto exit; 
     233                        } 
     234                        cnt += do_the_real_work(&chip, &error); 
     235                } 
     236 
     237                if (!cnt) { 
     238                        fprintf(stderr, "Specified sensor(s) not found!\n"); 
     239                        error = 1; 
     240                } 
     241        } 
    238242 
    239243exit: 
    240   sensors_cleanup(); 
    241   exit(res); 
     244        sensors_cleanup(); 
     245        exit(res); 
    242246} 
    243247 
     
    245249int do_the_real_work(const sensors_chip_name *match, int *error) 
    246250{ 
    247   const sensors_chip_name *chip; 
    248   int chip_nr; 
    249   int cnt = 0; 
    250  
    251   chip_nr = 0; 
    252   while ((chip = sensors_get_detected_chips(match, &chip_nr))) { 
    253     if (do_sets) { 
    254       if (do_a_set(chip)) 
    255         *error = 1; 
    256     } else 
    257       do_a_print(chip); 
    258     cnt++; 
    259   } 
    260   return cnt; 
     251        const sensors_chip_name *chip; 
     252        int chip_nr; 
     253        int cnt = 0; 
     254 
     255        chip_nr = 0; 
     256        while ((chip = sensors_get_detected_chips(match, &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        } 
     264        return cnt; 
    261265} 
    262266 
     
    264268int do_a_set(const sensors_chip_name *name) 
    265269{ 
    266   int res; 
    267  
    268   if ((res = sensors_do_chip_sets(name))) { 
    269     if (res == -SENSORS_ERR_PROC) { 
    270       fprintf(stderr,"%s: %s for writing;\n",sprintf_chip_name(name), 
    271               sensors_strerror(res)); 
    272       fprintf(stderr,"Run as root?\n"); 
    273       return 1; 
    274     } else if (res == -SENSORS_ERR_ACCESS_W) { 
    275       fprintf(stderr, "%s: At least one \"set\" statement failed\n", 
    276               sprintf_chip_name(name)); 
    277     } else { 
    278       fprintf(stderr,"%s: %s\n",sprintf_chip_name(name), 
    279               sensors_strerror(res)); 
    280     } 
    281   } 
    282   return 0; 
     270        int res; 
     271 
     272        if ((res = sensors_do_chip_sets(name))) { 
     273                if (res == -SENSORS_ERR_PROC) { 
     274                        fprintf(stderr, "%s: %s for writing;\n", 
     275                                sprintf_chip_name(name), 
     276                                sensors_strerror(res)); 
     277                        fprintf(stderr, "Run as root?\n"); 
     278                        return 1; 
     279                } else if (res == -SENSORS_ERR_ACCESS_W) { 
     280                        fprintf(stderr, 
     281                                "%s: At least one \"set\" statement failed\n", 
     282                                sprintf_chip_name(name)); 
     283                } else { 
     284                        fprintf(stderr, "%s: %s\n", sprintf_chip_name(name), 
     285                                sensors_strerror(res)); 
     286                } 
     287        } 
     288        return 0; 
    283289} 
    284290 
    285291const char *sprintf_chip_name(const sensors_chip_name *name) 
    286292{ 
    287   #define BUF_SIZE 200 
    288   static char buf[BUF_SIZE]; 
    289  
    290   if (sensors_snprintf_chip_name(buf, BUF_SIZE, name) < 0) 
    291     return NULL; 
    292   return buf; 
     293#define BUF_SIZE 200 
     294        static char buf[BUF_SIZE]; 
     295 
     296        if (sensors_snprintf_chip_name(buf, BUF_SIZE, name) < 0) 
     297                return NULL; 
     298        return buf; 
    293299} 
    294300 
    295301void do_a_print(const sensors_chip_name *name) 
    296302{ 
    297   printf("%s\n",sprintf_chip_name(name)); 
    298   if (!hide_adapter) { 
    299     const char *adap = sensors_get_adapter_name(&name->bus); 
    300     if (adap) 
    301       printf("Adapter: %s\n", adap); 
    302     else 
    303       fprintf(stderr, "Can't get adapter name\n"); 
    304   } 
    305   if (do_raw) 
    306     print_chip_raw(name); 
    307   else 
    308     print_generic_chip(name); 
    309   printf("\n"); 
    310 } 
     303        printf("%s\n", sprintf_chip_name(name)); 
     304        if (!hide_adapter) { 
     305                const char *adap = sensors_get_adapter_name(&name->bus); 
     306                if (adap) 
     307                        printf("Adapter: %s\n", adap); 
     308                else 
     309                        fprintf(stderr, "Can't get adapter name\n"); 
     310        } 
     311        if (do_raw) 
     312                print_chip_raw(name); 
     313        else 
     314                print_generic_chip(name); 
     315        printf("\n"); 
     316}