Changeset 4711

Show
Ignore:
Timestamp:
08/26/07 15:21:05 (6 years ago)
Author:
khali
Message:

config_file doesn't need to be a global. Also move the handling of
the configuration file in a single separate function for clarity.

Files:
1 modified

Legend:

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

    r4710 r4711  
    3939#define VERSION                 LM_VERSION 
    4040#define DEFAULT_CONFIG_FILE     ETCDIR "/sensors.conf" 
    41  
    42 FILE *config_file; 
    4341 
    4442extern int main(int argc, char *arv[]); 
     
    9088} 
    9189 
    92 /* This examines global var config_file, and leaves the name there too. 
    93    It also opens config_file. */ 
    94 static void open_config_file(const char* config_file_name) 
    95 { 
    96         if (!strcmp(config_file_name, "-")) { 
     90/* Return 0 on success, and an exit error code otherwise */ 
     91static int read_config_file(const char *config_file_name) 
     92{ 
     93        FILE *config_file; 
     94        int err; 
     95 
     96        if (!strcmp(config_file_name, "-")) 
    9797                config_file = stdin; 
    98                 return; 
    99         } 
    100  
    101         config_file = fopen(config_file_name, "r"); 
     98        else 
     99                config_file = fopen(config_file_name, "r"); 
     100 
    102101        if (!config_file) { 
    103102                fprintf(stderr, "Could not open config file\n"); 
    104103                perror(config_file_name); 
    105                 exit(1); 
    106         } 
    107 } 
    108  
    109 static void close_config_file(const char* config_file_name) 
    110 { 
    111         if (fclose(config_file) == EOF) { 
    112                 fprintf(stderr, "Could not close config file\n"); 
     104                return 1; 
     105        } 
     106 
     107        err = sensors_init(config_file); 
     108        if (err) { 
     109                fprintf(stderr, "sensors_init: %s\n", sensors_strerror(err)); 
     110                return 1; 
     111        } 
     112 
     113        if (fclose(config_file) == EOF) 
    113114                perror(config_file_name); 
    114         } 
     115 
     116        return 0; 
    115117} 
    116118 
     
    200202        } 
    201203 
    202         open_config_file(config_file_name); 
    203         if ((res = sensors_init(config_file))) { 
    204                 fprintf(stderr, "sensors_init: %s\n", sensors_strerror(res)); 
    205                 exit(1); 
    206         } 
    207         close_config_file(config_file_name); 
     204        res = read_config_file(config_file_name); 
     205        if (res) 
     206                exit(res); 
    208207 
    209208        /* build the degrees string */