From 26c52495741270dbb400abdf9b21a0483c935cab Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Fri, 19 Jul 2019 15:03:09 +0200 Subject: [PATCH] device: break string shortening out of device shortening --- src/util-device.c | 52 ++++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/src/util-device.c b/src/util-device.c index 1592d37b29..1187c62532 100644 --- a/src/util-device.c +++ b/src/util-device.c @@ -189,6 +189,33 @@ const char *LiveGetDeviceName(int number) return NULL; } +static void ShortenString(const char *input, + char *output, size_t output_size, char c) +{ + const size_t str_len = strlen(input); + size_t half = (output_size - 1) / 2; + + /* If the output size is an even number */ + if (half * 2 == (output_size - 1)) { + half = half - 1; + } + + size_t spaces = (output_size - 1) - (half * 2); + + /* Add the first half to the new string */ + snprintf(output, half+1, "%s", input); + + /* Add the amount of spaces wanted */ + size_t length = half; + for (size_t i = half; i < half + spaces; i++) { + char s[2] = ""; + snprintf(s, sizeof(s), "%c", c); + length = strlcat(output, s, output_size); + } + + snprintf(output + length, half + 1, "%s", input + (str_len - half)); +} + /** \internal * \brief Shorten a device name that is to long * @@ -198,7 +225,7 @@ const char *LiveGetDeviceName(int number) */ static int LiveSafeDeviceName(const char *devname, char *newdevname, size_t destlen) { - size_t devnamelen = strlen(devname); + const size_t devnamelen = strlen(devname); /* If we have to shorten the interface name */ if (devnamelen > MAX_DEVNAME) { @@ -215,29 +242,8 @@ static int LiveSafeDeviceName(const char *devname, char *newdevname, size_t dest return 1; } - size_t length; - size_t half; - size_t spaces; - - half = (destlen-1) / 2; - - /* If the destlen is an even number */ - if (half * 2 == (destlen-1)) { - half = half - 1; - } - - spaces = (destlen-1) - (half*2); - length = half; - - /* Add the first half to the new dev name */ - snprintf(newdevname, half+1, "%s", devname); - - /* Add the amount of spaces wanted */ - for (size_t i = half; i < half+spaces; i++) { - length = strlcat(newdevname, ".", destlen); - } + ShortenString(devname, newdevname, destlen, '.'); - snprintf(newdevname+length, half+1, "%s", devname+(devnamelen-half)); SCLogInfo("Shortening device name to: %s", newdevname); } else { strlcpy(newdevname, devname, destlen);