From 7c2f09a138a4103519a0063f90759d4211aaa525 Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Sat, 1 May 1999 18:56:42 +0000 Subject: 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. --- lib/libc/string/strlcpy.c | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) (limited to 'lib/libc') diff --git a/lib/libc/string/strlcpy.c b/lib/libc/string/strlcpy.c index 087f7c08838..300a28bc391 100644 --- a/lib/libc/string/strlcpy.c +++ b/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 */ } -- cgit v1.2.3