raw
genesis                 1 /*
genesis 2 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
genesis 3 *
genesis 4 * Permission to use, copy, modify, and distribute this software for any
genesis 5 * purpose with or without fee is hereby granted, provided that the above
genesis 6 * copyright notice and this permission notice appear in all copies.
genesis 7 *
genesis 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
genesis 9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
genesis 10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
genesis 11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
genesis 12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
genesis 13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
genesis 14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
genesis 15 */
genesis 16 #ifndef BITCOIN_STRLCPY_H
genesis 17 #define BITCOIN_STRLCPY_H
genesis 18 /*
genesis 19 * Copy src to string dst of size siz. At most siz-1 characters
genesis 20 * will be copied. Always NUL terminates (unless siz == 0).
genesis 21 * Returns strlen(src); if retval >= siz, truncation occurred.
genesis 22 */
genesis 23 inline size_t strlcpy(char *dst, const char *src, size_t siz)
genesis 24 {
genesis 25 char *d = dst;
genesis 26 const char *s = src;
genesis 27 size_t n = siz;
genesis 28
genesis 29 /* Copy as many bytes as will fit */
genesis 30 if (n != 0)
genesis 31 {
genesis 32 while (--n != 0)
genesis 33 {
genesis 34 if ((*d++ = *s++) == '\0')
genesis 35 break;
genesis 36 }
genesis 37 }
genesis 38
genesis 39 /* Not enough room in dst, add NUL and traverse rest of src */
genesis 40 if (n == 0)
genesis 41 {
genesis 42 if (siz != 0)
genesis 43 *d = '\0'; /* NUL-terminate dst */
genesis 44 while (*s++)
genesis 45 ;
genesis 46 }
genesis 47
genesis 48 return(s - src - 1); /* count does not include NUL */
genesis 49 }
genesis 50
genesis 51 /*
genesis 52 * Appends src to string dst of size siz (unlike strncat, siz is the
genesis 53 * full size of dst, not space left). At most siz-1 characters
genesis 54 * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
genesis 55 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
genesis 56 * If retval >= siz, truncation occurred.
genesis 57 */
genesis 58 inline size_t strlcat(char *dst, const char *src, size_t siz)
genesis 59 {
genesis 60 char *d = dst;
genesis 61 const char *s = src;
genesis 62 size_t n = siz;
genesis 63 size_t dlen;
genesis 64
genesis 65 /* Find the end of dst and adjust bytes left but don't go past end */
genesis 66 while (n-- != 0 && *d != '\0')
genesis 67 d++;
genesis 68 dlen = d - dst;
genesis 69 n = siz - dlen;
genesis 70
genesis 71 if (n == 0)
genesis 72 return(dlen + strlen(s));
genesis 73 while (*s != '\0')
genesis 74 {
genesis 75 if (n != 1)
genesis 76 {
genesis 77 *d++ = *s;
genesis 78 n--;
genesis 79 }
genesis 80 s++;
genesis 81 }
genesis 82 *d = '\0';
genesis 83
genesis 84 return(dlen + (s - src)); /* count does not include NUL */
genesis 85 }
genesis 86 #endif