| [3c89b8a] | 1 | /*-
|
|---|
| [c41ecbb] | 2 | * Copyright (c) 2006, 2008
|
|---|
| 3 | * Thorsten Glaser <tg@mirbsd.org>
|
|---|
| [3c89b8a] | 4 | *
|
|---|
| [3f0223f] | 5 | * Provided that these terms and disclaimer and all copyright notices
|
|---|
| 6 | * are retained or reproduced in an accompanying document, permission
|
|---|
| 7 | * is granted to deal in this work without restriction, including un-
|
|---|
| 8 | * limited rights to use, publicly perform, distribute, sell, modify,
|
|---|
| 9 | * merge, give away, or sublicence.
|
|---|
| [3c89b8a] | 10 | *
|
|---|
| [3f0223f] | 11 | * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
|
|---|
| 12 | * the utmost extent permitted by applicable law, neither express nor
|
|---|
| 13 | * implied; without malicious intent or gross negligence. In no event
|
|---|
| 14 | * may a licensor, author or contributor be held liable for indirect,
|
|---|
| 15 | * direct, other damage, loss, or other issues arising in any way out
|
|---|
| 16 | * of dealing in the work, even if advised of the possibility of such
|
|---|
| 17 | * damage or existence of a defect, except proven that it results out
|
|---|
| 18 | * of said person's immediate fault when using the work as intended.
|
|---|
| [6a232c1] | 19 | *-
|
|---|
| [c41ecbb] | 20 | * The original implementations of strlcpy(3) and strlcat(3) are from
|
|---|
| 21 | * Todd C. Miller; the licence is reproduced below. However, this ap-
|
|---|
| 22 | * plies only to the strlcpy(3) portion of the code, as Thorsten Gla-
|
|---|
| 23 | * ser write the following strlcat(3) implementation according to the
|
|---|
| 24 | * spec. Both functions below have been optimised according to sugge-
|
|---|
| 25 | * stions from Bodo Eggert. Thorsten Glaser also has merged this code
|
|---|
| 26 | * with strxfrm(3) for ISO-10646-only systems and wrote the wide char
|
|---|
| 27 | * variants wcslcat(3), wcslcpy(3), and wcsxfrm(3) (see wcslfun.c).
|
|---|
| [3c89b8a] | 28 | */
|
|---|
| 29 |
|
|---|
| [3f0223f] | 30 | #ifdef STRXFRM
|
|---|
| 31 | #undef HAVE_STRLCPY
|
|---|
| 32 | #undef HAVE_STRLCAT
|
|---|
| 33 | #define HAVE_STRLCPY 0
|
|---|
| 34 | #define HAVE_STRLCAT 1
|
|---|
| 35 | #define strlcpy strxfrm
|
|---|
| 36 | #endif
|
|---|
| 37 |
|
|---|
| [6a232c1] | 38 | #include <sys/types.h>
|
|---|
| 39 | #if defined(_KERNEL) || defined(_STANDALONE)
|
|---|
| 40 | #include <lib/libkern/libkern.h>
|
|---|
| 41 | #undef HAVE_STRLCPY
|
|---|
| 42 | #undef HAVE_STRLCAT
|
|---|
| 43 | #else
|
|---|
| [c41ecbb] | 44 | #include <stddef.h> /* for size_t in user space (SUSv3) */
|
|---|
| [6a232c1] | 45 | #if defined(HAVE_CONFIG_H) && (HAVE_CONFIG_H != 0)
|
|---|
| [3c89b8a] | 46 | /* usually when packaged with third-party software */
|
|---|
| [6a232c1] | 47 | #ifdef CONFIG_H_FILENAME
|
|---|
| 48 | #include CONFIG_H_FILENAME
|
|---|
| 49 | #else
|
|---|
| [3c89b8a] | 50 | #include "config.h"
|
|---|
| 51 | #endif
|
|---|
| [6a232c1] | 52 | #endif
|
|---|
| [c41ecbb] | 53 | /* do not include <string.h> to prevent redefinition warnings */
|
|---|
| [3c89b8a] | 54 | extern size_t strlen(const char *);
|
|---|
| [6a232c1] | 55 | #endif
|
|---|
| [3c89b8a] | 56 |
|
|---|
| 57 | #ifndef __RCSID
|
|---|
| [6a232c1] | 58 | #undef __IDSTRING
|
|---|
| 59 | #undef __IDSTRING_CONCAT
|
|---|
| 60 | #undef __IDSTRING_EXPAND
|
|---|
| 61 | #define __IDSTRING_CONCAT(l,p) __LINTED__ ## l ## _ ## p
|
|---|
| 62 | #define __IDSTRING_EXPAND(l,p) __IDSTRING_CONCAT(l,p)
|
|---|
| 63 | #define __IDSTRING(prefix, string) \
|
|---|
| 64 | static const char __IDSTRING_EXPAND(__LINE__,prefix) [] \
|
|---|
| 65 | __attribute__((used)) = "@(""#)" #prefix ": " string
|
|---|
| [c41ecbb] | 66 | #define __RCSID(x) __IDSTRING(rcsid,x)
|
|---|
| [3c89b8a] | 67 | #endif
|
|---|
| 68 |
|
|---|
| [6a232c1] | 69 | #ifndef __predict_true
|
|---|
| 70 | #define __predict_true(exp) ((exp) != 0)
|
|---|
| 71 | #endif
|
|---|
| 72 | #ifndef __predict_false
|
|---|
| 73 | #define __predict_false(exp) ((exp) != 0)
|
|---|
| [3c89b8a] | 74 | #endif
|
|---|
| 75 |
|
|---|
| [3f0223f] | 76 | #if !defined(_KERNEL) && !defined(_STANDALONE)
|
|---|
| [a569125] | 77 | __RCSID("$MirOS: contrib/code/jupp/strlfun.c,v 1.9 2008/08/01 12:29:27 tg Rel $");
|
|---|
| [3f0223f] | 78 | #endif
|
|---|
| [6a232c1] | 79 |
|
|---|
| [c41ecbb] | 80 | /* (multibyte) string functions */
|
|---|
| 81 | #undef NUL
|
|---|
| 82 | #undef char_t
|
|---|
| 83 | #define NUL '\0'
|
|---|
| 84 | #define char_t char
|
|---|
| 85 |
|
|---|
| 86 | size_t strlcat(char_t *, const char_t *, size_t);
|
|---|
| 87 | size_t strlcpy(char_t *, const char_t *, size_t);
|
|---|
| [3c89b8a] | 88 |
|
|---|
| [6a232c1] | 89 | #if !defined(HAVE_STRLCAT) || (HAVE_STRLCAT == 0)
|
|---|
| 90 | /*
|
|---|
| [c41ecbb] | 91 | * Appends src to string dst of size dlen (unlike strncat, dlen is the
|
|---|
| 92 | * full size of dst, not space left). At most dlen-1 characters
|
|---|
| 93 | * will be copied. Always NUL terminates (unless dlen <= strlen(dst)).
|
|---|
| 94 | * Returns strlen(src) + MIN(dlen, strlen(initial dst)), without the
|
|---|
| 95 | * trailing NUL byte counted. If retval >= dlen, truncation occurred.
|
|---|
| [6a232c1] | 96 | */
|
|---|
| 97 | size_t
|
|---|
| [c41ecbb] | 98 | strlcat(char_t *dst, const char_t *src, size_t dlen)
|
|---|
| [6a232c1] | 99 | {
|
|---|
| 100 | size_t n = 0, slen;
|
|---|
| 101 |
|
|---|
| 102 | slen = strlen(src);
|
|---|
| [c41ecbb] | 103 | while (__predict_true(n + 1 < dlen && dst[n] != NUL))
|
|---|
| [6a232c1] | 104 | ++n;
|
|---|
| [c41ecbb] | 105 | if (__predict_false(dlen == 0 || dst[n] != NUL))
|
|---|
| [6a232c1] | 106 | return (dlen + slen);
|
|---|
| 107 | while (__predict_true((slen > 0) && (n < (dlen - 1)))) {
|
|---|
| 108 | dst[n++] = *src++;
|
|---|
| 109 | --slen;
|
|---|
| 110 | }
|
|---|
| [c41ecbb] | 111 | dst[n] = NUL;
|
|---|
| [6a232c1] | 112 | return (n + slen);
|
|---|
| 113 | }
|
|---|
| 114 | #endif /* !HAVE_STRLCAT */
|
|---|
| 115 |
|
|---|
| [c41ecbb] | 116 | #if !defined(HAVE_STRLCPY) || (HAVE_STRLCPY == 0)
|
|---|
| 117 | /* $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $ */
|
|---|
| [6a232c1] | 118 |
|
|---|
| 119 | /*-
|
|---|
| 120 | * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
|
|---|
| 121 | *
|
|---|
| 122 | * Permission to use, copy, modify, and distribute this software for any
|
|---|
| 123 | * purpose with or without fee is hereby granted, provided that the above
|
|---|
| 124 | * copyright notice and this permission notice appear in all copies.
|
|---|
| [c41ecbb] | 125 | *
|
|---|
| 126 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|---|
| 127 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|---|
| 128 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|---|
| 129 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|---|
| 130 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|---|
| 131 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|---|
| 132 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|---|
| [6a232c1] | 133 | */
|
|---|
| 134 |
|
|---|
| [3c89b8a] | 135 | /*
|
|---|
| 136 | * Copy src to string dst of size siz. At most siz-1 characters
|
|---|
| 137 | * will be copied. Always NUL terminates (unless siz == 0).
|
|---|
| 138 | * Returns strlen(src); if retval >= siz, truncation occurred.
|
|---|
| 139 | */
|
|---|
| 140 | size_t
|
|---|
| [c41ecbb] | 141 | strlcpy(char_t *dst, const char_t *src, size_t siz)
|
|---|
| [3c89b8a] | 142 | {
|
|---|
| [c41ecbb] | 143 | const char_t *s = src;
|
|---|
| [3c89b8a] | 144 |
|
|---|
| [6a232c1] | 145 | if (__predict_false(siz == 0))
|
|---|
| 146 | goto traverse_src;
|
|---|
| [3c89b8a] | 147 |
|
|---|
| [6a232c1] | 148 | /* copy as many chars as will fit */
|
|---|
| 149 | while (--siz && (*dst++ = *s++))
|
|---|
| [3c89b8a] | 150 | ;
|
|---|
| 151 |
|
|---|
| [6a232c1] | 152 | /* not enough room in dst */
|
|---|
| 153 | if (__predict_false(siz == 0)) {
|
|---|
| 154 | /* safe to NUL-terminate dst since we copied <= siz-1 chars */
|
|---|
| [c41ecbb] | 155 | *dst = NUL;
|
|---|
| [6a232c1] | 156 | traverse_src:
|
|---|
| 157 | /* traverse rest of src */
|
|---|
| [3c89b8a] | 158 | while (*s++)
|
|---|
| 159 | ;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| [c41ecbb] | 162 | /* count does not include NUL */
|
|---|
| [6a232c1] | 163 | return (s - src - 1);
|
|---|
| [3c89b8a] | 164 | }
|
|---|
| 165 | #endif /* !HAVE_STRLCPY */
|
|---|