Changeset 2768

Show
Ignore:
Timestamp:
11/21/04 22:36:10 (10 years ago)
Author:
khali
Message:

Simplify the configuration file opening code.

Files:
1 modified

Legend:

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

    r2767 r2768  
    3636#define DEFAULT_CONFIG_FILE_NAME "sensors.conf" 
    3737 
    38 static char *config_file_name = NULL; 
    3938FILE *config_file; 
    4039extern const char *libsensors_version; 
     
    4443static void print_long_help(void); 
    4544static void print_version(void); 
    46 static void open_config_file(void); 
    47 static int open_this_config_file(char *filename); 
    4845static void do_a_print(sensors_chip_name name); 
    4946static int do_a_set(sensors_chip_name name); 
     
    9592/* This examines global var config_file, and leaves the name there too.  
    9693   It also opens config_file. */ 
    97 void open_config_file(void) 
    98 { 
    99 #define MAX_FILENAME_LEN 1024 
    100   char *filename; 
    101   char buffer[MAX_FILENAME_LEN]; 
    102   int res; 
    103  
    104   if (config_file_name && !strcmp(config_file_name,"-")) { 
     94void open_config_file(const char* config_file_name) 
     95{ 
     96  if (!strcmp(config_file_name,"-")) { 
    10597    config_file = stdin; 
    10698    return; 
    107   } else if (config_file_name && index(config_file_name,'/')) { 
    108     if ((res = open_this_config_file(config_file_name))) { 
    109       fprintf(stderr,"Could not locate or open config file\n"); 
    110       fprintf(stderr,"%s: %s\n",config_file_name,strerror(-res)); 
    111       exit(1); 
    112     } 
    113   } 
    114   else { 
    115     if (config_file_name) 
    116       filename = config_file_name; 
    117     else 
    118       filename = DEFAULT_CONFIG_FILE_NAME; 
    119     if ((snprintf(buffer,MAX_FILENAME_LEN, 
    120                   "%s/%s", ETCDIR, filename)) < 1) { 
    121       fprintf(stderr, 
    122               "open_config_file: ridiculous long config file name!\n"); 
    123       exit(1); 
    124     } 
    125     if (!open_this_config_file(buffer)) { 
    126       free(config_file_name); 
    127       config_file_name = strdup(buffer); 
    128       return; 
    129     } 
    130     fprintf(stderr,"Could not locate or open config file!\n"); 
     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); 
    131105    exit(1); 
    132106  } 
    133107} 
    134108     
    135 int open_this_config_file(char *filename) 
    136 { 
    137   config_file = fopen(filename,"r"); 
    138   if (! config_file) 
    139     return -errno; 
    140   return 0; 
    141 } 
    142  
    143 void close_config_file(void) 
    144 { 
    145   if (fclose(config_file)) { 
     109void close_config_file(const char* config_file_name) 
     110{ 
     111  if (fclose(config_file) == EOF) { 
    146112    fprintf(stderr,"Could not close config file\n"); 
    147     fprintf(stderr,"%s: %s\n",config_file_name,strerror(errno)); 
    148   } 
    149    
    150   free(config_file_name); 
     113    perror(config_file_name); 
     114  } 
    151115} 
    152116 
     
    179143{ 
    180144  int c,res,i,error; 
     145  char *config_file_name = NULL; 
    181146 
    182147  struct option long_opts[] =  { 
     
    259224 
    260225 
    261   open_config_file(); 
     226  if (config_file_name == NULL) 
     227    config_file_name = strdup(ETCDIR "/" DEFAULT_CONFIG_FILE_NAME); 
     228  open_config_file(config_file_name); 
    262229 
    263230  if ((res = sensors_init(config_file))) { 
     
    272239  } 
    273240 
    274   close_config_file(); 
     241  close_config_file(config_file_name); 
     242  free(config_file_name); 
    275243 
    276244  /* build the degrees string */