1290001Sglebius/* 2290001Sglebius * Why use strlcpy()/strlcat() instead of standard strncpy()/strncat()? 3290001Sglebius * To reduce likelihood of bugs and avoid wasteful zero fills. See: 4290001Sglebius * http://www.gratisoft.us/todd/papers/strlcpy.html 5290001Sglebius */ 6290001Sglebius 7290001Sglebius/* $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $ */ 8290001Sglebius 9290001Sglebius/* 10290001Sglebius * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> 11290001Sglebius * 12290001Sglebius * Permission to use, copy, modify, and distribute this software for any 13290001Sglebius * purpose with or without fee is hereby granted, provided that the above 14290001Sglebius * copyright notice and this permission notice appear in all copies. 15290001Sglebius * 16290001Sglebius * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 17290001Sglebius * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 18290001Sglebius * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 19290001Sglebius * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 20290001Sglebius * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 21290001Sglebius * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 22290001Sglebius * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 23290001Sglebius */ 24290001Sglebius 25290001Sglebius#include <config.h> /* + marks local changes */ 26290001Sglebius#ifdef HAVE_SYS_TYPES_H /* + */ 27290001Sglebius#include <sys/types.h> 28290001Sglebius#endif /* + */ 29290001Sglebius#include <string.h> 30290001Sglebius 31290001Sglebius#include "ntp_stdlib.h" /* + strlcpy, strlcat prototypes */ 32290001Sglebius 33290001Sglebius#ifndef HAVE_STRLCPY /* + */ 34290001Sglebius/* 35290001Sglebius * Copy src to string dst of size siz. At most siz-1 characters 36290001Sglebius * will be copied. Always NUL terminates (unless siz == 0). 37290001Sglebius * Returns strlen(src); if retval >= siz, truncation occurred. 38290001Sglebius */ 39290001Sglebiussize_t 40290001Sglebiusstrlcpy(char *dst, const char *src, size_t siz) 41290001Sglebius{ 42290001Sglebius char *d = dst; 43290001Sglebius const char *s = src; 44290001Sglebius size_t n = siz; 45290001Sglebius 46290001Sglebius /* Copy as many bytes as will fit */ 47290001Sglebius if (n != 0) { 48290001Sglebius while (--n != 0) { 49290001Sglebius if ((*d++ = *s++) == '\0') 50290001Sglebius break; 51290001Sglebius } 52290001Sglebius } 53290001Sglebius 54290001Sglebius /* Not enough room in dst, add NUL and traverse rest of src */ 55290001Sglebius if (n == 0) { 56290001Sglebius if (siz != 0) 57290001Sglebius *d = '\0'; /* NUL-terminate dst */ 58290001Sglebius while (*s++) 59290001Sglebius ; 60290001Sglebius } 61290001Sglebius 62290001Sglebius return(s - src - 1); /* count does not include NUL */ 63290001Sglebius} 64290001Sglebius#endif /* + */ 65290001Sglebius 66290001Sglebius 67290001Sglebius/* $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $ */ 68290001Sglebius 69290001Sglebius/* 70290001Sglebius * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> 71290001Sglebius * 72290001Sglebius * Permission to use, copy, modify, and distribute this software for any 73290001Sglebius * purpose with or without fee is hereby granted, provided that the above 74290001Sglebius * copyright notice and this permission notice appear in all copies. 75290001Sglebius * 76290001Sglebius * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 77290001Sglebius * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 78290001Sglebius * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 79290001Sglebius * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 80290001Sglebius * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 81290001Sglebius * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 82290001Sglebius * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 83290001Sglebius */ 84290001Sglebius 85290001Sglebius/* #include <sys/types.h> */ /* + */ 86290001Sglebius/* #include <string.h> */ /* + */ 87290001Sglebius 88290001Sglebius#ifndef HAVE_STRLCAT /* + */ 89290001Sglebius/* 90290001Sglebius * Appends src to string dst of size siz (unlike strncat, siz is the 91290001Sglebius * full size of dst, not space left). At most siz-1 characters 92290001Sglebius * will be copied. Always NUL terminates (unless siz <= strlen(dst)). 93290001Sglebius * Returns strlen(src) + MIN(siz, strlen(initial dst)). 94290001Sglebius * If retval >= siz, truncation occurred. 95290001Sglebius */ 96290001Sglebiussize_t 97290001Sglebiusstrlcat(char *dst, const char *src, size_t siz) 98290001Sglebius{ 99290001Sglebius char *d = dst; 100290001Sglebius const char *s = src; 101290001Sglebius size_t n = siz; 102290001Sglebius size_t dlen; 103290001Sglebius 104290001Sglebius /* Find the end of dst and adjust bytes left but don't go past end */ 105290001Sglebius while (n-- != 0 && *d != '\0') 106290001Sglebius d++; 107290001Sglebius dlen = d - dst; 108290001Sglebius n = siz - dlen; 109290001Sglebius 110290001Sglebius if (n == 0) 111290001Sglebius return(dlen + strlen(s)); 112290001Sglebius while (*s != '\0') { 113290001Sglebius if (n != 1) { 114290001Sglebius *d++ = *s; 115290001Sglebius n--; 116290001Sglebius } 117290001Sglebius s++; 118290001Sglebius } 119290001Sglebius *d = '\0'; 120290001Sglebius 121290001Sglebius return(dlen + (s - src)); /* count does not include NUL */ 122290001Sglebius} 123290001Sglebius#endif /* + */ 124