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