xref: /freebsd/contrib/openpam/lib/libpam/openpam_strlcpy.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_STRLCPY
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_strlcpy.h"
39*ce77a8d6SDag-Erling Smørgrav 
40*ce77a8d6SDag-Erling Smørgrav /* like strcpy(3), but always NUL-terminates; returns strlen(src) */
41*ce77a8d6SDag-Erling Smørgrav size_t
openpam_strlcpy(char * dst,const char * src,size_t size)42*ce77a8d6SDag-Erling Smørgrav openpam_strlcpy(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; *src && size > 1; ++len, --size)
47*ce77a8d6SDag-Erling Smørgrav 		*dst++ = *src++;
48*ce77a8d6SDag-Erling Smørgrav 	*dst = '\0';
49*ce77a8d6SDag-Erling Smørgrav 	while (*src)
50*ce77a8d6SDag-Erling Smørgrav 		++len, ++src;
51*ce77a8d6SDag-Erling Smørgrav 	return (len);
52*ce77a8d6SDag-Erling Smørgrav }
53*ce77a8d6SDag-Erling Smørgrav 
54*ce77a8d6SDag-Erling Smørgrav #endif
55