103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra/*-
203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra * Copyright (c) 2006, 2008, 2009
303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra *	Thorsten Glaser <tg@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
2103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra__RCSID("$MirOS: src/bin/mksh/strlcpy.c,v 1.7 2009/06/10 18:12:50 tg Rel $");
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 */
2803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condrasize_t
2903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condrastrlcpy(char *dst, const char *src, size_t siz)
3003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra{
3103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	const char *s = src;
3203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
3303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	if (siz == 0)
3403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		goto traverse_src;
3503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
3603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	/* copy as many chars as will fit */
3703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	while (--siz && (*dst++ = *s++))
3803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		;
3903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
4003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	/* not enough room in dst */
4103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	if (siz == 0) {
4203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		/* safe to NUL-terminate dst since we copied <= siz-1 chars */
4303ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		*dst = '\0';
4403ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra traverse_src:
4503ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		/* traverse rest of src */
4603ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra		while (*s++)
4703ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra			;
4803ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	}
4903ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra
5003ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	/* count does not include NUL */
5103ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra	return ((size_t)(s - src - 1));
5203ebf06f4e1112a0e9533b93062d169232c4cbfeGeremy Condra}
53