diff options
Diffstat (limited to 'src')
| -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 | } |
