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