]> Pileus Git - ~andy/fetchmail/blob - strlcat.c
db9d43c225038c58bb6a17b478752a9dd433fd16
[~andy/fetchmail] / strlcat.c
1 /*      $NetBSD: strlcat.c,v 1.16 2003/10/27 00:12:42 lukem Exp $       */
2 /*      $OpenBSD: strlcat.c,v 1.10 2003/04/12 21:56:39 millert Exp $    */
3
4 /*
5  * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND TODD C. MILLER DISCLAIMS ALL
12  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
13  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL TODD C. MILLER BE LIABLE
14  * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
16  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
17  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19
20 #if HAVE_NBTOOL_CONFIG_H
21 #include "nbtool_config.h"
22 #endif
23
24 #include <sys/cdefs.h>
25 #if defined(LIBC_SCCS) && !defined(lint)
26 __RCSID("$NetBSD: strlcat.c,v 1.16 2003/10/27 00:12:42 lukem Exp $");
27 #endif /* LIBC_SCCS and not lint */
28
29 #ifdef _LIBC
30 #include "namespace.h"
31 #endif
32 #include <sys/types.h>
33 #include <assert.h>
34 #include <string.h>
35
36 #ifdef _LIBC
37 # ifdef __weak_alias
38 __weak_alias(strlcat, _strlcat)
39 # endif
40 #endif
41
42 #if !HAVE_STRLCAT
43 /*
44  * Appends src to string dst of size siz (unlike strncat, siz is the
45  * full size of dst, not space left).  At most siz-1 characters
46  * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
47  * Returns strlen(src) + MIN(siz, strlen(initial dst)).
48  * If retval >= siz, truncation occurred.
49  */
50 size_t
51 #ifdef _LIBC
52 _strlcat(dst, src, siz)
53 #else
54 strlcat(dst, src, siz)
55 #endif
56         char *dst;
57         const char *src;
58         size_t siz;
59 {
60         char *d = dst;
61         const char *s = src;
62         size_t n = siz;
63         size_t dlen;
64
65         _DIAGASSERT(dst != NULL);
66         _DIAGASSERT(src != NULL);
67
68         /* Find the end of dst and adjust bytes left but don't go past end */
69         while (n-- != 0 && *d != '\0')
70                 d++;
71         dlen = d - dst;
72         n = siz - dlen;
73
74         if (n == 0)
75                 return(dlen + strlen(s));
76         while (*s != '\0') {
77                 if (n != 1) {
78                         *d++ = *s;
79                         n--;
80                 }
81                 s++;
82         }
83         *d = '\0';
84
85         return(dlen + (s - src));       /* count does not include NUL */
86 }
87 #endif