From 3902eb10ac629c96e09cbe9f1867c3b29afb800b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Ostertag?= Date: Sun, 11 Apr 2021 19:16:10 +0200 Subject: [PATCH] Reformat all Source Files with Eclipse K&R Formatter --- src/daemon.c | 272 +++++---- src/daemon.h | 1 - src/global.h | 36 +- src/main.c | 70 ++- src/mbpfan.c | 1062 ++++++++++++++++----------------- src/mbpfan.h | 16 +- src/settings.c | 1510 +++++++++++++++++++++++------------------------ src/settings.h | 351 +++++------ src/strmap.c | 884 ++++++++++++++------------- src/strmap.h | 207 ++++--- src/util.c | 23 +- src/util.h | 3 +- tests/main.c | 7 +- tests/minunit.c | 420 +++++++------ tests/minunit.h | 19 +- 15 files changed, 2433 insertions(+), 2448 deletions(-) diff --git a/src/daemon.c b/src/daemon.c index 3f933e8..bcb0093 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -16,7 +16,6 @@ * */ - #include #include #include @@ -38,52 +37,47 @@ int daemonize = 1; int verbose = 0; -int write_pid(int pid) -{ - FILE *file = NULL; - file = fopen(PROGRAM_PID, "w"); +int write_pid(int pid) { + FILE *file = NULL; + file = fopen(PROGRAM_PID, "w"); - if(file != NULL) { - fprintf(file, "%d", pid); - fclose(file); - return 1; + if (file != NULL) { + fprintf(file, "%d", pid); + fclose(file); + return 1; - } else { - return 0; - } + } else { + return 0; + } } -int read_pid() -{ - FILE *file = NULL; - int pid = -1; - file = fopen(PROGRAM_PID, "r"); - - if(file != NULL) { - fscanf(file, "%d", &pid); - fclose(file); - if (kill(pid, 0) == -1 && errno == ESRCH) - { /* a process with such a pid does not exist, remove the pid file */ - if (remove(PROGRAM_PID) == 0) { - return -1; - } - } - return pid; - } - - return -1; +int read_pid() { + FILE *file = NULL; + int pid = -1; + file = fopen(PROGRAM_PID, "r"); + + if (file != NULL) { + fscanf(file, "%d", &pid); + fclose(file); + if (kill(pid, 0) == -1 && errno == ESRCH) { /* a process with such a pid does not exist, remove the pid file */ + if (remove(PROGRAM_PID) == 0) { + return -1; + } + } + return pid; + } + + return -1; } -int delete_pid() -{ - return remove(PROGRAM_PID); +int delete_pid() { + return remove(PROGRAM_PID); } -static void cleanup_and_exit(int exit_code) -{ +static void cleanup_and_exit(int exit_code) { delete_pid(); set_fans_auto(fans); - + struct s_fans *next_fan; while (fans != NULL) { next_fan = fans->next; @@ -111,128 +105,130 @@ static void cleanup_and_exit(int exit_code) exit(exit_code); } -void signal_handler(int signal) -{ - - switch(signal) { - case SIGHUP: - syslog(LOG_WARNING, "Received SIGHUP signal."); - retrieve_settings(NULL, fans); - break; - - case SIGTERM: - syslog(LOG_WARNING, "Received SIGTERM signal."); - cleanup_and_exit(EXIT_SUCCESS); - break; - - case SIGQUIT: - syslog(LOG_WARNING, "Received SIGQUIT signal."); - cleanup_and_exit(EXIT_SUCCESS); - break; - - case SIGINT: - syslog(LOG_WARNING, "Received SIGINT signal."); - cleanup_and_exit(EXIT_SUCCESS); - break; - - default: - syslog(LOG_WARNING, "Unhandled signal (%d) %s", signal, strsignal(signal)); - break; - } +void signal_handler(int signal) { + + switch (signal) { + case SIGHUP: + syslog(LOG_WARNING, "Received SIGHUP signal."); + retrieve_settings(NULL, fans); + break; + + case SIGTERM: + syslog(LOG_WARNING, "Received SIGTERM signal."); + cleanup_and_exit(EXIT_SUCCESS); + break; + + case SIGQUIT: + syslog(LOG_WARNING, "Received SIGQUIT signal."); + cleanup_and_exit(EXIT_SUCCESS); + break; + + case SIGINT: + syslog(LOG_WARNING, "Received SIGINT signal."); + cleanup_and_exit(EXIT_SUCCESS); + break; + + default: + syslog(LOG_WARNING, "Unhandled signal (%d) %s", signal, + strsignal(signal)); + break; + } } -void go_daemon(void (*fan_control)()) -{ - - // Setup signal handling before we start - signal(SIGHUP, signal_handler); - signal(SIGTERM, signal_handler); - signal(SIGQUIT, signal_handler); - signal(SIGINT, signal_handler); - - // Setup syslog logging - see SETLOGMASK(3) - if(verbose) { - setlogmask(LOG_UPTO(LOG_DEBUG)); - openlog(PROGRAM_NAME, LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER); - - } else { - setlogmask(LOG_UPTO(LOG_INFO)); - openlog(PROGRAM_NAME, LOG_CONS, LOG_USER); - } - - mbp_log(LOG_INFO, "%s %s starting up", PROGRAM_NAME, PROGRAM_VERSION); - - // configure timer slack - int err = prctl(PR_SET_TIMERSLACK, 1000 * 1000 * 1000, 0, 0, 0); - if (err == -1) { - perror("prctl"); - } +void go_daemon(void (*fan_control)()) { - pid_t pid_slave; - pid_t sid_slave; + // Setup signal handling before we start + signal(SIGHUP, signal_handler); + signal(SIGTERM, signal_handler); + signal(SIGQUIT, signal_handler); + signal(SIGINT, signal_handler); - if (daemonize) { + // Setup syslog logging - see SETLOGMASK(3) + if (verbose) { + setlogmask(LOG_UPTO(LOG_DEBUG)); + openlog(PROGRAM_NAME, LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, + LOG_USER); - pid_slave = fork(); + } else { + setlogmask(LOG_UPTO(LOG_INFO)); + openlog(PROGRAM_NAME, LOG_CONS, LOG_USER); + } - if (pid_slave < 0) { - exit(EXIT_FAILURE); - } + mbp_log(LOG_INFO, "%s %s starting up", PROGRAM_NAME, PROGRAM_VERSION); - if (pid_slave > 0) { - signal(SIGCHLD, SIG_IGN); - // kill the father - exit(EXIT_SUCCESS); - } + // configure timer slack + int err = prctl(PR_SET_TIMERSLACK, 1000 * 1000 * 1000, 0, 0, 0); + if (err == -1) { + perror("prctl"); + } - umask(0022); + pid_t pid_slave; + pid_t sid_slave; - // new sid_slave for the child process - sid_slave = setsid(); + if (daemonize) { - if (sid_slave < 0) { - exit(EXIT_FAILURE); - } + pid_slave = fork(); - if ((chdir("/")) < 0) { - exit(EXIT_FAILURE); - } + if (pid_slave < 0) { + exit(EXIT_FAILURE); + } + if (pid_slave > 0) { + signal(SIGCHLD, SIG_IGN); + // kill the father + exit(EXIT_SUCCESS); + } + umask(0022); - /* Close out the standard file descriptors */ - close(STDIN_FILENO); - close(STDOUT_FILENO); - close(STDERR_FILENO); - } + // new sid_slave for the child process + sid_slave = setsid(); + if (sid_slave < 0) { + exit(EXIT_FAILURE); + } - int current_pid = getpid(); + if ((chdir("/")) < 0) { + exit(EXIT_FAILURE); + } - if (read_pid() == -1) { - if (verbose) { - mbp_log(LOG_INFO, "Writing a new .pid file with value %d at: %s", current_pid, PROGRAM_PID); - } + /* Close out the standard file descriptors */ + close(STDIN_FILENO); + close(STDOUT_FILENO); + close(STDERR_FILENO); + } - if (write_pid(current_pid) == 0) { - mbp_log(LOG_ERR, "Can not create a .pid file at: %s. Aborting", PROGRAM_PID); - exit(EXIT_FAILURE); + int current_pid = getpid(); - } else { - if (verbose) { - mbp_log(LOG_INFO, "Successfully written a new .pid file with value %d at: %s", current_pid, PROGRAM_PID); - } - } + if (read_pid() == -1) { + if (verbose) { + mbp_log(LOG_INFO, "Writing a new .pid file with value %d at: %s", + current_pid, PROGRAM_PID); + } - } else { - mbp_log(LOG_ERR, "A previously created .pid file exists at: %s. Aborting", PROGRAM_PID); - exit(EXIT_FAILURE); - } + if (write_pid(current_pid) == 0) { + mbp_log(LOG_ERR, "Can not create a .pid file at: %s. Aborting", + PROGRAM_PID); + exit(EXIT_FAILURE); + + } else { + if (verbose) { + mbp_log(LOG_INFO, + "Successfully written a new .pid file with value %d at: %s", + current_pid, PROGRAM_PID); + } + } + } else { + mbp_log(LOG_ERR, + "A previously created .pid file exists at: %s. Aborting", + PROGRAM_PID); + exit(EXIT_FAILURE); + } - fan_control(); + fan_control(); - if(daemonize) { - syslog(LOG_INFO, "%s daemon exiting", PROGRAM_NAME); - } + if (daemonize) { + syslog(LOG_INFO, "%s daemon exiting", PROGRAM_NAME); + } } diff --git a/src/daemon.h b/src/daemon.h index c52c4cc..cb26450 100644 --- a/src/daemon.h +++ b/src/daemon.h @@ -50,5 +50,4 @@ void signal_handler(int signal); */ void go_daemon(void (*function)()); - #endif diff --git a/src/global.h b/src/global.h index 4c32551..6bb4cf5 100644 --- a/src/global.h +++ b/src/global.h @@ -9,31 +9,31 @@ extern int daemonize; extern int verbose; struct s_sensors { - FILE* file; - char* path; - unsigned int temperature; - struct s_sensors *next; + FILE *file; + char *path; + unsigned int temperature; + struct s_sensors *next; }; struct s_fans { - FILE* file; - char* path; // TODO: unused - char* label; - char* fan_output_path; - char* fan_manual_path; - int step_up; - int step_down; - int fan_id; - int old_speed; - int fan_max_speed; - int fan_min_speed; - struct s_fans *next; + FILE *file; + char *path; // TODO: unused + char *label; + char *fan_output_path; + char *fan_manual_path; + int step_up; + int step_down; + int fan_id; + int old_speed; + int fan_max_speed; + int fan_min_speed; + struct s_fans *next; }; typedef struct s_sensors t_sensors; typedef struct s_fans t_fans; -extern t_sensors* sensors; -extern t_fans* fans; +extern t_sensors *sensors; +extern t_fans *fans; #endif diff --git a/src/main.c b/src/main.c index 41bc86f..da5a450 100644 --- a/src/main.c +++ b/src/main.c @@ -29,49 +29,47 @@ #include "global.h" #include "util.h" -void print_usage(int argc, char *argv[]) -{ - if (argc >=1) { - printf("Usage: %s OPTION(S) \n", argv[0]); - printf("Options:\n"); - printf("\t-h Show this help screen\n"); - printf("\t-f Run in foreground\n"); - printf("\t-v Be (a lot) verbose\n"); - printf("\n"); - } +void print_usage(int argc, char *argv[]) { + if (argc >= 1) { + printf("Usage: %s OPTION(S) \n", argv[0]); + printf("Options:\n"); + printf("\t-h Show this help screen\n"); + printf("\t-f Run in foreground\n"); + printf("\t-v Be (a lot) verbose\n"); + printf("\n"); + } } -int main(int argc, char *argv[]) -{ +int main(int argc, char *argv[]) { - int c; + int c; - while( (c = getopt(argc, argv, "hfv|help")) != -1) { - switch(c) { - case 'h': - print_usage(argc, argv); - exit(EXIT_SUCCESS); - break; + while ((c = getopt(argc, argv, "hfv|help")) != -1) { + switch (c) { + case 'h': + print_usage(argc, argv); + exit(EXIT_SUCCESS); + break; - case 'f': - daemonize = 0; - break; + case 'f': + daemonize = 0; + break; - case 'v': - verbose = 1; - break; + case 'v': + verbose = 1; + break; - default: - print_usage(argc, argv); - exit(EXIT_SUCCESS); - break; - } - } + default: + print_usage(argc, argv); + exit(EXIT_SUCCESS); + break; + } + } - check_requirements(argv[0]); + check_requirements(argv[0]); - // pointer to mbpfan() function in mbpfan.c - void (*fan_control)() = mbpfan; - go_daemon(fan_control); - exit(EXIT_SUCCESS); + // pointer to mbpfan() function in mbpfan.c + void (*fan_control)() = mbpfan; + go_daemon(fan_control); + exit(EXIT_SUCCESS); } diff --git a/src/mbpfan.c b/src/mbpfan.c index a06880c..2285371 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -72,627 +72,633 @@ int max_temp = 86; // do not set it > 90 int polling_interval = 1; -t_sensors* sensors = NULL; -t_fans* fans = NULL; - -char *smprintf(const char *fmt, ...) -{ - char *buf; - int cnt; - va_list ap; - - // find buffer length - va_start(ap, fmt); - cnt = vsnprintf(NULL, 0, fmt, ap); - va_end(ap); - if (cnt < 0) { - return NULL; - } - - // create and write to buffer - buf = malloc(cnt + 1); - va_start(ap, fmt); - vsnprintf(buf, cnt + 1, fmt, ap); - va_end(ap); - return buf; +t_sensors *sensors = NULL; +t_fans *fans = NULL; + +char* smprintf(const char *fmt, ...) { + char *buf; + int cnt; + va_list ap; + + // find buffer length + va_start(ap, fmt); + cnt = vsnprintf(NULL, 0, fmt, ap); + va_end(ap); + if (cnt < 0) { + return NULL; + } + + // create and write to buffer + buf = malloc(cnt + 1); + va_start(ap, fmt); + vsnprintf(buf, cnt + 1, fmt, ap); + va_end(ap); + return buf; } -bool is_modern_sensors_path() -{ - struct utsname kernel; - uname(&kernel); - - char *str_kernel_version; - str_kernel_version = strtok(kernel.release, "."); - - if (atoi(str_kernel_version) < 3){ - mbp_log(LOG_ERR, "mbpfan detected a pre-3.x.x linux kernel. Detected version: %s. Exiting.\n", kernel.release); - exit(EXIT_FAILURE); - } - - int counter; - - for (counter = 0; counter < NUM_HWMONS; counter++) { - int temp; - for (temp = 1; temp < NUM_TEMP_INPUTS; ++temp) { - char *path = smprintf("/sys/devices/platform/coretemp.0/hwmon/hwmon%d/temp%d_input", counter, temp); - int res = access(path, R_OK); - free(path); - if (res == 0) { - return 1; - } - } - } - - return 0; +bool is_modern_sensors_path() { + struct utsname kernel; + uname(&kernel); + + char *str_kernel_version; + str_kernel_version = strtok(kernel.release, "."); + + if (atoi(str_kernel_version) < 3) { + mbp_log(LOG_ERR, + "mbpfan detected a pre-3.x.x linux kernel. Detected version: %s. Exiting.\n", + kernel.release); + exit(EXIT_FAILURE); + } + + int counter; + + for (counter = 0; counter < NUM_HWMONS; counter++) { + int temp; + for (temp = 1; temp < NUM_TEMP_INPUTS; ++temp) { + char *path = + smprintf( + "/sys/devices/platform/coretemp.0/hwmon/hwmon%d/temp%d_input", + counter, temp); + int res = access(path, R_OK); + free(path); + if (res == 0) { + return 1; + } + } + } + + return 0; } +t_sensors* retrieve_sensors() { + t_sensors *sensors_head = NULL; + t_sensors *s = NULL; -t_sensors *retrieve_sensors() -{ - t_sensors *sensors_head = NULL; - t_sensors *s = NULL; + char *path = NULL; + char *path_begin = NULL; - char *path = NULL; - char *path_begin = NULL; + const char *path_end = "_input"; + int sensors_found = 0; - const char *path_end = "_input"; - int sensors_found = 0; + if (!is_modern_sensors_path()) { + if (verbose) { + mbp_log(LOG_INFO, "Using legacy path for kernel < 3.15.0"); + } - if (!is_modern_sensors_path()) { - if(verbose) { - mbp_log(LOG_INFO, "Using legacy path for kernel < 3.15.0"); - } + path_begin = strdup("/sys/devices/platform/coretemp.0/temp"); - path_begin = strdup("/sys/devices/platform/coretemp.0/temp"); + } else { - } else { + if (verbose) { + mbp_log(LOG_INFO, + "Using new sensor path for kernel >= 3.15.0 or some CentOS versions with kernel 3.10.0 "); + } - if(verbose) { - mbp_log(LOG_INFO, "Using new sensor path for kernel >= 3.15.0 or some CentOS versions with kernel 3.10.0 "); - } + // loop over up to 6 processors + int processor; + for (processor = 0; processor < NUM_PROCESSORS; processor++) { - // loop over up to 6 processors - int processor; - for (processor = 0; processor < NUM_PROCESSORS; processor++) { + if (path_begin != NULL) { + free(path_begin); + } + path_begin = smprintf( + "/sys/devices/platform/coretemp.%d/hwmon/hwmon", processor); - if (path_begin != NULL) { - free(path_begin); - } - path_begin = smprintf("/sys/devices/platform/coretemp.%d/hwmon/hwmon", processor); + int counter; + for (counter = 0; counter < NUM_HWMONS; counter++) { - int counter; - for (counter = 0; counter < NUM_HWMONS; counter++) { + char *hwmon_path = smprintf("%s%d", path_begin, counter); - char *hwmon_path = smprintf("%s%d", path_begin, counter); + int res = access(hwmon_path, R_OK); + if (res == 0) { - int res = access(hwmon_path, R_OK); - if (res == 0) { + free(path_begin); + path_begin = smprintf("%s/temp", hwmon_path); - free(path_begin); - path_begin = smprintf("%s/temp", hwmon_path); + if (verbose) { + mbp_log(LOG_INFO, "Found hwmon path at %s", path_begin); + } - if(verbose) { - mbp_log(LOG_INFO, "Found hwmon path at %s", path_begin); - } + free(hwmon_path); + break; + } - free(hwmon_path); - break; - } + free(hwmon_path); + } - free(hwmon_path); - } + int core = 0; + for (core = 0; core < NUM_TEMP_INPUTS; core++) { + path = smprintf("%s%d%s", path_begin, core, path_end); - int core = 0; - for(core = 0; corepath = strdup(path); + fscanf(file, "%d", &s->temperature); - if(file != NULL) { - s = (t_sensors *) malloc( sizeof( t_sensors ) ); - s->path = strdup(path); - fscanf(file, "%d", &s->temperature); + if (sensors_head == NULL) { + sensors_head = s; + sensors_head->next = NULL; - if (sensors_head == NULL) { - sensors_head = s; - sensors_head->next = NULL; + } else { + t_sensors *tmp = sensors_head; - } else { - t_sensors *tmp = sensors_head; + while (tmp->next != NULL) { + tmp = tmp->next; + } - while (tmp->next != NULL) { - tmp = tmp->next; - } + tmp->next = s; + tmp->next->next = NULL; + } - tmp->next = s; - tmp->next->next = NULL; - } + s->file = file; + sensors_found++; + } - s->file = file; - sensors_found++; + free(path); + path = NULL; + } } - - free(path); - path = NULL; - } } - } - if(verbose) { - mbp_log(LOG_INFO, "Found %d sensors", sensors_found); - } + if (verbose) { + mbp_log(LOG_INFO, "Found %d sensors", sensors_found); + } - if (sensors_found == 0){ - mbp_log(LOG_CRIT, "mbpfan could not detect any temp sensor. Please contact the developer."); - exit(EXIT_FAILURE); - } + if (sensors_found == 0) { + mbp_log(LOG_CRIT, + "mbpfan could not detect any temp sensor. Please contact the developer."); + exit(EXIT_FAILURE); + } - free(path_begin); - path_begin = NULL; + free(path_begin); + path_begin = NULL; - return sensors_head; + return sensors_head; } -static int read_value(const char *path) -{ - int value = -1; - FILE *file = fopen(path, "r"); - if (file != NULL) { - fscanf(file, "%d", &value); - fclose(file); - } - return value; +static int read_value(const char *path) { + int value = -1; + FILE *file = fopen(path, "r"); + if (file != NULL) { + fscanf(file, "%d", &value); + fclose(file); + } + return value; } -static void read_value_str(const char *path, char *str, size_t len) -{ - FILE *file = fopen(path, "r"); - if (file != NULL) { - fgets(str, len, file); - fclose(file); - } +static void read_value_str(const char *path, char *str, size_t len) { + FILE *file = fopen(path, "r"); + if (file != NULL) { + fgets(str, len, file); + fclose(file); + } } -static void trim_trailing_whitespace(char *str) -{ - for (ssize_t i = strlen(str) - 1; i >= 0; --i) { - if (isspace(str[i]) || str[i] == '\n') { - str[i] = '\0'; - } - } +static void trim_trailing_whitespace(char *str) { + for (ssize_t i = strlen(str) - 1; i >= 0; --i) { + if (isspace(str[i]) || str[i] == '\n') { + str[i] = '\0'; + } + } } -t_fans *retrieve_fans() -{ - t_fans *fans_head = NULL; - t_fans *fan = NULL; - - char *path_output = NULL; - char *path_label = NULL; - char *path_manual = NULL; - char *path_fan_max = NULL; - char *path_fan_min = NULL; - - const char *path_begin = "/sys/devices/platform/applesmc.768/fan"; - const char *path_output_end = "_output"; - const char *path_label_end = "_label"; - const char *path_man_end = "_manual"; - const char *path_max_speed = "_max"; - const char *path_min_speed = "_min"; - - int counter = 0; - int fans_found = 0; - - for(counter = 0; counterfan_output_path = strdup(path_output); - fan->fan_manual_path = strdup(path_manual); - fan->fan_id = counter; - - int fan_speed = read_value(path_fan_min); - if(fan_speed == -1 || fan_speed < MIN_FAN_SPEED_DEFAULT) - fan->fan_min_speed = MIN_FAN_SPEED_DEFAULT; - else - fan->fan_min_speed = fan_speed; - - fan_speed = read_value(path_fan_max); - if(fan_speed == -1 || fan_speed > MAX_FAN_SPEED_DEFAULT) - fan->fan_max_speed = MAX_FAN_SPEED_DEFAULT; - else - fan->fan_max_speed = fan_speed; - - size_t max_label_len = 64; - fan->label = malloc(max_label_len); - read_value_str(path_label, fan->label, max_label_len); - trim_trailing_whitespace(fan->label); - - fan->old_speed = 0; - - if (fans_head == NULL) { - fans_head = fan; - fans_head->next = NULL; - - } else { - t_fans *tmp = fans_head; - - while (tmp->next != NULL) { - tmp = tmp->next; - } - - tmp->next = fan; - tmp->next->next = NULL; - } - - fan->file = file; - fans_found++; - } - free(path_fan_min); - path_fan_min = NULL; - free(path_label); - path_label = NULL; - free(path_fan_max); - path_fan_max = NULL; - free(path_output); - path_output = NULL; - free(path_manual); - path_manual = NULL; - } - - if(verbose) { - mbp_log(LOG_INFO, "Found %d fans", fans_found); - } - - if (fans_found == 0){ - mbp_log(LOG_CRIT, "mbpfan could not detect any fan. Please contact the developer."); - exit(EXIT_FAILURE); - } - - return fans_head; +t_fans* retrieve_fans() { + t_fans *fans_head = NULL; + t_fans *fan = NULL; + + char *path_output = NULL; + char *path_label = NULL; + char *path_manual = NULL; + char *path_fan_max = NULL; + char *path_fan_min = NULL; + + const char *path_begin = "/sys/devices/platform/applesmc.768/fan"; + const char *path_output_end = "_output"; + const char *path_label_end = "_label"; + const char *path_man_end = "_manual"; + const char *path_max_speed = "_max"; + const char *path_min_speed = "_min"; + + int counter = 0; + int fans_found = 0; + + for (counter = 0; counter < NUM_FANS; counter++) { + + path_output = smprintf("%s%d%s", path_begin, counter, path_output_end); + path_label = smprintf("%s%d%s", path_begin, counter, path_label_end); + path_manual = smprintf("%s%d%s", path_begin, counter, path_man_end); + path_fan_min = smprintf("%s%d%s", path_begin, counter, path_min_speed); + path_fan_max = smprintf("%s%d%s", path_begin, counter, path_max_speed); + + FILE *file = fopen(path_output, "w"); + + if (file != NULL) { + fan = (t_fans*) malloc(sizeof(t_fans)); + fan->fan_output_path = strdup(path_output); + fan->fan_manual_path = strdup(path_manual); + fan->fan_id = counter; + + int fan_speed = read_value(path_fan_min); + if (fan_speed == -1 || fan_speed < MIN_FAN_SPEED_DEFAULT) + fan->fan_min_speed = MIN_FAN_SPEED_DEFAULT; + else + fan->fan_min_speed = fan_speed; + + fan_speed = read_value(path_fan_max); + if (fan_speed == -1 || fan_speed > MAX_FAN_SPEED_DEFAULT) + fan->fan_max_speed = MAX_FAN_SPEED_DEFAULT; + else + fan->fan_max_speed = fan_speed; + + size_t max_label_len = 64; + fan->label = malloc(max_label_len); + read_value_str(path_label, fan->label, max_label_len); + trim_trailing_whitespace(fan->label); + + fan->old_speed = 0; + + if (fans_head == NULL) { + fans_head = fan; + fans_head->next = NULL; + + } else { + t_fans *tmp = fans_head; + + while (tmp->next != NULL) { + tmp = tmp->next; + } + + tmp->next = fan; + tmp->next->next = NULL; + } + + fan->file = file; + fans_found++; + } + free(path_fan_min); + path_fan_min = NULL; + free(path_label); + path_label = NULL; + free(path_fan_max); + path_fan_max = NULL; + free(path_output); + path_output = NULL; + free(path_manual); + path_manual = NULL; + } + + if (verbose) { + mbp_log(LOG_INFO, "Found %d fans", fans_found); + } + + if (fans_found == 0) { + mbp_log(LOG_CRIT, + "mbpfan could not detect any fan. Please contact the developer."); + exit(EXIT_FAILURE); + } + + return fans_head; } -static void set_fans_mode(t_fans *fans, int mode) -{ - t_fans *tmp = fans; - FILE *file; +static void set_fans_mode(t_fans *fans, int mode) { + t_fans *tmp = fans; + FILE *file; - while(tmp != NULL) { - file = fopen(tmp->fan_manual_path, "rw+"); + while (tmp != NULL) { + file = fopen(tmp->fan_manual_path, "rw+"); - if(file != NULL) { - fprintf(file, "%d", mode); - fclose(file); - } + if (file != NULL) { + fprintf(file, "%d", mode); + fclose(file); + } - tmp = tmp->next; - } + tmp = tmp->next; + } } -void set_fans_man(t_fans *fans) -{ +void set_fans_man(t_fans *fans) { - set_fans_mode(fans, 1); + set_fans_mode(fans, 1); } -void set_fans_auto(t_fans *fans) -{ +void set_fans_auto(t_fans *fans) { - set_fans_mode(fans, 0); + set_fans_mode(fans, 0); } -t_sensors *refresh_sensors(t_sensors *sensors) -{ - t_sensors *tmp = sensors; +t_sensors* refresh_sensors(t_sensors *sensors) { + t_sensors *tmp = sensors; - while(tmp != NULL) { - if(tmp->file != NULL) { - char buf[16]; - int len = pread(fileno(tmp->file), buf, sizeof(buf), /*offset=*/ 0); - buf[len] = '\0'; - tmp->temperature = strtod(buf, NULL); - } + while (tmp != NULL) { + if (tmp->file != NULL) { + char buf[16]; + int len = pread(fileno(tmp->file), buf, sizeof(buf), /*offset=*/0); + buf[len] = '\0'; + tmp->temperature = strtod(buf, NULL); + } - tmp = tmp->next; - } + tmp = tmp->next; + } - return sensors; + return sensors; } /* Controls the speed of a fan */ -void set_fan_speed(t_fans* fan, int speed) -{ - if(fan != NULL && fan->file != NULL && fan->old_speed != speed) { - char buf[16]; - int len = snprintf(buf, sizeof(buf), "%d", speed); - int res = pwrite(fileno(fan->file), buf, len, /*offset=*/ 0); - if (res == -1) { - perror("Could not set fan speed"); - } - fan->old_speed = speed; - } +void set_fan_speed(t_fans *fan, int speed) { + if (fan != NULL && fan->file != NULL && fan->old_speed != speed) { + char buf[16]; + int len = snprintf(buf, sizeof(buf), "%d", speed); + int res = pwrite(fileno(fan->file), buf, len, /*offset=*/0); + if (res == -1) { + perror("Could not set fan speed"); + } + fan->old_speed = speed; + } } -void set_fan_minimum_speed(t_fans* fans) -{ - t_fans *tmp = fans; +void set_fan_minimum_speed(t_fans *fans) { + t_fans *tmp = fans; - while(tmp != NULL) { - set_fan_speed(tmp,tmp->fan_min_speed); - tmp = tmp->next; - } + while (tmp != NULL) { + set_fan_speed(tmp, tmp->fan_min_speed); + tmp = tmp->next; + } } -unsigned short get_temp(t_sensors* sensors) -{ - sensors = refresh_sensors(sensors); - unsigned int temp = 0; +unsigned short get_temp(t_sensors *sensors) { + sensors = refresh_sensors(sensors); + unsigned int temp = 0; - t_sensors* tmp = sensors; + t_sensors *tmp = sensors; - while(tmp != NULL) { - temp = max(temp, tmp->temperature); - tmp = tmp->next; - } + while (tmp != NULL) { + temp = max(temp, tmp->temperature); + tmp = tmp->next; + } - return temp / 1000; + return temp / 1000; } -void retrieve_settings(const char* settings_path, t_fans* fans) -{ - Settings *settings = NULL; - int result = 0; - FILE *f = NULL; - - if (settings_path == NULL) { - f = fopen("/etc/mbpfan.conf", "r"); - - } else { - f = fopen(settings_path, "r"); - } - - - if (f == NULL) { - /* Could not open configfile */ - if(verbose) { - mbp_log(LOG_INFO, "Couldn't open configfile, using defaults"); - } - - } else { - settings = settings_open(f); - fclose(f); - - if (settings == NULL) { - /* Could not read configfile */ - if(verbose) { - mbp_log(LOG_WARNING, "Couldn't read configfile"); - } - - } else { - - t_fans *fan = fans; - - while(fan != NULL) { - - char* config_key; - config_key = smprintf("min_fan%d_speed", fan->fan_id); - /* Read configfile values */ - result = settings_get_int(settings, "general", config_key); - if (result != 0) { - fan->fan_min_speed = result; - } - free(config_key); - - config_key = smprintf("max_fan%d_speed", fan->fan_id); - result = settings_get_int(settings, "general", config_key); - - if (result != 0) { - fan->fan_max_speed = result; - } - free(config_key); - fan = fan->next; - } - result = settings_get_int(settings, "general", "low_temp"); +void retrieve_settings(const char *settings_path, t_fans *fans) { + Settings *settings = NULL; + int result = 0; + FILE *f = NULL; - if (result != 0) { - low_temp = result; - } + if (settings_path == NULL) { + f = fopen("/etc/mbpfan.conf", "r"); - result = settings_get_int(settings, "general", "high_temp"); + } else { + f = fopen(settings_path, "r"); + } - if (result != 0) { - high_temp = result; - } + if (f == NULL) { + /* Could not open configfile */ + if (verbose) { + mbp_log(LOG_INFO, "Couldn't open configfile, using defaults"); + } - result = settings_get_int(settings, "general", "max_temp"); + } else { + settings = settings_open(f); + fclose(f); - if (result != 0) { - max_temp = result; - } + if (settings == NULL) { + /* Could not read configfile */ + if (verbose) { + mbp_log(LOG_WARNING, "Couldn't read configfile"); + } - result = settings_get_int(settings, "general", "polling_interval"); + } else { - if (result != 0) { - polling_interval = result; - } + t_fans *fan = fans; - /* Destroy the settings object */ - settings_delete(settings); - } - } -} + while (fan != NULL) { + + char *config_key; + config_key = smprintf("min_fan%d_speed", fan->fan_id); + /* Read configfile values */ + result = settings_get_int(settings, "general", config_key); + if (result != 0) { + fan->fan_min_speed = result; + } + free(config_key); + + config_key = smprintf("max_fan%d_speed", fan->fan_id); + result = settings_get_int(settings, "general", config_key); + + if (result != 0) { + fan->fan_max_speed = result; + } + free(config_key); + fan = fan->next; + } + result = settings_get_int(settings, "general", "low_temp"); + + if (result != 0) { + low_temp = result; + } + + result = settings_get_int(settings, "general", "high_temp"); + + if (result != 0) { + high_temp = result; + } + + result = settings_get_int(settings, "general", "max_temp"); + + if (result != 0) { + max_temp = result; + } -void check_requirements(const char* program_path) -{ + result = settings_get_int(settings, "general", "polling_interval"); - /** - * Check for root - */ + if (result != 0) { + polling_interval = result; + } + + /* Destroy the settings object */ + settings_delete(settings); + } + } +} - uid_t uid=getuid(), euid=geteuid(); +void check_requirements(const char *program_path) { - if (uid != 0 || euid != 0) { - mbp_log(LOG_ERR, "%s needs root privileges. Please run %s as root. Exiting.", program_path, program_path); - exit(EXIT_FAILURE); - } + /** + * Check for root + */ - /** - * Check for coretemp and applesmc modules - */ - DIR* dir = opendir(CORETEMP_PATH); + uid_t uid = getuid(), euid = geteuid(); - if (ENOENT == errno) { - mbp_log(LOG_ERR, "%s needs coretemp support. Please either load it or build it into the kernel. Exiting.", program_path); - exit(EXIT_FAILURE); - } + if (uid != 0 || euid != 0) { + mbp_log(LOG_ERR, + "%s needs root privileges. Please run %s as root. Exiting.", + program_path, program_path); + exit(EXIT_FAILURE); + } - closedir(dir); + /** + * Check for coretemp and applesmc modules + */ + DIR *dir = opendir(CORETEMP_PATH); + if (ENOENT == errno) { + mbp_log(LOG_ERR, + "%s needs coretemp support. Please either load it or build it into the kernel. Exiting.", + program_path); + exit(EXIT_FAILURE); + } - dir = opendir(APPLESMC_PATH); + closedir(dir); - if (ENOENT == errno) { - mbp_log(LOG_ERR, "%s needs applesmc support. Please either load it or build it into the kernel. Exiting.", program_path); - exit(EXIT_FAILURE); - } + dir = opendir(APPLESMC_PATH); - closedir(dir); + if (ENOENT == errno) { + mbp_log(LOG_ERR, + "%s needs applesmc support. Please either load it or build it into the kernel. Exiting.", + program_path); + exit(EXIT_FAILURE); + } + closedir(dir); } -int get_max_mhz(void) -{ - int max_mhz = -1; - DIR *dir = opendir("/sys/devices/system/cpu"); - if (dir == NULL) { - return -1; - } - struct dirent *ent; - while ((ent = readdir(dir)) != NULL) { - if (strncmp(ent->d_name, "cpu", 3) != 0 || - strcmp(ent->d_name, "cpufreq") == 0 || - strcmp(ent->d_name, "cpuidle") == 0) { - continue; - } - char *path = smprintf("/sys/devices/system/cpu/%s/cpufreq/scaling_cur_freq", ent->d_name); - max_mhz = max(max_mhz, read_value(path) / 1000); - free(path); - } - closedir(dir); - return max_mhz; +int get_max_mhz(void) { + int max_mhz = -1; + DIR *dir = opendir("/sys/devices/system/cpu"); + if (dir == NULL) { + return -1; + } + struct dirent *ent; + while ((ent = readdir(dir)) != NULL) { + if (strncmp(ent->d_name, "cpu", 3) != 0 + || strcmp(ent->d_name, "cpufreq") == 0 + || strcmp(ent->d_name, "cpuidle") == 0) { + continue; + } + char *path = smprintf( + "/sys/devices/system/cpu/%s/cpufreq/scaling_cur_freq", + ent->d_name); + max_mhz = max(max_mhz, read_value(path) / 1000); + free(path); + } + closedir(dir); + return max_mhz; } -void mbpfan() -{ - int old_temp, new_temp, fan_speed, steps; - int temp_change; - - sensors = retrieve_sensors(); - fans = retrieve_fans(); - - retrieve_settings(NULL, fans); - - t_fans* fan = fans; - while(fan != NULL) { - - if (fan->fan_min_speed > fan->fan_max_speed) { - mbp_log(LOG_ERR, "Invalid fan speeds: %d %d", fan->fan_min_speed, fan->fan_max_speed); - exit(EXIT_FAILURE); - } - fan = fan->next; - } - - if (low_temp > high_temp || high_temp > max_temp) { - mbp_log(LOG_ERR, "Invalid temperatures: %d %d %d", low_temp, high_temp, max_temp); - exit(EXIT_FAILURE); - } - - set_fans_man(fans); - - new_temp = get_temp(sensors); - set_fan_minimum_speed(fans); - - fan = fans; - while(fan != NULL) { - - fan->step_up = (float)( fan->fan_max_speed - fan->fan_min_speed ) / - (float)( ( max_temp - high_temp ) * ( max_temp - high_temp + 1 ) / 2.0 ); - - fan->step_down = (float)( fan->fan_max_speed - fan->fan_min_speed ) / - (float)( ( max_temp - low_temp ) * ( max_temp - low_temp + 1 ) / 2.0 ); - fan = fan->next; - } - -recalibrate: - if(verbose) { - mbp_log(LOG_INFO, "Sleeping for 2 seconds to get first temp delta"); - } - sleep(2); - - while(1) { - old_temp = new_temp; - new_temp = get_temp(sensors); - - fan = fans; - - while(fan != NULL) { - fan_speed = fan->old_speed; - - if(new_temp >= max_temp && fan->old_speed != fan->fan_max_speed) { - fan_speed = fan->fan_max_speed; - } - - if(new_temp <= low_temp && fan_speed != fan->fan_min_speed) { - fan_speed = fan->fan_min_speed; - } - - temp_change = new_temp - old_temp; - - if(temp_change > 0 && new_temp > high_temp && new_temp < max_temp) { - steps = ( new_temp - high_temp ) * ( new_temp - high_temp + 1 ) / 2; - fan_speed = max( fan_speed, ceil(fan->fan_min_speed + steps * fan->step_up) ); - } - - if(temp_change < 0 && new_temp > low_temp && new_temp < max_temp) { - steps = ( max_temp - new_temp ) * ( max_temp - new_temp + 1 ) / 2; - fan_speed = min( fan_speed, floor(fan->fan_max_speed - steps * fan->step_down) ); - } - - if(verbose) { - mbp_log(LOG_INFO, "Old Temp: %d New Temp: %d Fan: %s Speed: %d Max MHz: %d", old_temp, new_temp, fan->label, fan_speed, get_max_mhz()); - } - - set_fan_speed(fan, fan_speed); - fan = fan->next; - } - - if(verbose) { - mbp_log(LOG_INFO, "Sleeping for %d seconds", polling_interval); - } - - time_t before_sleep = time(NULL); - - // call nanosleep instead of sleep to avoid rt_sigprocmask and - // rt_sigaction - struct timespec ts; - ts.tv_sec = polling_interval; - ts.tv_nsec = 0; - nanosleep(&ts, NULL); - - time_t after_sleep = time(NULL); - if(after_sleep - before_sleep > 2 * polling_interval) { - mbp_log(LOG_INFO, "Clock skew detected - slept for %ld seconds but expected %d", after_sleep - before_sleep, polling_interval); - set_fans_man(fans); - goto recalibrate; - } - } +void mbpfan() { + int old_temp, new_temp, fan_speed, steps; + int temp_change; + + sensors = retrieve_sensors(); + fans = retrieve_fans(); + + retrieve_settings(NULL, fans); + + t_fans *fan = fans; + while (fan != NULL) { + + if (fan->fan_min_speed > fan->fan_max_speed) { + mbp_log(LOG_ERR, "Invalid fan speeds: %d %d", fan->fan_min_speed, + fan->fan_max_speed); + exit(EXIT_FAILURE); + } + fan = fan->next; + } + + if (low_temp > high_temp || high_temp > max_temp) { + mbp_log(LOG_ERR, "Invalid temperatures: %d %d %d", low_temp, high_temp, + max_temp); + exit(EXIT_FAILURE); + } + + set_fans_man(fans); + + new_temp = get_temp(sensors); + set_fan_minimum_speed(fans); + + fan = fans; + while (fan != NULL) { + + fan->step_up = (float) (fan->fan_max_speed - fan->fan_min_speed) + / (float) ((max_temp - high_temp) * (max_temp - high_temp + 1) + / 2.0); + + fan->step_down = (float) (fan->fan_max_speed - fan->fan_min_speed) + / (float) ((max_temp - low_temp) * (max_temp - low_temp + 1) + / 2.0); + fan = fan->next; + } + + recalibrate: if (verbose) { + mbp_log(LOG_INFO, "Sleeping for 2 seconds to get first temp delta"); + } + sleep(2); + + while (1) { + old_temp = new_temp; + new_temp = get_temp(sensors); + + fan = fans; + + while (fan != NULL) { + fan_speed = fan->old_speed; + + if (new_temp >= max_temp && fan->old_speed != fan->fan_max_speed) { + fan_speed = fan->fan_max_speed; + } + + if (new_temp <= low_temp && fan_speed != fan->fan_min_speed) { + fan_speed = fan->fan_min_speed; + } + + temp_change = new_temp - old_temp; + + if (temp_change > 0 && new_temp > high_temp + && new_temp < max_temp) { + steps = (new_temp - high_temp) * (new_temp - high_temp + 1) / 2; + fan_speed = max(fan_speed, + ceil(fan->fan_min_speed + steps * fan->step_up)); + } + + if (temp_change < 0 && new_temp > low_temp && new_temp < max_temp) { + steps = (max_temp - new_temp) * (max_temp - new_temp + 1) / 2; + fan_speed = min(fan_speed, + floor(fan->fan_max_speed - steps * fan->step_down)); + } + + if (verbose) { + mbp_log(LOG_INFO, + "Old Temp: %d New Temp: %d Fan: %s Speed: %d Max MHz: %d", + old_temp, new_temp, fan->label, fan_speed, + get_max_mhz()); + } + + set_fan_speed(fan, fan_speed); + fan = fan->next; + } + + if (verbose) { + mbp_log(LOG_INFO, "Sleeping for %d seconds", polling_interval); + } + + time_t before_sleep = time(NULL); + + // call nanosleep instead of sleep to avoid rt_sigprocmask and + // rt_sigaction + struct timespec ts; + ts.tv_sec = polling_interval; + ts.tv_nsec = 0; + nanosleep(&ts, NULL); + + time_t after_sleep = time(NULL); + if (after_sleep - before_sleep > 2 * polling_interval) { + mbp_log(LOG_INFO, + "Clock skew detected - slept for %ld seconds but expected %d", + after_sleep - before_sleep, polling_interval); + set_fans_man(fans); + goto recalibrate; + } + } } diff --git a/src/mbpfan.h b/src/mbpfan.h index f673a58..15a483d 100644 --- a/src/mbpfan.h +++ b/src/mbpfan.h @@ -40,7 +40,7 @@ typedef struct s_sensors t_sensors; struct s_fans; typedef struct s_fans t_fans; -char *smprintf(const char *fmt, ...) __attribute__((format (printf, 1, 2))); +char* smprintf(const char *fmt, ...) __attribute__((format (printf, 1, 2))); /** * Return true if the kernel is < 3.15.0 @@ -52,20 +52,20 @@ bool is_legacy_sensors_path(); * /etc/mbpfan.conf * If it fails, the default hardcoded settings are used */ -void retrieve_settings(const char* settings_path, t_fans *fans); +void retrieve_settings(const char *settings_path, t_fans *fans); /** * Detect the sensors in /sys/devices/platform/coretemp.0/temp * and /sys/devices/platform/coretemp.1/temp etc * Return a linked list of t_sensors (first temperature detected) */ -t_sensors *retrieve_sensors(); +t_sensors* retrieve_sensors(); /** * Given a linked list of t_sensors, refresh their detected * temperature */ -t_sensors *refresh_sensors(t_sensors *sensors); +t_sensors* refresh_sensors(t_sensors *sensors); /** * Detect the fans in /sys/devices/platform/applesmc.768/ @@ -89,22 +89,22 @@ void set_fans_auto(t_fans *fans); * Given a sensors with associated fans * Change their speed */ -void set_fan_speed(t_fans* fan, int speed); +void set_fan_speed(t_fans *fan, int speed); /** * Given a list of fans set their minumum fan speed */ -void set_fan_minimum_speed(t_fans* fans); +void set_fan_minimum_speed(t_fans *fans); /** * Return maximum CPU temp in degrees */ -unsigned short get_temp(t_sensors* sensors); +unsigned short get_temp(t_sensors *sensors); /** * Check if user has proper access and that required * kernel modules are available */ -void check_requirements(const char* program_path); +void check_requirements(const char *program_path); /** * Main Program diff --git a/src/settings.c b/src/settings.c index 20d18f3..4f3a2ce 100644 --- a/src/settings.c +++ b/src/settings.c @@ -52,25 +52,23 @@ typedef struct Section Section; typedef struct ParseState ParseState; struct Settings { - Section *sections; - unsigned int section_count; + Section *sections; + unsigned int section_count; }; struct Section { - char *name; - StrMap *map; + char *name; + StrMap *map; }; struct ParseState { - char *current_section; - unsigned int current_section_n; - int has_section; + char *current_section; + unsigned int current_section_n; + int has_section; }; enum ConvertMode { - CONVERT_MODE_INT, - CONVERT_MODE_LONG, - CONVERT_MODE_DOUBLE, + CONVERT_MODE_INT, CONVERT_MODE_LONG, CONVERT_MODE_DOUBLE, }; typedef enum ConvertMode ConvertMode; @@ -83,300 +81,304 @@ static int is_comment_str(const char *str); static int is_section_str(const char *str); static int is_key_value_str(const char *str); static int is_key_without_value_str(const char *str); -static const char * get_token(char *str, char delim, char **last); -static int get_section_from_str(const char *str, char *out_buf, unsigned int out_buf_n); -static int get_key_value_from_str(const char *str, char *out_buf1, unsigned int out_buf1_n, char *out_buf2, unsigned int out_buf2_n); -static int get_key_without_value_from_str(const char *str, char *out_buf, unsigned int out_buf_n); -static int get_converted_value(const Settings *settings, const char *section, const char *key, ConvertMode mode, void *out); -static int get_converted_tuple(const Settings *settings, const char *section, const char *key, char delim, ConvertMode mode, void *out, unsigned int n_out); -static Section * get_section(Section *sections, unsigned int n, const char *name); +static const char* get_token(char *str, char delim, char **last); +static int get_section_from_str(const char *str, char *out_buf, + unsigned int out_buf_n); +static int get_key_value_from_str(const char *str, char *out_buf1, + unsigned int out_buf1_n, char *out_buf2, unsigned int out_buf2_n); +static int get_key_without_value_from_str(const char *str, char *out_buf, + unsigned int out_buf_n); +static int get_converted_value(const Settings *settings, const char *section, + const char *key, ConvertMode mode, void *out); +static int get_converted_tuple(const Settings *settings, const char *section, + const char *key, char delim, ConvertMode mode, void *out, + unsigned int n_out); +static Section* get_section(Section *sections, unsigned int n, const char *name); static void enum_map(const char *key, const char *value, const void *obj); -Settings * settings_new() -{ - Settings *settings; +Settings* settings_new() { + Settings *settings; - settings = (Settings*)malloc(sizeof(Settings)); + settings = (Settings*) malloc(sizeof(Settings)); - if (settings == NULL) { - return NULL; - } + if (settings == NULL) { + return NULL; + } - settings->section_count = 0; - settings->sections = NULL; - return settings; + settings->section_count = 0; + settings->sections = NULL; + return settings; } -void settings_delete(Settings *settings) -{ - unsigned int i, n; - Section *section; +void settings_delete(Settings *settings) { + unsigned int i, n; + Section *section; - if (settings == NULL) { - return; - } + if (settings == NULL) { + return; + } - section = settings->sections; - n = settings->section_count; - i = 0; + section = settings->sections; + n = settings->section_count; + i = 0; - while (i < n) { - sm_delete(section->map); + while (i < n) { + sm_delete(section->map); - if (section->name != NULL) { - free(section->name); - } + if (section->name != NULL) { + free(section->name); + } - section++; - i++; - } + section++; + i++; + } - free(settings->sections); - free(settings); + free(settings->sections); + free(settings); } -Settings * settings_open(FILE *stream) -{ - Settings *settings; - char buf[MAX_LINECHARS]; - char trimmed_buf[MAX_LINECHARS]; - char section_buf[MAX_LINECHARS]; - ParseState parse_state; +Settings* settings_open(FILE *stream) { + Settings *settings; + char buf[MAX_LINECHARS]; + char trimmed_buf[MAX_LINECHARS]; + char section_buf[MAX_LINECHARS]; + ParseState parse_state; - if (stream == NULL) { - return NULL; - } + if (stream == NULL) { + return NULL; + } - settings = settings_new(); + settings = settings_new(); - if (settings == NULL) { - return NULL; - } + if (settings == NULL) { + return NULL; + } - parse_state.current_section = section_buf; - parse_state.current_section_n = sizeof(section_buf); - parse_state.has_section = 0; - trim_str("", trimmed_buf); + parse_state.current_section = section_buf; + parse_state.current_section_n = sizeof(section_buf); + parse_state.has_section = 0; + trim_str("", trimmed_buf); - while (fgets(buf, MAX_LINECHARS, stream) != NULL) { - trim_str(buf, trimmed_buf); + while (fgets(buf, MAX_LINECHARS, stream) != NULL) { + trim_str(buf, trimmed_buf); - if (!parse_str(settings, trimmed_buf, &parse_state)) { - return NULL; - } - } + if (!parse_str(settings, trimmed_buf, &parse_state)) { + return NULL; + } + } - return settings; + return settings; } -int settings_save(const Settings *settings, FILE *stream) -{ - unsigned int i, n; - Section *section; - char buf[MAX_LINECHARS]; - - if (settings == NULL) { - return 0; - } - - if (stream == NULL) { - return 0; - } - - section = settings->sections; - n = settings->section_count; - i = 0; - - while (i < n) { - sprintf(buf, "[%s]\n", section->name); - fputs(buf, stream); - sm_enum(section->map, enum_map, stream); - section++; - i++; - fputs("\n", stream); - } - - return 0; +int settings_save(const Settings *settings, FILE *stream) { + unsigned int i, n; + Section *section; + char buf[MAX_LINECHARS]; + + if (settings == NULL) { + return 0; + } + + if (stream == NULL) { + return 0; + } + + section = settings->sections; + n = settings->section_count; + i = 0; + + while (i < n) { + sprintf(buf, "[%s]\n", section->name); + fputs(buf, stream); + sm_enum(section->map, enum_map, stream); + section++; + i++; + fputs("\n", stream); + } + + return 0; } -int settings_get(const Settings *settings, const char *section, const char *key, char *out_buf, unsigned int n_out_buf) -{ - Section *s; +int settings_get(const Settings *settings, const char *section, const char *key, + char *out_buf, unsigned int n_out_buf) { + Section *s; - if (settings == NULL) { - return 0; - } + if (settings == NULL) { + return 0; + } - s = get_section(settings->sections, settings->section_count, section); + s = get_section(settings->sections, settings->section_count, section); - if (s == NULL) { - return 0; - } + if (s == NULL) { + return 0; + } - return sm_get(s->map, key, out_buf, n_out_buf); + return sm_get(s->map, key, out_buf, n_out_buf); } -int settings_get_int(const Settings *settings, const char *section, const char *key) -{ - int i; +int settings_get_int(const Settings *settings, const char *section, + const char *key) { + int i; - if (get_converted_value(settings, section, key, CONVERT_MODE_INT, &i)) { - return i; - } + if (get_converted_value(settings, section, key, CONVERT_MODE_INT, &i)) { + return i; + } - return 0; + return 0; } -long settings_get_long(const Settings *settings, const char *section, const char *key) -{ - long l; +long settings_get_long(const Settings *settings, const char *section, + const char *key) { + long l; - if (get_converted_value(settings, section, key, CONVERT_MODE_LONG, &l)) { - return l; - } + if (get_converted_value(settings, section, key, CONVERT_MODE_LONG, &l)) { + return l; + } - return 0; + return 0; } -double settings_get_double(const Settings *settings, const char *section, const char *key) -{ - double d; +double settings_get_double(const Settings *settings, const char *section, + const char *key) { + double d; - if (get_converted_value(settings, section, key, CONVERT_MODE_DOUBLE, &d)) { - return d; - } + if (get_converted_value(settings, section, key, CONVERT_MODE_DOUBLE, &d)) { + return d; + } - return 0; + return 0; } -int settings_get_int_tuple(const Settings *settings, const char *section, const char *key, int *out, unsigned int n_out) -{ - return get_converted_tuple(settings, section, key, ',', CONVERT_MODE_INT, out, n_out); +int settings_get_int_tuple(const Settings *settings, const char *section, + const char *key, int *out, unsigned int n_out) { + return get_converted_tuple(settings, section, key, ',', CONVERT_MODE_INT, + out, n_out); } -long settings_get_long_tuple(const Settings *settings, const char *section, const char *key, long *out, unsigned int n_out) -{ - return get_converted_tuple(settings, section, key, ',', CONVERT_MODE_LONG, out, n_out); +long settings_get_long_tuple(const Settings *settings, const char *section, + const char *key, long *out, unsigned int n_out) { + return get_converted_tuple(settings, section, key, ',', CONVERT_MODE_LONG, + out, n_out); } -double settings_get_double_tuple(const Settings *settings, const char *section, const char *key, double *out, unsigned int n_out) -{ - return get_converted_tuple(settings, section, key, ',', CONVERT_MODE_DOUBLE, out, n_out); +double settings_get_double_tuple(const Settings *settings, const char *section, + const char *key, double *out, unsigned int n_out) { + return get_converted_tuple(settings, section, key, ',', CONVERT_MODE_DOUBLE, + out, n_out); } -int settings_set(Settings *settings, const char *section, const char *key, const char *value) -{ - Section *s; +int settings_set(Settings *settings, const char *section, const char *key, + const char *value) { + Section *s; - if (settings == NULL) { - return 0; - } + if (settings == NULL) { + return 0; + } - if (section == NULL || key == NULL || value == NULL) { - return 0; - } + if (section == NULL || key == NULL || value == NULL) { + return 0; + } - if (strlen(section) == 0) { - return 0; - } + if (strlen(section) == 0) { + return 0; + } - /* Get a pointer to the section */ - s = get_section(settings->sections, settings->section_count, section); + /* Get a pointer to the section */ + s = get_section(settings->sections, settings->section_count, section); - if (s == NULL) { - /* The section is not created---create it */ - s = (Section*)realloc(settings->sections, (settings->section_count + 1) * sizeof(Section)); + if (s == NULL) { + /* The section is not created---create it */ + s = (Section*) realloc(settings->sections, + (settings->section_count + 1) * sizeof(Section)); - if (s == NULL) { - return 0; - } + if (s == NULL) { + return 0; + } - settings->sections = s; - settings->section_count++; - s = &(settings->sections[settings->section_count - 1]); - s->map = sm_new(DEFAULT_STRMAP_CAPACITY); + settings->sections = s; + settings->section_count++; + s = &(settings->sections[settings->section_count - 1]); + s->map = sm_new(DEFAULT_STRMAP_CAPACITY); - if (s->map == NULL) { - free(s); - return 0; - } + if (s->map == NULL) { + free(s); + return 0; + } - s->name = (char*)malloc((strlen(section) + 1) * sizeof(char)); + s->name = (char*) malloc((strlen(section) + 1) * sizeof(char)); - if (s->name == NULL) { - sm_delete(s->map); - free(s); - return 0; - } + if (s->name == NULL) { + sm_delete(s->map); + free(s); + return 0; + } - strcpy(s->name, section); - } + strcpy(s->name, section); + } - return sm_put(s->map, key, value); + return sm_put(s->map, key, value); } -int settings_section_get_count(const Settings *settings, const char *section) -{ - Section *sect; +int settings_section_get_count(const Settings *settings, const char *section) { + Section *sect; - if (settings == NULL) { - return 0; - } + if (settings == NULL) { + return 0; + } - sect = get_section(settings->sections, settings->section_count, section); + sect = get_section(settings->sections, settings->section_count, section); - if (sect == NULL) { - return 0; - } + if (sect == NULL) { + return 0; + } - return sm_get_count(sect->map); + return sm_get_count(sect->map); } -int settings_section_enum(const Settings *settings, const char *section, settings_section_enum_func enum_func, const void *obj) -{ - Section *sect; +int settings_section_enum(const Settings *settings, const char *section, + settings_section_enum_func enum_func, const void *obj) { + Section *sect; - sect = get_section(settings->sections, settings->section_count, section); + sect = get_section(settings->sections, settings->section_count, section); - if (sect == NULL) { - return 0; - } + if (sect == NULL) { + return 0; + } - return sm_enum(sect->map, enum_func, obj); + return sm_enum(sect->map, enum_func, obj); } /* Copies a trimmed variant without leading and trailing blank characters * of the input string into the output buffer. The output buffer is assumed * to be large enough to contain the entire input string. */ -static void trim_str(const char *str, char *out_buf) -{ - unsigned int len; - const char *s0; - - while (*str != '\0' && is_blank_char(*str)) { - str++; - } - - s0 = str; - len = 0; - - while (*str != '\0') { - len++; - str++; - } - - if (len > 0) { - str--; - } - - while (is_blank_char(*str)) { - str--; - len--; - } - - memcpy(out_buf, s0, len); - out_buf[len] = '\0'; +static void trim_str(const char *str, char *out_buf) { + unsigned int len; + const char *s0; + + while (*str != '\0' && is_blank_char(*str)) { + str++; + } + + s0 = str; + len = 0; + + while (*str != '\0') { + len++; + str++; + } + + if (len > 0) { + str--; + } + + while (is_blank_char(*str)) { + str--; + len--; + } + + memcpy(out_buf, s0, len); + out_buf[len] = '\0'; } /* Parses a single input string and updates the provided settings object. @@ -390,321 +392,315 @@ static void trim_str(const char *str, char *out_buf) * parse_state->current_section_n: sizeof(parse_state->current_section) * parse_state->has_section: 0 (false) */ -static int parse_str(Settings *settings, char *str, ParseState *parse_state) -{ - char buf[MAX_LINECHARS]; - char buf1[MAX_LINECHARS]; - char buf2[MAX_LINECHARS]; - int result; +static int parse_str(Settings *settings, char *str, ParseState *parse_state) { + char buf[MAX_LINECHARS]; + char buf1[MAX_LINECHARS]; + char buf2[MAX_LINECHARS]; + int result; - if (*str == '\0') { - return 1; + if (*str == '\0') { + return 1; - } else if (is_blank_str(str)) { - return 1; + } else if (is_blank_str(str)) { + return 1; - } else if (is_comment_str(str)) { - return 1; + } else if (is_comment_str(str)) { + return 1; - } else if (is_section_str(str)) { - result = get_section_from_str(str, buf, sizeof(buf)); + } else if (is_section_str(str)) { + result = get_section_from_str(str, buf, sizeof(buf)); - if (!result) { - return 0; - } + if (!result) { + return 0; + } - if (strlen(buf) + 1 > parse_state->current_section_n) { - return 0; - } + if (strlen(buf) + 1 > parse_state->current_section_n) { + return 0; + } - strcpy(parse_state->current_section, buf); - parse_state->has_section = 1; - return 1; + strcpy(parse_state->current_section, buf); + parse_state->has_section = 1; + return 1; - } else if (is_key_value_str(str)) { - result = get_key_value_from_str(str, buf1, sizeof(buf1), buf2, sizeof(buf2)); + } else if (is_key_value_str(str)) { + result = get_key_value_from_str(str, buf1, sizeof(buf1), buf2, + sizeof(buf2)); - if (!result) { - return 0; - } + if (!result) { + return 0; + } - if (!parse_state->has_section) { - return 0; - } + if (!parse_state->has_section) { + return 0; + } - return settings_set(settings, parse_state->current_section, buf1, buf2); + return settings_set(settings, parse_state->current_section, buf1, buf2); - } else if (is_key_without_value_str(str)) { - result = get_key_without_value_from_str(str, buf, sizeof(buf)); + } else if (is_key_without_value_str(str)) { + result = get_key_without_value_from_str(str, buf, sizeof(buf)); - if (!result) { - return 0; - } + if (!result) { + return 0; + } - if (!parse_state->has_section) { - return 0; - } + if (!parse_state->has_section) { + return 0; + } - return settings_set(settings, parse_state->current_section, buf, ""); + return settings_set(settings, parse_state->current_section, buf, ""); - } else { - return 0; - } + } else { + return 0; + } } /* Returns true if the input character is blank, * false otherwise. */ -static int is_blank_char(char c) -{ - return c == ' ' || c == '\t' || c == '\r' || c == '\n'; +static int is_blank_char(char c) { + return c == ' ' || c == '\t' || c == '\r' || c == '\n'; } /* Returns true if the input string is blank, * false otherwise. */ -static int is_blank_str(const char *str) -{ - while (*str != '\0') { - if (!is_blank_char(*str)) { - return 0; - } +static int is_blank_str(const char *str) { + while (*str != '\0') { + if (!is_blank_char(*str)) { + return 0; + } - str++; - } + str++; + } - return 1; + return 1; } /* Returns true if the input string denotes a comment, * false otherwise. */ -static int is_comment_str(const char *str) -{ - if (*str == COMMENT_CHAR) { - /* To be a comment the first character must be the - * comment character. - */ - return 1; - } - - return 0; +static int is_comment_str(const char *str) { + if (*str == COMMENT_CHAR) { + /* To be a comment the first character must be the + * comment character. + */ + return 1; + } + + return 0; } /* Returns true if the input string denotes a section name, * false otherwise. */ -static int is_section_str(const char *str) -{ - if (*str != SECTION_START_CHAR) { - /* The first character must be the section start character */ - return 0; - } - - while (*str != '\0' && *str != SECTION_END_CHAR) { - str++; - } - - if (*str != SECTION_END_CHAR) { - /* The section end character must be present somewhere thereafter */ - return 0; - } - - return 1; +static int is_section_str(const char *str) { + if (*str != SECTION_START_CHAR) { + /* The first character must be the section start character */ + return 0; + } + + while (*str != '\0' && *str != SECTION_END_CHAR) { + str++; + } + + if (*str != SECTION_END_CHAR) { + /* The section end character must be present somewhere thereafter */ + return 0; + } + + return 1; } /* Returns true if the input string denotes a key-value pair, * false otherwise. */ -static int is_key_value_str(const char *str) -{ - if (*str == KEY_VALUE_SEPARATOR_CHAR) { - /* It is illegal to start with the key-value separator */ - return 0; - } - - while (*str != '\0' && *str != KEY_VALUE_SEPARATOR_CHAR) { - str++; - } - - if (*str != KEY_VALUE_SEPARATOR_CHAR) { - /* The key-value separator must be present after the key part */ - return 0; - } - - return 1; +static int is_key_value_str(const char *str) { + if (*str == KEY_VALUE_SEPARATOR_CHAR) { + /* It is illegal to start with the key-value separator */ + return 0; + } + + while (*str != '\0' && *str != KEY_VALUE_SEPARATOR_CHAR) { + str++; + } + + if (*str != KEY_VALUE_SEPARATOR_CHAR) { + /* The key-value separator must be present after the key part */ + return 0; + } + + return 1; } /* Returns true if the input string denotes a key without a value, * false otherwise. */ -static int is_key_without_value_str(const char *str) -{ - if (*str == KEY_VALUE_SEPARATOR_CHAR) { - /* It is illegal to start with the key-value separator */ - return 0; - } - - while (*str != '\0' && *str != KEY_VALUE_SEPARATOR_CHAR) { - str++; - } - - if (*str == KEY_VALUE_SEPARATOR_CHAR) { - /* The key-value separator must not be present after the key part */ - return 0; - } - - return 1; +static int is_key_without_value_str(const char *str) { + if (*str == KEY_VALUE_SEPARATOR_CHAR) { + /* It is illegal to start with the key-value separator */ + return 0; + } + + while (*str != '\0' && *str != KEY_VALUE_SEPARATOR_CHAR) { + str++; + } + + if (*str == KEY_VALUE_SEPARATOR_CHAR) { + /* The key-value separator must not be present after the key part */ + return 0; + } + + return 1; } /* * Parses a section name from an input string. The input string is assumed to * already have been identified as a valid input string denoting a section name. */ -static int get_section_from_str(const char *str, char *out_buf, unsigned int out_buf_n) -{ - unsigned int count; - - count = 0; - /* Jump past the section begin character */ - str++; - - while (*str != '\0' && *str != SECTION_END_CHAR) { - /* Read in the section name into the output buffer */ - if (count == out_buf_n) { - return 0; - } - - *out_buf = *str; - out_buf++; - str++; - count++; - } - - /* Terminate the output buffer */ - if (count == out_buf_n) { - return 0; - } - - *out_buf = '\0'; - return 1; +static int get_section_from_str(const char *str, char *out_buf, + unsigned int out_buf_n) { + unsigned int count; + + count = 0; + /* Jump past the section begin character */ + str++; + + while (*str != '\0' && *str != SECTION_END_CHAR) { + /* Read in the section name into the output buffer */ + if (count == out_buf_n) { + return 0; + } + + *out_buf = *str; + out_buf++; + str++; + count++; + } + + /* Terminate the output buffer */ + if (count == out_buf_n) { + return 0; + } + + *out_buf = '\0'; + return 1; } /* * Parses a key and value from an input string. The input string is assumed to * already have been identified as a valid input string denoting a key-value pair. */ -static int get_key_value_from_str(const char *str, char *out_buf1, unsigned int out_buf1_n, char *out_buf2, unsigned int out_buf2_n) -{ - unsigned int count1; - unsigned int count2; - - count1 = 0; - count2 = 0; - - /* Read the key value from the input string and write it sequentially - * to the first output buffer by walking the input string until we either hit - * the null-terminator or the key-value separator. - */ - while (*str != '\0' && *str != KEY_VALUE_SEPARATOR_CHAR) { - /* Ensure the first output buffer is large enough. */ - if (count1 == out_buf1_n) { - return 0; - } - - /* Copy the character to the first output buffer */ - *out_buf1 = *str; - out_buf1++; - str++; - count1++; - } - - /* Terminate the first output buffer */ - if (count1 == out_buf1_n) { - return 0; - } - - *out_buf1 = '\0'; - - /* Now trace the first input buffer backwards until we hit a non-blank character */ - while (is_blank_char(*(out_buf1 - 1))) { - out_buf1--; - } - - *out_buf1 = '\0'; - - /* Try to proceed one more character, past the last read key-value - * delimiter, in the input string. - */ - if (*str != '\0') { - str++; - } - - /* Now find start of the value in the input string by walking the input - * string until we either hit the null-terminator or a blank character. - */ - while (*str != '\0' && is_blank_char(*str)) { - str++; - } - - while (*str != '\0') { - /* Fail if there is a possibility that we are overwriting the second - * input buffer. - */ - if (count2 == out_buf2_n) { - return 0; - } - - /* Copy the character to the second output buffer */ - *out_buf2 = *str; - out_buf2++; - str++; - count2++; - } - - /* Terminate the second output buffer */ - if (count2 == out_buf2_n) { - return 0; - } - - *out_buf2 = '\0'; - return 1; +static int get_key_value_from_str(const char *str, char *out_buf1, + unsigned int out_buf1_n, char *out_buf2, unsigned int out_buf2_n) { + unsigned int count1; + unsigned int count2; + + count1 = 0; + count2 = 0; + + /* Read the key value from the input string and write it sequentially + * to the first output buffer by walking the input string until we either hit + * the null-terminator or the key-value separator. + */ + while (*str != '\0' && *str != KEY_VALUE_SEPARATOR_CHAR) { + /* Ensure the first output buffer is large enough. */ + if (count1 == out_buf1_n) { + return 0; + } + + /* Copy the character to the first output buffer */ + *out_buf1 = *str; + out_buf1++; + str++; + count1++; + } + + /* Terminate the first output buffer */ + if (count1 == out_buf1_n) { + return 0; + } + + *out_buf1 = '\0'; + + /* Now trace the first input buffer backwards until we hit a non-blank character */ + while (is_blank_char(*(out_buf1 - 1))) { + out_buf1--; + } + + *out_buf1 = '\0'; + + /* Try to proceed one more character, past the last read key-value + * delimiter, in the input string. + */ + if (*str != '\0') { + str++; + } + + /* Now find start of the value in the input string by walking the input + * string until we either hit the null-terminator or a blank character. + */ + while (*str != '\0' && is_blank_char(*str)) { + str++; + } + + while (*str != '\0') { + /* Fail if there is a possibility that we are overwriting the second + * input buffer. + */ + if (count2 == out_buf2_n) { + return 0; + } + + /* Copy the character to the second output buffer */ + *out_buf2 = *str; + out_buf2++; + str++; + count2++; + } + + /* Terminate the second output buffer */ + if (count2 == out_buf2_n) { + return 0; + } + + *out_buf2 = '\0'; + return 1; } /* * Parses a key from an input string. The input string is assumed to already * have been identified as a valid input string denoting a key without a value. */ -static int get_key_without_value_from_str(const char *str, char *out_buf, unsigned int out_buf_n) -{ - unsigned int count; - - count = 0; - - /* Now read the key value from the input string and write it sequentially - * to the output buffer by walking the input string until we either hit - * the null-terminator or the key-value separator. - */ - while (*str != '\0') { - /* Ensure the output buffer is large enough. */ - if (count == out_buf_n) { - return 0; - } - - /* Copy the character to the input buffer */ - *out_buf = *str; - out_buf++; - str++; - count++; - } - - /* Terminate the output buffer */ - if (count == out_buf_n) { - return 0; - } - - *out_buf = '\0'; - return 1; +static int get_key_without_value_from_str(const char *str, char *out_buf, + unsigned int out_buf_n) { + unsigned int count; + + count = 0; + + /* Now read the key value from the input string and write it sequentially + * to the output buffer by walking the input string until we either hit + * the null-terminator or the key-value separator. + */ + while (*str != '\0') { + /* Ensure the output buffer is large enough. */ + if (count == out_buf_n) { + return 0; + } + + /* Copy the character to the input buffer */ + *out_buf = *str; + out_buf++; + str++; + count++; + } + + /* Terminate the output buffer */ + if (count == out_buf_n) { + return 0; + } + + *out_buf = '\0'; + return 1; } /* Returns a pointer to the next token in the input string delimited @@ -723,36 +719,35 @@ static int get_key_without_value_from_str(const char *str, char *out_buf, unsign * printf("token: %s", token); * } */ -static const char * get_token(char *str, char delim, char **last) -{ - - char *s0; - - s0 = str; - - /* If we hit the null-terminator the string - * is exhausted and another token does not - * exist. - */ - if (*str == '\0') { - return NULL; - } - - /* Walk the string until we encounter a - * null-terminator or the delimiter. - */ - while (*str != '\0' && *str != delim) { - str++; - } - - /* Terminate the return token, if necessary */ - if (*str != '\0') { - *str = '\0'; - str++; - } - - *last = str; - return s0; +static const char* get_token(char *str, char delim, char **last) { + + char *s0; + + s0 = str; + + /* If we hit the null-terminator the string + * is exhausted and another token does not + * exist. + */ + if (*str == '\0') { + return NULL; + } + + /* Walk the string until we encounter a + * null-terminator or the delimiter. + */ + while (*str != '\0' && *str != delim) { + str++; + } + + /* Terminate the return token, if necessary */ + if (*str != '\0') { + *str = '\0'; + str++; + } + + *last = str; + return s0; } /* Returns a converted value pointed to by the provided key in the given section. @@ -761,29 +756,29 @@ static const char * get_token(char *str, char delim, char **last) * value assuming conversion is succesful. The function returns 1 if conversion * is succsessful and 0 if the convertion could not be carried out. */ -static int get_converted_value(const Settings *settings, const char *section, const char *key, ConvertMode mode, void *out) -{ - char value[MAX_VALUECHARS]; +static int get_converted_value(const Settings *settings, const char *section, + const char *key, ConvertMode mode, void *out) { + char value[MAX_VALUECHARS]; - if (!settings_get(settings, section, key, value, MAX_VALUECHARS)) { - return 0; - } + if (!settings_get(settings, section, key, value, MAX_VALUECHARS)) { + return 0; + } - switch (mode) { - case CONVERT_MODE_INT: - *((int *)out) = atoi(value); - return 1; + switch (mode) { + case CONVERT_MODE_INT: + *((int*) out) = atoi(value); + return 1; - case CONVERT_MODE_LONG: - *((long *)out) = atol(value); - return 1; + case CONVERT_MODE_LONG: + *((long*) out) = atol(value); + return 1; - case CONVERT_MODE_DOUBLE: - *((double *)out) = atof(value); - return 1; - } + case CONVERT_MODE_DOUBLE: + *((double*) out) = atof(value); + return 1; + } - return 0; + return 0; } /* Returns a converted tuple pointed to by the provided key in the given section. @@ -793,273 +788,272 @@ static int get_converted_value(const Settings *settings, const char *section, co * assuming conversion is succesful. The function returns 1 if conversion * is succsessful and 0 if the convertion could not be carried out. */ -static int get_converted_tuple(const Settings *settings, const char *section, const char *key, char delim, ConvertMode mode, void *out, unsigned int n_out) -{ - unsigned int count; - const char *token; - static char value[MAX_VALUECHARS]; - char *v; - - if (out == NULL) { - return 0; - } - - if (n_out == 0) { - return 0; - } - - if (!settings_get(settings, section, key, value, MAX_VALUECHARS)) { - return 0; - } - - v = value; - count = 0; - - /* Walk over all tokens in the value, and convert them and assign them - * to the output array as specified by the mode. - */ - while ((token = get_token(v, delim, &v)) != NULL && count < n_out) { - switch (mode) { - case CONVERT_MODE_INT: - ((int *)out)[count] = atoi(token); - break; - - case CONVERT_MODE_LONG: - ((long *)out)[count] = atol(token); - break; - - case CONVERT_MODE_DOUBLE: - ((double *)out)[count] = atof(token); - break; - - default: - return 0; - } - - count++; - } - - return 1; +static int get_converted_tuple(const Settings *settings, const char *section, + const char *key, char delim, ConvertMode mode, void *out, + unsigned int n_out) { + unsigned int count; + const char *token; + static char value[MAX_VALUECHARS]; + char *v; + + if (out == NULL) { + return 0; + } + + if (n_out == 0) { + return 0; + } + + if (!settings_get(settings, section, key, value, MAX_VALUECHARS)) { + return 0; + } + + v = value; + count = 0; + + /* Walk over all tokens in the value, and convert them and assign them + * to the output array as specified by the mode. + */ + while ((token = get_token(v, delim, &v)) != NULL && count < n_out) { + switch (mode) { + case CONVERT_MODE_INT: + ((int*) out)[count] = atoi(token); + break; + + case CONVERT_MODE_LONG: + ((long*) out)[count] = atol(token); + break; + + case CONVERT_MODE_DOUBLE: + ((double*) out)[count] = atof(token); + break; + + default: + return 0; + } + + count++; + } + + return 1; } /* Returns a pointer to the section or null if the named section does not * exist. */ -static Section * get_section(Section *sections, unsigned int n, const char *name) -{ - unsigned int i; - Section *section; +static Section* get_section(Section *sections, unsigned int n, const char *name) { + unsigned int i; + Section *section; - if (name == NULL) { - return NULL; - } + if (name == NULL) { + return NULL; + } - section = sections; - i = 0; + section = sections; + i = 0; - while (i < n) { - if (strcmp(section->name, name) == 0) { - return section; - } + while (i < n) { + if (strcmp(section->name, name) == 0) { + return section; + } - section++; - i++; - } + section++; + i++; + } - return NULL; + return NULL; } /* Callback function that is passed into the enumeration function in the * string map. It casts the passed into object into a FILE pointer and * writes out the key and value to the file. */ -static void enum_map(const char *key, const char *value, const void *obj) -{ - FILE *stream; - char buf[MAX_LINECHARS]; +static void enum_map(const char *key, const char *value, const void *obj) { + FILE *stream; + char buf[MAX_LINECHARS]; - if (key == NULL || value == NULL) { - return; - } + if (key == NULL || value == NULL) { + return; + } - if (obj == NULL) { - return; - } + if (obj == NULL) { + return; + } - stream = (FILE *)obj; + stream = (FILE*) obj; - if (strlen(key) < MAX_KEYCHARS && strlen(value) < MAX_VALUECHARS) { - sprintf(buf, "%s%c%s\n", key, KEY_VALUE_SEPARATOR_CHAR, value); - fputs(buf, stream); - } + if (strlen(key) < MAX_KEYCHARS && strlen(value) < MAX_VALUECHARS) { + sprintf(buf, "%s%c%s\n", key, KEY_VALUE_SEPARATOR_CHAR, value); + fputs(buf, stream); + } } /* - GNU LESSER GENERAL PUBLIC LICENSE - Version 3, 29 June 2007 + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. - This version of the GNU Lesser General Public License incorporates -the terms and conditions of version 3 of the GNU General Public -License, supplemented by the additional permissions listed below. + This version of the GNU Lesser General Public License incorporates + the terms and conditions of version 3 of the GNU General Public + License, supplemented by the additional permissions listed below. - 0. Additional Definitions. + 0. Additional Definitions. - As used herein, "this License" refers to version 3 of the GNU Lesser -General Public License, and the "GNU GPL" refers to version 3 of the GNU -General Public License. + As used herein, "this License" refers to version 3 of the GNU Lesser + General Public License, and the "GNU GPL" refers to version 3 of the GNU + General Public License. - "The Library" refers to a covered work governed by this License, -other than an Application or a Combined Work as defined below. + "The Library" refers to a covered work governed by this License, + other than an Application or a Combined Work as defined below. - An "Application" is any work that makes use of an interface provided -by the Library, but which is not otherwise based on the Library. -Defining a subclass of a class defined by the Library is deemed a mode -of using an interface provided by the Library. - - A "Combined Work" is a work produced by combining or linking an -Application with the Library. The particular version of the Library -with which the Combined Work was made is also called the "Linked -Version". - - The "Minimal Corresponding Source" for a Combined Work means the -Corresponding Source for the Combined Work, excluding any source code -for portions of the Combined Work that, considered in isolation, are -based on the Application, and not on the Linked Version. - - The "Corresponding Application Code" for a Combined Work means the -object code and/or source code for the Application, including any data -and utility programs needed for reproducing the Combined Work from the -Application, but excluding the System Libraries of the Combined Work. - - 1. Exception to Section 3 of the GNU GPL. - - You may convey a covered work under sections 3 and 4 of this License -without being bound by section 3 of the GNU GPL. - - 2. Conveying Modified Versions. - - If you modify a copy of the Library, and, in your modifications, a -facility refers to a function or data to be supplied by an Application -that uses the facility (other than as an argument passed when the -facility is invoked), then you may convey a copy of the modified -version: - - a) under this License, provided that you make a good faith effort to - ensure that, in the event an Application does not supply the - function or data, the facility still operates, and performs - whatever part of its purpose remains meaningful, or - - b) under the GNU GPL, with none of the additional permissions of - this License applicable to that copy. - - 3. Object Code Incorporating Material from Library Header Files. - - The object code form of an Application may incorporate material from -a header file that is part of the Library. You may convey such object -code under terms of your choice, provided that, if the incorporated -material is not limited to numerical parameters, data structure -layouts and accessors, or small macros, inline functions and templates -(ten or fewer lines in length), you do both of the following: - - a) Give prominent notice with each copy of the object code that the - Library is used in it and that the Library and its use are - covered by this License. - - b) Accompany the object code with a copy of the GNU GPL and this license - document. - - 4. Combined Works. - - You may convey a Combined Work under terms of your choice that, -taken together, effectively do not restrict modification of the -portions of the Library contained in the Combined Work and reverse -engineering for debugging such modifications, if you also do each of -the following: - - a) Give prominent notice with each copy of the Combined Work that - the Library is used in it and that the Library and its use are - covered by this License. - - b) Accompany the Combined Work with a copy of the GNU GPL and this license - document. - - c) For a Combined Work that displays copyright notices during - execution, include the copyright notice for the Library among - these notices, as well as a reference directing the user to the - copies of the GNU GPL and this license document. - - d) Do one of the following: - - 0) Convey the Minimal Corresponding Source under the terms of this - License, and the Corresponding Application Code in a form - suitable for, and under terms that permit, the user to - recombine or relink the Application with a modified version of - the Linked Version to produce a modified Combined Work, in the - manner specified by section 6 of the GNU GPL for conveying - Corresponding Source. - - 1) Use a suitable shared library mechanism for linking with the - Library. A suitable mechanism is one that (a) uses at run time - a copy of the Library already present on the user's computer - system, and (b) will operate properly with a modified version - of the Library that is interface-compatible with the Linked - Version. - - e) Provide Installation Information, but only if you would otherwise - be required to provide such information under section 6 of the - GNU GPL, and only to the extent that such information is - necessary to install and execute a modified version of the - Combined Work produced by recombining or relinking the - Application with a modified version of the Linked Version. (If - you use option 4d0, the Installation Information must accompany - the Minimal Corresponding Source and Corresponding Application - Code. If you use option 4d1, you must provide the Installation - Information in the manner specified by section 6 of the GNU GPL - for conveying Corresponding Source.) - - 5. Combined Libraries. - - You may place library facilities that are a work based on the -Library side by side in a single library together with other library -facilities that are not Applications and are not covered by this -License, and convey such a combined library under terms of your -choice, if you do both of the following: - - a) Accompany the combined library with a copy of the same work based - on the Library, uncombined with any other library facilities, - conveyed under the terms of this License. - - b) Give prominent notice with the combined library that part of it - is a work based on the Library, and explaining where to find the - accompanying uncombined form of the same work. - - 6. Revised Versions of the GNU Lesser General Public License. - - The Free Software Foundation may publish revised and/or new versions -of the GNU Lesser General Public License from time to time. Such new -versions will be similar in spirit to the present version, but may -differ in detail to address new problems or concerns. - - Each version is given a distinguishing version number. If the -Library as you received it specifies that a certain numbered version -of the GNU Lesser General Public License "or any later version" -applies to it, you have the option of following the terms and -conditions either of that published version or of any later version -published by the Free Software Foundation. If the Library as you -received it does not specify a version number of the GNU Lesser -General Public License, you may choose any version of the GNU Lesser -General Public License ever published by the Free Software Foundation. - - If the Library as you received it specifies that a proxy can decide -whether future versions of the GNU Lesser General Public License shall -apply, that proxy's public statement of acceptance of any version is -permanent authorization for you to choose that version for the -Library. - -*/ + An "Application" is any work that makes use of an interface provided + by the Library, but which is not otherwise based on the Library. + Defining a subclass of a class defined by the Library is deemed a mode + of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an + Application with the Library. The particular version of the Library + with which the Combined Work was made is also called the "Linked + Version". + + The "Minimal Corresponding Source" for a Combined Work means the + Corresponding Source for the Combined Work, excluding any source code + for portions of the Combined Work that, considered in isolation, are + based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the + object code and/or source code for the Application, including any data + and utility programs needed for reproducing the Combined Work from the + Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License + without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a + facility refers to a function or data to be supplied by an Application + that uses the facility (other than as an argument passed when the + facility is invoked), then you may convey a copy of the modified + version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from + a header file that is part of the Library. You may convey such object + code under terms of your choice, provided that, if the incorporated + material is not limited to numerical parameters, data structure + layouts and accessors, or small macros, inline functions and templates + (ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, + taken together, effectively do not restrict modification of the + portions of the Library contained in the Combined Work and reverse + engineering for debugging such modifications, if you also do each of + the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the + Library side by side in a single library together with other library + facilities that are not Applications and are not covered by this + License, and convey such a combined library under terms of your + choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions + of the GNU Lesser General Public License from time to time. Such new + versions will be similar in spirit to the present version, but may + differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the + Library as you received it specifies that a certain numbered version + of the GNU Lesser General Public License "or any later version" + applies to it, you have the option of following the terms and + conditions either of that published version or of any later version + published by the Free Software Foundation. If the Library as you + received it does not specify a version number of the GNU Lesser + General Public License, you may choose any version of the GNU Lesser + General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide + whether future versions of the GNU Lesser General Public License shall + apply, that proxy's public statement of acceptance of any version is + permanent authorization for you to choose that version for the + Library. + + */ diff --git a/src/settings.h b/src/settings.h index 4b3ada8..986839c 100644 --- a/src/settings.h +++ b/src/settings.h @@ -39,8 +39,7 @@ #include "strmap.h" #ifdef __cplusplus -extern "C" -{ +extern "C" { #endif typedef struct Settings Settings; @@ -62,7 +61,8 @@ typedef struct Settings Settings; * * Return value: None. */ -typedef void(*settings_section_enum_func)(const char *key, const char *value, const void *obj); +typedef void (*settings_section_enum_func)(const char *key, const char *value, + const void *obj); /* * Creates a settings object. @@ -70,7 +70,7 @@ typedef void(*settings_section_enum_func)(const char *key, const char *value, co * Return value: A pointer to a settings object, * or null if a new settings object could not be allocated. */ -Settings * settings_new(); +Settings* settings_new(); /* * Releases all memory held by a settings object. @@ -98,7 +98,7 @@ void settings_delete(Settings *settings); * Return value: A pointer to a settings object, * or null if an error occurred. */ -Settings * settings_open(FILE *stream); +Settings* settings_open(FILE *stream); /* * Saves the current settings object in textual form to the given stream. @@ -138,7 +138,8 @@ int settings_save(const Settings *settings, FILE *stream); * is 1 if an associated value was found and completely copied into the output buffer, * 0 otherwise. */ -int settings_get(const Settings *settings, const char *section, const char *key, char *out_buf, unsigned int n_out_buf); +int settings_get(const Settings *settings, const char *section, const char *key, + char *out_buf, unsigned int n_out_buf); /* * Returns the integer value associated with the supplied key in the @@ -157,7 +158,8 @@ int settings_get(const Settings *settings, const char *section, const char *key, * Return value: The integer value associated to the provided section and * key, or 0 if no such value exists. */ -int settings_get_int(const Settings *settings, const char *section, const char *key); +int settings_get_int(const Settings *settings, const char *section, + const char *key); /* * Returns the long integer value associated with the supplied key in the @@ -176,7 +178,8 @@ int settings_get_int(const Settings *settings, const char *section, const char * * Return value: The long integer value associated to the provided section and * key, or 0 if no such value exists. */ -long settings_get_long(const Settings *settings, const char *section, const char *key); +long settings_get_long(const Settings *settings, const char *section, + const char *key); /* * Returns the double value associated with the supplied key in the @@ -195,7 +198,8 @@ long settings_get_long(const Settings *settings, const char *section, const char * Return value: The double value associated to the provided section and * key, or 0 if no such value exists. */ -double settings_get_double(const Settings *settings, const char *section, const char *key); +double settings_get_double(const Settings *settings, const char *section, + const char *key); /* * Returns the integer tuple associated with the supplied key in the @@ -218,7 +222,8 @@ double settings_get_double(const Settings *settings, const char *section, const * Return value: 1 if the entire tuple was copied into the output buffer, * 0 otherwise. */ -int settings_get_int_tuple(const Settings *settings, const char *section, const char *key, int *out, unsigned int n_out); +int settings_get_int_tuple(const Settings *settings, const char *section, + const char *key, int *out, unsigned int n_out); /* * Returns the long tuple associated with the supplied key in the @@ -241,7 +246,8 @@ int settings_get_int_tuple(const Settings *settings, const char *section, const * Return value: 1 if the entire tuple was copied into the output buffer, * 0 otherwise. */ -long settings_get_long_tuple(const Settings *settings, const char *section, const char *key, long *out, unsigned int n_out); +long settings_get_long_tuple(const Settings *settings, const char *section, + const char *key, long *out, unsigned int n_out); /* * Returns the double tuple associated with the supplied key in the @@ -264,7 +270,8 @@ long settings_get_long_tuple(const Settings *settings, const char *section, cons * Return value: 1 if the entire tuple was copied into the output buffer, * 0 otherwise. */ -double settings_get_double_tuple(const Settings *settings, const char *section, const char *key, double *out, unsigned int n_out); +double settings_get_double_tuple(const Settings *settings, const char *section, + const char *key, double *out, unsigned int n_out); /* * Associates a value with the supplied key in the provided section. @@ -289,7 +296,8 @@ double settings_get_double_tuple(const Settings *settings, const char *section, * * Return value: 1 if the association succeeded, 0 otherwise. */ -int settings_set(Settings *setting, const char *section, const char *key, const char *value); +int settings_set(Settings *setting, const char *section, const char *key, + const char *value); /* * Returns the number of associations between keys and values that exist @@ -328,7 +336,8 @@ int settings_section_get_count(const Settings *settings, const char *section); * * Return value: 1 if enumeration completed, 0 otherwise. */ -int settings_section_enum(const Settings *settings, const char *section, settings_section_enum_func enum_func, const void *obj); +int settings_section_enum(const Settings *settings, const char *section, + settings_section_enum_func enum_func, const void *obj); #ifdef __cplusplus } @@ -338,170 +347,170 @@ int settings_section_enum(const Settings *settings, const char *section, setting /* - GNU LESSER GENERAL PUBLIC LICENSE - Version 3, 29 June 2007 + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. - This version of the GNU Lesser General Public License incorporates -the terms and conditions of version 3 of the GNU General Public -License, supplemented by the additional permissions listed below. + This version of the GNU Lesser General Public License incorporates + the terms and conditions of version 3 of the GNU General Public + License, supplemented by the additional permissions listed below. - 0. Additional Definitions. + 0. Additional Definitions. - As used herein, "this License" refers to version 3 of the GNU Lesser -General Public License, and the "GNU GPL" refers to version 3 of the GNU -General Public License. + As used herein, "this License" refers to version 3 of the GNU Lesser + General Public License, and the "GNU GPL" refers to version 3 of the GNU + General Public License. - "The Library" refers to a covered work governed by this License, -other than an Application or a Combined Work as defined below. + "The Library" refers to a covered work governed by this License, + other than an Application or a Combined Work as defined below. - An "Application" is any work that makes use of an interface provided -by the Library, but which is not otherwise based on the Library. -Defining a subclass of a class defined by the Library is deemed a mode -of using an interface provided by the Library. - - A "Combined Work" is a work produced by combining or linking an -Application with the Library. The particular version of the Library -with which the Combined Work was made is also called the "Linked -Version". - - The "Minimal Corresponding Source" for a Combined Work means the -Corresponding Source for the Combined Work, excluding any source code -for portions of the Combined Work that, considered in isolation, are -based on the Application, and not on the Linked Version. - - The "Corresponding Application Code" for a Combined Work means the -object code and/or source code for the Application, including any data -and utility programs needed for reproducing the Combined Work from the -Application, but excluding the System Libraries of the Combined Work. - - 1. Exception to Section 3 of the GNU GPL. - - You may convey a covered work under sections 3 and 4 of this License -without being bound by section 3 of the GNU GPL. - - 2. Conveying Modified Versions. - - If you modify a copy of the Library, and, in your modifications, a -facility refers to a function or data to be supplied by an Application -that uses the facility (other than as an argument passed when the -facility is invoked), then you may convey a copy of the modified -version: - - a) under this License, provided that you make a good faith effort to - ensure that, in the event an Application does not supply the - function or data, the facility still operates, and performs - whatever part of its purpose remains meaningful, or - - b) under the GNU GPL, with none of the additional permissions of - this License applicable to that copy. - - 3. Object Code Incorporating Material from Library Header Files. - - The object code form of an Application may incorporate material from -a header file that is part of the Library. You may convey such object -code under terms of your choice, provided that, if the incorporated -material is not limited to numerical parameters, data structure -layouts and accessors, or small macros, inline functions and templates -(ten or fewer lines in length), you do both of the following: - - a) Give prominent notice with each copy of the object code that the - Library is used in it and that the Library and its use are - covered by this License. - - b) Accompany the object code with a copy of the GNU GPL and this license - document. - - 4. Combined Works. - - You may convey a Combined Work under terms of your choice that, -taken together, effectively do not restrict modification of the -portions of the Library contained in the Combined Work and reverse -engineering for debugging such modifications, if you also do each of -the following: - - a) Give prominent notice with each copy of the Combined Work that - the Library is used in it and that the Library and its use are - covered by this License. - - b) Accompany the Combined Work with a copy of the GNU GPL and this license - document. - - c) For a Combined Work that displays copyright notices during - execution, include the copyright notice for the Library among - these notices, as well as a reference directing the user to the - copies of the GNU GPL and this license document. - - d) Do one of the following: - - 0) Convey the Minimal Corresponding Source under the terms of this - License, and the Corresponding Application Code in a form - suitable for, and under terms that permit, the user to - recombine or relink the Application with a modified version of - the Linked Version to produce a modified Combined Work, in the - manner specified by section 6 of the GNU GPL for conveying - Corresponding Source. - - 1) Use a suitable shared library mechanism for linking with the - Library. A suitable mechanism is one that (a) uses at run time - a copy of the Library already present on the user's computer - system, and (b) will operate properly with a modified version - of the Library that is interface-compatible with the Linked - Version. - - e) Provide Installation Information, but only if you would otherwise - be required to provide such information under section 6 of the - GNU GPL, and only to the extent that such information is - necessary to install and execute a modified version of the - Combined Work produced by recombining or relinking the - Application with a modified version of the Linked Version. (If - you use option 4d0, the Installation Information must accompany - the Minimal Corresponding Source and Corresponding Application - Code. If you use option 4d1, you must provide the Installation - Information in the manner specified by section 6 of the GNU GPL - for conveying Corresponding Source.) - - 5. Combined Libraries. - - You may place library facilities that are a work based on the -Library side by side in a single library together with other library -facilities that are not Applications and are not covered by this -License, and convey such a combined library under terms of your -choice, if you do both of the following: - - a) Accompany the combined library with a copy of the same work based - on the Library, uncombined with any other library facilities, - conveyed under the terms of this License. - - b) Give prominent notice with the combined library that part of it - is a work based on the Library, and explaining where to find the - accompanying uncombined form of the same work. - - 6. Revised Versions of the GNU Lesser General Public License. - - The Free Software Foundation may publish revised and/or new versions -of the GNU Lesser General Public License from time to time. Such new -versions will be similar in spirit to the present version, but may -differ in detail to address new problems or concerns. - - Each version is given a distinguishing version number. If the -Library as you received it specifies that a certain numbered version -of the GNU Lesser General Public License "or any later version" -applies to it, you have the option of following the terms and -conditions either of that published version or of any later version -published by the Free Software Foundation. If the Library as you -received it does not specify a version number of the GNU Lesser -General Public License, you may choose any version of the GNU Lesser -General Public License ever published by the Free Software Foundation. - - If the Library as you received it specifies that a proxy can decide -whether future versions of the GNU Lesser General Public License shall -apply, that proxy's public statement of acceptance of any version is -permanent authorization for you to choose that version for the -Library. - -*/ + An "Application" is any work that makes use of an interface provided + by the Library, but which is not otherwise based on the Library. + Defining a subclass of a class defined by the Library is deemed a mode + of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an + Application with the Library. The particular version of the Library + with which the Combined Work was made is also called the "Linked + Version". + + The "Minimal Corresponding Source" for a Combined Work means the + Corresponding Source for the Combined Work, excluding any source code + for portions of the Combined Work that, considered in isolation, are + based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the + object code and/or source code for the Application, including any data + and utility programs needed for reproducing the Combined Work from the + Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License + without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a + facility refers to a function or data to be supplied by an Application + that uses the facility (other than as an argument passed when the + facility is invoked), then you may convey a copy of the modified + version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from + a header file that is part of the Library. You may convey such object + code under terms of your choice, provided that, if the incorporated + material is not limited to numerical parameters, data structure + layouts and accessors, or small macros, inline functions and templates + (ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, + taken together, effectively do not restrict modification of the + portions of the Library contained in the Combined Work and reverse + engineering for debugging such modifications, if you also do each of + the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the + Library side by side in a single library together with other library + facilities that are not Applications and are not covered by this + License, and convey such a combined library under terms of your + choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions + of the GNU Lesser General Public License from time to time. Such new + versions will be similar in spirit to the present version, but may + differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the + Library as you received it specifies that a certain numbered version + of the GNU Lesser General Public License "or any later version" + applies to it, you have the option of following the terms and + conditions either of that published version or of any later version + published by the Free Software Foundation. If the Library as you + received it does not specify a version number of the GNU Lesser + General Public License, you may choose any version of the GNU Lesser + General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide + whether future versions of the GNU Lesser General Public License shall + apply, that proxy's public statement of acceptance of any version is + permanent authorization for you to choose that version for the + Library. + + */ diff --git a/src/strmap.c b/src/strmap.c index 45853e3..f00a64e 100644 --- a/src/strmap.c +++ b/src/strmap.c @@ -40,528 +40,522 @@ typedef struct Pair Pair; typedef struct Bucket Bucket; struct Pair { - char *key; - char *value; + char *key; + char *value; }; struct Bucket { - unsigned int count; - Pair *pairs; + unsigned int count; + Pair *pairs; }; struct StrMap { - unsigned int count; - Bucket *buckets; + unsigned int count; + Bucket *buckets; }; -static Pair * get_pair(Bucket *bucket, const char *key); +static Pair* get_pair(Bucket *bucket, const char *key); static unsigned long hash(const char *str); -StrMap * sm_new(unsigned int capacity) -{ - StrMap *map; +StrMap* sm_new(unsigned int capacity) { + StrMap *map; - map = (StrMap*)malloc(sizeof(StrMap)); + map = (StrMap*) malloc(sizeof(StrMap)); - if (map == NULL) { - return NULL; - } + if (map == NULL) { + return NULL; + } - map->count = capacity; - map->buckets = (Bucket*)malloc(map->count * sizeof(Bucket)); + map->count = capacity; + map->buckets = (Bucket*) malloc(map->count * sizeof(Bucket)); - if (map->buckets == NULL) { - free(map); - return NULL; - } + if (map->buckets == NULL) { + free(map); + return NULL; + } - memset(map->buckets, 0, map->count * sizeof(Bucket)); - return map; + memset(map->buckets, 0, map->count * sizeof(Bucket)); + return map; } -void sm_delete(StrMap *map) -{ - unsigned int i, j, n, m; - Bucket *bucket; - Pair *pair; - - if (map == NULL) { - return; - } - - n = map->count; - bucket = map->buckets; - i = 0; - - while (i < n) { - m = bucket->count; - pair = bucket->pairs; - j = 0; - - while(j < m) { - free(pair->key); - free(pair->value); - pair++; - j++; - } - - free(bucket->pairs); - bucket++; - i++; - } - - free(map->buckets); - free(map); +void sm_delete(StrMap *map) { + unsigned int i, j, n, m; + Bucket *bucket; + Pair *pair; + + if (map == NULL) { + return; + } + + n = map->count; + bucket = map->buckets; + i = 0; + + while (i < n) { + m = bucket->count; + pair = bucket->pairs; + j = 0; + + while (j < m) { + free(pair->key); + free(pair->value); + pair++; + j++; + } + + free(bucket->pairs); + bucket++; + i++; + } + + free(map->buckets); + free(map); } -int sm_get(const StrMap *map, const char *key, char *out_buf, unsigned int n_out_buf) -{ - unsigned int index; - Bucket *bucket; - Pair *pair; +int sm_get(const StrMap *map, const char *key, char *out_buf, + unsigned int n_out_buf) { + unsigned int index; + Bucket *bucket; + Pair *pair; - if (map == NULL) { - return 0; - } + if (map == NULL) { + return 0; + } - if (key == NULL) { - return 0; - } + if (key == NULL) { + return 0; + } - index = hash(key) % map->count; - bucket = &(map->buckets[index]); - pair = get_pair(bucket, key); + index = hash(key) % map->count; + bucket = &(map->buckets[index]); + pair = get_pair(bucket, key); - if (pair == NULL) { - return 0; - } + if (pair == NULL) { + return 0; + } - if (out_buf == NULL && n_out_buf == 0) { - return strlen(pair->value) + 1; - } + if (out_buf == NULL && n_out_buf == 0) { + return strlen(pair->value) + 1; + } - if (out_buf == NULL) { - return 0; - } + if (out_buf == NULL) { + return 0; + } - if (strlen(pair->value) >= n_out_buf) { - return 0; - } + if (strlen(pair->value) >= n_out_buf) { + return 0; + } - strcpy(out_buf, pair->value); - return 1; + strcpy(out_buf, pair->value); + return 1; } -int sm_exists(const StrMap *map, const char *key) -{ - unsigned int index; - Bucket *bucket; - Pair *pair; +int sm_exists(const StrMap *map, const char *key) { + unsigned int index; + Bucket *bucket; + Pair *pair; - if (map == NULL) { - return 0; - } + if (map == NULL) { + return 0; + } - if (key == NULL) { - return 0; - } + if (key == NULL) { + return 0; + } - index = hash(key) % map->count; - bucket = &(map->buckets[index]); - pair = get_pair(bucket, key); + index = hash(key) % map->count; + bucket = &(map->buckets[index]); + pair = get_pair(bucket, key); - if (pair == NULL) { - return 0; - } + if (pair == NULL) { + return 0; + } - return 1; + return 1; } -int sm_put(StrMap *map, const char *key, const char *value) -{ - unsigned int key_len, value_len, index; - Bucket *bucket; - Pair *tmp_pairs, *pair; - char *tmp_value; - char *new_key, *new_value; - - if (map == NULL) { - return 0; - } - - if (key == NULL || value == NULL) { - return 0; - } - - key_len = strlen(key); - value_len = strlen(value); - /* Get a pointer to the bucket the key string hashes to */ - index = hash(key) % map->count; - bucket = &(map->buckets[index]); - - /* Check if we can handle insertion by simply replacing - * an existing value in a key-value pair in the bucket. - */ - if ((pair = get_pair(bucket, key)) != NULL) { - /* The bucket contains a pair that matches the provided key, - * change the value for that pair to the new value. - */ - if (strlen(pair->value) < value_len) { - /* If the new value is larger than the old value, re-allocate - * space for the new larger value. - */ - tmp_value = (char*)realloc(pair->value, (value_len + 1) * sizeof(char)); - - if (tmp_value == NULL) { - return 0; - } - - pair->value = tmp_value; - } - - /* Copy the new value into the pair that matches the key */ - strcpy(pair->value, value); - return 1; - } - - /* Allocate space for a new key and value */ - new_key = (char*)malloc((key_len + 1) * sizeof(char)); - - if (new_key == NULL) { - return 0; - } - - new_value = (char*)malloc((value_len + 1) * sizeof(char)); - - if (new_value == NULL) { - free(new_key); - return 0; - } - - /* Create a key-value pair */ - if (bucket->count == 0) { - /* The bucket is empty, lazily allocate space for a single - * key-value pair. - */ - bucket->pairs = (Pair*)malloc(sizeof(Pair)); - - if (bucket->pairs == NULL) { - free(new_key); - free(new_value); - return 0; - } - - bucket->count = 1; - - } else { - /* The bucket wasn't empty but no pair existed that matches the provided - * key, so create a new key-value pair. - */ - tmp_pairs = (Pair*)realloc(bucket->pairs, (bucket->count + 1) * sizeof(Pair)); - - if (tmp_pairs == NULL) { - free(new_key); - free(new_value); - return 0; - } - - bucket->pairs = tmp_pairs; - bucket->count++; - } - - /* Get the last pair in the chain for the bucket */ - pair = &(bucket->pairs[bucket->count - 1]); - pair->key = new_key; - pair->value = new_value; - /* Copy the key and its value into the key-value pair */ - strcpy(pair->key, key); - strcpy(pair->value, value); - return 1; +int sm_put(StrMap *map, const char *key, const char *value) { + unsigned int key_len, value_len, index; + Bucket *bucket; + Pair *tmp_pairs, *pair; + char *tmp_value; + char *new_key, *new_value; + + if (map == NULL) { + return 0; + } + + if (key == NULL || value == NULL) { + return 0; + } + + key_len = strlen(key); + value_len = strlen(value); + /* Get a pointer to the bucket the key string hashes to */ + index = hash(key) % map->count; + bucket = &(map->buckets[index]); + + /* Check if we can handle insertion by simply replacing + * an existing value in a key-value pair in the bucket. + */ + if ((pair = get_pair(bucket, key)) != NULL) { + /* The bucket contains a pair that matches the provided key, + * change the value for that pair to the new value. + */ + if (strlen(pair->value) < value_len) { + /* If the new value is larger than the old value, re-allocate + * space for the new larger value. + */ + tmp_value = (char*) realloc(pair->value, + (value_len + 1) * sizeof(char)); + + if (tmp_value == NULL) { + return 0; + } + + pair->value = tmp_value; + } + + /* Copy the new value into the pair that matches the key */ + strcpy(pair->value, value); + return 1; + } + + /* Allocate space for a new key and value */ + new_key = (char*) malloc((key_len + 1) * sizeof(char)); + + if (new_key == NULL) { + return 0; + } + + new_value = (char*) malloc((value_len + 1) * sizeof(char)); + + if (new_value == NULL) { + free(new_key); + return 0; + } + + /* Create a key-value pair */ + if (bucket->count == 0) { + /* The bucket is empty, lazily allocate space for a single + * key-value pair. + */ + bucket->pairs = (Pair*) malloc(sizeof(Pair)); + + if (bucket->pairs == NULL) { + free(new_key); + free(new_value); + return 0; + } + + bucket->count = 1; + + } else { + /* The bucket wasn't empty but no pair existed that matches the provided + * key, so create a new key-value pair. + */ + tmp_pairs = (Pair*) realloc(bucket->pairs, + (bucket->count + 1) * sizeof(Pair)); + + if (tmp_pairs == NULL) { + free(new_key); + free(new_value); + return 0; + } + + bucket->pairs = tmp_pairs; + bucket->count++; + } + + /* Get the last pair in the chain for the bucket */ + pair = &(bucket->pairs[bucket->count - 1]); + pair->key = new_key; + pair->value = new_value; + /* Copy the key and its value into the key-value pair */ + strcpy(pair->key, key); + strcpy(pair->value, value); + return 1; } -int sm_get_count(const StrMap *map) -{ - unsigned int i, j, n, m; - unsigned int count; - Bucket *bucket; - Pair *pair; - - if (map == NULL) { - return 0; - } - - bucket = map->buckets; - n = map->count; - i = 0; - count = 0; - - while (i < n) { - pair = bucket->pairs; - m = bucket->count; - j = 0; - - while (j < m) { - count++; - pair++; - j++; - } - - bucket++; - i++; - } - - return count; +int sm_get_count(const StrMap *map) { + unsigned int i, j, n, m; + unsigned int count; + Bucket *bucket; + Pair *pair; + + if (map == NULL) { + return 0; + } + + bucket = map->buckets; + n = map->count; + i = 0; + count = 0; + + while (i < n) { + pair = bucket->pairs; + m = bucket->count; + j = 0; + + while (j < m) { + count++; + pair++; + j++; + } + + bucket++; + i++; + } + + return count; } -int sm_enum(const StrMap *map, sm_enum_func enum_func, const void *obj) -{ - unsigned int i, j, n, m; - Bucket *bucket; - Pair *pair; +int sm_enum(const StrMap *map, sm_enum_func enum_func, const void *obj) { + unsigned int i, j, n, m; + Bucket *bucket; + Pair *pair; - if (map == NULL) { - return 0; - } + if (map == NULL) { + return 0; + } - if (enum_func == NULL) { - return 0; - } + if (enum_func == NULL) { + return 0; + } - bucket = map->buckets; - n = map->count; - i = 0; + bucket = map->buckets; + n = map->count; + i = 0; - while (i < n) { - pair = bucket->pairs; - m = bucket->count; - j = 0; + while (i < n) { + pair = bucket->pairs; + m = bucket->count; + j = 0; - while (j < m) { - enum_func(pair->key, pair->value, obj); - pair++; - j++; - } + while (j < m) { + enum_func(pair->key, pair->value, obj); + pair++; + j++; + } - bucket++; - i++; - } + bucket++; + i++; + } - return 1; + return 1; } /* * Returns a pair from the bucket that matches the provided key, * or null if no such pair exist. */ -static Pair * get_pair(Bucket *bucket, const char *key) -{ - unsigned int i, n; - Pair *pair; +static Pair* get_pair(Bucket *bucket, const char *key) { + unsigned int i, n; + Pair *pair; - n = bucket->count; + n = bucket->count; - if (n == 0) { - return NULL; - } + if (n == 0) { + return NULL; + } - pair = bucket->pairs; - i = 0; + pair = bucket->pairs; + i = 0; - while (i < n) { - if (pair->key != NULL && pair->value != NULL) { - if (strcmp(pair->key, key) == 0) { - return pair; - } - } + while (i < n) { + if (pair->key != NULL && pair->value != NULL) { + if (strcmp(pair->key, key) == 0) { + return pair; + } + } - pair++; - i++; - } + pair++; + i++; + } - return NULL; + return NULL; } /* * Returns a hash code for the provided string. */ -static unsigned long hash(const char *str) -{ - unsigned long hash = 5381; - int c; +static unsigned long hash(const char *str) { + unsigned long hash = 5381; + int c; - while ((c = *str++)) { - hash = ((hash << 5) + hash) + c; - } + while ((c = *str++)) { + hash = ((hash << 5) + hash) + c; + } - return hash; + return hash; } /* - GNU LESSER GENERAL PUBLIC LICENSE - Version 3, 29 June 2007 + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. - This version of the GNU Lesser General Public License incorporates -the terms and conditions of version 3 of the GNU General Public -License, supplemented by the additional permissions listed below. + This version of the GNU Lesser General Public License incorporates + the terms and conditions of version 3 of the GNU General Public + License, supplemented by the additional permissions listed below. - 0. Additional Definitions. + 0. Additional Definitions. - As used herein, "this License" refers to version 3 of the GNU Lesser -General Public License, and the "GNU GPL" refers to version 3 of the GNU -General Public License. + As used herein, "this License" refers to version 3 of the GNU Lesser + General Public License, and the "GNU GPL" refers to version 3 of the GNU + General Public License. - "The Library" refers to a covered work governed by this License, -other than an Application or a Combined Work as defined below. + "The Library" refers to a covered work governed by this License, + other than an Application or a Combined Work as defined below. - An "Application" is any work that makes use of an interface provided -by the Library, but which is not otherwise based on the Library. -Defining a subclass of a class defined by the Library is deemed a mode -of using an interface provided by the Library. - - A "Combined Work" is a work produced by combining or linking an -Application with the Library. The particular version of the Library -with which the Combined Work was made is also called the "Linked -Version". - - The "Minimal Corresponding Source" for a Combined Work means the -Corresponding Source for the Combined Work, excluding any source code -for portions of the Combined Work that, considered in isolation, are -based on the Application, and not on the Linked Version. - - The "Corresponding Application Code" for a Combined Work means the -object code and/or source code for the Application, including any data -and utility programs needed for reproducing the Combined Work from the -Application, but excluding the System Libraries of the Combined Work. - - 1. Exception to Section 3 of the GNU GPL. - - You may convey a covered work under sections 3 and 4 of this License -without being bound by section 3 of the GNU GPL. - - 2. Conveying Modified Versions. - - If you modify a copy of the Library, and, in your modifications, a -facility refers to a function or data to be supplied by an Application -that uses the facility (other than as an argument passed when the -facility is invoked), then you may convey a copy of the modified -version: - - a) under this License, provided that you make a good faith effort to - ensure that, in the event an Application does not supply the - function or data, the facility still operates, and performs - whatever part of its purpose remains meaningful, or - - b) under the GNU GPL, with none of the additional permissions of - this License applicable to that copy. - - 3. Object Code Incorporating Material from Library Header Files. - - The object code form of an Application may incorporate material from -a header file that is part of the Library. You may convey such object -code under terms of your choice, provided that, if the incorporated -material is not limited to numerical parameters, data structure -layouts and accessors, or small macros, inline functions and templates -(ten or fewer lines in length), you do both of the following: - - a) Give prominent notice with each copy of the object code that the - Library is used in it and that the Library and its use are - covered by this License. - - b) Accompany the object code with a copy of the GNU GPL and this license - document. - - 4. Combined Works. - - You may convey a Combined Work under terms of your choice that, -taken together, effectively do not restrict modification of the -portions of the Library contained in the Combined Work and reverse -engineering for debugging such modifications, if you also do each of -the following: - - a) Give prominent notice with each copy of the Combined Work that - the Library is used in it and that the Library and its use are - covered by this License. - - b) Accompany the Combined Work with a copy of the GNU GPL and this license - document. - - c) For a Combined Work that displays copyright notices during - execution, include the copyright notice for the Library among - these notices, as well as a reference directing the user to the - copies of the GNU GPL and this license document. - - d) Do one of the following: - - 0) Convey the Minimal Corresponding Source under the terms of this - License, and the Corresponding Application Code in a form - suitable for, and under terms that permit, the user to - recombine or relink the Application with a modified version of - the Linked Version to produce a modified Combined Work, in the - manner specified by section 6 of the GNU GPL for conveying - Corresponding Source. - - 1) Use a suitable shared library mechanism for linking with the - Library. A suitable mechanism is one that (a) uses at run time - a copy of the Library already present on the user's computer - system, and (b) will operate properly with a modified version - of the Library that is interface-compatible with the Linked - Version. - - e) Provide Installation Information, but only if you would otherwise - be required to provide such information under section 6 of the - GNU GPL, and only to the extent that such information is - necessary to install and execute a modified version of the - Combined Work produced by recombining or relinking the - Application with a modified version of the Linked Version. (If - you use option 4d0, the Installation Information must accompany - the Minimal Corresponding Source and Corresponding Application - Code. If you use option 4d1, you must provide the Installation - Information in the manner specified by section 6 of the GNU GPL - for conveying Corresponding Source.) - - 5. Combined Libraries. - - You may place library facilities that are a work based on the -Library side by side in a single library together with other library -facilities that are not Applications and are not covered by this -License, and convey such a combined library under terms of your -choice, if you do both of the following: - - a) Accompany the combined library with a copy of the same work based - on the Library, uncombined with any other library facilities, - conveyed under the terms of this License. - - b) Give prominent notice with the combined library that part of it - is a work based on the Library, and explaining where to find the - accompanying uncombined form of the same work. - - 6. Revised Versions of the GNU Lesser General Public License. - - The Free Software Foundation may publish revised and/or new versions -of the GNU Lesser General Public License from time to time. Such new -versions will be similar in spirit to the present version, but may -differ in detail to address new problems or concerns. - - Each version is given a distinguishing version number. If the -Library as you received it specifies that a certain numbered version -of the GNU Lesser General Public License "or any later version" -applies to it, you have the option of following the terms and -conditions either of that published version or of any later version -published by the Free Software Foundation. If the Library as you -received it does not specify a version number of the GNU Lesser -General Public License, you may choose any version of the GNU Lesser -General Public License ever published by the Free Software Foundation. - - If the Library as you received it specifies that a proxy can decide -whether future versions of the GNU Lesser General Public License shall -apply, that proxy's public statement of acceptance of any version is -permanent authorization for you to choose that version for the -Library. - -*/ + An "Application" is any work that makes use of an interface provided + by the Library, but which is not otherwise based on the Library. + Defining a subclass of a class defined by the Library is deemed a mode + of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an + Application with the Library. The particular version of the Library + with which the Combined Work was made is also called the "Linked + Version". + + The "Minimal Corresponding Source" for a Combined Work means the + Corresponding Source for the Combined Work, excluding any source code + for portions of the Combined Work that, considered in isolation, are + based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the + object code and/or source code for the Application, including any data + and utility programs needed for reproducing the Combined Work from the + Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License + without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a + facility refers to a function or data to be supplied by an Application + that uses the facility (other than as an argument passed when the + facility is invoked), then you may convey a copy of the modified + version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from + a header file that is part of the Library. You may convey such object + code under terms of your choice, provided that, if the incorporated + material is not limited to numerical parameters, data structure + layouts and accessors, or small macros, inline functions and templates + (ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, + taken together, effectively do not restrict modification of the + portions of the Library contained in the Combined Work and reverse + engineering for debugging such modifications, if you also do each of + the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the + Library side by side in a single library together with other library + facilities that are not Applications and are not covered by this + License, and convey such a combined library under terms of your + choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions + of the GNU Lesser General Public License from time to time. Such new + versions will be similar in spirit to the present version, but may + differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the + Library as you received it specifies that a certain numbered version + of the GNU Lesser General Public License "or any later version" + applies to it, you have the option of following the terms and + conditions either of that published version or of any later version + published by the Free Software Foundation. If the Library as you + received it does not specify a version number of the GNU Lesser + General Public License, you may choose any version of the GNU Lesser + General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide + whether future versions of the GNU Lesser General Public License shall + apply, that proxy's public statement of acceptance of any version is + permanent authorization for you to choose that version for the + Library. + + */ diff --git a/src/strmap.h b/src/strmap.h index 416a1ce..6bc61d3 100644 --- a/src/strmap.h +++ b/src/strmap.h @@ -37,8 +37,7 @@ #define _STRMAP_H_ #ifdef __cplusplus -extern "C" -{ +extern "C" { #endif #include @@ -186,157 +185,157 @@ int sm_enum(const StrMap *map, sm_enum_func enum_func, const void *obj); /* - GNU LESSER GENERAL PUBLIC LICENSE - Version 3, 29 June 2007 + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. - This version of the GNU Lesser General Public License incorporates -the terms and conditions of version 3 of the GNU General Public -License, supplemented by the additional permissions listed below. + This version of the GNU Lesser General Public License incorporates + the terms and conditions of version 3 of the GNU General Public + License, supplemented by the additional permissions listed below. - 0. Additional Definitions. + 0. Additional Definitions. - As used herein, "this License" refers to version 3 of the GNU Lesser -General Public License, and the "GNU GPL" refers to version 3 of the GNU -General Public License. + As used herein, "this License" refers to version 3 of the GNU Lesser + General Public License, and the "GNU GPL" refers to version 3 of the GNU + General Public License. - "The Library" refers to a covered work governed by this License, -other than an Application or a Combined Work as defined below. + "The Library" refers to a covered work governed by this License, + other than an Application or a Combined Work as defined below. - An "Application" is any work that makes use of an interface provided -by the Library, but which is not otherwise based on the Library. -Defining a subclass of a class defined by the Library is deemed a mode -of using an interface provided by the Library. + An "Application" is any work that makes use of an interface provided + by the Library, but which is not otherwise based on the Library. + Defining a subclass of a class defined by the Library is deemed a mode + of using an interface provided by the Library. - A "Combined Work" is a work produced by combining or linking an -Application with the Library. The particular version of the Library -with which the Combined Work was made is also called the "Linked -Version". + A "Combined Work" is a work produced by combining or linking an + Application with the Library. The particular version of the Library + with which the Combined Work was made is also called the "Linked + Version". - The "Minimal Corresponding Source" for a Combined Work means the -Corresponding Source for the Combined Work, excluding any source code -for portions of the Combined Work that, considered in isolation, are -based on the Application, and not on the Linked Version. + The "Minimal Corresponding Source" for a Combined Work means the + Corresponding Source for the Combined Work, excluding any source code + for portions of the Combined Work that, considered in isolation, are + based on the Application, and not on the Linked Version. - The "Corresponding Application Code" for a Combined Work means the -object code and/or source code for the Application, including any data -and utility programs needed for reproducing the Combined Work from the -Application, but excluding the System Libraries of the Combined Work. + The "Corresponding Application Code" for a Combined Work means the + object code and/or source code for the Application, including any data + and utility programs needed for reproducing the Combined Work from the + Application, but excluding the System Libraries of the Combined Work. - 1. Exception to Section 3 of the GNU GPL. + 1. Exception to Section 3 of the GNU GPL. - You may convey a covered work under sections 3 and 4 of this License + You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL. - 2. Conveying Modified Versions. +2. Conveying Modified Versions. - If you modify a copy of the Library, and, in your modifications, a +If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the -facility is invoked), then you may convey a copy of the modified + facility is invoked), then you may convey a copy of the modified version: - a) under this License, provided that you make a good faith effort to - ensure that, in the event an Application does not supply the - function or data, the facility still operates, and performs - whatever part of its purpose remains meaningful, or +a) under this License, provided that you make a good faith effort to +ensure that, in the event an Application does not supply the +function or data, the facility still operates, and performs +whatever part of its purpose remains meaningful, or - b) under the GNU GPL, with none of the additional permissions of - this License applicable to that copy. +b) under the GNU GPL, with none of the additional permissions of +this License applicable to that copy. - 3. Object Code Incorporating Material from Library Header Files. +3. Object Code Incorporating Material from Library Header Files. - The object code form of an Application may incorporate material from -a header file that is part of the Library. You may convey such object +The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: - a) Give prominent notice with each copy of the object code that the - Library is used in it and that the Library and its use are - covered by this License. +a) Give prominent notice with each copy of the object code that the +Library is used in it and that the Library and its use are +covered by this License. - b) Accompany the object code with a copy of the GNU GPL and this license - document. +b) Accompany the object code with a copy of the GNU GPL and this license +document. - 4. Combined Works. +4. Combined Works. - You may convey a Combined Work under terms of your choice that, +You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following: - a) Give prominent notice with each copy of the Combined Work that - the Library is used in it and that the Library and its use are - covered by this License. - - b) Accompany the Combined Work with a copy of the GNU GPL and this license - document. - - c) For a Combined Work that displays copyright notices during - execution, include the copyright notice for the Library among - these notices, as well as a reference directing the user to the - copies of the GNU GPL and this license document. - - d) Do one of the following: - - 0) Convey the Minimal Corresponding Source under the terms of this - License, and the Corresponding Application Code in a form - suitable for, and under terms that permit, the user to - recombine or relink the Application with a modified version of - the Linked Version to produce a modified Combined Work, in the - manner specified by section 6 of the GNU GPL for conveying - Corresponding Source. - - 1) Use a suitable shared library mechanism for linking with the - Library. A suitable mechanism is one that (a) uses at run time - a copy of the Library already present on the user's computer - system, and (b) will operate properly with a modified version - of the Library that is interface-compatible with the Linked - Version. - - e) Provide Installation Information, but only if you would otherwise - be required to provide such information under section 6 of the - GNU GPL, and only to the extent that such information is - necessary to install and execute a modified version of the - Combined Work produced by recombining or relinking the - Application with a modified version of the Linked Version. (If - you use option 4d0, the Installation Information must accompany - the Minimal Corresponding Source and Corresponding Application - Code. If you use option 4d1, you must provide the Installation - Information in the manner specified by section 6 of the GNU GPL - for conveying Corresponding Source.) - - 5. Combined Libraries. - - You may place library facilities that are a work based on the +a) Give prominent notice with each copy of the Combined Work that +the Library is used in it and that the Library and its use are +covered by this License. + +b) Accompany the Combined Work with a copy of the GNU GPL and this license +document. + +c) For a Combined Work that displays copyright notices during +execution, include the copyright notice for the Library among +these notices, as well as a reference directing the user to the +copies of the GNU GPL and this license document. + +d) Do one of the following: + +0) Convey the Minimal Corresponding Source under the terms of this +License, and the Corresponding Application Code in a form +suitable for, and under terms that permit, the user to +recombine or relink the Application with a modified version of +the Linked Version to produce a modified Combined Work, in the +manner specified by section 6 of the GNU GPL for conveying +Corresponding Source. + +1) Use a suitable shared library mechanism for linking with the +Library. A suitable mechanism is one that (a) uses at run time +a copy of the Library already present on the user's computer +system, and (b) will operate properly with a modified version +of the Library that is interface-compatible with the Linked +Version. + +e) Provide Installation Information, but only if you would otherwise +be required to provide such information under section 6 of the +GNU GPL, and only to the extent that such information is +necessary to install and execute a modified version of the +Combined Work produced by recombining or relinking the +Application with a modified version of the Linked Version. (If +you use option 4d0, the Installation Information must accompany +the Minimal Corresponding Source and Corresponding Application +Code. If you use option 4d1, you must provide the Installation +Information in the manner specified by section 6 of the GNU GPL +for conveying Corresponding Source.) + +5. Combined Libraries. + +You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following: - a) Accompany the combined library with a copy of the same work based - on the Library, uncombined with any other library facilities, - conveyed under the terms of this License. +a) Accompany the combined library with a copy of the same work based +on the Library, uncombined with any other library facilities, +conveyed under the terms of this License. - b) Give prominent notice with the combined library that part of it - is a work based on the Library, and explaining where to find the - accompanying uncombined form of the same work. +b) Give prominent notice with the combined library that part of it +is a work based on the Library, and explaining where to find the +accompanying uncombined form of the same work. - 6. Revised Versions of the GNU Lesser General Public License. +6. Revised Versions of the GNU Lesser General Public License. - The Free Software Foundation may publish revised and/or new versions +The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. - Each version is given a distinguishing version number. If the +Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License "or any later version" applies to it, you have the option of following the terms and @@ -346,7 +345,7 @@ received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation. - If the Library as you received it specifies that a proxy can decide +If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the diff --git a/src/util.c b/src/util.c index 136bcc6..a44d7b6 100644 --- a/src/util.c +++ b/src/util.c @@ -5,17 +5,16 @@ #include "global.h" -void mbp_log(int level, const char *fmt, ...) -{ - va_list args; - if (daemonize) { - va_start(args, fmt); - vsyslog(level, fmt, args); - va_end(args); - } +void mbp_log(int level, const char *fmt, ...) { + va_list args; + if (daemonize) { + va_start(args, fmt); + vsyslog(level, fmt, args); + va_end(args); + } - va_start(args, fmt); - vprintf(fmt, args); - puts(""); // trailing newline - va_end(args); + va_start(args, fmt); + vprintf(fmt, args); + puts(""); // trailing newline + va_end(args); } diff --git a/src/util.h b/src/util.h index 9bf99ce..7bb9913 100644 --- a/src/util.h +++ b/src/util.h @@ -1,6 +1,7 @@ #ifndef _UTIL_H_ #define _UTIL_H_ -void mbp_log(int level, const char *fmt, ...) __attribute__ ((format (printf, 2, 3))); +void mbp_log(int level, const char *fmt, ...) + __attribute__ ((format (printf, 2, 3))); #endif diff --git a/tests/main.c b/tests/main.c index 2870554..1e16a96 100644 --- a/tests/main.c +++ b/tests/main.c @@ -1,7 +1,6 @@ #include "minunit.h" -int main(int argc, char *argv[]) -{ - (void)argc; - tests(argv[0]); +int main(int argc, char *argv[]) { + (void) argc; + tests(argv[0]); } diff --git a/tests/minunit.c b/tests/minunit.c index 5a54299..a1e606b 100644 --- a/tests/minunit.c +++ b/tests/minunit.c @@ -13,263 +13,255 @@ int tests_run = 0; -static void free_fans(t_fans* fans) -{ - while (fans != NULL) { - t_fans* tmp = fans->next; - free(fans->fan_manual_path); - free(fans->fan_output_path); - free(fans->label); - free(fans); - fans = tmp; - } +static void free_fans(t_fans *fans) { + while (fans != NULL) { + t_fans *tmp = fans->next; + free(fans->fan_manual_path); + free(fans->fan_output_path); + free(fans->label); + free(fans); + fans = tmp; + } } -static void free_sensors(t_sensors* sensors) -{ - while (sensors != NULL) { - t_sensors* tmp = sensors->next; - free(sensors->path); - free(sensors); - sensors = tmp; - } +static void free_sensors(t_sensors *sensors) { + while (sensors != NULL) { + t_sensors *tmp = sensors->next; + free(sensors->path); + free(sensors); + sensors = tmp; + } } -static const char *test_sensor_paths() -{ - t_sensors* sensors = retrieve_sensors(); - mu_assert("No sensors found", sensors != NULL); - t_sensors* tmp = sensors; +static const char* test_sensor_paths() { + t_sensors *sensors = retrieve_sensors(); + mu_assert("No sensors found", sensors != NULL); + t_sensors *tmp = sensors; - while(tmp != NULL) { - mu_assert("Sensor does not have a valid path", tmp->path != NULL); + while (tmp != NULL) { + mu_assert("Sensor does not have a valid path", tmp->path != NULL); - if(tmp->path != NULL) { - mu_assert("Sensor does not have valid temperature", tmp->temperature > 0); - } + if (tmp->path != NULL) { + mu_assert("Sensor does not have valid temperature", + tmp->temperature > 0); + } - tmp = tmp->next; - } + tmp = tmp->next; + } - free_sensors(sensors); - return 0; + free_sensors(sensors); + return 0; } +static const char* test_fan_paths() { + t_fans *fans = retrieve_fans(); + mu_assert("No fans found", fans != NULL); + t_fans *tmp = fans; + int found_fan_path = 0; -static const char *test_fan_paths() -{ - t_fans* fans = retrieve_fans(); - mu_assert("No fans found", fans != NULL); - t_fans* tmp = fans; - int found_fan_path = 0; + while (tmp != NULL) { + if (tmp->fan_output_path != NULL) { + found_fan_path++; + } - while(tmp != NULL) { - if(tmp->fan_output_path != NULL) { - found_fan_path++; - } + tmp = tmp->next; + } - tmp = tmp->next; - } - - mu_assert("No fans found", found_fan_path != 0); - free_fans(fans); - return 0; + mu_assert("No fans found", found_fan_path != 0); + free_fans(fans); + return 0; } -unsigned time_seed() -{ - time_t now = time ( 0 ); - unsigned char *p = (unsigned char *)&now; - unsigned seed = 0; - size_t i; +unsigned time_seed() { + time_t now = time(0); + unsigned char *p = (unsigned char*) &now; + unsigned seed = 0; + size_t i; - for ( i = 0; i < sizeof now; i++ ) { - seed = seed * ( UCHAR_MAX + 2U ) + p[i]; - } + for (i = 0; i < sizeof now; i++) { + seed = seed * ( UCHAR_MAX + 2U) + p[i]; + } - return seed; + return seed; } // nothing better than a horrible piece of code to // stress a little bit the CPU -int stress(int n) -{ - int f = n; - - while (f > 0) { - while(n > 0) { - srand ( time_seed() ); - n--; - } - - f--; - n = f; - } - return 0; +int stress(int n) { + int f = n; + + while (f > 0) { + while (n > 0) { + srand(time_seed()); + n--; + } + + f--; + n = f; + } + return 0; } -static const char *test_get_temp() -{ - t_sensors* sensors = retrieve_sensors(); - mu_assert("No sensors found", sensors != NULL); - unsigned short temp_1 = get_temp(sensors); - mu_assert("Invalid Global Temperature Found", temp_1 > 1 && temp_1 < 150); - stress(2000); - unsigned short temp_2 = get_temp(sensors); - mu_assert("Invalid Higher temp test (if fan was already spinning high, this is not worrying)", temp_1 < temp_2); - free_sensors(sensors); - return 0; +static const char* test_get_temp() { + t_sensors *sensors = retrieve_sensors(); + mu_assert("No sensors found", sensors != NULL); + unsigned short temp_1 = get_temp(sensors); + mu_assert("Invalid Global Temperature Found", temp_1 > 1 && temp_1 < 150); + stress(2000); + unsigned short temp_2 = get_temp(sensors); + mu_assert( + "Invalid Higher temp test (if fan was already spinning high, this is not worrying)", + temp_1 < temp_2); + free_sensors(sensors); + return 0; } -static const char *test_config_file() -{ - FILE *f = NULL; - Settings *settings = NULL; - f = fopen("/etc/mbpfan.conf", "r"); - mu_assert("No config file found", f != NULL); - - if (f == NULL) { - return 0; - } - - settings = settings_open(f); - fclose(f); - mu_assert("Could not read settings from config file", settings != NULL); - - if (settings == NULL) { - return 0; - } - - mu_assert("Could not read low_temp from config file",settings_get_int(settings, "general", "low_temp") != 0); - mu_assert("Could not read high_temp from config file",settings_get_int(settings, "general", "high_temp") != 0); - mu_assert("Could not read max_temp from config file",settings_get_int(settings, "general", "max_temp") != 0); - mu_assert("Could not read polling_interval from config file",settings_get_int(settings, "general", "polling_interval") != 0); - /* Destroy the settings object */ - settings_delete(settings); - - return 0; +static const char* test_config_file() { + FILE *f = NULL; + Settings *settings = NULL; + f = fopen("/etc/mbpfan.conf", "r"); + mu_assert("No config file found", f != NULL); + + if (f == NULL) { + return 0; + } + + settings = settings_open(f); + fclose(f); + mu_assert("Could not read settings from config file", settings != NULL); + + if (settings == NULL) { + return 0; + } + + mu_assert("Could not read low_temp from config file", + settings_get_int(settings, "general", "low_temp") != 0); + mu_assert("Could not read high_temp from config file", + settings_get_int(settings, "general", "high_temp") != 0); + mu_assert("Could not read max_temp from config file", + settings_get_int(settings, "general", "max_temp") != 0); + mu_assert("Could not read polling_interval from config file", + settings_get_int(settings, "general", "polling_interval") != 0); + /* Destroy the settings object */ + settings_delete(settings); + + return 0; } -static const char *test_settings() -{ - t_fans* fan = (t_fans *) malloc( sizeof( t_fans ) ); - fan->fan_id = 1; - fan->fan_max_speed = -1; - fan->next = NULL; - - retrieve_settings("./mbpfan.conf.test1", fan); - // choosing the maximum for iMac mid 2011 - mu_assert("max_fan_speed value is not 2600", fan->fan_max_speed == 2600); - mu_assert("polling_interval is not 2", polling_interval == 2); - - fan->fan_min_speed = -1; - retrieve_settings("./mbpfan.conf.test0", fan); - mu_assert("min_fan_speed value is not 2000", fan->fan_min_speed == 2000); - mu_assert("polling_interval is not 7", polling_interval == 7); - - t_fans* fan2 = (t_fans *)malloc(sizeof(t_fans)); - fan2->fan_id = 2; - fan2->fan_max_speed = -1; - fan2->next = NULL; - fan->next = fan2; - - retrieve_settings("./mbpfan.conf.test2", fan); - mu_assert("min_fan1_speed value is not 2000", fan->fan_min_speed == 2000); - mu_assert("min_fan2_speed value is not 2000", fan->next->fan_min_speed == 2000); - - free(fan2); - fan->next = NULL; - free(fan); - - return 0; +static const char* test_settings() { + t_fans *fan = (t_fans*) malloc(sizeof(t_fans)); + fan->fan_id = 1; + fan->fan_max_speed = -1; + fan->next = NULL; + + retrieve_settings("./mbpfan.conf.test1", fan); + // choosing the maximum for iMac mid 2011 + mu_assert("max_fan_speed value is not 2600", fan->fan_max_speed == 2600); + mu_assert("polling_interval is not 2", polling_interval == 2); + + fan->fan_min_speed = -1; + retrieve_settings("./mbpfan.conf.test0", fan); + mu_assert("min_fan_speed value is not 2000", fan->fan_min_speed == 2000); + mu_assert("polling_interval is not 7", polling_interval == 7); + + t_fans *fan2 = (t_fans*) malloc(sizeof(t_fans)); + fan2->fan_id = 2; + fan2->fan_max_speed = -1; + fan2->next = NULL; + fan->next = fan2; + + retrieve_settings("./mbpfan.conf.test2", fan); + mu_assert("min_fan1_speed value is not 2000", fan->fan_min_speed == 2000); + mu_assert("min_fan2_speed value is not 2000", + fan->next->fan_min_speed == 2000); + + free(fan2); + fan->next = NULL; + free(fan); + + return 0; } int received = 0; -static void handler(int signal) -{ - t_fans* fan = (t_fans *) malloc( sizeof( t_fans ) ); - fan->fan_id = 1; - fan->next = NULL; - - - switch(signal) { - case SIGHUP: - received = 1; - retrieve_settings("./mbpfan.conf.test1", fan); - free(fan); - break; - - default: - received = 0; - free(fan); - break; - } +static void handler(int signal) { + t_fans *fan = (t_fans*) malloc(sizeof(t_fans)); + fan->fan_id = 1; + fan->next = NULL; + + switch (signal) { + case SIGHUP: + received = 1; + retrieve_settings("./mbpfan.conf.test1", fan); + free(fan); + break; + + default: + received = 0; + free(fan); + break; + } } -static const char *test_sighup_receive() -{ - signal(SIGHUP, handler); - raise(SIGHUP); - mu_assert("did not receive SIGHUP signal", received == 1); - return 0; +static const char* test_sighup_receive() { + signal(SIGHUP, handler); + raise(SIGHUP); + mu_assert("did not receive SIGHUP signal", received == 1); + return 0; } -static const char *test_settings_reload() -{ - t_fans* fan = (t_fans *) malloc( sizeof( t_fans ) ); - fan->fan_id = 1; - fan->fan_min_speed = -1; - fan->fan_manual_path = NULL; - fan->fan_output_path = NULL; - fan->label = NULL; - fan->next = NULL; - - signal(SIGHUP, handler); - retrieve_settings("./mbpfan.conf", fan); - printf("Testing the _supplied_ mbpfan.conf (not the one you are using)..\n"); - // cannot tests min_fan_speed since it is not set and thus auto-detected - mu_assert("polling_interval is not 1 before SIGHUP", polling_interval == 1); - raise(SIGHUP); - // cannot tests min_fan_speed since it is not set and thus auto-detected - mu_assert("polling_interval is not 2 after SIGHUP", polling_interval == 2); - retrieve_settings("./mbpfan.conf", fan); - free_fans(fan); - return 0; +static const char* test_settings_reload() { + t_fans *fan = (t_fans*) malloc(sizeof(t_fans)); + fan->fan_id = 1; + fan->fan_min_speed = -1; + fan->fan_manual_path = NULL; + fan->fan_output_path = NULL; + fan->label = NULL; + fan->next = NULL; + + signal(SIGHUP, handler); + retrieve_settings("./mbpfan.conf", fan); + printf( + "Testing the _supplied_ mbpfan.conf (not the one you are using)..\n"); + // cannot tests min_fan_speed since it is not set and thus auto-detected + mu_assert("polling_interval is not 1 before SIGHUP", polling_interval == 1); + raise(SIGHUP); + // cannot tests min_fan_speed since it is not set and thus auto-detected + mu_assert("polling_interval is not 2 after SIGHUP", polling_interval == 2); + retrieve_settings("./mbpfan.conf", fan); + free_fans(fan); + return 0; } - -static const char *all_tests() -{ - mu_run_test(test_sensor_paths); - mu_run_test(test_fan_paths); - mu_run_test(test_get_temp); - mu_run_test(test_config_file); - mu_run_test(test_settings); - mu_run_test(test_sighup_receive); - mu_run_test(test_settings_reload); - return 0; +static const char* all_tests() { + mu_run_test(test_sensor_paths); + mu_run_test(test_fan_paths); + mu_run_test(test_get_temp); + mu_run_test(test_config_file); + mu_run_test(test_settings); + mu_run_test(test_sighup_receive); + mu_run_test(test_settings_reload); + return 0; } -int tests(const char *program_path) -{ - verbose = 1; +int tests(const char *program_path) { + verbose = 1; + + check_requirements(program_path); - check_requirements(program_path); + printf("Starting the tests..\n"); + printf("It is normal for them to take a bit to finish.\n"); - printf("Starting the tests..\n"); - printf("It is normal for them to take a bit to finish.\n"); - - const char *result = all_tests(); + const char *result = all_tests(); - if (result != 0) { - printf("Error: %s \n", result); + if (result != 0) { + printf("Error: %s \n", result); - } else { - printf("ALL TESTS PASSED\n"); - } + } else { + printf("ALL TESTS PASSED\n"); + } - printf("Tests run: %d\n", tests_run); + printf("Tests run: %d\n", tests_run); - return result != 0; + return result != 0; } diff --git a/tests/minunit.h b/tests/minunit.h index c1ab00b..41ed0df 100644 --- a/tests/minunit.h +++ b/tests/minunit.h @@ -11,17 +11,16 @@ extern int tests_run; - -static const char *test_sensor_paths(); -static const char *test_fan_paths(); -static const char *test_get_temp(); -static const char *test_config_file(); -static const char *test_settings(); +static const char* test_sensor_paths(); +static const char* test_fan_paths(); +static const char* test_get_temp(); +static const char* test_config_file(); +static const char* test_settings(); static void handler(int signal); -static const char *test_sighup_receive(); -static const char *test_settings_reload(); -static const char *all_tests(); +static const char* test_sighup_receive(); +static const char* test_settings_reload(); +static const char* all_tests(); int tests(); -#endif \ No newline at end of file +#endif