xref: /netbsd-src/common/lib/libc/string/strncat.c (revision 8fdd01b3ee3648a1f82d1d1e902a4d74eef2c998)
1*8fdd01b3Smrg /*	$NetBSD: strncat.c,v 1.3 2018/02/04 01:13:45 mrg Exp $	*/
2967db624Schristos 
3967db624Schristos /*-
4967db624Schristos  * Copyright (c) 1990, 1993
5967db624Schristos  *	The Regents of the University of California.  All rights reserved.
6967db624Schristos  *
7967db624Schristos  * This code is derived from software contributed to Berkeley by
8967db624Schristos  * Chris Torek.
9967db624Schristos  *
10967db624Schristos  * Redistribution and use in source and binary forms, with or without
11967db624Schristos  * modification, are permitted provided that the following conditions
12967db624Schristos  * are met:
13967db624Schristos  * 1. Redistributions of source code must retain the above copyright
14967db624Schristos  *    notice, this list of conditions and the following disclaimer.
15967db624Schristos  * 2. Redistributions in binary form must reproduce the above copyright
16967db624Schristos  *    notice, this list of conditions and the following disclaimer in the
17967db624Schristos  *    documentation and/or other materials provided with the distribution.
18967db624Schristos  * 3. Neither the name of the University nor the names of its contributors
19967db624Schristos  *    may be used to endorse or promote products derived from this software
20967db624Schristos  *    without specific prior written permission.
21967db624Schristos  *
22967db624Schristos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23967db624Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24967db624Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25967db624Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26967db624Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27967db624Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28967db624Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29967db624Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30967db624Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31967db624Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32967db624Schristos  * SUCH DAMAGE.
33967db624Schristos  */
34967db624Schristos 
35967db624Schristos #include <sys/cdefs.h>
36967db624Schristos #if defined(LIBC_SCCS) && !defined(lint)
37967db624Schristos #if 0
38967db624Schristos static char sccsid[] = "@(#)strncat.c	8.1 (Berkeley) 6/4/93";
39967db624Schristos #else
40*8fdd01b3Smrg __RCSID("$NetBSD: strncat.c,v 1.3 2018/02/04 01:13:45 mrg Exp $");
41967db624Schristos #endif
42967db624Schristos #endif /* LIBC_SCCS and not lint */
43967db624Schristos 
4496602b9eSchristos #if !defined(_KERNEL) && !defined(_STANDALONE)
45967db624Schristos #include <assert.h>
46967db624Schristos #include <string.h>
4796602b9eSchristos #else
4896602b9eSchristos #include <lib/libkern/libkern.h>
4996602b9eSchristos #endif
50967db624Schristos 
51967db624Schristos #ifdef _FORTIFY_SOURCE
52967db624Schristos #undef strncat
53967db624Schristos #endif
54967db624Schristos 
55967db624Schristos /*
56967db624Schristos  * Concatenate src on the end of dst.  At most strlen(dst)+n+1 bytes
57967db624Schristos  * are written at dst (at most n+1 bytes being appended).  Return dst.
58967db624Schristos  */
59967db624Schristos char *
strncat(char * dst,const char * src,size_t n)60967db624Schristos strncat(char *dst, const char *src, size_t n)
61967db624Schristos {
62967db624Schristos 
63967db624Schristos 	if (n != 0) {
64967db624Schristos 		char *d = dst;
65967db624Schristos 		const char *s = src;
66967db624Schristos 
67967db624Schristos 		while (*d != 0)
68967db624Schristos 			d++;
69967db624Schristos 		do {
70967db624Schristos 			if ((*d = *s++) == 0)
71967db624Schristos 				break;
72967db624Schristos 			d++;
73967db624Schristos 		} while (--n != 0);
74967db624Schristos 		*d = 0;
75967db624Schristos 	}
76967db624Schristos 	return (dst);
77967db624Schristos }
78