1 /* $NetBSD: string.c,v 1.9 2025/01/26 16:25:38 christos Exp $ */ 2 3 /* 4 * Copyright (C) Internet Systems Consortium, Inc. ("ISC") 5 * 6 * SPDX-License-Identifier: MPL-2.0 7 * 8 * This Source Code Form is subject to the terms of the Mozilla Public 9 * License, v. 2.0. If a copy of the MPL was not distributed with this 10 * file, you can obtain one at https://mozilla.org/MPL/2.0/. 11 * 12 * See the COPYRIGHT file distributed with this work for additional 13 * information regarding copyright ownership. 14 */ 15 16 /* 17 * SPDX-License-Identifier: BSD-3-Clause 18 * 19 * Copyright (c) 2001 Mike Barcroft <mike@FreeBSD.org> 20 * Copyright (c) 1990, 1993 21 * The Regents of the University of California. All rights reserved. 22 * 23 * This code is derived from software contributed to Berkeley by 24 * Chris Torek. 25 * 26 * Redistribution and use in source and binary forms, with or without 27 * modification, are permitted provided that the following conditions 28 * are met: 29 * 1. Redistributions of source code must retain the above copyright 30 * notice, this list of conditions and the following disclaimer. 31 * 2. Redistributions in binary form must reproduce the above copyright 32 * notice, this list of conditions and the following disclaimer in the 33 * documentation and/or other materials provided with the distribution. 34 * 3. Neither the name of the University nor the names of its contributors 35 * may be used to endorse or promote products derived from this software 36 * without specific prior written permission. 37 * 38 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 39 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 41 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 42 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 43 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 44 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 45 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 46 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 47 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 */ 50 51 /*! \file */ 52 53 #ifdef _GNU_SOURCE 54 #undef _GNU_SOURCE 55 #endif /* ifdef _GNU_SOURCE */ 56 #include <string.h> 57 58 #include <isc/string.h> /* IWYU pragma: keep */ 59 60 /* 61 * We undef _GNU_SOURCE above to get the POSIX strerror_r() 62 */ 63 int 64 isc_string_strerror_r(int errnum, char *buf, size_t buflen) { 65 return strerror_r(errnum, buf, buflen); 66 } 67 68 #if !defined(HAVE_STRLCPY) 69 size_t 70 strlcpy(char *dst, const char *src, size_t size) { 71 char *d = dst; 72 const char *s = src; 73 size_t n = size; 74 75 /* Copy as many bytes as will fit */ 76 if (n != 0U && --n != 0U) { 77 do { 78 if ((*d++ = *s++) == 0) { 79 break; 80 } 81 } while (--n != 0U); 82 } 83 84 /* Not enough room in dst, add NUL and traverse rest of src */ 85 if (n == 0U) { 86 if (size != 0U) { 87 *d = '\0'; /* NUL-terminate dst */ 88 } 89 while (*s++) { 90 } 91 } 92 93 return s - src - 1; /* count does not include NUL */ 94 } 95 #endif /* !defined(HAVE_STRLCPY) */ 96 97 #if !defined(HAVE_STRLCAT) 98 size_t 99 strlcat(char *dst, const char *src, size_t size) { 100 char *d = dst; 101 const char *s = src; 102 size_t n = size; 103 size_t dlen; 104 105 /* Find the end of dst and adjust bytes left but don't go past end */ 106 while (n-- != 0U && *d != '\0') { 107 d++; 108 } 109 dlen = d - dst; 110 n = size - dlen; 111 112 if (n == 0U) { 113 return dlen + strlen(s); 114 } 115 while (*s != '\0') { 116 if (n != 1U) { 117 *d++ = *s; 118 n--; 119 } 120 s++; 121 } 122 *d = '\0'; 123 124 return dlen + (s - src); /* count does not include NUL */ 125 } 126 #endif /* !defined(HAVE_STRLCAT) */ 127 128 #if !defined(HAVE_STRNSTR) 129 char * 130 strnstr(const char *s, const char *find, size_t slen) { 131 char c, sc; 132 size_t len; 133 134 if ((c = *find++) != '\0') { 135 len = strlen(find); 136 do { 137 do { 138 if (slen-- < 1 || (sc = *s++) == '\0') { 139 return NULL; 140 } 141 } while (sc != c); 142 if (len > slen) { 143 return NULL; 144 } 145 } while (strncmp(s, find, len) != 0); 146 s--; 147 } 148 return (char *)s; 149 } 150 #endif 151