diff options
author | Ingo Bürk <admin@airblader.de> | 2020-03-06 13:52:11 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-06 13:52:11 +0100 |
commit | 6eb70f10d202e1d6ea7f7c01527191de6e861872 (patch) | |
tree | 953b6d007be463ff71f0c601abea4b831d1e4ea2 | |
parent | 5576d03f90a5d5697bbb2a4b87ff392bba45cc50 (diff) | |
parent | 48119d890a7d8809d15090993b1b44012669ca36 (diff) |
Merge pull request #390 from Stunkymonkey/format_placeholder-eth
use format_placeholder for eth.info
-rw-r--r-- | src/print_eth_info.c | 37 |
1 files changed, 17 insertions, 20 deletions
diff --git a/src/print_eth_info.c b/src/print_eth_info.c index 81e74a7..c0d6649 100644 --- a/src/print_eth_info.c +++ b/src/print_eth_info.c @@ -15,6 +15,8 @@ #include "i3status.h" +#define STRING_SIZE 20 + #if defined(__linux__) #include <linux/ethtool.h> #include <linux/sockios.h> @@ -137,8 +139,8 @@ static int print_eth_speed(char *outwalk, const char *interface) { void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down) { const char *format = format_down; // default format - const char *walk; char *outwalk = buffer; + size_t num = 0; INSTANCE(interface); @@ -177,29 +179,24 @@ void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, cons START_COLOR("color_good"); } -out: - for (walk = format; *walk != '\0'; walk++) { - if (*walk != '%') { - *(outwalk++) = *walk; - - } else if (BEGINS_WITH(walk + 1, "ip")) { - outwalk += sprintf(outwalk, "%s", ip_address); - walk += strlen("ip"); - - } else if (BEGINS_WITH(walk + 1, "speed")) { - outwalk += print_eth_speed(outwalk, interface); - walk += strlen("speed"); + char string_ip[STRING_SIZE]; + char string_speed[STRING_SIZE]; + char string_interface[STRING_SIZE]; + snprintf(string_ip, STRING_SIZE, "%s", ip_address); + print_eth_speed(string_speed, interface); + snprintf(string_interface, STRING_SIZE, "%s", interface); + placeholder_t placeholders[] = { + {.name = "%ip", .value = string_ip}, + {.name = "%speed", .value = string_speed}, + {.name = "%interface", .value = string_interface}}; + num = sizeof(placeholders) / sizeof(placeholder_t); - } else if (BEGINS_WITH(walk + 1, "interface")) { - outwalk += sprintf(outwalk, "%s", interface); - walk += strlen("interface"); +out: + buffer = format_placeholders(format, &placeholders[0], num); - } else { - *(outwalk++) = '%'; - } - } END_COLOR; free(ipv4_address); free(ipv6_address); OUTPUT_FULL_TEXT(buffer); + free(buffer); } |