From 322baa66fd5f3d72ecc48c9dc9d043bd66e8e0fb Mon Sep 17 00:00:00 2001 From: millert <> Date: Sat, 1 May 1999 18:56:41 +0000 Subject: [PATCH] Break up into two loops, one for the copy, another to finish traversal of the src string if len(src) >= size. Speeds up the common case a bit. --- src/lib/libc/string/strlcpy.c | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/lib/libc/string/strlcpy.c b/src/lib/libc/string/strlcpy.c index 087f7c08..300a28bc 100644 --- a/src/lib/libc/string/strlcpy.c +++ b/src/lib/libc/string/strlcpy.c @@ -1,4 +1,4 @@ -/* $OpenBSD: strlcpy.c,v 1.3 1999/04/24 01:17:37 millert Exp $ */ +/* $OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $ */ /* * Copyright (c) 1998 Todd C. Miller @@ -28,7 +28,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char *rcsid = "$OpenBSD: strlcpy.c,v 1.3 1999/04/24 01:17:37 millert Exp $"; +static char *rcsid = "$OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $"; #endif /* LIBC_SCCS and not lint */ #include @@ -48,17 +48,21 @@ size_t strlcpy(dst, src, siz) register const char *s = src; register size_t n = siz; - if (n) - n--; /* don't count the NUL */ - while (*s) { - if (n) { - *d++ = *s; - n--; - } - s++; + /* Copy as many bytes as will fit */ + if (n != 0 && --n != 0) { + do { + if ((*d++ = *s++) == 0) + break; + } while (--n != 0); } - if (siz) - *d = '\0'; - return(s - src); /* count does not include NUL */ + /* Not enough room in dst, add NUL and traverse rest of src */ + if (n == 0) { + if (siz != 0) + *d = '\0'; /* NUL-terminate dst */ + while (*s++) + ; + } + + return(s - src - 1); /* count does not include NUL */ }