From: Francois Fleuret Date: Fri, 30 Oct 2009 18:53:02 +0000 (-0400) Subject: Starting conversion to C. X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=commitdiff_plain;h=ca901b237ada614c88663c582f924d46c6e87648;p=breezed.git Starting conversion to C. --- diff --git a/breezed.cc b/breezed.cc index ae61293..473e409 100644 --- a/breezed.cc +++ b/breezed.cc @@ -22,7 +22,6 @@ */ -#include #include #include #include @@ -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) { - cerr << "Buffer overflow in next_word." << endl; + fprintf(stderr, "Buffer overflow in next_word.\n"); 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) { - cerr << "Error in setting the fan level (" << strerror(errno) << ")." - << endl; + fprintf(stderr, "Error in setting the fan level (%s).\n", + strerror(errno)); exit(1); } } @@ -118,7 +117,7 @@ void define_thermal_files(char *definition) { char token[buffer_size]; if(file_thermal) { - cerr << "Thermal files already defined." << endl; + fprintf(stderr, "Thermal files already defined.\n"); exit(1); } @@ -144,7 +143,7 @@ void define_temperature_thresholds(char *definition) { char token[buffer_size]; if(temperature_thresholds) { - cerr << "Temperature thresholds already defined." << endl; + fprintf(stderr, "Temperature thresholds already defined.\n"); exit(1); } @@ -168,8 +167,7 @@ void define_temperature_thresholds(char *definition) { 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++; @@ -194,11 +192,8 @@ int main(int argc, char **argv) { } 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); } @@ -213,7 +208,7 @@ int main(int argc, char **argv) { 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); } @@ -228,7 +223,7 @@ int main(int argc, char **argv) { 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]); @@ -239,12 +234,12 @@ int main(int argc, char **argv) { 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) { - cerr << "Fan file already defined." << endl; + fprintf(stderr, "Fan file already defined.\n"); exit(1); } file_fan = new char[strlen(argv[i]) + 1]; @@ -258,7 +253,7 @@ int main(int argc, char **argv) { i++; if(i == argc) { - cerr << "Missing parameter for " << argv[i - 1] << endl; + fprintf(stderr, "Missing parameter for %s.\n", argv[i - 1]); exit(1); } @@ -268,7 +263,7 @@ int main(int argc, char **argv) { 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 ]\\\n\ [--no-configuration-file|-ncf]\\\n\ [--thermal-files|-tf ] \\\n\ @@ -302,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\ -Version " << major_version_number << "." - << minor_version_number << ", December 2008.\n\ +Version %d.%d, November 2009.\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 { - cerr << "Unknown argument " << argv[i] << "." << endl; + fprintf(stderr, "Unknown argument %s.\n", argv[i]); exit(1); } } @@ -323,7 +319,7 @@ Written by Francois Fleuret (francois@fleuret.org).\n"; ifstream cf(configuration_file); 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); } @@ -339,8 +335,8 @@ Written by Francois Fleuret (francois@fleuret.org).\n"; 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); @@ -352,12 +348,12 @@ Written by Francois Fleuret (francois@fleuret.org).\n"; 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) { - 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]; @@ -366,16 +362,16 @@ Written by Francois Fleuret (francois@fleuret.org).\n"; 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] != '#') { - 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); } } @@ -384,26 +380,25 @@ Written by Francois Fleuret (francois@fleuret.org).\n"; ////////////////////////////////////////////////////////////////////// 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) { - cerr << "No thermal file was provided." << endl; + fprintf(stderr, "No thermal file was provided.\n"); 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) { - 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); } } @@ -411,9 +406,8 @@ Written by Francois Fleuret (francois@fleuret.org).\n"; 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); } @@ -421,14 +415,14 @@ Written by Francois Fleuret (francois@fleuret.org).\n"; 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++) { - cout << "temperature_thresholds[" << t << "] " - << temperature_thresholds[t] << endl; + printf("temperature_thresholds[%d] %d", + t, temperature_thresholds[t]); } } @@ -439,7 +433,7 @@ Written by Francois Fleuret (francois@fleuret.org).\n"; int temperature = -1; if(debug) { - cout << "Temperature:"; + printf("Temperature:"); } for(int i = 0; i < nb_file_thermal; i++) { @@ -468,7 +462,7 @@ Written by Francois Fleuret (francois@fleuret.org).\n"; t *= sign; if(debug) { - cout << " " << t; + printf(" %d", t); } // So that we can deal with the new files where the @@ -481,26 +475,26 @@ Written by Francois Fleuret (francois@fleuret.org).\n"; if(debug) { if(i < nb_file_thermal - 1) - cout << " /"; + printf(" /"); else - cout << endl; + printf("\n"); } } 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) { - cerr << "Error while reading " << file_thermal[i] - << " (" << strerror(errno) << ")." << endl; + fprintf(stderr, "Error while reading %s (%s).\n", + file_thermal[i], strerror(errno)); } 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) { - cerr << "Could not read a meaningful temperature." << endl; + fprintf(stderr, "Could not read a meaningful temperature.\n"); exit(1); } @@ -539,9 +533,8 @@ Written by Francois Fleuret (francois@fleuret.org).\n"; 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); } }