1*05b00f60SXin Li /* $NetBSD: strlcpy.c,v 1.5 1999/09/20 04:39:47 lukem Exp $ */
2*05b00f60SXin Li /* from OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp */
3*05b00f60SXin Li
4*05b00f60SXin Li /*
5*05b00f60SXin Li * Copyright (c) 1998 Todd C. Miller <[email protected]>
6*05b00f60SXin Li * All rights reserved.
7*05b00f60SXin Li *
8*05b00f60SXin Li * Redistribution and use in source and binary forms, with or without
9*05b00f60SXin Li * modification, are permitted provided that the following conditions
10*05b00f60SXin Li * are met:
11*05b00f60SXin Li * 1. Redistributions of source code must retain the above copyright
12*05b00f60SXin Li * notice, this list of conditions and the following disclaimer.
13*05b00f60SXin Li * 2. Redistributions in binary form must reproduce the above copyright
14*05b00f60SXin Li * notice, this list of conditions and the following disclaimer in the
15*05b00f60SXin Li * documentation and/or other materials provided with the distribution.
16*05b00f60SXin Li * 3. The name of the author may not be used to endorse or promote products
17*05b00f60SXin Li * derived from this software without specific prior written permission.
18*05b00f60SXin Li *
19*05b00f60SXin Li * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
20*05b00f60SXin Li * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21*05b00f60SXin Li * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22*05b00f60SXin Li * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23*05b00f60SXin Li * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24*05b00f60SXin Li * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25*05b00f60SXin Li * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26*05b00f60SXin Li * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27*05b00f60SXin Li * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28*05b00f60SXin Li * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*05b00f60SXin Li */
30*05b00f60SXin Li
31*05b00f60SXin Li #ifdef HAVE_CONFIG_H
32*05b00f60SXin Li #include <config.h>
33*05b00f60SXin Li #endif
34*05b00f60SXin Li
35*05b00f60SXin Li #include <netdissect-stdinc.h>
36*05b00f60SXin Li
37*05b00f60SXin Li #include <string.h>
38*05b00f60SXin Li
39*05b00f60SXin Li #include "netdissect.h"
40*05b00f60SXin Li
41*05b00f60SXin Li /*
42*05b00f60SXin Li * Copy src to string dst of size siz. At most siz-1 characters
43*05b00f60SXin Li * will be copied. Always NUL terminates (unless siz == 0).
44*05b00f60SXin Li * Returns strlen(src); if retval >= siz, truncation occurred.
45*05b00f60SXin Li */
46*05b00f60SXin Li size_t
strlcpy(char * dst,const char * src,size_t siz)47*05b00f60SXin Li strlcpy(char *dst, const char *src, size_t siz)
48*05b00f60SXin Li {
49*05b00f60SXin Li char *d = dst;
50*05b00f60SXin Li const char *s = src;
51*05b00f60SXin Li size_t n = siz;
52*05b00f60SXin Li
53*05b00f60SXin Li /* Copy as many bytes as will fit */
54*05b00f60SXin Li if (n != 0 && --n != 0) {
55*05b00f60SXin Li do {
56*05b00f60SXin Li if ((*d++ = *s++) == 0)
57*05b00f60SXin Li break;
58*05b00f60SXin Li } while (--n != 0);
59*05b00f60SXin Li }
60*05b00f60SXin Li
61*05b00f60SXin Li /* Not enough room in dst, add NUL and traverse rest of src */
62*05b00f60SXin Li if (n == 0) {
63*05b00f60SXin Li if (siz != 0)
64*05b00f60SXin Li *d = '\0'; /* NUL-terminate dst */
65*05b00f60SXin Li while (*s++)
66*05b00f60SXin Li ;
67*05b00f60SXin Li }
68*05b00f60SXin Li
69*05b00f60SXin Li return(s - src - 1); /* count does not include NUL */
70*05b00f60SXin Li }
71