Blame ext/include/strlcpy.c

Packit 437b5e
/*	$OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $	*/
Packit 437b5e
Packit 437b5e
/*
Packit 437b5e
 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
Packit 437b5e
 * All rights reserved.
Packit 437b5e
 *
Packit 437b5e
 * Redistribution and use in source and binary forms, with or without
Packit 437b5e
 * modification, are permitted provided that the following conditions
Packit 437b5e
 * are met:
Packit 437b5e
 * 1. Redistributions of source code must retain the above copyright
Packit 437b5e
 *    notice, this list of conditions and the following disclaimer.
Packit 437b5e
 * 2. Redistributions in binary form must reproduce the above copyright
Packit 437b5e
 *    notice, this list of conditions and the following disclaimer in the
Packit 437b5e
 *    documentation and/or other materials provided with the distribution.
Packit 437b5e
 * 3. The name of the author may not be used to endorse or promote products
Packit 437b5e
 *    derived from this software without specific prior written permission.
Packit 437b5e
 *
Packit 437b5e
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
Packit 437b5e
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
Packit 437b5e
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
Packit 437b5e
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
Packit 437b5e
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
Packit 437b5e
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
Packit 437b5e
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
Packit 437b5e
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
Packit 437b5e
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
Packit 437b5e
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 437b5e
 */
Packit 437b5e
Packit 437b5e
#ifndef LUAPOSIX_STRLCPY_C
Packit 437b5e
#define LUAPOSIX_STRLCPY_C 1
Packit 437b5e
Packit 437b5e
#include <config.h>
Packit 437b5e
Packit 437b5e
#ifndef HAVE_STRLCPY
Packit 437b5e
Packit 437b5e
#if defined LIBC_SCCS && ! defined lint
Packit 437b5e
static char *rcsid = "$OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $";
Packit 437b5e
#endif /* LIBC_SCCS and not lint */
Packit 437b5e
Packit 437b5e
#include <sys/types.h>
Packit 437b5e
#include <string.h>
Packit 437b5e
Packit 437b5e
static size_t strlcpy(char *dst, const char *src, size_t siz);
Packit 437b5e
Packit 437b5e
/*
Packit 437b5e
 * Copy src to string dst of size siz.  At most siz-1 characters
Packit 437b5e
 * will be copied.  Always NUL terminates (unless siz == 0).
Packit 437b5e
 * Returns strlen(src); if retval >= siz, truncation occurred.
Packit 437b5e
 */
Packit 437b5e
static size_t strlcpy(char *dst, const char *src, size_t siz)
Packit 437b5e
{
Packit 437b5e
        register char *d = dst;
Packit 437b5e
        register const char *s = src;
Packit 437b5e
        register size_t n = siz;
Packit 437b5e
Packit 437b5e
        /* Copy as many bytes as will fit */
Packit 437b5e
        if (n != 0 && --n != 0) {
Packit 437b5e
                do {
Packit 437b5e
                        if ((*d++ = *s++) == 0)
Packit 437b5e
                                break;
Packit 437b5e
                } while (--n != 0);
Packit 437b5e
        }
Packit 437b5e
Packit 437b5e
        /* Not enough room in dst, add NUL and traverse rest of src */
Packit 437b5e
        if (n == 0) {
Packit 437b5e
                if (siz != 0)
Packit 437b5e
                        *d = '\0';		/* NUL-terminate dst */
Packit 437b5e
                while (*s++)
Packit 437b5e
                        ;
Packit 437b5e
        }
Packit 437b5e
Packit 437b5e
        return(s - src - 1);	/* count does not include NUL */
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
#endif /* !HAVE_STRLCPY */
Packit 437b5e
Packit 437b5e
#endif /* !LUAPOSIX_STRLCPY_C */