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