summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Mares <mj@ucw.cz>2011-10-02 12:41:07 +0200
committerMartin Mares <mj@ucw.cz>2011-10-02 12:41:07 +0200
commite616394f2082948b8aba0a75cf40e7f50d2fc9ab (patch)
tree22a254fd8396837afab104235578893387ae6686
parent9b50f808d1ff9bab21a1c8f68fe8c119393ac75b (diff)
downloadpciutils-e616394f2082948b8aba0a75cf40e7f50d2fc9ab.tar.gz
Truncate names which are too long
When any name turned out too long, we returned "<pci_lookup_name: buffer too small>", 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.
-rw-r--r--lib/names.c14
1 files 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 "<pci_lookup_name: buffer too small>";
- 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 "<pci_lookup_name: buffer too small>";
- else
- return buf;
+ return buf;
}
char *