Fix macOS-specific code to be more generic

This commit is contained in:
William Brawner 2019-12-31 14:00:58 -06:00
parent 6fa3948ea2
commit 9da4f5e95d
3 changed files with 14 additions and 16 deletions

View file

@ -2,18 +2,17 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include "cli.h" #include "cli.h"
#include "log.h"
#include "network.h"
#include "pihelper.h" #include "pihelper.h"
int main(int argc, char ** argv) { int main(int argc, char ** argv) {
bool configure, enable; bool configure, enable;
char * disable; char * disable = NULL;
char * config_path; char * config_path;
char ch; 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) { switch(ch) {
case 'c': case 'c':
configure = true; configure = true;
@ -90,16 +89,16 @@ int main(int argc, char ** argv) {
} }
if (config == NULL) { 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); exit(1);
} }
if (enable && disable != 0) { if (enable && disable != NULL) {
print_usage(); print_usage();
return PIHELPER_INVALID_COMMANDS; return PIHELPER_INVALID_COMMANDS;
} else if (enable) { } else if (enable) {
return enable_pihole(config); return enable_pihole(config);
} else if (disable != 0) { } else if (disable != NULL) {
return disable_pihole(config, disable); return disable_pihole(config, disable);
} else { } else {
return get_status(config); return get_status(config);

View file

@ -1,5 +1,6 @@
#include <getopt.h> #include <getopt.h>
#include "config.h"
static char * shortopts = "cd:ef:hqv";
static struct option longopts[] = { static struct option longopts[] = {
{ "configure", no_argument, NULL, 'c' }, { "configure", no_argument, NULL, 'c' },
@ -13,5 +14,3 @@ static struct option longopts[] = {
void print_usage(); void print_usage();
pihole_config * configure_pihole(char * config_path);

View file

@ -12,9 +12,7 @@ static char * AUTH_QUERY = "auth";
static char * ENABLE_QUERY = "enable"; static char * ENABLE_QUERY = "enable";
static char * DISABLE_QUERY = "disable"; static char * DISABLE_QUERY = "disable";
static char * HTTP_SCHEME = "http://"; static char * HTTP_SCHEME = "http://";
static int HTTP_SCHEME_LEN = 8;
static char * HTTPS_SCHEME = "https://"; static char * HTTPS_SCHEME = "https://";
static int HTTPS_SCHEME_LEN = 9;
int get_status(pihole_config * config) { int get_status(pihole_config * config) {
char * formatted_host = prepend_scheme(config->host); 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 * 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. * 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) { static char * prepend_scheme(char * raw_host) {
if (raw_host == NULL) return NULL;
char * formatted_host; char * formatted_host;
if (strnstr(raw_host, HTTP_SCHEME, HTTP_SCHEME_LEN) == NULL if (strstr(raw_host, HTTP_SCHEME) != raw_host
&& strnstr(raw_host, HTTPS_SCHEME, HTTPS_SCHEME_LEN) == NULL) { && strstr(raw_host, HTTPS_SCHEME) != raw_host) {
formatted_host = malloc(URL_FORMAT_LEN + strlen(raw_host)); formatted_host = malloc(URL_FORMAT_LEN + strlen(raw_host));
sprintf(formatted_host, URL_FORMAT, raw_host); sprintf(formatted_host, URL_FORMAT, raw_host);
} else { } else {