Blame strlcpy.c

Packit Service 95ac19
/*-
Packit Service 95ac19
 * Copyright (c) 2006, 2008, 2009, 2013
Packit Service 95ac19
 *	mirabilos <m@mirbsd.org>
Packit Service 95ac19
 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
Packit Service 95ac19
 *
Packit Service 95ac19
 * Permission to use, copy, modify, and distribute this software for any
Packit Service 95ac19
 * purpose with or without fee is hereby granted, provided that the above
Packit Service 95ac19
 * copyright notice and this permission notice appear in all copies.
Packit Service 95ac19
 *
Packit Service 95ac19
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
Packit Service 95ac19
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
Packit Service 95ac19
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
Packit Service 95ac19
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
Packit Service 95ac19
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
Packit Service 95ac19
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
Packit Service 95ac19
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Packit Service 95ac19
 */
Packit Service 95ac19
Packit Service 95ac19
#include "sh.h"
Packit Service 95ac19
Packit Service 95ac19
__RCSID("$MirOS: src/bin/mksh/strlcpy.c,v 1.10 2015/11/29 17:05:02 tg Exp $");
Packit Service 95ac19
Packit Service 95ac19
/*
Packit Service 95ac19
 * Copy src to string dst of size siz. At most siz-1 characters
Packit Service 95ac19
 * will be copied. Always NUL terminates (unless siz == 0).
Packit Service 95ac19
 * Returns strlen(src); if retval >= siz, truncation occurred.
Packit Service 95ac19
 */
Packit Service 95ac19
#undef strlcpy
Packit Service 95ac19
size_t
Packit Service 95ac19
strlcpy(char *dst, const char *src, size_t siz)
Packit Service 95ac19
{
Packit Service 95ac19
	const char *s = src;
Packit Service 95ac19
Packit Service 95ac19
	if (siz == 0)
Packit Service 95ac19
		goto traverse_src;
Packit Service 95ac19
Packit Service 95ac19
	/* copy as many chars as will fit */
Packit Service 95ac19
	while (--siz && (*dst++ = *s++))
Packit Service 95ac19
		;
Packit Service 95ac19
Packit Service 95ac19
	/* not enough room in dst */
Packit Service 95ac19
	if (siz == 0) {
Packit Service 95ac19
		/* safe to NUL-terminate dst since we copied <= siz-1 chars */
Packit Service 95ac19
		*dst = '\0';
Packit Service 95ac19
 traverse_src:
Packit Service 95ac19
		/* traverse rest of src */
Packit Service 95ac19
		while (*s++)
Packit Service 95ac19
			;
Packit Service 95ac19
	}
Packit Service 95ac19
Packit Service 95ac19
	/* count does not include NUL */
Packit Service 95ac19
	return ((size_t)(s - src - 1));
Packit Service 95ac19
}