| 1 | /* $MirOS: src/lib/libc/string/strlfun.c,v 1.5 2005/09/19 19:01:11 tg Exp $ */
|
|---|
| 2 | /* $OpenBSD: strlcpy.c,v 1.10 2005/08/08 08:05:37 espie Exp $ */
|
|---|
| 3 | /* $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $ */
|
|---|
| 4 |
|
|---|
| 5 | /*-
|
|---|
| 6 | * Copyright (c) 2004, 2005 Thorsten "mirabile" Glaser <tg@66h.42h.de>
|
|---|
| 7 | * Thanks to Bodo Eggert for optimisation hints
|
|---|
| 8 | * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
|
|---|
| 9 | *
|
|---|
| 10 | * Permission to use, copy, modify, and distribute this software for any
|
|---|
| 11 | * purpose with or without fee is hereby granted, provided that the above
|
|---|
| 12 | * copyright notice and this permission notice appear in all copies.
|
|---|
| 13 | *
|
|---|
| 14 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|---|
| 15 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|---|
| 16 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|---|
| 17 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|---|
| 18 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|---|
| 19 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|---|
| 20 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|---|
| 21 | */
|
|---|
| 22 |
|
|---|
| 23 | #if !defined(_KERNEL) && !defined(_STANDALONE)
|
|---|
| 24 | #ifdef HAVE_CONFIG_H
|
|---|
| 25 | /* usually when packaged with third-party software */
|
|---|
| 26 | #include "config.h"
|
|---|
| 27 | #endif
|
|---|
| 28 | #include <sys/types.h>
|
|---|
| 29 |
|
|---|
| 30 | extern size_t strlen(const char *);
|
|---|
| 31 |
|
|---|
| 32 | #ifndef __RCSID
|
|---|
| 33 | #define __RCSID(x) static const char __rcsid[] = (x)
|
|---|
| 34 | #endif
|
|---|
| 35 |
|
|---|
| 36 | __RCSID("$MirOS: src/lib/libc/string/strlfun.c,v 1.5 2005/09/19 19:01:11 tg Exp $");
|
|---|
| 37 | #else
|
|---|
| 38 | #include <lib/libkern/libkern.h>
|
|---|
| 39 | #undef HAVE_STRLCPY
|
|---|
| 40 | #undef HAVE_STRLCAT
|
|---|
| 41 | #endif
|
|---|
| 42 |
|
|---|
| 43 | size_t strlcat(char *, const char *, size_t);
|
|---|
| 44 | size_t strlcpy(char *, const char *, size_t);
|
|---|
| 45 |
|
|---|
| 46 | #ifndef HAVE_STRLCPY
|
|---|
| 47 | /*
|
|---|
| 48 | * Copy src to string dst of size siz. At most siz-1 characters
|
|---|
| 49 | * will be copied. Always NUL terminates (unless siz == 0).
|
|---|
| 50 | * Returns strlen(src); if retval >= siz, truncation occurred.
|
|---|
| 51 | */
|
|---|
| 52 | size_t
|
|---|
| 53 | strlcpy(char *dst, const char *src, size_t siz)
|
|---|
| 54 | {
|
|---|
| 55 | const char *s = src;
|
|---|
| 56 |
|
|---|
| 57 | if (!siz) goto traverse_src;
|
|---|
| 58 |
|
|---|
| 59 | /* Copy as many bytes as will fit */
|
|---|
| 60 | for (; --siz && (*dst++ = *s++); /* nothing */)
|
|---|
| 61 | ;
|
|---|
| 62 |
|
|---|
| 63 | /* Not enough room in dst, add NUL and traverse rest of src */
|
|---|
| 64 | if (!siz) {
|
|---|
| 65 | /* Save, since we've copied at max. (siz-1) characters */
|
|---|
| 66 | *dst = '\0'; /* NUL-terminate dst */
|
|---|
| 67 | traverse_src:
|
|---|
| 68 | while (*s++)
|
|---|
| 69 | ;
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | return (s - src - 1); /* count does not include NUL */
|
|---|
| 73 | }
|
|---|
| 74 | #endif /* !HAVE_STRLCPY */
|
|---|
| 75 |
|
|---|
| 76 | #ifndef HAVE_STRLCAT
|
|---|
| 77 | /*
|
|---|
| 78 | * Appends src to string dst of size siz (unlike strncat, siz is the
|
|---|
| 79 | * full size of dst, not space left). At most siz-1 characters
|
|---|
| 80 | * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
|
|---|
| 81 | * Returns strlen(src) + MIN(siz, strlen(initial dst)).
|
|---|
| 82 | * If retval >= siz, truncation occurred.
|
|---|
| 83 | */
|
|---|
| 84 | size_t
|
|---|
| 85 | strlcat(char *dst, const char *src, size_t siz)
|
|---|
| 86 | {
|
|---|
| 87 | char *d = dst;
|
|---|
| 88 | size_t dl, n = siz;
|
|---|
| 89 | const size_t sl = strlen(src);
|
|---|
| 90 |
|
|---|
| 91 | while (n-- && (*d++ != '\0'))
|
|---|
| 92 | ;
|
|---|
| 93 | if (!++n && (*d != '\0'))
|
|---|
| 94 | return strlen(src);
|
|---|
| 95 |
|
|---|
| 96 | dl = --d - dst; /* original strlen(dst), max. siz-1 */
|
|---|
| 97 | n = siz - dl;
|
|---|
| 98 | dl += sl;
|
|---|
| 99 |
|
|---|
| 100 | if (!n--)
|
|---|
| 101 | return dl;
|
|---|
| 102 |
|
|---|
| 103 | if (n > sl)
|
|---|
| 104 | n = sl; /* number of octets to copy */
|
|---|
| 105 | for (; n-- && (*d++ = *src++); /* nothing */)
|
|---|
| 106 | ;
|
|---|
| 107 | *d = '\0'; /* NUL-terminate dst */
|
|---|
| 108 | return dl;
|
|---|
| 109 | }
|
|---|
| 110 | #endif /* !HAVE_STRLCAT */
|
|---|