summaryrefslogtreecommitdiffstats
path: root/sys/libkern/strlcpy.c
diff options
context:
space:
mode:
authorimp <imp@FreeBSD.org>1999-08-10 05:58:58 +0000
committerimp <imp@FreeBSD.org>1999-08-10 05:58:58 +0000
commitbedbde67f9d8aaa2cfc9543034594c777c88681f (patch)
tree824310c2514c6caca9b046c68318fdd421c5e897 /sys/libkern/strlcpy.c
parentd27613c67e8bb774f79ec9f36cbdd02b14fcee73 (diff)
downloadFreeBSD-src-bedbde67f9d8aaa2cfc9543034594c777c88681f.zip
FreeBSD-src-bedbde67f9d8aaa2cfc9543034594c777c88681f.tar.gz
Use the latest version of these files from OpenBSD.
1) Safty change from casper dik was added to OpenBSD's sources since I grabbed them. milltert@openbsd.org 2) Split up strlcpy to improve efficiency of the common case. milltert@openbsd.org 3) Cleanup of cross references for man page. {alex,aaron}@openbsd.org Pointed out by: deraadt@openbsd.org
Diffstat (limited to 'sys/libkern/strlcpy.c')
-rw-r--r--sys/libkern/strlcpy.c29
1 files changed, 17 insertions, 12 deletions
diff --git a/sys/libkern/strlcpy.c b/sys/libkern/strlcpy.c
index 2b6a166..300a28b 100644
--- a/sys/libkern/strlcpy.c
+++ b/sys/libkern/strlcpy.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: strlcpy.c,v 1.2 1998/11/06 04:33:16 wvdputte Exp $ */
+/* $OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $ */
/*
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
@@ -28,7 +28,7 @@
*/
#if defined(LIBC_SCCS) && !defined(lint)
-static char *rcsid = "$OpenBSD: strlcpy.c,v 1.2 1998/11/06 04:33:16 wvdputte 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 <sys/types.h>
@@ -48,16 +48,21 @@ size_t strlcpy(dst, src, siz)
register const char *s = src;
register size_t n = siz;
- if (n == 0)
- return(strlen(s));
- while (*s != '\0') {
- if (n != 1) {
- *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);
}
- *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 */
}
OpenPOWER on IntegriCloud