1999-02-05 04:57:20 +00:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
1999-02-05 04:57:20 +00:00
|
|
|
*/
|
|
|
|
|
2014-01-09 22:02:06 -08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1990, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2014-01-24 09:46:00 -08:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
2014-01-09 22:02:06 -08:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
2005-04-27 04:57:32 +00:00
|
|
|
|
|
|
|
/*! \file */
|
2000-06-22 22:00:42 +00:00
|
|
|
|
2018-04-05 13:44:25 +02:00
|
|
|
#include <config.h> // IWYU pragma: keep
|
2000-04-28 22:40:10 +00:00
|
|
|
|
2018-04-05 13:44:25 +02:00
|
|
|
#include <string.h>
|
2000-04-28 22:40:10 +00:00
|
|
|
|
2018-04-05 13:44:25 +02:00
|
|
|
#include "isc/string.h" // IWYU pragma: keep
|
1999-02-05 04:57:20 +00:00
|
|
|
|
2003-04-10 04:47:56 +00:00
|
|
|
size_t
|
2003-11-17 01:19:52 +00:00
|
|
|
isc_string_strlcpy(char *dst, const char *src, size_t size)
|
2003-04-10 04:47:56 +00:00
|
|
|
{
|
|
|
|
char *d = dst;
|
|
|
|
const char *s = src;
|
|
|
|
size_t n = size;
|
|
|
|
|
|
|
|
/* Copy as many bytes as will fit */
|
2004-03-18 02:58:08 +00:00
|
|
|
if (n != 0U && --n != 0U) {
|
2003-04-10 04:47:56 +00:00
|
|
|
do {
|
2018-04-05 13:44:25 +02:00
|
|
|
if ((*d++ = *s++) == 0) {
|
2003-04-10 04:47:56 +00:00
|
|
|
break;
|
2018-04-05 13:44:25 +02:00
|
|
|
}
|
2004-03-18 02:58:08 +00:00
|
|
|
} while (--n != 0U);
|
2003-04-10 04:47:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Not enough room in dst, add NUL and traverse rest of src */
|
2004-03-18 02:58:08 +00:00
|
|
|
if (n == 0U) {
|
2018-04-05 13:44:25 +02:00
|
|
|
if (size != 0U) {
|
2003-04-10 04:47:56 +00:00
|
|
|
*d = '\0'; /* NUL-terminate dst */
|
2018-04-05 13:44:25 +02:00
|
|
|
}
|
2003-04-10 04:47:56 +00:00
|
|
|
while (*s++)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
return(s - src - 1); /* count does not include NUL */
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2003-11-17 01:19:52 +00:00
|
|
|
isc_string_strlcat(char *dst, const char *src, size_t size)
|
2003-04-10 04:47:56 +00:00
|
|
|
{
|
|
|
|
char *d = dst;
|
|
|
|
const char *s = src;
|
|
|
|
size_t n = size;
|
|
|
|
size_t dlen;
|
|
|
|
|
|
|
|
/* Find the end of dst and adjust bytes left but don't go past end */
|
2018-04-05 13:44:25 +02:00
|
|
|
while (n-- != 0U && *d != '\0') {
|
2003-04-10 04:47:56 +00:00
|
|
|
d++;
|
2018-04-05 13:44:25 +02:00
|
|
|
}
|
2003-04-10 04:47:56 +00:00
|
|
|
dlen = d - dst;
|
|
|
|
n = size - dlen;
|
|
|
|
|
2018-04-05 13:44:25 +02:00
|
|
|
if (n == 0U) {
|
2003-04-10 04:47:56 +00:00
|
|
|
return(dlen + strlen(s));
|
2018-04-05 13:44:25 +02:00
|
|
|
}
|
2003-04-10 04:47:56 +00:00
|
|
|
while (*s != '\0') {
|
2004-03-18 02:58:08 +00:00
|
|
|
if (n != 1U) {
|
2003-04-10 04:47:56 +00:00
|
|
|
*d++ = *s;
|
|
|
|
n--;
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
*d = '\0';
|
|
|
|
|
|
|
|
return(dlen + (s - src)); /* count does not include NUL */
|
|
|
|
}
|