summaryrefslogtreecommitdiff
path: root/src/lib/libc/string/strlcpy.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/string/strlcpy.c')
-rw-r--r--src/lib/libc/string/strlcpy.c37
1 files changed, 18 insertions, 19 deletions
diff --git a/src/lib/libc/string/strlcpy.c b/src/lib/libc/string/strlcpy.c
index d32b6590f1..e9a7fe4be7 100644
--- a/src/lib/libc/string/strlcpy.c
+++ b/src/lib/libc/string/strlcpy.c
@@ -1,7 +1,7 @@
1/* $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $ */ 1/* $OpenBSD: strlcpy.c,v 1.12 2015/01/15 03:54:12 millert Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> 4 * Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com>
5 * 5 *
6 * Permission to use, copy, modify, and distribute this software for any 6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above 7 * purpose with or without fee is hereby granted, provided that the above
@@ -20,32 +20,31 @@
20#include <string.h> 20#include <string.h>
21 21
22/* 22/*
23 * Copy src to string dst of size siz. At most siz-1 characters 23 * Copy string src to buffer dst of size dsize. At most dsize-1
24 * will be copied. Always NUL terminates (unless siz == 0). 24 * chars will be copied. Always NUL terminates (unless dsize == 0).
25 * Returns strlen(src); if retval >= siz, truncation occurred. 25 * Returns strlen(src); if retval >= dsize, truncation occurred.
26 */ 26 */
27size_t 27size_t
28strlcpy(char *dst, const char *src, size_t siz) 28strlcpy(char *dst, const char *src, size_t dsize)
29{ 29{
30 char *d = dst; 30 const char *osrc = src;
31 const char *s = src; 31 size_t nleft = dsize;
32 size_t n = siz;
33 32
34 /* Copy as many bytes as will fit */ 33 /* Copy as many bytes as will fit. */
35 if (n != 0) { 34 if (nleft != 0) {
36 while (--n != 0) { 35 while (--nleft != 0) {
37 if ((*d++ = *s++) == '\0') 36 if ((*dst++ = *src++) == '\0')
38 break; 37 break;
39 } 38 }
40 } 39 }
41 40
42 /* Not enough room in dst, add NUL and traverse rest of src */ 41 /* Not enough room in dst, add NUL and traverse rest of src. */
43 if (n == 0) { 42 if (nleft == 0) {
44 if (siz != 0) 43 if (dsize != 0)
45 *d = '\0'; /* NUL-terminate dst */ 44 *dst = '\0'; /* NUL-terminate dst */
46 while (*s++) 45 while (*src++)
47 ; 46 ;
48 } 47 }
49 48
50 return(s - src - 1); /* count does not include NUL */ 49 return(src - osrc - 1); /* count does not include NUL */
51} 50}