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