logsys.c: avoid redundant strlen in else-block

* exec/logsys.c (strcpy_cutoff): Also, with a field width (aka cutoff),
and a shorter-than-field-width string, don't write the same memory
twice: once with strncpy using NUL bytes, then again with spaces
via the memset.

git-svn-id: http://svn.fedorahosted.org/svn/corosync/trunk@2177 fd59a12c-fef9-0310-b244-a6a79926bd2f
This commit is contained in:
Jim Meyering 2009-05-12 08:35:09 +00:00
parent fbbc934196
commit d04702854b

View File

@ -65,6 +65,8 @@
#define YIELD_AFTER_LOG_OPS 10
#define MIN(x,y) ((x) < (y) ? (x) : (y))
/*
* similar to syslog facilities/priorities tables,
* make a tag table for internal use
@ -381,18 +383,15 @@ do { \
*/
static inline int strcpy_cutoff (char *dest, const char *src, int cutoff)
{
size_t len = strlen (src);
if (cutoff <= 0) {
size_t len = strlen (src);
memcpy (dest, src, len + 1);
return (len);
} else {
size_t len;
strncpy (dest, src, cutoff);
len = MIN (len, cutoff);
memcpy (dest, src, len);
memset (dest + len, ' ', cutoff - len);
dest[cutoff] = '\0';
len = strlen (dest);
if (len != cutoff) {
memset (&dest[len], ' ', cutoff - len);
}
}
return (cutoff);
}