xref: /freebsd/sys/libkern/strncat.c (revision 29363fb446372cb3f10bc98664e9767c53fbb457)
1f8146b88SJung-uk Kim /*-
2f8146b88SJung-uk Kim  * Copyright (c) 1990, 1993
3f8146b88SJung-uk Kim  *	The Regents of the University of California.  All rights reserved.
4f8146b88SJung-uk Kim  *
5f8146b88SJung-uk Kim  * This code is derived from software contributed to Berkeley by
6f8146b88SJung-uk Kim  * Chris Torek.
7f8146b88SJung-uk Kim  *
8f8146b88SJung-uk Kim  * Redistribution and use in source and binary forms, with or without
9f8146b88SJung-uk Kim  * modification, are permitted provided that the following conditions
10f8146b88SJung-uk Kim  * are met:
11f8146b88SJung-uk Kim  * 1. Redistributions of source code must retain the above copyright
12f8146b88SJung-uk Kim  *    notice, this list of conditions and the following disclaimer.
13f8146b88SJung-uk Kim  * 2. Redistributions in binary form must reproduce the above copyright
14f8146b88SJung-uk Kim  *    notice, this list of conditions and the following disclaimer in the
15f8146b88SJung-uk Kim  *    documentation and/or other materials provided with the distribution.
16f8146b88SJung-uk Kim  * 3. Neither the name of the University nor the names of its contributors
17f8146b88SJung-uk Kim  *    may be used to endorse or promote products derived from this software
18f8146b88SJung-uk Kim  *    without specific prior written permission.
19f8146b88SJung-uk Kim  *
20f8146b88SJung-uk Kim  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21f8146b88SJung-uk Kim  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22f8146b88SJung-uk Kim  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23f8146b88SJung-uk Kim  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24f8146b88SJung-uk Kim  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25f8146b88SJung-uk Kim  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26f8146b88SJung-uk Kim  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27f8146b88SJung-uk Kim  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28f8146b88SJung-uk Kim  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29f8146b88SJung-uk Kim  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30f8146b88SJung-uk Kim  * SUCH DAMAGE.
31f8146b88SJung-uk Kim  */
32f8146b88SJung-uk Kim 
33f8146b88SJung-uk Kim #include <sys/cdefs.h>
34f8146b88SJung-uk Kim #include <sys/libkern.h>
35f8146b88SJung-uk Kim 
36f8146b88SJung-uk Kim /*
37f8146b88SJung-uk Kim  * Concatenate src on the end of dst.  At most strlen(dst)+n+1 bytes
38f8146b88SJung-uk Kim  * are written at dst (at most n+1 bytes being appended).  Return dst.
39f8146b88SJung-uk Kim  */
40f8146b88SJung-uk Kim char *
strncat(char * dst,const char * src,size_t n)41f8146b88SJung-uk Kim strncat(char *dst, const char *src, size_t n)
42f8146b88SJung-uk Kim {
43f8146b88SJung-uk Kim 
44f8146b88SJung-uk Kim 	if (n != 0) {
45f8146b88SJung-uk Kim 		char *d = dst;
46f8146b88SJung-uk Kim 		const char *s = src;
47f8146b88SJung-uk Kim 
48f8146b88SJung-uk Kim 		while (*d != 0)
49f8146b88SJung-uk Kim 			d++;
50f8146b88SJung-uk Kim 		do {
51*85b64892SEd Maste 			if ((*d = *s++) == '\0')
52f8146b88SJung-uk Kim 				break;
53f8146b88SJung-uk Kim 			d++;
54f8146b88SJung-uk Kim 		} while (--n != 0);
55*85b64892SEd Maste 		*d = '\0';
56f8146b88SJung-uk Kim 	}
57f8146b88SJung-uk Kim 	return (dst);
58f8146b88SJung-uk Kim }
59