From: Martin Mares Date: Sun, 2 Oct 2011 10:41:07 +0000 (+0200) Subject: Truncate names which are too long X-Git-Tag: v3.1.8~4 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=e616394f2082948b8aba0a75cf40e7f50d2fc9ab;p=pciutils.git 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. --- 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 *