diff options
| author | millert <> | 1999-05-01 18:56:41 +0000 | 
|---|---|---|
| committer | millert <> | 1999-05-01 18:56:41 +0000 | 
| commit | cca189ae987ddf4e5e3f62d65b18366f355b6806 (patch) | |
| tree | 3013dbe8eb86a8d987e41c380d0395bee37f4a64 /src | |
| parent | 226fa66bb3d10435232bfb7ff943b0c9f06a44c2 (diff) | |
| download | openbsd-cca189ae987ddf4e5e3f62d65b18366f355b6806.tar.gz openbsd-cca189ae987ddf4e5e3f62d65b18366f355b6806.tar.bz2 openbsd-cca189ae987ddf4e5e3f62d65b18366f355b6806.zip | |
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.
Diffstat (limited to '')
| -rw-r--r-- | src/lib/libc/string/strlcpy.c | 30 | 
1 files changed, 17 insertions, 13 deletions
| diff --git a/src/lib/libc/string/strlcpy.c b/src/lib/libc/string/strlcpy.c index 087f7c0883..300a28bc39 100644 --- a/src/lib/libc/string/strlcpy.c +++ b/src/lib/libc/string/strlcpy.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: strlcpy.c,v 1.3 1999/04/24 01:17:37 millert Exp $ */ | 1 | /* $OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $ */ | 
| 2 | 2 | ||
| 3 | /* | 3 | /* | 
| 4 | * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> | 4 | * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> | 
| @@ -28,7 +28,7 @@ | |||
| 28 | */ | 28 | */ | 
| 29 | 29 | ||
| 30 | #if defined(LIBC_SCCS) && !defined(lint) | 30 | #if defined(LIBC_SCCS) && !defined(lint) | 
| 31 | static char *rcsid = "$OpenBSD: strlcpy.c,v 1.3 1999/04/24 01:17:37 millert Exp $"; | 31 | static char *rcsid = "$OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $"; | 
| 32 | #endif /* LIBC_SCCS and not lint */ | 32 | #endif /* LIBC_SCCS and not lint */ | 
| 33 | 33 | ||
| 34 | #include <sys/types.h> | 34 | #include <sys/types.h> | 
| @@ -48,17 +48,21 @@ size_t strlcpy(dst, src, siz) | |||
| 48 | register const char *s = src; | 48 | register const char *s = src; | 
| 49 | register size_t n = siz; | 49 | register size_t n = siz; | 
| 50 | 50 | ||
| 51 | if (n) | 51 | /* Copy as many bytes as will fit */ | 
| 52 | n--; /* don't count the NUL */ | 52 | if (n != 0 && --n != 0) { | 
| 53 | while (*s) { | 53 | do { | 
| 54 | if (n) { | 54 | if ((*d++ = *s++) == 0) | 
| 55 | *d++ = *s; | 55 | break; | 
| 56 | n--; | 56 | } while (--n != 0); | 
| 57 | } | ||
| 58 | s++; | ||
| 59 | } | 57 | } | 
| 60 | if (siz) | ||
| 61 | *d = '\0'; | ||
| 62 | 58 | ||
| 63 | return(s - src); /* count does not include NUL */ | 59 | /* Not enough room in dst, add NUL and traverse rest of src */ | 
| 60 | if (n == 0) { | ||
| 61 | if (siz != 0) | ||
| 62 | *d = '\0'; /* NUL-terminate dst */ | ||
| 63 | while (*s++) | ||
| 64 | ; | ||
| 65 | } | ||
| 66 | |||
| 67 | return(s - src - 1); /* count does not include NUL */ | ||
| 64 | } | 68 | } | 
