1/* $NetBSD: strl_obsd.c,v 1.5 2020/05/25 20:47:24 christos Exp $ */ 2 3/* 4 * Why use strlcpy()/strlcat() instead of standard strncpy()/strncat()? 5 * To reduce likelihood of bugs and avoid wasteful zero fills. See: 6 * http://www.gratisoft.us/todd/papers/strlcpy.html 7 */ 8 9/* $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $ */ 10 11/* 12 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> 13 * 14 * Permission to use, copy, modify, and distribute this software for any 15 * purpose with or without fee is hereby granted, provided that the above 16 * copyright notice and this permission notice appear in all copies. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 19 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 20 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 21 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 23 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 24 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 25 */ 26 27#include <config.h> /* + marks local changes */ 28#ifdef HAVE_SYS_TYPES_H /* + */ 29#include <sys/types.h> 30#endif /* + */ 31#include <string.h> 32 33#include "ntp_stdlib.h" /* + strlcpy, strlcat prototypes */ 34 35#ifndef HAVE_STRLCPY /* + */ 36/* 37 * Copy src to string dst of size siz. At most siz-1 characters 38 * will be copied. Always NUL terminates (unless siz == 0). 39 * Returns strlen(src); if retval >= siz, truncation occurred. 40 */ 41size_t 42strlcpy(char *dst, const char *src, size_t siz) 43{ 44 char *d = dst; 45 const char *s = src; 46 size_t n = siz; 47 48 /* Copy as many bytes as will fit */ 49 if (n != 0) { 50 while (--n != 0) { 51 if ((*d++ = *s++) == '\0') 52 break; 53 } 54 } 55 56 /* Not enough room in dst, add NUL and traverse rest of src */ 57 if (n == 0) { 58 if (siz != 0) 59 *d = '\0'; /* NUL-terminate dst */ 60 while (*s++) 61 ; 62 } 63 64 return(s - src - 1); /* count does not include NUL */ 65} 66#endif /* + */ 67 68 69/* $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $ */ 70 71/* 72 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> 73 * 74 * Permission to use, copy, modify, and distribute this software for any 75 * purpose with or without fee is hereby granted, provided that the above 76 * copyright notice and this permission notice appear in all copies. 77 * 78 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 79 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 80 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 81 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 82 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 83 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 84 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 85 */ 86 87/* #include <sys/types.h> */ /* + */ 88/* #include <string.h> */ /* + */ 89 90#ifndef HAVE_STRLCAT /* + */ 91/* 92 * Appends src to string dst of size siz (unlike strncat, siz is the 93 * full size of dst, not space left). At most siz-1 characters 94 * will be copied. Always NUL terminates (unless siz <= strlen(dst)). 95 * Returns strlen(src) + MIN(siz, strlen(initial dst)). 96 * If retval >= siz, truncation occurred. 97 */ 98size_t 99strlcat(char *dst, const char *src, size_t siz) 100{ 101 char *d = dst; 102 const char *s = src; 103 size_t n = siz; 104 size_t dlen; 105 106 /* Find the end of dst and adjust bytes left but don't go past end */ 107 while (n-- != 0 && *d != '\0') 108 d++; 109 dlen = d - dst; 110 n = siz - dlen; 111 112 if (n == 0) 113 return(dlen + strlen(s)); 114 while (*s != '\0') { 115 if (n != 1) { 116 *d++ = *s; 117 n--; 118 } 119 s++; 120 } 121 *d = '\0'; 122 123 return(dlen + (s - src)); /* count does not include NUL */ 124} 125#endif /* + */ 126