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