Blame src/strlcpy.c

Packit 9f0df5
/*	$OpenBSD: strlcpy.c,v 1.10 2005/08/08 08:05:37 espie Exp $	*/
Packit 9f0df5
Packit 9f0df5
/*
Packit 9f0df5
 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
Packit 9f0df5
 *
Packit 9f0df5
 * Permission to use, copy, modify, and distribute this software for any
Packit 9f0df5
 * purpose with or without fee is hereby granted, provided that the above
Packit 9f0df5
 * copyright notice and this permission notice appear in all copies.
Packit 9f0df5
 *
Packit 9f0df5
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
Packit 9f0df5
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
Packit 9f0df5
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
Packit 9f0df5
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
Packit 9f0df5
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
Packit 9f0df5
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
Packit 9f0df5
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Packit 9f0df5
 */
Packit 9f0df5
Packit 9f0df5
#ifdef HAVE_CONFIG_H
Packit 9f0df5
#include <config.h>
Packit 9f0df5
#endif
Packit 9f0df5
Packit 9f0df5
#ifndef HAVE_STRLCPY
Packit 9f0df5
Packit 9f0df5
#include <sys/types.h>
Packit 9f0df5
#include <string.h>
Packit 9f0df5
#include "strlcpycat.h"
Packit 9f0df5
Packit 9f0df5
/*
Packit 9f0df5
 * Copy src to string dst of size siz.  At most siz-1 characters
Packit 9f0df5
 * will be copied.  Always NUL terminates (unless siz == 0).
Packit 9f0df5
 * Returns strlen(src); if retval >= siz, truncation occurred.
Packit 9f0df5
 */
Packit 9f0df5
size_t
Packit 9f0df5
strlcpy(char *dst, const char *src, size_t siz)
Packit 9f0df5
{
Packit 9f0df5
	char *d = dst;
Packit 9f0df5
	const char *s = src;
Packit 9f0df5
	size_t n = siz;
Packit 9f0df5
Packit 9f0df5
	/* Copy as many bytes as will fit */
Packit 9f0df5
	if (n != 0 && --n != 0) {
Packit 9f0df5
		do {
Packit 9f0df5
			if ((*d++ = *s++) == 0)
Packit 9f0df5
				break;
Packit 9f0df5
		} while (--n != 0);
Packit 9f0df5
	}
Packit 9f0df5
Packit 9f0df5
	/* Not enough room in dst, add NUL and traverse rest of src */
Packit 9f0df5
	if (n == 0) {
Packit 9f0df5
		if (siz != 0)
Packit 9f0df5
			*d = '\0';		/* NUL-terminate dst */
Packit 9f0df5
		while (*s++)
Packit 9f0df5
			;
Packit 9f0df5
	}
Packit 9f0df5
Packit 9f0df5
	return(s - src - 1);	/* count does not include NUL */
Packit 9f0df5
}
Packit 9f0df5
Packit 9f0df5
#endif