xref: /freebsd/contrib/openpam/lib/libpam/openpam_strlcat.c (revision 3ba4c8c81a28de3e00ccf6d7f92c6f8e58bef456)
1*ce77a8d6SDag-Erling Smørgrav /*-
2*ce77a8d6SDag-Erling Smørgrav  * Copyright (c) 2011-2012 Dag-Erling Smørgrav
3*ce77a8d6SDag-Erling Smørgrav  * All rights reserved.
4*ce77a8d6SDag-Erling Smørgrav  *
5*ce77a8d6SDag-Erling Smørgrav  * Redistribution and use in source and binary forms, with or without
6*ce77a8d6SDag-Erling Smørgrav  * modification, are permitted provided that the following conditions
7*ce77a8d6SDag-Erling Smørgrav  * are met:
8*ce77a8d6SDag-Erling Smørgrav  * 1. Redistributions of source code must retain the above copyright
9*ce77a8d6SDag-Erling Smørgrav  *    notice, this list of conditions and the following disclaimer.
10*ce77a8d6SDag-Erling Smørgrav  * 2. Redistributions in binary form must reproduce the above copyright
11*ce77a8d6SDag-Erling Smørgrav  *    notice, this list of conditions and the following disclaimer in the
12*ce77a8d6SDag-Erling Smørgrav  *    documentation and/or other materials provided with the distribution.
13*ce77a8d6SDag-Erling Smørgrav  * 3. The name of the author may not be used to endorse or promote
14*ce77a8d6SDag-Erling Smørgrav  *    products derived from this software without specific prior written
15*ce77a8d6SDag-Erling Smørgrav  *    permission.
16*ce77a8d6SDag-Erling Smørgrav  *
17*ce77a8d6SDag-Erling Smørgrav  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18*ce77a8d6SDag-Erling Smørgrav  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*ce77a8d6SDag-Erling Smørgrav  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*ce77a8d6SDag-Erling Smørgrav  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21*ce77a8d6SDag-Erling Smørgrav  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*ce77a8d6SDag-Erling Smørgrav  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23*ce77a8d6SDag-Erling Smørgrav  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24*ce77a8d6SDag-Erling Smørgrav  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25*ce77a8d6SDag-Erling Smørgrav  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26*ce77a8d6SDag-Erling Smørgrav  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*ce77a8d6SDag-Erling Smørgrav  * SUCH DAMAGE.
28*ce77a8d6SDag-Erling Smørgrav  */
29*ce77a8d6SDag-Erling Smørgrav 
30*ce77a8d6SDag-Erling Smørgrav #ifdef HAVE_CONFIG_H
31*ce77a8d6SDag-Erling Smørgrav # include "config.h"
32*ce77a8d6SDag-Erling Smørgrav #endif
33*ce77a8d6SDag-Erling Smørgrav 
34*ce77a8d6SDag-Erling Smørgrav #ifndef HAVE_STRLCAT
35*ce77a8d6SDag-Erling Smørgrav 
36*ce77a8d6SDag-Erling Smørgrav #include <stddef.h>
37*ce77a8d6SDag-Erling Smørgrav 
38*ce77a8d6SDag-Erling Smørgrav #include "openpam_strlcat.h"
39*ce77a8d6SDag-Erling Smørgrav 
40*ce77a8d6SDag-Erling Smørgrav /* like strcat(3), but always NUL-terminates; returns strlen(src) */
41*ce77a8d6SDag-Erling Smørgrav size_t
openpam_strlcat(char * dst,const char * src,size_t size)42*ce77a8d6SDag-Erling Smørgrav openpam_strlcat(char *dst, const char *src, size_t size)
43*ce77a8d6SDag-Erling Smørgrav {
44*ce77a8d6SDag-Erling Smørgrav 	size_t len;
45*ce77a8d6SDag-Erling Smørgrav 
46*ce77a8d6SDag-Erling Smørgrav 	for (len = 0; *dst && size > 1; ++len, --size)
47*ce77a8d6SDag-Erling Smørgrav 		dst++;
48*ce77a8d6SDag-Erling Smørgrav 	for (; *src && size > 1; ++len, --size)
49*ce77a8d6SDag-Erling Smørgrav 		*dst++ = *src++;
50*ce77a8d6SDag-Erling Smørgrav 	*dst = '\0';
51*ce77a8d6SDag-Erling Smørgrav 	while (*src)
52*ce77a8d6SDag-Erling Smørgrav 		++len, ++src;
53*ce77a8d6SDag-Erling Smørgrav 	return (len);
54*ce77a8d6SDag-Erling Smørgrav }
55*ce77a8d6SDag-Erling Smørgrav 
56*ce77a8d6SDag-Erling Smørgrav #endif
57