1 #if 0 /* comment in gmake; next line ignored by gcc */
2 ifeq (0,gmake ignores from here)
5 * Copyright (c) 2006, 2008, 2011
6 * mirabilos <m@mirbsd.org>
8 * Provided that these terms and disclaimer and all copyright notices
9 * are retained or reproduced in an accompanying document, permission
10 * is granted to deal in this work without restriction, including un-
11 * limited rights to use, publicly perform, distribute, sell, modify,
12 * merge, give away, or sublicence.
14 * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
15 * the utmost extent permitted by applicable law, neither express nor
16 * implied; without malicious intent or gross negligence. In no event
17 * may a licensor, author or contributor be held liable for indirect,
18 * direct, other damage, loss, or other issues arising in any way out
19 * of dealing in the work, even if advised of the possibility of such
20 * damage or existence of a defect, except proven that it results out
21 * of said person's immediate fault when using the work as intended.
23 * The original implementations of strlcpy(3) and strlcat(3) are from
24 * Todd C. Miller; the licence is reproduced below. However, this ap-
25 * plies only to the strlcpy(3) portion of the code, as Thorsten Gla-
26 * ser write the following strlcat(3) implementation according to the
27 * spec. Both functions below have been optimised according to sugge-
28 * stions from Bodo Eggert. mirabilos merged the code with strxfrm(3)
29 * (Unicode-only systems) and the wide character variants wcslcat(3),
30 * wcslcpy(3), and wcsxfrm(3).
33 #include <sys/types.h>
34 #ifndef OUTSIDE_OF_LIBKERN
39 #define __RCSID(x) static const char __rcsid[] = x
42 __RCSID("$MirOS: contrib/code/jupp/strlfun.inc,v 1.5 2016/03/06 13:47:13 tg Exp $");
45 #ifdef OUTSIDE_OF_LIBKERN
47 typedef __WCHAR_TYPE__ wchar_t;
52 /* wide character string functions */
54 #define char_t wchar_t
56 #define fn_cat wcslcat
57 #define fn_cpy wcslcpy
59 /* (multibyte) string functions */
63 #define fn_cat strlcat
64 #define fn_cpy strlcpy
68 #define strlcpy strxfrm
69 #define wcslcpy wcsxfrm
73 #ifdef OUTSIDE_OF_LIBKERN
74 extern size_t fn_len(const char_t *);
77 #ifndef __predict_true
78 #define __predict_true(exp) (exp)
79 #define __predict_false(exp) (exp)
84 * Appends src to string dst of size dlen (unlike strncat, dlen is the
85 * full size of dst, not space left). At most dlen-1 characters
86 * will be copied. Always NUL terminates (unless dlen <= strlen(dst)).
87 * Returns strlen(src) + MIN(dlen, strlen(initial dst)), without the
88 * trailing NUL byte counted. If retval >= dlen, truncation occurred.
91 fn_cat(char_t *dst, const char_t *src, size_t dlen)
96 while (__predict_true(n + 1 < dlen && dst[n] != NUL))
98 if (__predict_false(dlen == 0 || dst[n] != NUL))
100 while (__predict_true((slen > 0) && (n < (dlen - 1)))) {
110 /* $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $ */
113 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
115 * Permission to use, copy, modify, and distribute this software for any
116 * purpose with or without fee is hereby granted, provided that the above
117 * copyright notice and this permission notice appear in all copies.
119 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
120 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
121 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
122 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
123 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
124 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
125 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
129 * Copy src to string dst of size siz. At most siz-1 characters
130 * will be copied. Always NUL terminates (unless siz == 0).
131 * Returns strlen(src); if retval >= siz, truncation occurred.
134 fn_cpy(char_t *dst, const char_t *src, size_t siz)
136 const char_t *s = src;
138 if (__predict_false(siz == 0))
141 /* copy as many chars as will fit */
142 while (--siz && (*dst++ = *s++))
145 /* not enough room in dst */
146 if (__predict_false(siz == 0)) {
147 /* safe to NUL-terminate dst since we copied <= siz-1 chars */
150 /* traverse rest of src */
155 /* count does not include NUL */
156 return (s - src - 1);
160 #if 0 /* gcc ignored from here; gmake stops ignoring */
166 OBJS= strlcpy.o strlcat.o
167 ifeq (1,$(strip $(USE_WIDEC)))
168 OBJS+= wcslcpy.o wcslcat.o
170 DEFS= -DOUTSIDE_OF_LIBKERN
171 DEFS_strlcpy.o= -DL_strlcpy
172 DEFS_strlcat.o= -DL_strlcat
173 DEFS_wcslcpy.o= -DL_strlcpy -DWIDEC
174 DEFS_wcslcat.o= -DL_strlcat -DWIDEC
183 $(CC) $(CFLAGS) $(CPPFLAGS) $(DEFS) $(DEFS_$@) -c -o $@ strlfun.c
185 #endif /* EOF for gmake and gcc */