xref: /freebsd-src/contrib/libpcap/missing/strlcpy.c (revision afdbf109c6a661a729938f68211054a0a50d38ac)
1*afdbf109SJoseph Mingrone /*	$OpenBSD: strlcpy.c,v 1.12 2015/01/15 03:54:12 millert Exp $	*/
257e22627SCy Schubert 
357e22627SCy Schubert /*
457e22627SCy Schubert  * Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com>
557e22627SCy Schubert  *
657e22627SCy Schubert  * Permission to use, copy, modify, and distribute this software for any
757e22627SCy Schubert  * purpose with or without fee is hereby granted, provided that the above
857e22627SCy Schubert  * copyright notice and this permission notice appear in all copies.
957e22627SCy Schubert  *
1057e22627SCy Schubert  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1157e22627SCy Schubert  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1257e22627SCy Schubert  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1357e22627SCy Schubert  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1457e22627SCy Schubert  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1557e22627SCy Schubert  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1657e22627SCy Schubert  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1757e22627SCy Schubert  */
1857e22627SCy Schubert 
1957e22627SCy Schubert #include <config.h>
2057e22627SCy Schubert 
2157e22627SCy Schubert #include <stddef.h>
2257e22627SCy Schubert #include <string.h>
2357e22627SCy Schubert 
2457e22627SCy Schubert #include "portability.h"
2557e22627SCy Schubert 
2657e22627SCy Schubert /*
2757e22627SCy Schubert  * Copy string src to buffer dst of size dsize.  At most dsize-1
2857e22627SCy Schubert  * chars will be copied.  Always NUL terminates (unless dsize == 0).
2957e22627SCy Schubert  * Returns strlen(src); if retval >= dsize, truncation occurred.
3057e22627SCy Schubert  */
3157e22627SCy Schubert size_t
32*afdbf109SJoseph Mingrone pcapint_strlcpy(char * restrict dst, const char * restrict src, size_t dsize)
3357e22627SCy Schubert {
3457e22627SCy Schubert 	const char *osrc = src;
3557e22627SCy Schubert 	size_t nleft = dsize;
3657e22627SCy Schubert 
3757e22627SCy Schubert 	/* Copy as many bytes as will fit. */
3857e22627SCy Schubert 	if (nleft != 0) {
3957e22627SCy Schubert 		while (--nleft != 0) {
4057e22627SCy Schubert 			if ((*dst++ = *src++) == '\0')
4157e22627SCy Schubert 				break;
4257e22627SCy Schubert 		}
4357e22627SCy Schubert 	}
4457e22627SCy Schubert 
4557e22627SCy Schubert 	/* Not enough room in dst, add NUL and traverse rest of src. */
4657e22627SCy Schubert 	if (nleft == 0) {
4757e22627SCy Schubert 		if (dsize != 0)
4857e22627SCy Schubert 			*dst = '\0';		/* NUL-terminate dst */
4957e22627SCy Schubert 		while (*src++)
5057e22627SCy Schubert 			;
5157e22627SCy Schubert 	}
5257e22627SCy Schubert 
5357e22627SCy Schubert 	return(src - osrc - 1);	/* count does not include NUL */
5457e22627SCy Schubert }
55