From 9da4f5e95d67302f976ad07d001daa03f5399dae Mon Sep 17 00:00:00 2001 From: William Brawner Date: Tue, 31 Dec 2019 14:00:58 -0600 Subject: [PATCH] Fix macOS-specific code to be more generic --- PiHelper/cli.c | 13 ++++++------- PiHelper/cli.h | 5 ++--- PiHelper/network.c | 12 ++++++------ 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/PiHelper/cli.c b/PiHelper/cli.c index ea25cb8..4a0c07e 100644 --- a/PiHelper/cli.c +++ b/PiHelper/cli.c @@ -2,18 +2,17 @@ #include #include #include +#include #include "cli.h" -#include "log.h" -#include "network.h" #include "pihelper.h" int main(int argc, char ** argv) { bool configure, enable; - char * disable; + char * disable = NULL; char * config_path; char ch; - while ((ch = getopt_long(argc, argv, "cd:ef:hv", longopts, NULL)) != -1) { + while ((ch = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) { switch(ch) { case 'c': configure = true; @@ -90,16 +89,16 @@ int main(int argc, char ** argv) { } if (config == NULL) { - printf("Failed to parse Pi-Helper config\n"); + write_log(PIHELPER_LOG_ERROR, "Failed to parse Pi-Helper config at %s", config_path); exit(1); } - if (enable && disable != 0) { + if (enable && disable != NULL) { print_usage(); return PIHELPER_INVALID_COMMANDS; } else if (enable) { return enable_pihole(config); - } else if (disable != 0) { + } else if (disable != NULL) { return disable_pihole(config, disable); } else { return get_status(config); diff --git a/PiHelper/cli.h b/PiHelper/cli.h index 85f39e6..ba33c6b 100644 --- a/PiHelper/cli.h +++ b/PiHelper/cli.h @@ -1,5 +1,6 @@ #include -#include "config.h" + +static char * shortopts = "cd:ef:hqv"; static struct option longopts[] = { { "configure", no_argument, NULL, 'c' }, @@ -13,5 +14,3 @@ static struct option longopts[] = { void print_usage(); -pihole_config * configure_pihole(char * config_path); - diff --git a/PiHelper/network.c b/PiHelper/network.c index 95b2d61..143f049 100644 --- a/PiHelper/network.c +++ b/PiHelper/network.c @@ -12,9 +12,7 @@ static char * AUTH_QUERY = "auth"; static char * ENABLE_QUERY = "enable"; static char * DISABLE_QUERY = "disable"; static char * HTTP_SCHEME = "http://"; -static int HTTP_SCHEME_LEN = 8; static char * HTTPS_SCHEME = "https://"; -static int HTTPS_SCHEME_LEN = 9; int get_status(pihole_config * config) { char * formatted_host = prepend_scheme(config->host); @@ -89,15 +87,17 @@ static char * get(char endpoint[]) { } } -/* +/** * Given a potentially unformatted host (missing scheme), prepends the scheme (http://) to the host. Note * that the caller is responsible for freeing the memory allocated by this method. - * @return a pointer to the host with the scheme prepended + * @return a pointer to the host with the scheme prepended, or the given pointer if the host is already + * properly formatted. */ static char * prepend_scheme(char * raw_host) { + if (raw_host == NULL) return NULL; char * formatted_host; - if (strnstr(raw_host, HTTP_SCHEME, HTTP_SCHEME_LEN) == NULL - && strnstr(raw_host, HTTPS_SCHEME, HTTPS_SCHEME_LEN) == NULL) { + if (strstr(raw_host, HTTP_SCHEME) != raw_host + && strstr(raw_host, HTTPS_SCHEME) != raw_host) { formatted_host = malloc(URL_FORMAT_LEN + strlen(raw_host)); sprintf(formatted_host, URL_FORMAT, raw_host); } else {