19546e36dSchristos /* NetBSD: strlcpy.c,v 1.5 1999/09/20 04:39:47 lukem Exp */ 20f74e101Schristos /* from OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp */ 30f74e101Schristos 40f74e101Schristos /* 50f74e101Schristos * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> 60f74e101Schristos * All rights reserved. 70f74e101Schristos * 80f74e101Schristos * Redistribution and use in source and binary forms, with or without 90f74e101Schristos * modification, are permitted provided that the following conditions 100f74e101Schristos * are met: 110f74e101Schristos * 1. Redistributions of source code must retain the above copyright 120f74e101Schristos * notice, this list of conditions and the following disclaimer. 130f74e101Schristos * 2. Redistributions in binary form must reproduce the above copyright 140f74e101Schristos * notice, this list of conditions and the following disclaimer in the 150f74e101Schristos * documentation and/or other materials provided with the distribution. 160f74e101Schristos * 3. The name of the author may not be used to endorse or promote products 170f74e101Schristos * derived from this software without specific prior written permission. 180f74e101Schristos * 190f74e101Schristos * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 200f74e101Schristos * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 210f74e101Schristos * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 220f74e101Schristos * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 230f74e101Schristos * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 240f74e101Schristos * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 250f74e101Schristos * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 260f74e101Schristos * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 270f74e101Schristos * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 280f74e101Schristos * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 290f74e101Schristos */ 300f74e101Schristos 310f74e101Schristos #include <config.h> 320f74e101Schristos 33784088dfSchristos #include <netdissect-stdinc.h> 340f74e101Schristos 350f74e101Schristos #include <string.h> 360f74e101Schristos 37784088dfSchristos #include "netdissect.h" 38c47fd378Schristos 390f74e101Schristos /* 400f74e101Schristos * Copy src to string dst of size siz. At most siz-1 characters 410f74e101Schristos * will be copied. Always NUL terminates (unless siz == 0). 420f74e101Schristos * Returns strlen(src); if retval >= siz, truncation occurred. 430f74e101Schristos */ 440f74e101Schristos size_t 450f74e101Schristos strlcpy(char *dst, const char *src, size_t siz) 460f74e101Schristos { 47*d881c474Schristos char *d = dst; 48*d881c474Schristos const char *s = src; 49*d881c474Schristos size_t n = siz; 500f74e101Schristos 510f74e101Schristos /* Copy as many bytes as will fit */ 520f74e101Schristos if (n != 0 && --n != 0) { 530f74e101Schristos do { 540f74e101Schristos if ((*d++ = *s++) == 0) 550f74e101Schristos break; 560f74e101Schristos } while (--n != 0); 570f74e101Schristos } 580f74e101Schristos 590f74e101Schristos /* Not enough room in dst, add NUL and traverse rest of src */ 600f74e101Schristos if (n == 0) { 610f74e101Schristos if (siz != 0) 620f74e101Schristos *d = '\0'; /* NUL-terminate dst */ 630f74e101Schristos while (*s++) 640f74e101Schristos ; 650f74e101Schristos } 660f74e101Schristos 670f74e101Schristos return(s - src - 1); /* count does not include NUL */ 680f74e101Schristos } 69