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

Revision 104, 5.3 KB (checked in by frodo, 14 years ago)

Library/program updates.

YES! It now pretty-prints the LM75 and LM78 data.

  • 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  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
30#define PROGRAM "sensors"
31#define VERSION "0.0"
32#define DEFAULT_CONFIG_FILE_NAME "sensors.conf"
33
34static char *config_file_name;
35FILE *config_file;
36static const char *config_file_path[] = 
37  { "/etc", "/usr/lib/sensors", "/usr/local/lib/sensors", "/usr/lib",
38    "/usr/local/lib", ".", 0 };
39
40extern int main(int argc, char *arv[]);
41static void print_short_help(void);
42static void print_long_help(void);
43static void print_version(void);
44static void open_config_file(void);
45static int open_this_config_file(char *filename);
46
47void print_short_help(void)
48{
49  printf("Try `%s -h' for more information\n",PROGRAM);
50}
51
52void print_long_help(void)
53{
54  printf("Usage: %s [OPTION]...\n",PROGRAM);
55  printf("  -c, --config-file     Specify a config file\n");
56  printf("  -h, --help            Display this help text\n");
57  printf("  -v, --version         Display the program version\n");
58  printf("\n");
59  printf("By default, a list of directories is examined for the config file `sensors.conf'\n");
60  printf("Use `-' after `-c' to read the config file from stdin\n");
61}
62
63void print_version(void)
64{
65  printf("%s version %s\n", PROGRAM, VERSION);
66}
67
68/* This examines global var config_file, and leaves the name there too.
69   It also opens config_file. */
70void open_config_file(void)
71{
72#define MAX_FILENAME_LEN 1024
73  char *filename;
74  char buffer[MAX_FILENAME_LEN];
75  int res,i;
76
77  if (config_file_name && !strcmp(config_file_name,"-")) {
78    config_file = stdin;
79    return;
80  } else if (config_file_name && index(config_file_name,'/')) {
81    if ((res = open_this_config_file(config_file_name))) {
82      fprintf(stderr,"Could not locate or open config file\n");
83      fprintf(stderr,"%s: %s\n",config_file_name,strerror(res));
84      exit(1);
85    }
86  }
87  else {
88    if (config_file_name)
89      filename = config_file_name;
90    else
91      filename = strdup(DEFAULT_CONFIG_FILE_NAME);
92    for (i = 0; config_file_path[i]; i++) {
93      if ((snprintf(buffer,MAX_FILENAME_LEN,
94                   "%s/%s",config_file_path[i],filename)) < 1) {
95        fprintf(stderr,
96                "open_config_file: ridiculous long config file name!\n");
97        exit(1);
98      }
99      if (!open_this_config_file(buffer)) {
100        free(config_file_name);
101        config_file_name = strdup(buffer);
102        return;
103      }
104    }
105    fprintf(stderr,"Could not locate or open config file!\n");
106    exit(1);
107  }
108}
109   
110int open_this_config_file(char *filename)
111{
112  config_file = fopen(filename,"r");
113  if (! config_file)
114    return -errno;
115  return 0;
116}
117 
118
119int main (int argc, char *argv[])
120{
121  int c,res;
122
123  int chip_nr;
124  const sensors_chip_name *chip;
125  const char *algo,*adap;
126
127  struct option long_opts[] =  {
128    { "help", no_argument, NULL, 'h' },
129    { "version", no_argument, NULL, 'v'},
130    { "config-file", required_argument, NULL, 'c' }
131  };
132
133  while (1) {
134    c = getopt_long(argc,argv,"hvc:",long_opts,NULL);
135    if (c == EOF)
136      break;
137    switch(c) {
138    case ':':
139    case '?':
140      print_short_help();
141      exit(1);
142    case 'h':
143      print_long_help();
144      exit(0);
145    case 'v':
146      print_version();
147      exit(0);
148    case 'c':
149      config_file_name = strdup(optarg);
150      break;
151    default:
152      fprintf(stderr,"Internal error while parsing options!\n");
153    }
154  }
155  open_config_file();
156
157  if ((res = sensors_init(config_file))) {
158    if (res == SENSORS_ERR_PROC)
159      fprintf(stderr,
160              "/proc/sys/dev/sensors/chips or /proc/bus/i2c unreadable:\n"
161              "Make sure you have inserted modules sensors.o and i2c-proc.o!");
162    else
163      fprintf(stderr,"%s\n",sensors_strerror(res));
164    exit(1);
165  }
166
167  /* Here comes the real code... */
168  for (chip_nr = 0; (chip = sensors_get_detected_chips(&chip_nr));) {
169    if (chip->bus == SENSORS_CHIP_NAME_BUS_ISA)
170      printf("%s-isa-%04x\n",chip->prefix,chip->addr);
171    else
172      printf("%s-i2c-%d-%02x\n",chip->prefix,chip->bus,chip->addr);
173    adap = sensors_get_adapter_name(chip->bus);
174    if (adap)
175      printf("Adapter: %s\n",adap);
176    algo = sensors_get_algorithm_name(chip->bus);
177    if (algo)
178      printf("Algorithm: %s\n",algo);
179    if (!algo || !adap)
180      printf(" ERROR: Can't get adapter or algorithm?!?\n");
181    if (!strcmp(chip->prefix,"lm75"))
182      print_lm75(chip);
183    else if (!strcmp(chip->prefix,"lm78") || !strcmp(chip->prefix,"lm78-j") ||
184             !strcmp(chip->prefix,"lm79"))
185      print_lm78(chip);
186    printf("\n");
187  }
188  exit(0);
189}
Note: See TracBrowser for help on using the browser.