root/lm-sensors/trunk/prog/sensors/main.c @ 1591

Revision 1591, 10.6 KB (checked in by khali, 11 years ago)

Use strcmp instead of strncmp for it87

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2    main.c - Part of sensors, a user-space program for hardware monitoring
3    Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl>
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*/
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <getopt.h>
23#include <string.h>
24#include <errno.h>
25
26#include "lib/sensors.h"
27#include "lib/error.h"
28#include "chips.h"
29#include "version.h"
30
31#define PROGRAM "sensors"
32#define VERSION LM_VERSION
33#define DEFAULT_CONFIG_FILE_NAME "sensors.conf"
34
35static char *config_file_name;
36FILE *config_file;
37static const char *config_file_path[] = 
38  { "/etc", "/usr/local/etc", "/usr/lib/sensors", "/usr/local/lib/sensors",
39    "/usr/lib", "/usr/local/lib", ".", 0 };
40
41extern int main(int argc, char *arv[]);
42static void print_short_help(void);
43static void print_long_help(void);
44static void print_version(void);
45static void open_config_file(void);
46static int open_this_config_file(char *filename);
47static void do_a_print(sensors_chip_name name);
48static void do_a_set(sensors_chip_name name);
49static int do_the_real_work(void);
50static const char *sprintf_chip_name(sensors_chip_name name);
51
52#define CHIPS_MAX 20
53sensors_chip_name chips[CHIPS_MAX];
54int chips_count=0;
55int do_sets, do_unknown, fahrenheit, hide_adapter;
56
57void print_short_help(void)
58{
59  printf("Try `%s -h' for more information\n",PROGRAM);
60}
61
62void print_long_help(void)
63{
64  printf("Usage: %s [OPTION]... [CHIP]...\n",PROGRAM);
65  printf("  -c, --config-file     Specify a config file\n");
66  printf("  -h, --help            Display this help text\n");
67  printf("  -s, --set             Execute `set' statements too (root only)\n");
68  printf("  -f, --fahrenheit      Show temperatures in degrees fahrenheit\n");
69  printf("  -A, --no-adapter      Do not show adapter and algorithm for each chip\n");
70  printf("  -u, --unknown         Treat chips as unknown ones (testing only)\n");
71  printf("  -v, --version         Display the program version\n");
72  printf("\n");
73  printf("By default, a list of directories is examined for config file `sensors.conf'.\n");
74  printf("Use `-' after `-c' to read the config file from stdin.\n");
75  printf("If no chips are specified, all chip info will be printed.\n");
76  printf("Example chip names:\n");
77  printf("\tlm78-i2c-0-2d\t*-i2c-0-2d\n");
78  printf("\tlm78-i2c-0-*\t*-i2c-0-*\n");
79  printf("\tlm78-i2c-*-2d\t*-i2c-*-2d\n");
80  printf("\tlm78-i2c-*-*\t*-i2c-*-*\n");
81  printf("\tlm78-isa-0290\t*-isa-0290\n");
82  printf("\tlm78-isa-*\t*-isa-*\n");
83  printf("\tlm78-*\n");
84}
85
86void print_version(void)
87{
88  printf("%s version %s\n", PROGRAM, VERSION);
89}
90
91/* This examines global var config_file, and leaves the name there too.
92   It also opens config_file. */
93void open_config_file(void)
94{
95#define MAX_FILENAME_LEN 1024
96  char *filename;
97  char buffer[MAX_FILENAME_LEN];
98  int res,i;
99
100  if (config_file_name && !strcmp(config_file_name,"-")) {
101    config_file = stdin;
102    return;
103  } else if (config_file_name && index(config_file_name,'/')) {
104    if ((res = open_this_config_file(config_file_name))) {
105      fprintf(stderr,"Could not locate or open config file\n");
106      fprintf(stderr,"%s: %s\n",config_file_name,strerror(res));
107      exit(1);
108    }
109  }
110  else {
111    if (config_file_name)
112      filename = config_file_name;
113    else
114      filename = strdup(DEFAULT_CONFIG_FILE_NAME);
115    for (i = 0; config_file_path[i]; i++) {
116      if ((snprintf(buffer,MAX_FILENAME_LEN,
117                   "%s/%s",config_file_path[i],filename)) < 1) {
118        fprintf(stderr,
119                "open_config_file: ridiculous long config file name!\n");
120        exit(1);
121      }
122      if (!open_this_config_file(buffer)) {
123        free(config_file_name);
124        config_file_name = strdup(buffer);
125        return;
126      }
127    }
128    fprintf(stderr,"Could not locate or open config file!\n");
129    exit(1);
130  }
131}
132   
133int open_this_config_file(char *filename)
134{
135  config_file = fopen(filename,"r");
136  if (! config_file)
137    return -errno;
138  return 0;
139}
140 
141
142int main (int argc, char *argv[])
143{
144  int c,res,i;
145
146  struct option long_opts[] =  {
147    { "help", no_argument, NULL, 'h' },
148    { "set", no_argument, NULL, 's' },
149    { "version", no_argument, NULL, 'v'},
150    { "fahrenheit", no_argument, NULL, 'f' },
151    { "no-adapter", no_argument, NULL, 'A' },
152    { "config-file", required_argument, NULL, 'c' },
153    { "unknown", no_argument, NULL, 'u' },
154    { 0,0,0,0 }
155  };
156
157  do_unknown = 0;
158  do_sets = 0;
159  hide_adapter = 0;
160  while (1) {
161    c = getopt_long(argc,argv,"hsvfAc:u",long_opts,NULL);
162    if (c == EOF)
163      break;
164    switch(c) {
165    case ':':
166    case '?':
167      print_short_help();
168      exit(1);
169    case 'h':
170      print_long_help();
171      exit(0);
172    case 'v':
173      print_version();
174      exit(0);
175    case 'c':
176      config_file_name = strdup(optarg);
177      break;
178    case 's':
179      do_sets = 1;
180      break;
181    case 'f':
182      fahrenheit = 1;
183      break;
184    case 'A':
185      hide_adapter = 1;
186      break;
187    case 'u':
188      do_unknown = 1;
189      break;
190    default:
191      fprintf(stderr,"Internal error while parsing options!\n");
192      exit(1);
193    }
194  }
195
196  if (optind == argc) {
197    chips[0].prefix = SENSORS_CHIP_NAME_PREFIX_ANY;
198    chips[0].bus = SENSORS_CHIP_NAME_BUS_ANY;
199    chips[0].addr = SENSORS_CHIP_NAME_ADDR_ANY;
200    chips_count = 1;
201  } else 
202    for(i = optind; i < argc; i++) 
203      if ((res = sensors_parse_chip_name(argv[i],chips+chips_count))) {
204        fprintf(stderr,"Parse error in chip name `%s'\n",argv[i]);
205        print_short_help();
206        exit(1);
207      } else if (++chips_count == CHIPS_MAX) {
208        fprintf(stderr,"Too many chips on command line!\n");
209        exit(1);
210      }
211
212
213  open_config_file();
214
215  if ((res = sensors_init(config_file))) {
216    fprintf(stderr,"%s\n",sensors_strerror(res));
217    if (res == -SENSORS_ERR_PROC)
218      fprintf(stderr,
219              "/proc/sys/dev/sensors/chips or /proc/bus/i2c unreadable;\n"
220              "Make sure you have done 'modprobe i2c-proc'!\n");
221    exit(1);
222  }
223
224  if(do_the_real_work()) {
225    exit(0);
226  } else {
227    if(chips[0].prefix == SENSORS_CHIP_NAME_PREFIX_ANY)
228            fprintf(stderr,"No sensors found!\n");
229    else
230            fprintf(stderr,"Specified sensor(s) not found!\n");
231    exit(1);
232  }
233}
234
235/* returns number of chips found */
236int do_the_real_work(void)
237{
238  const sensors_chip_name *chip;
239  int chip_nr,i;
240  int cnt = 0;
241
242  for (chip_nr = 0; (chip = sensors_get_detected_chips(&chip_nr));)
243    for(i = 0; i < chips_count; i++)
244      if (sensors_match_chip(*chip,chips[i])) {
245        if(do_sets)
246          do_a_set(*chip);
247        else
248          do_a_print(*chip);
249        i = chips_count;
250        cnt++;
251      }
252   return(cnt);
253}
254
255void do_a_set(sensors_chip_name name)
256{
257  int res;
258  if ((res = sensors_do_chip_sets(name))) {
259    if (res == -SENSORS_ERR_PROC) {
260      fprintf(stderr,"%s: %s for writing;\n",sprintf_chip_name(name),
261              sensors_strerror(res));
262      fprintf(stderr,"Run as root?\n");
263    } else {
264      fprintf(stderr,"%s: %s\n",sprintf_chip_name(name),
265              sensors_strerror(res));
266    }
267  }
268}
269
270const char *sprintf_chip_name(sensors_chip_name name)
271{
272  #define BUF_SIZE 200
273  static char buf[BUF_SIZE];
274
275  if (name.bus == SENSORS_CHIP_NAME_BUS_ISA)
276    snprintf(buf,BUF_SIZE,"%s-isa-%04x",name.prefix,name.addr);
277  else
278    snprintf(buf,BUF_SIZE,"%s-i2c-%d-%02x",name.prefix,name.bus,name.addr);
279  return buf;
280}
281
282void do_a_print(sensors_chip_name name)
283{
284  const char *algo,*adap;
285
286  printf("%s\n",sprintf_chip_name(name));
287  adap = sensors_get_adapter_name(name.bus);
288  if (adap && !hide_adapter)
289    printf("Adapter: %s\n",adap);
290  algo = sensors_get_algorithm_name(name.bus);
291  if (algo && !hide_adapter)
292    printf("Algorithm: %s\n",algo);
293  if (!algo || !adap)
294    printf(" ERROR: Can't get adapter or algorithm?!?\n");
295  if (do_unknown)
296    print_unknown_chip(&name);
297  else if (!strcmp(name.prefix,"ds1621"))
298    print_ds1621(&name);
299  else if (!strcmp(name.prefix,"lm75"))
300    print_lm75(&name);
301  else if (!strcmp(name.prefix,"adm1021") || !strcmp(name.prefix,"max1617") ||
302           !strcmp(name.prefix,"max1617a") || !strcmp(name.prefix, "thmc10") ||
303           !strcmp(name.prefix,"lm84") || !strcmp(name.prefix, "gl523") ||
304           !strcmp(name.prefix, "adm1023") || !strcmp(name.prefix, "mc1066"))
305    print_adm1021(&name);
306  else if (!strcmp(name.prefix,"adm9240") ||
307           !strcmp(name.prefix,"ds1780") ||
308           !strcmp(name.prefix,"lm81"))
309    print_adm9240(&name);
310  else if (!strcmp(name.prefix,"lm78") || !strcmp(name.prefix,"lm78-j") ||
311           !strcmp(name.prefix,"lm79"))
312    print_lm78(&name);
313  else if (!strcmp(name.prefix,"mtp008"))
314    print_mtp008(&name);
315  else if (!strcmp(name.prefix,"sis5595"))
316    print_sis5595(&name);
317  else if (!strcmp(name.prefix,"via686a"))
318    print_via686a(&name);
319  else if (!strcmp(name.prefix,"lm80"))
320    print_lm80(&name);
321  else if (!strcmp(name.prefix,"lm87"))
322    print_lm87(&name);
323  else if (!strcmp(name.prefix,"gl518sm"))
324    print_gl518(&name);
325  else if (!strcmp(name.prefix,"adm1025"))
326    print_adm1025(&name);
327  else if (!strcmp(name.prefix,"adm1024"))
328    print_adm1024(&name);
329  else if ((!strcmp(name.prefix,"w83781d")) ||
330           (!strcmp(name.prefix,"w83782d")) ||
331           (!strcmp(name.prefix,"w83783s")) ||
332           (!strcmp(name.prefix,"w83627hf")) ||
333           (!strcmp(name.prefix,"w83697hf")) ||
334           (!strcmp(name.prefix,"as99127f")))
335    print_w83781d(&name);
336  else if (!strncmp(name.prefix,"maxilife-", 9))
337    print_maxilife(&name);
338  else if (!strcmp(name.prefix,"it87"))
339    print_it87(&name);
340  else if (!strcmp(name.prefix,"ddcmon"))
341    print_ddcmon(&name);
342  else if (!strcmp(name.prefix,"eeprom"))
343    print_eeprom(&name);
344  else if (!strcmp(name.prefix,"fscpos"))
345    print_fscpos(&name);
346  else if (!strcmp(name.prefix,"fscscy"))
347    print_fscscy(&name);
348  else if (!strcmp(name.prefix,"pcf8591"))
349    print_pcf8591(&name);
350  else if (!strcmp(name.prefix,"vt1211"))
351    print_vt1211(&name);
352  else if (!strcmp(name.prefix,"smsc47m1"))
353    print_smsc47m1(&name);
354  else if (!strcmp(name.prefix,"lm92"))
355    print_lm92(&name);
356  else if (!strcmp(name.prefix,"vt8231"))
357    print_vt8231(&name);
358  else
359    print_unknown_chip(&name);
360  printf("\n");
361}
Note: See TracBrowser for help on using the browser.