1/* $NetBSD: string.c,v 1.8 2024/02/21 22:52:29 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#if !defined(HAVE_STRLCPY) 61size_t 62strlcpy(char *dst, const char *src, size_t size) { 63 char *d = dst; 64 const char *s = src; 65 size_t n = size; 66 67 /* Copy as many bytes as will fit */ 68 if (n != 0U && --n != 0U) { 69 do { 70 if ((*d++ = *s++) == 0) { 71 break; 72 } 73 } while (--n != 0U); 74 } 75 76 /* Not enough room in dst, add NUL and traverse rest of src */ 77 if (n == 0U) { 78 if (size != 0U) { 79 *d = '\0'; /* NUL-terminate dst */ 80 } 81 while (*s++) { 82 } 83 } 84 85 return (s - src - 1); /* count does not include NUL */ 86} 87#endif /* !defined(HAVE_STRLCPY) */ 88 89#if !defined(HAVE_STRLCAT) 90size_t 91strlcat(char *dst, const char *src, size_t size) { 92 char *d = dst; 93 const char *s = src; 94 size_t n = size; 95 size_t dlen; 96 97 /* Find the end of dst and adjust bytes left but don't go past end */ 98 while (n-- != 0U && *d != '\0') { 99 d++; 100 } 101 dlen = d - dst; 102 n = size - dlen; 103 104 if (n == 0U) { 105 return (dlen + strlen(s)); 106 } 107 while (*s != '\0') { 108 if (n != 1U) { 109 *d++ = *s; 110 n--; 111 } 112 s++; 113 } 114 *d = '\0'; 115 116 return (dlen + (s - src)); /* count does not include NUL */ 117} 118#endif /* !defined(HAVE_STRLCAT) */ 119 120#if !defined(HAVE_STRNSTR) 121char * 122strnstr(const char *s, const char *find, size_t slen) { 123 char c, sc; 124 size_t len; 125 126 if ((c = *find++) != '\0') { 127 len = strlen(find); 128 do { 129 do { 130 if (slen-- < 1 || (sc = *s++) == '\0') 131 return (NULL); 132 } while (sc != c); 133 if (len > slen) 134 return (NULL); 135 } while (strncmp(s, find, len) != 0); 136 s--; 137 } 138 return ((char *)s); 139} 140#endif 141 142int 143isc_string_strerror_r(int errnum, char *buf, size_t buflen) { 144 return (strerror_r(errnum, buf, buflen)); 145} 146