From 85200c177afd412e6d6180c35df73c7f499abf1e Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Sun, 31 May 2015 21:23:32 +0200 Subject: move actual replace to a function --- dyndhcpd.c | 60 ++++++++++++++++++++++++------------------------------------ dyndhcpd.h | 7 +++++++ 2 files changed, 31 insertions(+), 36 deletions(-) diff --git a/dyndhcpd.c b/dyndhcpd.c index 5d6d16a..9d7a85a 100644 --- a/dyndhcpd.c +++ b/dyndhcpd.c @@ -17,6 +17,20 @@ const static struct option options_long[] = { { 0, 0, 0, 0 } }; +/*** replace ***/ +int replace(char ** config, size_t *length, const char ** tmp, + const char * template, const char * value) { + size_t templatelength = strlen(template); + + if (strncmp(template, *tmp, templatelength) == 0) { + *config = realloc(*config, *length + strlen(value) + 1); + *length += sprintf(*config + *length, value); + *tmp += templatelength; + return 1; + } + return 0; +} + /*** main ***/ int main(int argc, char ** argv) { int i, rc = EXIT_FAILURE, verbose = 0; @@ -197,42 +211,16 @@ int main(int argc, char ** argv) { /* replace strings with real values */ for (tmp = template; *tmp;) { if (*tmp == '_') { - if (strncmp("__INTERFACE__", tmp, 13) == 0) { - config = realloc(config, length + strlen(interface) + 1); - length += sprintf(config + length, interface); - tmp += 13; - } else if (strncmp("__VERSION__", tmp, 11) == 0) { - config = realloc(config, length + strlen(VERSION) + 1); - length += sprintf(config + length, VERSION); - tmp += 11; - } else if (strncmp("__DOMAINNAME__", tmp, 14) == 0) { - config = realloc(config, length + strlen(domainname) + 1); - length += sprintf(config + length, domainname); - tmp += 14; - } else if (strncmp("__ADDRESS__", tmp, 11) == 0) { - config = realloc(config, length + strlen(c_address) + 1); - length += sprintf(config + length, c_address); - tmp += 11; - } else if (strncmp("__NETADDRESS__", tmp, 14) == 0) { - config = realloc(config, length + strlen(c_netaddress) + 1); - length += sprintf(config + length, c_netaddress); - tmp += 14; - } else if (strncmp("__BROADCAST__", tmp, 13) == 0) { - config = realloc(config, length + strlen(c_broadcast) + 1); - length += sprintf(config + length, c_broadcast); - tmp += 13; - } else if (strncmp("__NETMASK__", tmp, 11) == 0) { - config = realloc(config, length + strlen(c_netmask) + 1); - length += sprintf(config + length, c_netmask); - tmp += 11; - } else if (strncmp("__MINHOST__", tmp, 11) == 0) { - config = realloc(config, length + strlen(c_minhost) + 1); - length += sprintf(config + length, c_minhost); - tmp += 11; - } else if (strncmp("__MAXHOST__", tmp, 11) == 0) { - config = realloc(config, length + strlen(c_maxhost) + 1); - length += sprintf(config + length, c_maxhost); - tmp += 11; + if (replace(&config, &length, &tmp, "__INTERFACE__", interface) || + replace(&config, &length, &tmp, "__VERSION__", VERSION) || + replace(&config, &length, &tmp, "__DOMAINNAME__", domainname) || + replace(&config, &length, &tmp, "__ADDRESS__", c_address) || + replace(&config, &length, &tmp, "__NETADDRESS__", c_netaddress) || + replace(&config, &length, &tmp, "__BROADCAST__", c_broadcast) || + replace(&config, &length, &tmp, "__NETMASK__", c_netmask) || + replace(&config, &length, &tmp, "__MINHOST__", c_minhost) || + replace(&config, &length, &tmp, "__MAXHOST__", c_maxhost)) { + /* do nothing, work has been done */ } else { config = realloc(config, length + 1); config[length++] = *tmp++; diff --git a/dyndhcpd.h b/dyndhcpd.h index 9c56d11..75724e5 100644 --- a/dyndhcpd.h +++ b/dyndhcpd.h @@ -24,6 +24,13 @@ #include "config.h" #include "version.h" +/*** replace ***/ +int replace(char ** config, size_t *length, const char ** tmp, + const char * template, const char * value); + +/*** main ***/ +int main (int argc, char ** argv); + #endif /* _DYNDHCPD_H */ // vim: set syntax=c: -- cgit v1.2.3-70-g09d2