1*0b57cec5SDimitry Andric /*
2*0b57cec5SDimitry Andric * This code is derived from OpenBSD's libc, original license follows:
3*0b57cec5SDimitry Andric *
4*0b57cec5SDimitry Andric * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
5*0b57cec5SDimitry Andric *
6*0b57cec5SDimitry Andric * Permission to use, copy, modify, and distribute this software for any
7*0b57cec5SDimitry Andric * purpose with or without fee is hereby granted, provided that the above
8*0b57cec5SDimitry Andric * copyright notice and this permission notice appear in all copies.
9*0b57cec5SDimitry Andric *
10*0b57cec5SDimitry Andric * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11*0b57cec5SDimitry Andric * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12*0b57cec5SDimitry Andric * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13*0b57cec5SDimitry Andric * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14*0b57cec5SDimitry Andric * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15*0b57cec5SDimitry Andric * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16*0b57cec5SDimitry Andric * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*0b57cec5SDimitry Andric */
18*0b57cec5SDimitry Andric
19*0b57cec5SDimitry Andric #include <sys/types.h>
20*0b57cec5SDimitry Andric #include <string.h>
21*0b57cec5SDimitry Andric
22*0b57cec5SDimitry Andric #include "regex_impl.h"
23*0b57cec5SDimitry Andric /*
24*0b57cec5SDimitry Andric * Copy src to string dst of size siz. At most siz-1 characters
25*0b57cec5SDimitry Andric * will be copied. Always NUL terminates (unless siz == 0).
26*0b57cec5SDimitry Andric * Returns strlen(src); if retval >= siz, truncation occurred.
27*0b57cec5SDimitry Andric */
28*0b57cec5SDimitry Andric size_t
llvm_strlcpy(char * dst,const char * src,size_t siz)29*0b57cec5SDimitry Andric llvm_strlcpy(char *dst, const char *src, size_t siz)
30*0b57cec5SDimitry Andric {
31*0b57cec5SDimitry Andric char *d = dst;
32*0b57cec5SDimitry Andric const char *s = src;
33*0b57cec5SDimitry Andric size_t n = siz;
34*0b57cec5SDimitry Andric
35*0b57cec5SDimitry Andric /* Copy as many bytes as will fit */
36*0b57cec5SDimitry Andric if (n != 0) {
37*0b57cec5SDimitry Andric while (--n != 0) {
38*0b57cec5SDimitry Andric if ((*d++ = *s++) == '\0')
39*0b57cec5SDimitry Andric break;
40*0b57cec5SDimitry Andric }
41*0b57cec5SDimitry Andric }
42*0b57cec5SDimitry Andric
43*0b57cec5SDimitry Andric /* Not enough room in dst, add NUL and traverse rest of src */
44*0b57cec5SDimitry Andric if (n == 0) {
45*0b57cec5SDimitry Andric if (siz != 0)
46*0b57cec5SDimitry Andric *d = '\0'; /* NUL-terminate dst */
47*0b57cec5SDimitry Andric while (*s++)
48*0b57cec5SDimitry Andric ;
49*0b57cec5SDimitry Andric }
50*0b57cec5SDimitry Andric
51*0b57cec5SDimitry Andric return(s - src - 1); /* count does not include NUL */
52*0b57cec5SDimitry Andric }
53