-
+ 6184E61CD8740055811EF3B1E662C5A0161F9F209F85EC79287C5391D66FB91ECD625D9D7F9327C751AB1D799821409AFC9552147C45A2BD636574DBA2B78955bitcoin/src/strlcpy.h(0 . 0)(1 . 86)
20958 /*
20959 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
20960 *
20961 * Permission to use, copy, modify, and distribute this software for any
20962 * purpose with or without fee is hereby granted, provided that the above
20963 * copyright notice and this permission notice appear in all copies.
20964 *
20965 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
20966 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
20967 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
20968 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20969 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20970 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20971 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20972 */
20973 #ifndef BITCOIN_STRLCPY_H
20974 #define BITCOIN_STRLCPY_H
20975 /*
20976 * Copy src to string dst of size siz. At most siz-1 characters
20977 * will be copied. Always NUL terminates (unless siz == 0).
20978 * Returns strlen(src); if retval >= siz, truncation occurred.
20979 */
20980 inline size_t strlcpy(char *dst, const char *src, size_t siz)
20981 {
20982 char *d = dst;
20983 const char *s = src;
20984 size_t n = siz;
20985
20986 /* Copy as many bytes as will fit */
20987 if (n != 0)
20988 {
20989 while (--n != 0)
20990 {
20991 if ((*d++ = *s++) == '\0')
20992 break;
20993 }
20994 }
20995
20996 /* Not enough room in dst, add NUL and traverse rest of src */
20997 if (n == 0)
20998 {
20999 if (siz != 0)
21000 *d = '\0'; /* NUL-terminate dst */
21001 while (*s++)
21002 ;
21003 }
21004
21005 return(s - src - 1); /* count does not include NUL */
21006 }
21007
21008 /*
21009 * Appends src to string dst of size siz (unlike strncat, siz is the
21010 * full size of dst, not space left). At most siz-1 characters
21011 * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
21012 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
21013 * If retval >= siz, truncation occurred.
21014 */
21015 inline size_t strlcat(char *dst, const char *src, size_t siz)
21016 {
21017 char *d = dst;
21018 const char *s = src;
21019 size_t n = siz;
21020 size_t dlen;
21021
21022 /* Find the end of dst and adjust bytes left but don't go past end */
21023 while (n-- != 0 && *d != '\0')
21024 d++;
21025 dlen = d - dst;
21026 n = siz - dlen;
21027
21028 if (n == 0)
21029 return(dlen + strlen(s));
21030 while (*s != '\0')
21031 {
21032 if (n != 1)
21033 {
21034 *d++ = *s;
21035 n--;
21036 }
21037 s++;
21038 }
21039 *d = '\0';
21040
21041 return(dlen + (s - src)); /* count does not include NUL */
21042 }
21043 #endif