1#if 0 /* comment in gmake; next line ignored by gcc */
2ifeq (0,gmake ignores from here)
3#endif
4/*-
5 * Copyright (c) 2006, 2008, 2011
6 *	Thorsten Glaser <tg@mirbsd.org>
7 *
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.
13 *
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.
22 *-
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. Thorsten Glaser also has merged this code
29 * with strxfrm(3) for ISO-10646-only systems and the wide char vari-
30 * ants wcslcat(3), wcslcpy(3), and wcsxfrm(3).
31 */
32
33#include <sys/types.h>
34#ifndef OUTSIDE_OF_LIBKERN
35#include <libckern.h>
36#endif
37
38#ifndef __RCSID
39#define __RCSID(x)		static const char __rcsid[] = x
40#endif
41
42__RCSID("$MirOS: src/bin/pax/.linked/strlfun.inc,v 1.4 2011/11/11 20:39:31 tg Exp $");
43
44#ifdef WIDEC
45#ifdef OUTSIDE_OF_LIBKERN
46#ifdef __WCHAR_TYPE__
47typedef __WCHAR_TYPE__ wchar_t;
48#else
49#include <wchar.h>
50#endif
51#endif
52/* wide character string functions */
53#define NUL			L'\0'
54#define char_t			wchar_t
55#define fn_len			wcslen
56#define	fn_cat			wcslcat
57#define fn_cpy			wcslcpy
58#else
59/* (multibyte) string functions */
60#define NUL			'\0'
61#define char_t			char
62#define fn_len			strlen
63#define	fn_cat			strlcat
64#define fn_cpy			strlcpy
65#endif
66
67#ifdef L_strxfrm
68#define strlcpy			strxfrm
69#define wcslcpy			wcsxfrm
70#define L_strlcpy
71#endif
72
73#ifdef OUTSIDE_OF_LIBKERN
74extern size_t fn_len(const char_t *);
75#endif
76
77#ifndef __predict_true
78#define __predict_true(exp)	(exp)
79#define __predict_false(exp)	(exp)
80#endif
81
82#ifdef L_strlcat
83/*
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.
89 */
90size_t
91fn_cat(char_t *dst, const char_t *src, size_t dlen)
92{
93	size_t n = 0, slen;
94
95	slen = fn_len(src);
96	while (__predict_true(n + 1 < dlen && dst[n] != NUL))
97		++n;
98	if (__predict_false(dlen == 0 || dst[n] != NUL))
99		return (dlen + slen);
100	while (__predict_true((slen > 0) && (n < (dlen - 1)))) {
101		dst[n++] = *src++;
102		--slen;
103	}
104	dst[n] = NUL;
105	return (n + slen);
106}
107#endif
108
109#ifdef L_strlcpy
110/* $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $ */
111
112/*-
113 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
114 *
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.
118 *
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.
126 */
127
128/*
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.
132 */
133size_t
134fn_cpy(char_t *dst, const char_t *src, size_t siz)
135{
136	const char_t *s = src;
137
138	if (__predict_false(siz == 0))
139		goto traverse_src;
140
141	/* copy as many chars as will fit */
142	while (--siz && (*dst++ = *s++))
143		;
144
145	/* not enough room in dst */
146	if (__predict_false(siz == 0)) {
147		/* safe to NUL-terminate dst since we copied <= siz-1 chars */
148		*dst = NUL;
149 traverse_src:
150		/* traverse rest of src */
151		while (*s++)
152			;
153	}
154
155	/* count does not include NUL */
156	return (s - src - 1);
157}
158#endif
159
160#if 0 /* gcc ignored from here; gmake stops ignoring */
161endif
162
163USE_WIDEC?=	1
164
165LIB=		libstrlfun.a
166OBJS=		strlcpy.o strlcat.o
167ifeq (1,$(strip $(USE_WIDEC)))
168OBJS+=		wcslcpy.o wcslcat.o
169endif
170DEFS=		-DOUTSIDE_OF_LIBKERN
171DEFS_strlcpy.o=	-DL_strlcpy
172DEFS_strlcat.o=	-DL_strlcat
173DEFS_wcslcpy.o=	-DL_strlcpy -DWIDEC
174DEFS_wcslcat.o=	-DL_strlcat -DWIDEC
175
176all: $(LIB)
177
178$(LIB): $(OBJS)
179	ar rc $(LIB) $(OBJS)
180	-ranlib $(LIB)
181
182$(OBJS): strlfun.c
183	$(CC) $(CFLAGS) $(CPPFLAGS) $(DEFS) $(DEFS_$@) -c -o $@ strlfun.c
184
185#endif /* EOF for gmake and gcc */
186