103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra/*-
2737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes * Copyright (c) 2006, 2008, 2009, 2013
3fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes *	mirabilos <m@mirbsd.org>
403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra *
603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra * Permission to use, copy, modify, and distribute this software for any
703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra * purpose with or without fee is hereby granted, provided that the above
803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra * copyright notice and this permission notice appear in all copies.
903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra *
1003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra */
1803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
1903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra#include "sh.h"
2003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
21fc0307d88e2ab13777f102dc63c0d1c968dc8bb2Elliott Hughes__RCSID("$MirOS: src/bin/mksh/strlcpy.c,v 1.10 2015/11/29 17:05:02 tg Exp $");
2203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
2303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra/*
2403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra * Copy src to string dst of size siz. At most siz-1 characters
2503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra * will be copied. Always NUL terminates (unless siz == 0).
2603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra * Returns strlen(src); if retval >= siz, truncation occurred.
2703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra */
28737fdce098f804459a925438e48dd711c31bbc9eElliott Hughes#undef strlcpy
2903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condrasize_t
3003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condrastrlcpy(char *dst, const char *src, size_t siz)
3103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra{
3203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	const char *s = src;
3303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
3403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	if (siz == 0)
3503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		goto traverse_src;
3603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
3703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	/* copy as many chars as will fit */
3803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	while (--siz && (*dst++ = *s++))
3903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		;
4003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
4103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	/* not enough room in dst */
4203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	if (siz == 0) {
4303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		/* safe to NUL-terminate dst since we copied <= siz-1 chars */
4403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		*dst = '\0';
4503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra traverse_src:
4603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		/* traverse rest of src */
4703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		while (*s++)
4803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			;
4903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	}
5003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
5103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	/* count does not include NUL */
5203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	return ((size_t)(s - src - 1));
5303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra}
54