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