2012 copyright
[psensor.git] / src / lib / lmsensor.c
index 65c41d1..c33dfb2 100644 (file)
@@ -1,22 +1,21 @@
 /*
-    Copyright (C) 2010-2011 jeanfi@gmail.com
-
-    This program is free software; you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation; either version 2 of the License, or
-    (at your option) any later version.
-
-    This program is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program; if not, write to the Free Software
-    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-    02110-1301 USA
-*/
-
+ * Copyright (C) 2010-2012 jeanfi@gmail.com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
 #include <locale.h>
 #include <libintl.h>
 #define _(str) gettext(str)
@@ -30,6 +29,8 @@
 
 #include "psensor.h"
 
+static int init_done;
+
 static double get_value(const sensors_chip_name *name,
                        const sensors_subfeature *sub)
 {
@@ -80,6 +81,9 @@ void lmsensor_psensor_list_update(struct psensor **sensors)
 {
        struct psensor **s_ptr = sensors;
 
+       if (!init_done)
+               return ;
+
        while (*s_ptr) {
                struct psensor *sensor = *s_ptr;
 
@@ -94,7 +98,7 @@ void lmsensor_psensor_list_update(struct psensor **sensors)
        }
 }
 
-struct psensor *
+static struct psensor *
 lmsensor_psensor_create(const sensors_chip_name *chip,
                        const sensors_feature *feature,
                        int values_max_length)
@@ -156,20 +160,6 @@ lmsensor_psensor_create(const sensors_chip_name *chip,
        return psensor;
 }
 
-int lmsensor_init()
-{
-       int err = sensors_init(NULL);
-
-       if (err) {
-               fprintf(stderr,
-                       _("ERROR: lm-sensors initialization failure: %s\n"),
-                       sensors_strerror(err));
-               return 0;
-       } else {
-               return 1;
-       }
-}
-
 struct psensor **lmsensor_psensor_list_add(struct psensor **sensors,
                                           int vn)
 {
@@ -180,6 +170,9 @@ struct psensor **lmsensor_psensor_list_add(struct psensor **sensors,
        struct psensor *s;
        int i;
 
+       if (!init_done)
+               return NULL;
+
        result = sensors;
        while ((chip = sensors_get_detected_chips(NULL, &chip_nr))) {
 
@@ -205,3 +198,23 @@ struct psensor **lmsensor_psensor_list_add(struct psensor **sensors,
 
        return result;
 }
+
+void lmsensor_init()
+{
+       int err = sensors_init(NULL);
+
+       if (err) {
+               fprintf(stderr,
+                       _("ERROR: lm-sensors initialization failure: %s\n"),
+                       sensors_strerror(err));
+               init_done = 0;
+       } else {
+               init_done = 1;
+       }
+}
+
+void lmsensor_cleanup()
+{
+       if (init_done)
+               sensors_cleanup();
+}