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