From e616394f2082948b8aba0a75cf40e7f50d2fc9ab Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Sun, 2 Oct 2011 12:41:07 +0200 Subject: [PATCH] Truncate names which are too long When any name turned out too long, we returned "", which wasn't very useful. Instead of that, we truncate the name and we add "..." at the end. Please note that for simplicity, the truncation happens only on systems with proper C99 snprintf(). Ancient systems where snprintf() returns a negative number and promises nothing about the state of the buffer still use the error message. --- lib/names.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/names.c b/lib/names.c index 26de128..bda8c90 100644 --- a/lib/names.c +++ b/lib/names.c @@ -69,10 +69,11 @@ format_name(char *buf, int size, int flags, char *name, char *num, char *unknown res = snprintf(buf, size, "%s", name); else res = snprintf(buf, size, "%s [%s]", name, num); - if (res < 0 || res >= size) + if (res >= size && size >= 4) + buf[size-2] = buf[size-3] = buf[size-4] = '.'; + else if (res < 0 || res >= size) return ""; - else - return buf; + return buf; } static char * @@ -101,10 +102,11 @@ format_name_pair(char *buf, int size, int flags, char *v, char *d, char *num) else /* v && !d */ res = snprintf(buf, size, "%s Device %s", v, num+5); } - if (res < 0 || res >= size) + if (res >= size && size >= 4) + buf[size-2] = buf[size-3] = buf[size-4] = '.'; + else if (res < 0 || res >= size) return ""; - else - return buf; + return buf; } char * -- 2.39.5