From: Francois Fleuret Date: Sat, 31 Oct 2009 01:29:46 +0000 (-0400) Subject: Finished the conversion to C. X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=commitdiff_plain;h=5e61d0ea33e99a3b61d90fc3c8efe1c1b371a4c3;p=breezed.git Finished the conversion to C. --- diff --git a/Makefile b/Makefile index 624d723..ae15304 100644 --- a/Makefile +++ b/Makefile @@ -22,10 +22,12 @@ BINARY_PATH = /usr/bin MAN_PATH = /usr/share/man/man1 PM_SLEEPD = /usr/lib/pm-utils/sleep.d +CFLAGS = -Wall -ansi -pedantic -DREVISION_NUMBER=$(REVISION_NUMBER) $(OPTIMIZE_FLAG) + all: breezed -breezed: breezed.cc - g++ -o breezed breezed.cc +breezed: breezed.c + $(CC) -o $@ $^ $(LDFLAGS) install: breezed /etc/breezed.conf install -m 755 breezed $(BINARY_PATH) diff --git a/breezed.cc b/breezed.c similarity index 79% rename from breezed.cc rename to breezed.c index 14809ac..054179e 100644 --- a/breezed.cc +++ b/breezed.c @@ -29,10 +29,6 @@ #include #include -#define BUFFER_SIZE 4096 - -using namespace std; - const int major_version_number = 1; const int minor_version_number = 2; @@ -40,14 +36,14 @@ const int buffer_size = 1024; const char *default_configuration_file = "/etc/breezed.conf"; -// The time period to check the temperature. +/* The time period to check the temperature. */ const int polling_delay = 5; -// Minimum time between last change and a change down. +/* Minimum time between last change and a change down. */ const int minimum_delay_to_go_down = 30; -// Gap between a threshold to go up and a threshold to go down to -// reduce the oscillations. +/* Gap between a threshold to go up and a threshold to go down to + reduce the oscillations. */ const int down_temperature_delta = 2; char *file_fan = 0; @@ -67,7 +63,7 @@ int *file_thermal_fd = 0; char *configuration_file; -////////////////////////////////////////////////////////////////////// +/******************************************************************/ char *next_word(char *buffer, char *r, int buffer_size) { char *s; @@ -125,14 +121,14 @@ void define_thermal_files(char *definition) { s = next_word(token, s, buffer_size); nb_file_thermal++; } - file_thermal = new char *[nb_file_thermal]; - file_thermal_fd = new int[nb_file_thermal]; + + file_thermal = (char **) malloc(nb_file_thermal * sizeof(char *)); + file_thermal_fd = (int *) malloc(nb_file_thermal * sizeof(int)); s = definition; int k = 0; while(s) { s = next_word(token, s, buffer_size); - file_thermal[k] = new char[strlen(token) + 1]; - strcpy(file_thermal[k], token); + file_thermal[k] = strdup(token); k++; } } @@ -154,7 +150,8 @@ void define_temperature_thresholds(char *definition) { nb_temperature_thresholds++; } - temperature_thresholds = new int[nb_temperature_thresholds]; + temperature_thresholds = + (int *) malloc(nb_temperature_thresholds * sizeof(int)); temperature_thresholds[0] = -1; @@ -172,16 +169,63 @@ void define_temperature_thresholds(char *definition) { } } -////////////////////////////////////////////////////////////////////// +void evaluate_one_configuration_line(char *line, int line_number) { + char token[buffer_size]; + char *s; -int main(int argc, char **argv) { + s = next_word(token, line, buffer_size); + + if(strcmp(token, "thermal_files") == 0) { + if(s == 0) { + fprintf(stderr, "Missing parameter in %s:%d\n", + configuration_file, line_number); + exit(1); + } + define_thermal_files(s); + } + + else if(strcmp(token, "debug") == 0) { + debug = 1; + } + + else if(strcmp(token, "fan_file") == 0) { + if(file_fan) { + fprintf(stderr, "Fan file already defined.\n"); + exit(1); + } + if(s == 0) { + fprintf(stderr, "Missing parameter in %s:%d\n", + configuration_file, line_number); + exit(1); + } + file_fan = strdup(s); + } + + else if(strcmp(token, "temperature_thresholds") == 0) { + if(s == 0) { + fprintf(stderr, "Missing parameter in %s:%d\n", + configuration_file, line_number); + exit(1); + } + define_temperature_thresholds(s); + } + + else if(token[0] && token[0] != '#') { + fprintf(stderr, "Unknown keyword '%s' in %s:%d.\n", + token, configuration_file, line_number); + exit(1); + } +} + +/******************************************************************/ - char buffer[buffer_size], token[buffer_size]; +int main(int argc, char **argv) { + char buffer[buffer_size]; + int i, t; - configuration_file = new char[strlen(default_configuration_file) + 1]; - strcpy(configuration_file, default_configuration_file); + configuration_file = strdup(default_configuration_file); - int i = 1; + i = 1; while(i < argc) { if(strcmp(argv[i], "--debug") == 0 || strcmp(argv[i], "-d") == 0) { @@ -197,7 +241,7 @@ int main(int argc, char **argv) { else if(strcmp(argv[i], "--no-configuration-file") == 0 || strcmp(argv[i], "-ncf") == 0) { - delete[] configuration_file; + free(configuration_file); configuration_file = 0; i++; } @@ -210,9 +254,8 @@ int main(int argc, char **argv) { exit(1); } - delete[] configuration_file; - configuration_file = new char[strlen(argv[i]) + 1]; - strcpy(configuration_file, argv[i]); + free(configuration_file); + configuration_file = strdup(argv[i]); i++; } @@ -240,8 +283,7 @@ int main(int argc, char **argv) { fprintf(stderr, "Fan file already defined.\n"); exit(1); } - file_fan = new char[strlen(argv[i]) + 1]; - strcpy(file_fan, argv[i]); + file_fan = strdup(argv[i]); i++; } @@ -311,10 +353,10 @@ Written by Francois Fleuret (francois@fleuret.org).\n", } - ////////////////////////////////////////////////////////////////////// + /******************************************************************/ if(configuration_file) { - char raw_line[BUFFER_SIZE]; + char raw_line[buffer_size]; int start, end, eol, k; FILE *file; @@ -349,17 +391,17 @@ Written by Francois Fleuret (francois@fleuret.org).\n", end -= start; eol -= start; start = 0; - end += fread(raw_line + end, sizeof(char), BUFFER_SIZE - end, file); + end += fread(raw_line + end, sizeof(char), buffer_size - end, file); while(eol < end && raw_line[eol] != '\n') eol++; } /* The end of the line is the buffer size, which means the line is too long */ - if(eol == BUFFER_SIZE) { - raw_line[BUFFER_SIZE - 1] = '\0'; + if(eol == buffer_size) { + raw_line[buffer_size - 1] = '\0'; fprintf(stderr, "Selector: Line too long (max is %d characters):\n", - BUFFER_SIZE); + buffer_size); fprintf(stderr, raw_line); fprintf(stderr, "\n"); exit(1); @@ -379,56 +421,14 @@ Written by Francois Fleuret (francois@fleuret.org).\n", configuration_file, line_number, raw_line + start); } - s = next_word(token, raw_line + start, buffer_size); - - if(strcmp(token, "thermal_files") == 0) { - if(s == 0) { - fprintf(stderr, "Missing parameter in %s:%d\n", - configuration_file, line_number); - exit(1); - } - define_thermal_files(s); - } - - else if(strcmp(token, "debug") == 0) { - debug = 1; - } - - else if(strcmp(token, "fan_file") == 0) { - if(file_fan) { - fprintf(stderr, "Fan file already defined.\n"); - exit(1); - } - if(s == 0) { - fprintf(stderr, "Missing parameter in %s:%d\n", - configuration_file, line_number); - exit(1); - } - file_fan = new char[strlen(s) + 1]; - strcpy(file_fan, s); - } - - else if(strcmp(token, "temperature_thresholds") == 0) { - if(s == 0) { - fprintf(stderr, "Missing parameter in %s:%d\n", - configuration_file, line_number); - exit(1); - } - define_temperature_thresholds(s); - } - - else if(token[0] && token[0] != '#') { - fprintf(stderr, "Unknown keyword '%s' in %s:%d.\n", - token, configuration_file, line_number); - exit(1); - } + evaluate_one_configuration_line(raw_line + start, line_number); start = eol + 1; } } - ////////////////////////////////////////////////////////////////////// + /******************************************************************/ if(nb_temperature_thresholds == 0) { fprintf(stderr, "No temperature threshold was provided.\n"); @@ -445,7 +445,7 @@ Written by Francois Fleuret (francois@fleuret.org).\n", exit(1); } - for(int i = 0; i < nb_file_thermal; i++) { + for(i = 0; i < nb_file_thermal; i++) { file_thermal_fd[i] = open(file_thermal[i], O_RDONLY); if(file_thermal_fd[i] < 0) { fprintf(stderr, "Can not open %s for reading (%s).\n", @@ -462,22 +462,22 @@ Written by Francois Fleuret (francois@fleuret.org).\n", exit(1); } - ////////////////////////////////////////////////////////////////////// + /******************************************************************/ if(debug) { - for(int t = 0; t < nb_file_thermal; t++) { + for(t = 0; t < nb_file_thermal; t++) { printf("file_thermal[%d] %s\n", t, file_thermal[t]); } printf("file_fan %s\n", file_fan); - for(int t = 0; t < nb_temperature_thresholds; t++) { + for(t = 0; t < nb_temperature_thresholds; t++) { printf("temperature_thresholds[%d] %d", t, temperature_thresholds[t]); } } - ////////////////////////////////////////////////////////////////////// + /******************************************************************/ while(1) { @@ -487,7 +487,7 @@ Written by Francois Fleuret (francois@fleuret.org).\n", printf("Temperature:"); } - for(int i = 0; i < nb_file_thermal; i++) { + for(i = 0; i < nb_file_thermal; i++) { lseek(file_thermal_fd[i], 0, SEEK_SET); int size = read(file_thermal_fd[i], buffer, buffer_size); @@ -516,8 +516,8 @@ Written by Francois Fleuret (francois@fleuret.org).\n", printf(" %d", t); } - // So that we can deal with the new files where the - // temperature are in 1/1000th of C + /* 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; @@ -574,9 +574,9 @@ Written by Francois Fleuret (francois@fleuret.org).\n", } } - // We set it every time, even when there is no change, to handle - // when the level has been modified somewhere else (for instance - // when combing back from suspend). + /* We set it every time, even when there is no change, to handle + when the level has been modified somewhere else (for instance + when combing back from suspend). */ set_fan_level(file_fan_fd, new_level, nb_temperature_thresholds - 1);