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