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