Starting conversion to C.
[breezed.git] / breezed.cc
index 19e500c..473e409 100644 (file)
@@ -3,7 +3,7 @@
 
    breezed is a fan speed control daemon for Linux computers.
 
 
    breezed is a fan speed control daemon for Linux computers.
 
-   Copyright (c) 2008 Francois Fleuret
+   Copyright (c) 2008, 2009 Francois Fleuret
    Written by Francois Fleuret <francois@fleuret.org>
 
    This file is part of breezed.
    Written by Francois Fleuret <francois@fleuret.org>
 
    This file is part of breezed.
@@ -22,7 +22,6 @@
 
 */
 
 
 */
 
-#include <iostream>
 #include <fstream>
 #include <cmath>
 #include <stdio.h>
 #include <fstream>
 #include <cmath>
 #include <stdio.h>
 using namespace std;
 
 const int major_version_number = 1;
 using namespace std;
 
 const int major_version_number = 1;
-const int minor_version_number = 0;
+const int minor_version_number = 2;
 
 const int buffer_size = 1024;
 
 
 const int buffer_size = 1024;
 
-const char *default_confguration_file = "/etc/breezed.conf";
+const char *default_configuration_file = "/etc/breezed.conf";
 
 // The time period to check the temperature.
 const int polling_delay = 5;
 
 // The time period to check the temperature.
 const int polling_delay = 5;
@@ -61,7 +60,7 @@ int last_level = -1;
 
 int file_fan_fd;
 
 
 int file_fan_fd;
 
-int nb_temperature_thresholds = 0;
+int nb_temperature_thresholds;
 int *temperature_thresholds = 0;
 
 int nb_file_thermal = 0;
 int *temperature_thresholds = 0;
 
 int nb_file_thermal = 0;
@@ -88,7 +87,7 @@ char *next_word(char *buffer, char *r, int buffer_size) {
       while((*r != '\r') && (*r != '\n') && (*r != '\0') &&
             (*r != '\t') && (*r != ' ') && (*r != ',')) {
         if(s == buffer + buffer_size) {
       while((*r != '\r') && (*r != '\n') && (*r != '\0') &&
             (*r != '\t') && (*r != ' ') && (*r != ',')) {
         if(s == buffer + buffer_size) {
-          cerr << "Buffer overflow in next_word." << endl;
+          fprintf(stderr, "Buffer overflow in next_word.\n");
           exit(1);
         }
         *s++ = *r++;
           exit(1);
         }
         *s++ = *r++;
@@ -108,8 +107,8 @@ void set_fan_level(int fan_fd, int f, int max_fan_level) {
   if(f < 0 || f > max_fan_level) f = max_fan_level;
   sprintf(buffer, "level %d\n", f);
   if(write(fan_fd, buffer, strlen(buffer)) < 0) {
   if(f < 0 || f > max_fan_level) f = max_fan_level;
   sprintf(buffer, "level %d\n", f);
   if(write(fan_fd, buffer, strlen(buffer)) < 0) {
-    cerr << "Error in setting the fan level (" << strerror(errno) << ")."
-         << endl;
+    fprintf(stderr, "Error in setting the fan level (%s).\n",
+            strerror(errno));
     exit(1);
   }
 }
     exit(1);
   }
 }
@@ -118,7 +117,7 @@ void define_thermal_files(char *definition) {
   char token[buffer_size];
 
   if(file_thermal) {
   char token[buffer_size];
 
   if(file_thermal) {
-    cerr << "Thermal files already defined." << endl;
+    fprintf(stderr, "Thermal files already defined.\n");
     exit(1);
   }
 
     exit(1);
   }
 
@@ -144,10 +143,12 @@ void define_temperature_thresholds(char *definition) {
   char token[buffer_size];
 
   if(temperature_thresholds) {
   char token[buffer_size];
 
   if(temperature_thresholds) {
-    cerr << "Temperature thresholds already defined." << endl;
+    fprintf(stderr, "Temperature thresholds already defined.\n");
     exit(1);
   }
 
     exit(1);
   }
 
+  nb_temperature_thresholds = 1;
+
   char *s;
   s = definition;
   while(s) {
   char *s;
   s = definition;
   while(s) {
@@ -157,21 +158,20 @@ void define_temperature_thresholds(char *definition) {
 
   temperature_thresholds = new int[nb_temperature_thresholds];
 
 
   temperature_thresholds = new int[nb_temperature_thresholds];
 
+  temperature_thresholds[0] = -1;
+
   s = definition;
   s = definition;
-  int k = 0;
+  int k = 1;
   while(s) {
     s = next_word(token, s, buffer_size);
     temperature_thresholds[k] = atoi(token);
     if(k > 0 &&
        temperature_thresholds[k] < temperature_thresholds[k-1]) {
   while(s) {
     s = next_word(token, s, buffer_size);
     temperature_thresholds[k] = atoi(token);
     if(k > 0 &&
        temperature_thresholds[k] < temperature_thresholds[k-1]) {
-      cerr << "The temperature thresholds have to be increasing."
-           << endl;
+      fprintf(stderr, "The temperature thresholds have to be increasing.\n");
       exit(0);
     }
     k++;
   }
       exit(0);
     }
     k++;
   }
-
-  temperature_thresholds[0] = -1;
 }
 
 //////////////////////////////////////////////////////////////////////
 }
 
 //////////////////////////////////////////////////////////////////////
@@ -180,8 +180,8 @@ int main(int argc, char **argv) {
 
   char buffer[buffer_size], token[buffer_size];
 
 
   char buffer[buffer_size], token[buffer_size];
 
-  configuration_file = new char[strlen(default_confguration_file) + 1];
-  strcpy(configuration_file, default_confguration_file);
+  configuration_file = new char[strlen(default_configuration_file) + 1];
+  strcpy(configuration_file, default_configuration_file);
 
   int i = 1;
   while(i < argc) {
 
   int i = 1;
   while(i < argc) {
@@ -192,11 +192,8 @@ int main(int argc, char **argv) {
     }
 
     else if(strcmp(argv[i], "--version") == 0 || strcmp(argv[i], "-v") == 0) {
     }
 
     else if(strcmp(argv[i], "--version") == 0 || strcmp(argv[i], "-v") == 0) {
-      cout << "Breezed, "
-           << "v" << major_version_number
-           << "." << minor_version_number
-           << ". "
-           << "Written by Francois Fleuret (francois@fleuret.org).\n";
+      printf("Breezed v%d.%d. Written by Francois Fleuret (francois@fleuret.org).\n",
+             major_version_number, minor_version_number);
       exit(0);
     }
 
       exit(0);
     }
 
@@ -211,7 +208,7 @@ int main(int argc, char **argv) {
             strcmp(argv[i], "-cf") == 0) {
       i++;
       if(i == argc) {
             strcmp(argv[i], "-cf") == 0) {
       i++;
       if(i == argc) {
-        cerr << "Missing parameter for " << argv[i - 1] << endl;
+        fprintf(stderr, "Missing parameter for %s.\n", argv[i - 1]);
         exit(1);
       }
 
         exit(1);
       }
 
@@ -226,7 +223,7 @@ int main(int argc, char **argv) {
             strcmp(argv[i], "-tf") == 0) {
       i++;
       if(i == argc) {
             strcmp(argv[i], "-tf") == 0) {
       i++;
       if(i == argc) {
-        cerr << "Missing parameter for " << argv[i - 1] << endl;
+        fprintf(stderr, "Missing parameter for %s.\n", argv[i - 1]);
         exit(1);
       }
       define_thermal_files(argv[i]);
         exit(1);
       }
       define_thermal_files(argv[i]);
@@ -237,12 +234,12 @@ int main(int argc, char **argv) {
             strcmp(argv[i], "-ff") == 0) {
       i++;
       if(i == argc) {
             strcmp(argv[i], "-ff") == 0) {
       i++;
       if(i == argc) {
-        cerr << "Missing parameter for " << argv[i - 1] << endl;
+        fprintf(stderr, "Missing parameter for %s.\n", argv[i - 1]);
         exit(1);
       }
 
       if(file_fan) {
         exit(1);
       }
 
       if(file_fan) {
-        cerr << "Fan file already defined." << endl;
+        fprintf(stderr, "Fan file already defined.\n");
         exit(1);
       }
       file_fan = new char[strlen(argv[i]) + 1];
         exit(1);
       }
       file_fan = new char[strlen(argv[i]) + 1];
@@ -256,7 +253,7 @@ int main(int argc, char **argv) {
       i++;
 
       if(i == argc) {
       i++;
 
       if(i == argc) {
-        cerr << "Missing parameter for " << argv[i - 1] << endl;
+        fprintf(stderr, "Missing parameter for %s.\n", argv[i - 1]);
         exit(1);
       }
 
         exit(1);
       }
 
@@ -266,7 +263,7 @@ int main(int argc, char **argv) {
 
     else if(strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) {
 
 
     else if(strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) {
 
-      cout << argv[0] << " [--version|-v] [--help|-h] [--debug|-d]\\\n\
+      printf("%s [--version|-v] [--help|-h] [--debug|-d]\\\n\
   [--configuration-file|-cf <file>]\\\n\
   [--no-configuration-file|-ncf]\\\n\
   [--thermal-files|-tf <thermalfile1,thermalfile2,...>] \\\n\
   [--configuration-file|-cf <file>]\\\n\
   [--no-configuration-file|-ncf]\\\n\
   [--thermal-files|-tf <thermalfile1,thermalfile2,...>] \\\n\
@@ -300,16 +297,17 @@ This daemon should be started through the adequate shell script in\n\
 Options can be set either in the configuration file, or on the \n\
 command line. Options can not be set twice.\n\
 \n\
 Options can be set either in the configuration file, or on the \n\
 command line. Options can not be set twice.\n\
 \n\
-Version " << major_version_number << "."
-           << minor_version_number << ", December 2008.\n\
+Version %d.%d, November 2009.\n\
 \n\
 \n\
-Written by Francois Fleuret (francois@fleuret.org).\n";
+Written by Francois Fleuret (francois@fleuret.org).\n",
+             argv[0],
+             major_version_number, minor_version_number);
 
       exit(0);
     }
 
     else {
 
       exit(0);
     }
 
     else {
-      cerr << "Unknown argument " << argv[i] << "." << endl;
+      fprintf(stderr, "Unknown argument %s.\n", argv[i]);
       exit(1);
     }
   }
       exit(1);
     }
   }
@@ -319,8 +317,9 @@ Written by Francois Fleuret (francois@fleuret.org).\n";
 
   if(configuration_file) {
     ifstream cf(configuration_file);
 
   if(configuration_file) {
     ifstream cf(configuration_file);
+
     if(cf.fail()) {
     if(cf.fail()) {
-      cerr << "Can not open " << configuration_file << " for reading." << endl;
+      fprintf(stderr, "Can not open %s for reading.\n", configuration_file);
       exit(1);
     }
 
       exit(1);
     }
 
@@ -336,8 +335,8 @@ Written by Francois Fleuret (francois@fleuret.org).\n";
 
       if(strcmp(token, "thermal_files") == 0) {
         if(s == 0) {
 
       if(strcmp(token, "thermal_files") == 0) {
         if(s == 0) {
-          cerr << "Missing parameter in "
-               << configuration_file << ":" << line_number << endl;
+          fprintf(stderr, "Missing parameter in %s:%d\n",
+                  configuration_file, line_number);
           exit(1);
         }
         define_thermal_files(s);
           exit(1);
         }
         define_thermal_files(s);
@@ -349,12 +348,12 @@ Written by Francois Fleuret (francois@fleuret.org).\n";
 
       else if(strcmp(token, "fan_file") == 0) {
         if(file_fan) {
 
       else if(strcmp(token, "fan_file") == 0) {
         if(file_fan) {
-          cerr << "Fan file already defined." << endl;
+          fprintf(stderr, "Fan file already defined.\n");
           exit(1);
         }
         if(s == 0) {
           exit(1);
         }
         if(s == 0) {
-          cerr << "Missing parameter in "
-               << configuration_file << ":" << line_number << endl;
+          fprintf(stderr, "Missing parameter in %s:%d\n",
+                  configuration_file, line_number);
           exit(1);
         }
         file_fan = new char[strlen(s) + 1];
           exit(1);
         }
         file_fan = new char[strlen(s) + 1];
@@ -363,16 +362,16 @@ Written by Francois Fleuret (francois@fleuret.org).\n";
 
       else if(strcmp(token, "temperature_thresholds") == 0) {
         if(s == 0) {
 
       else if(strcmp(token, "temperature_thresholds") == 0) {
         if(s == 0) {
-          cerr << "Missing parameter in "
-               << configuration_file << ":" << line_number << endl;
+          fprintf(stderr, "Missing parameter in %s:%d\n",
+                  configuration_file, line_number);
           exit(1);
         }
         define_temperature_thresholds(s);
       }
 
       else if(token[0] && token[0] != '#') {
           exit(1);
         }
         define_temperature_thresholds(s);
       }
 
       else if(token[0] && token[0] != '#') {
-        cerr << "Unknown keyword '" << token << "' in "
-             << configuration_file << ":" << line_number << endl;
+        fprintf(stderr, "Unknown keyword '%s' in %s:%d.\n",
+                token, configuration_file, line_number);
         exit(1);
       }
     }
         exit(1);
       }
     }
@@ -381,26 +380,25 @@ Written by Francois Fleuret (francois@fleuret.org).\n";
   //////////////////////////////////////////////////////////////////////
 
   if(nb_temperature_thresholds == 0) {
   //////////////////////////////////////////////////////////////////////
 
   if(nb_temperature_thresholds == 0) {
-    cerr << "No temperature threshold was provided." << endl;
+    fprintf(stderr, "No temperature threshold was provided.\n");
     exit(1);
   }
 
   if(nb_file_thermal == 0) {
     exit(1);
   }
 
   if(nb_file_thermal == 0) {
-    cerr << "No thermal file was provided." << endl;
+    fprintf(stderr, "No thermal file was provided.\n");
     exit(1);
   }
 
   if(file_fan == 0){
     exit(1);
   }
 
   if(file_fan == 0){
-    cerr << "No fan file was provided." << endl;
+    fprintf(stderr, "No fan file was provided.\n");
     exit(1);
   }
 
   for(int i = 0; i < nb_file_thermal; i++) {
     file_thermal_fd[i] = open(file_thermal[i], O_RDONLY);
     if(file_thermal_fd[i] < 0) {
     exit(1);
   }
 
   for(int i = 0; i < nb_file_thermal; i++) {
     file_thermal_fd[i] = open(file_thermal[i], O_RDONLY);
     if(file_thermal_fd[i] < 0) {
-      cerr << "Can not open " << file_thermal[i]
-           << " for reading (" << strerror(errno) << ")."
-           << endl;
+      fprintf(stderr, "Can not open %s for reading (%s).\n",
+              file_thermal[i], strerror(errno));
       exit(1);
     }
   }
       exit(1);
     }
   }
@@ -408,9 +406,8 @@ Written by Francois Fleuret (francois@fleuret.org).\n";
   file_fan_fd = open(file_fan, O_WRONLY);
 
   if(file_fan_fd < 0) {
   file_fan_fd = open(file_fan, O_WRONLY);
 
   if(file_fan_fd < 0) {
-    cerr << "Can not open " << file_fan
-         << " for writing (" << strerror(errno) << ")."
-         << endl;
+    fprintf(stderr, "Can not open %s for writing (%s).\n",
+            file_fan, strerror(errno));
     exit(1);
   }
 
     exit(1);
   }
 
@@ -418,14 +415,14 @@ Written by Francois Fleuret (francois@fleuret.org).\n";
 
   if(debug) {
     for(int t = 0; t < nb_file_thermal; t++) {
 
   if(debug) {
     for(int t = 0; t < nb_file_thermal; t++) {
-      cout << "file_thermal[" << t << "] " << file_thermal[t] << endl;
+      printf("file_thermal[%d] %s\n", t, file_thermal[t]);
     }
 
     }
 
-    cout << "file_fan " << file_fan << endl;
+    printf("file_fan %s\n", file_fan);
 
     for(int t = 0; t < nb_temperature_thresholds; t++) {
 
     for(int t = 0; t < nb_temperature_thresholds; t++) {
-      cout << "temperature_thresholds[" << t << "] "
-           << temperature_thresholds[t] << endl;
+      printf("temperature_thresholds[%d] %d",
+             t, temperature_thresholds[t]);
     }
   }
 
     }
   }
 
@@ -436,7 +433,7 @@ Written by Francois Fleuret (francois@fleuret.org).\n";
     int temperature = -1;
 
     if(debug) {
     int temperature = -1;
 
     if(debug) {
-      cout << "Temperature:";
+      printf("Temperature:");
     }
 
     for(int i = 0; i < nb_file_thermal; i++) {
     }
 
     for(int i = 0; i < nb_file_thermal; i++) {
@@ -465,35 +462,39 @@ Written by Francois Fleuret (francois@fleuret.org).\n";
 
             t *= sign;
             if(debug) {
 
             t *= sign;
             if(debug) {
-              cout << " " << t;
+              printf(" %d", t);
             }
 
             }
 
+            // So that we can deal with the new files where the
+            // temperature are in 1/1000th of C
+            if(t > 1000) t /= 1000;
+
             if(t > temperature) temperature = t;
           }
         }
 
         if(debug) {
           if(i < nb_file_thermal - 1)
             if(t > temperature) temperature = t;
           }
         }
 
         if(debug) {
           if(i < nb_file_thermal - 1)
-            cout << " /";
+            printf(" /");
           else
           else
-            cout << endl;
+            printf("\n");
         }
       } else {
         if(size == 0) {
         }
       } else {
         if(size == 0) {
-          cerr << "Nothing to read in " << file_thermal[i] << endl;
+          fprintf(stderr, "Nothing to read in %d.\n", file_thermal[i]);
         } else if(size < 0) {
         } else if(size < 0) {
-          cerr << "Error while reading " << file_thermal[i]
-               << " (" << strerror(errno) << ")." << endl;
+          fprintf(stderr, "Error while reading %s (%s).\n",
+                  file_thermal[i], strerror(errno));
         } else {
         } else {
-          cerr << "Error while reading " << file_thermal[i]
-               << " (too large)." << endl;
+          fprintf(stderr, "Error while reading %s (too large).\n",
+                  file_thermal[i]);
         }
         exit(1);
       }
     }
 
     if(temperature < 0) {
         }
         exit(1);
       }
     }
 
     if(temperature < 0) {
-      cerr << "Could not read a meaningful temperature." << endl;
+      fprintf(stderr, "Could not read a meaningful temperature.\n");
       exit(1);
     }
 
       exit(1);
     }
 
@@ -532,9 +533,8 @@ Written by Francois Fleuret (francois@fleuret.org).\n";
       nb_rounds_since_last_change = 0;
       last_level = new_level;
       if(debug) {
       nb_rounds_since_last_change = 0;
       last_level = new_level;
       if(debug) {
-        cout << "Temperature is " << temperature << "C,"
-             << " setting the fan level to " << new_level
-             << endl;
+        printf("Temperature is %dC setting the fan level to %d.",
+               temperature, new_level);
       }
     }
 
       }
     }