| 1 |
#if 0 /* comment in gmake; next line ignored by gcc */ |
| 2 |
ifeq (0,gmake ignores from here) |
| 3 |
#endif |
| 4 |
/*- |
| 5 |
* Copyright (c) 2006, 2008, 2011 |
| 6 |
* mirabilos <[email protected]> |
| 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; its licence is reproduced in the L_strlcpy part as |
| 25 |
* it applies to only that now; mirabilos rewrote strlcat from specs, |
| 26 |
* optimised both from suggestions by Bodo Eggert, aliased strlcpy as |
| 27 |
* just-copying strxfrm(3) and introduced wcslcat(3), wcslcpy(3) and, |
| 28 |
* symmetrically, wcsxfrm(3) as wide character variants. |
| 29 |
*/ |
| 30 |
|
| 31 |
#include <sys/types.h> |
| 32 |
#if defined(MBSDPORT_H) |
| 33 |
#include MBSDPORT_H |
| 34 |
#elif !defined(OUTSIDE_OF_LIBKERN) |
| 35 |
#include <libckern.h> |
| 36 |
#endif |
| 37 |
|
| 38 |
#ifndef __RCSID |
| 39 |
#define __RCSID(x) /* fallback */ static const char __rcsid[] = x |
| 40 |
#endif |
| 41 |
|
| 42 |
__RCSID("$MirOS: src/bin/pax/.linked/strlfun.inc,v 1.9 2020/09/06 02:24:02 tg Exp $"); |
| 43 |
|
| 44 |
#ifdef WIDEC |
| 45 |
#if defined(MBSDPORT_H) || defined(OUTSIDE_OF_LIBKERN) |
| 46 |
#ifdef __WCHAR_TYPE__ |
| 47 |
typedef __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 |
#if defined(MBSDPORT_H) || defined(OUTSIDE_OF_LIBKERN) |
| 74 |
extern size_t fn_len(const char_t *); |
| 75 |
#ifdef NEED_STRLFUN_PROTOS |
| 76 |
size_t fn_cat(char_t *, const char_t *, size_t); |
| 77 |
size_t fn_cpy(char_t *, const char_t *, size_t); |
| 78 |
#endif |
| 79 |
#endif |
| 80 |
|
| 81 |
#ifndef __predict_true |
| 82 |
#define __predict_true(exp) (exp) |
| 83 |
#define __predict_false(exp) (exp) |
| 84 |
#endif |
| 85 |
|
| 86 |
#ifdef L_strlcat |
| 87 |
/* |
| 88 |
* Appends src to string dst of size dlen (unlike strncat, dlen is the |
| 89 |
* full size of dst, not space left). At most dlen-1 characters |
| 90 |
* will be copied. Always NUL terminates (unless dlen <= strlen(dst)). |
| 91 |
* Returns strlen(src) + MIN(dlen, strlen(initial dst)), without the |
| 92 |
* trailing NUL byte counted. If retval >= dlen, truncation occurred. |
| 93 |
*/ |
| 94 |
size_t |
| 95 |
fn_cat(char_t *dst, const char_t *src, size_t dlen) |
| 96 |
{ |
| 97 |
size_t n = 0, slen; |
| 98 |
|
| 99 |
slen = fn_len(src); |
| 100 |
while (__predict_true(n + 1 < dlen && dst[n] != NUL)) |
| 101 |
++n; |
| 102 |
if (__predict_false(dlen == 0 || dst[n] != NUL)) |
| 103 |
return (dlen + slen); |
| 104 |
while (__predict_true((slen > 0) && (n < (dlen - 1)))) { |
| 105 |
dst[n++] = *src++; |
| 106 |
--slen; |
| 107 |
} |
| 108 |
dst[n] = NUL; |
| 109 |
return (n + slen); |
| 110 |
} |
| 111 |
#endif |
| 112 |
|
| 113 |
#ifdef L_strlcpy |
| 114 |
/* $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $ */ |
| 115 |
|
| 116 |
/*- |
| 117 |
* Copyright (c) 1998 Todd C. Miller <[email protected]> |
| 118 |
* |
| 119 |
* Permission to use, copy, modify, and distribute this software for any |
| 120 |
* purpose with or without fee is hereby granted, provided that the above |
| 121 |
* copyright notice and this permission notice appear in all copies. |
| 122 |
* |
| 123 |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 124 |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 125 |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 126 |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 127 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 128 |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 129 |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 130 |
*/ |
| 131 |
|
| 132 |
/* |
| 133 |
* Copy src to string dst of size siz. At most siz-1 characters |
| 134 |
* will be copied. Always NUL terminates (unless siz == 0). |
| 135 |
* Returns strlen(src); if retval >= siz, truncation occurred. |
| 136 |
*/ |
| 137 |
size_t |
| 138 |
fn_cpy(char_t *dst, const char_t *src, size_t siz) |
| 139 |
{ |
| 140 |
const char_t *s = src; |
| 141 |
|
| 142 |
if (__predict_false(siz == 0)) |
| 143 |
goto traverse_src; |
| 144 |
|
| 145 |
/* copy as many chars as will fit */ |
| 146 |
while (--siz && (*dst++ = *s++)) |
| 147 |
; |
| 148 |
|
| 149 |
/* not enough room in dst */ |
| 150 |
if (__predict_false(siz == 0)) { |
| 151 |
/* safe to NUL-terminate dst since we copied <= siz-1 chars */ |
| 152 |
*dst = NUL; |
| 153 |
traverse_src: |
| 154 |
/* traverse rest of src */ |
| 155 |
while (*s++) |
| 156 |
; |
| 157 |
} |
| 158 |
|
| 159 |
/* count does not include NUL */ |
| 160 |
return (s - src - 1); |
| 161 |
} |
| 162 |
#endif |
| 163 |
|
| 164 |
#if 0 /* gcc ignored from here; gmake stops ignoring */ |
| 165 |
endif |
| 166 |
|
| 167 |
USE_WIDEC?= 1 |
| 168 |
|
| 169 |
LIB= libstrlfun.a |
| 170 |
OBJS= strlcpy.o strlcat.o |
| 171 |
ifeq (1,$(strip $(USE_WIDEC))) |
| 172 |
OBJS+= wcslcpy.o wcslcat.o |
| 173 |
endif |
| 174 |
DEFS= -DOUTSIDE_OF_LIBKERN |
| 175 |
DEFS_strlcpy.o= -DL_strlcpy |
| 176 |
DEFS_strlcat.o= -DL_strlcat |
| 177 |
DEFS_wcslcpy.o= -DL_strlcpy -DWIDEC |
| 178 |
DEFS_wcslcat.o= -DL_strlcat -DWIDEC |
| 179 |
|
| 180 |
all: $(LIB) |
| 181 |
|
| 182 |
$(LIB): $(OBJS) |
| 183 |
ar rc $(LIB) $(OBJS) |
| 184 |
-ranlib $(LIB) |
| 185 |
|
| 186 |
$(OBJS): strlfun.c |
| 187 |
$(CC) $(CFLAGS) $(CPPFLAGS) $(DEFS) $(DEFS_$@) -c -o $@ strlfun.c |
| 188 |
|
| 189 |
#endif /* EOF for gmake and gcc */ |