1*27c36eabStron /* $NetBSD: stpncpy.c,v 1.2 2013/11/06 21:05:27 tron Exp $ */
2be118519Sperry
3be118519Sperry /*-
4be118519Sperry * Copyright (c) 2009 David Schultz <das@FreeBSD.org>
5be118519Sperry * All rights reserved.
6be118519Sperry *
7be118519Sperry * Redistribution and use in source and binary forms, with or without
8be118519Sperry * modification, are permitted provided that the following conditions
9be118519Sperry * are met:
10be118519Sperry * 1. Redistributions of source code must retain the above copyright
11be118519Sperry * notice, this list of conditions and the following disclaimer.
12be118519Sperry * 2. Redistributions in binary form must reproduce the above copyright
13be118519Sperry * notice, this list of conditions and the following disclaimer in the
14be118519Sperry * documentation and/or other materials provided with the distribution.
15be118519Sperry *
16be118519Sperry * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17be118519Sperry * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18be118519Sperry * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19be118519Sperry * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20be118519Sperry * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21be118519Sperry * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22be118519Sperry * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23be118519Sperry * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24be118519Sperry * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25be118519Sperry * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26be118519Sperry * SUCH DAMAGE.
27be118519Sperry */
28be118519Sperry
29be118519Sperry #include <sys/cdefs.h>
30be118519Sperry #if defined(LIBC_SCCS) && !defined(lint)
31*27c36eabStron __RCSID("$NetBSD: stpncpy.c,v 1.2 2013/11/06 21:05:27 tron Exp $");
32be118519Sperry #endif /* LIBC_SCCS and not lint */
33be118519Sperry /* FreeBSD: src/lib/libc/string/stpncpy.c,v 1.1 2009/02/28 06:00:58 das Exp */
34be118519Sperry
35be118519Sperry #include <string.h>
36be118519Sperry
37*27c36eabStron #ifdef _FORTIFY_SOURCE
38*27c36eabStron #undef stpncpy
39*27c36eabStron #endif
40*27c36eabStron
41be118519Sperry char *
stpncpy(char * __restrict dst,const char * __restrict src,size_t n)42be118519Sperry stpncpy(char * __restrict dst, const char * __restrict src, size_t n)
43be118519Sperry {
44be118519Sperry
45be118519Sperry for (; n--; dst++, src++) {
46be118519Sperry if (!(*dst = *src)) {
47be118519Sperry char *ret = dst;
48be118519Sperry while (n--)
49be118519Sperry *++dst = '\0';
50be118519Sperry return (ret);
51be118519Sperry }
52be118519Sperry }
53be118519Sperry return (dst);
54be118519Sperry }
55