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