summaryrefslogtreecommitdiff
path: root/src/lib/libc/string/wcslcpy.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/string/wcslcpy.c')
-rw-r--r--src/lib/libc/string/wcslcpy.c38
1 files changed, 18 insertions, 20 deletions
diff --git a/src/lib/libc/string/wcslcpy.c b/src/lib/libc/string/wcslcpy.c
index f49936a885..1c4811577f 100644
--- a/src/lib/libc/string/wcslcpy.c
+++ b/src/lib/libc/string/wcslcpy.c
@@ -1,8 +1,7 @@
1/* $OpenBSD: wcslcpy.c,v 1.5 2011/07/24 15:21:28 millert Exp $ */ 1/* $OpenBSD: wcslcpy.c,v 1.6 2015/01/15 03:54:12 millert Exp $ */
2/* $NetBSD: wcslcpy.c,v 1.2 2001/01/03 14:33:02 lukem Exp $ */
3 2
4/* 3/*
5 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> 4 * Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com>
6 * 5 *
7 * Permission to use, copy, modify, and distribute this software for any 6 * Permission to use, copy, modify, and distribute this software for any
8 * 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
@@ -21,32 +20,31 @@
21#include <wchar.h> 20#include <wchar.h>
22 21
23/* 22/*
24 * 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
25 * will be copied. Always NUL terminates (unless siz == 0). 24 * chars will be copied. Always NUL terminates (unless dsize == 0).
26 * Returns wcslen(src); if retval >= siz, truncation occurred. 25 * Returns wcslen(src); if retval >= dsize, truncation occurred.
27 */ 26 */
28size_t 27size_t
29wcslcpy(wchar_t *dst, const wchar_t *src, size_t siz) 28wcslcpy(wchar_t *dst, const wchar_t *src, size_t dsize)
30{ 29{
31 wchar_t *d = dst; 30 const wchar_t *osrc = src;
32 const wchar_t *s = src; 31 size_t nleft = dsize;
33 size_t n = siz;
34 32
35 /* Copy as many bytes as will fit */ 33 /* Copy as many bytes as will fit. */
36 if (n != 0) { 34 if (nleft != 0) {
37 while (--n != 0) { 35 while (--nleft != 0) {
38 if ((*d++ = *s++) == '\0') 36 if ((*dst++ = *src++) == L'\0')
39 break; 37 break;
40 } 38 }
41 } 39 }
42 40
43 /* 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. */
44 if (n == 0) { 42 if (nleft == 0) {
45 if (siz != 0) 43 if (dsize != 0)
46 *d = '\0'; /* NUL-terminate dst */ 44 *dst = L'\0'; /* NUL-terminate dst */
47 while (*s++) 45 while (*src++)
48 ; 46 ;
49 } 47 }
50 48
51 return(s - src - 1); /* count does not include NUL */ 49 return(src - osrc - 1); /* count does not include NUL */
52} 50}