xref: /OK3568_Linux_fs/external/xserver/os/strlcat.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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 /*
26*4882a593Smuzhiyun  * Appends src to string dst of size siz (unlike strncat, siz is the
27*4882a593Smuzhiyun  * full size of dst, not space left).  At most siz-1 characters
28*4882a593Smuzhiyun  * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
29*4882a593Smuzhiyun  * Returns strlen(src) + MIN(siz, strlen(initial dst)).
30*4882a593Smuzhiyun  * If retval >= siz, truncation occurred.
31*4882a593Smuzhiyun  */
32*4882a593Smuzhiyun size_t
strlcat(char * dst,const char * src,size_t siz)33*4882a593Smuzhiyun strlcat(char *dst, const char *src, size_t siz)
34*4882a593Smuzhiyun {
35*4882a593Smuzhiyun     register char *d = dst;
36*4882a593Smuzhiyun     register const char *s = src;
37*4882a593Smuzhiyun     register size_t n = siz;
38*4882a593Smuzhiyun     size_t dlen;
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun     /* Find the end of dst and adjust bytes left but don't go past end */
41*4882a593Smuzhiyun     while (n-- != 0 && *d != '\0')
42*4882a593Smuzhiyun         d++;
43*4882a593Smuzhiyun     dlen = d - dst;
44*4882a593Smuzhiyun     n = siz - dlen;
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun     if (n == 0)
47*4882a593Smuzhiyun         return (dlen + strlen(s));
48*4882a593Smuzhiyun     while (*s != '\0') {
49*4882a593Smuzhiyun         if (n != 1) {
50*4882a593Smuzhiyun             *d++ = *s;
51*4882a593Smuzhiyun             n--;
52*4882a593Smuzhiyun         }
53*4882a593Smuzhiyun         s++;
54*4882a593Smuzhiyun     }
55*4882a593Smuzhiyun     *d = '\0';
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun     return (dlen + (s - src));  /* count does not include NUL */
58*4882a593Smuzhiyun }
59