xref: /netbsd-src/external/bsd/ntp/dist/libntp/strl_obsd.c (revision cdfa2a7ef92791ba9db70a584a1d904730e6fb46)
1*cdfa2a7eSchristos /*	$NetBSD: strl_obsd.c,v 1.5 2020/05/25 20:47:24 christos Exp $	*/
28585484eSchristos 
38585484eSchristos /*
48585484eSchristos  * Why use strlcpy()/strlcat() instead of standard strncpy()/strncat()?
58585484eSchristos  * To reduce likelihood of bugs and avoid wasteful zero fills.  See:
68585484eSchristos  * http://www.gratisoft.us/todd/papers/strlcpy.html
78585484eSchristos  */
88585484eSchristos 
98585484eSchristos /*	$OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $	*/
108585484eSchristos 
118585484eSchristos /*
128585484eSchristos  * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
138585484eSchristos  *
148585484eSchristos  * Permission to use, copy, modify, and distribute this software for any
158585484eSchristos  * purpose with or without fee is hereby granted, provided that the above
168585484eSchristos  * copyright notice and this permission notice appear in all copies.
178585484eSchristos  *
188585484eSchristos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
198585484eSchristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
208585484eSchristos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
218585484eSchristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
228585484eSchristos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
238585484eSchristos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
248585484eSchristos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
258585484eSchristos  */
268585484eSchristos 
278585484eSchristos #include <config.h>		/* + marks local changes */
288585484eSchristos #ifdef HAVE_SYS_TYPES_H		/* + */
298585484eSchristos #include <sys/types.h>
308585484eSchristos #endif				/* + */
318585484eSchristos #include <string.h>
328585484eSchristos 
338585484eSchristos #include "ntp_stdlib.h"		/* + strlcpy, strlcat prototypes */
348585484eSchristos 
358585484eSchristos #ifndef HAVE_STRLCPY		/* + */
368585484eSchristos /*
378585484eSchristos  * Copy src to string dst of size siz.  At most siz-1 characters
388585484eSchristos  * will be copied.  Always NUL terminates (unless siz == 0).
398585484eSchristos  * Returns strlen(src); if retval >= siz, truncation occurred.
408585484eSchristos  */
418585484eSchristos size_t
strlcpy(char * dst,const char * src,size_t siz)428585484eSchristos strlcpy(char *dst, const char *src, size_t siz)
438585484eSchristos {
448585484eSchristos 	char *d = dst;
458585484eSchristos 	const char *s = src;
468585484eSchristos 	size_t n = siz;
478585484eSchristos 
488585484eSchristos 	/* Copy as many bytes as will fit */
498585484eSchristos 	if (n != 0) {
508585484eSchristos 		while (--n != 0) {
518585484eSchristos 			if ((*d++ = *s++) == '\0')
528585484eSchristos 				break;
538585484eSchristos 		}
548585484eSchristos 	}
558585484eSchristos 
568585484eSchristos 	/* Not enough room in dst, add NUL and traverse rest of src */
578585484eSchristos 	if (n == 0) {
588585484eSchristos 		if (siz != 0)
598585484eSchristos 			*d = '\0';		/* NUL-terminate dst */
608585484eSchristos 		while (*s++)
618585484eSchristos 			;
628585484eSchristos 	}
638585484eSchristos 
648585484eSchristos 	return(s - src - 1);	/* count does not include NUL */
658585484eSchristos }
668585484eSchristos #endif				/* + */
678585484eSchristos 
688585484eSchristos 
698585484eSchristos /*	$OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $	*/
708585484eSchristos 
718585484eSchristos /*
728585484eSchristos  * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
738585484eSchristos  *
748585484eSchristos  * Permission to use, copy, modify, and distribute this software for any
758585484eSchristos  * purpose with or without fee is hereby granted, provided that the above
768585484eSchristos  * copyright notice and this permission notice appear in all copies.
778585484eSchristos  *
788585484eSchristos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
798585484eSchristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
808585484eSchristos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
818585484eSchristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
828585484eSchristos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
838585484eSchristos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
848585484eSchristos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
858585484eSchristos  */
868585484eSchristos 
878585484eSchristos /* #include <sys/types.h> */	/* + */
888585484eSchristos /* #include <string.h> */	/* + */
898585484eSchristos 
908585484eSchristos #ifndef HAVE_STRLCAT		/* + */
918585484eSchristos /*
928585484eSchristos  * Appends src to string dst of size siz (unlike strncat, siz is the
938585484eSchristos  * full size of dst, not space left).  At most siz-1 characters
948585484eSchristos  * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
958585484eSchristos  * Returns strlen(src) + MIN(siz, strlen(initial dst)).
968585484eSchristos  * If retval >= siz, truncation occurred.
978585484eSchristos  */
988585484eSchristos size_t
strlcat(char * dst,const char * src,size_t siz)998585484eSchristos strlcat(char *dst, const char *src, size_t siz)
1008585484eSchristos {
1018585484eSchristos 	char *d = dst;
1028585484eSchristos 	const char *s = src;
1038585484eSchristos 	size_t n = siz;
1048585484eSchristos 	size_t dlen;
1058585484eSchristos 
1068585484eSchristos 	/* Find the end of dst and adjust bytes left but don't go past end */
1078585484eSchristos 	while (n-- != 0 && *d != '\0')
1088585484eSchristos 		d++;
1098585484eSchristos 	dlen = d - dst;
1108585484eSchristos 	n = siz - dlen;
1118585484eSchristos 
1128585484eSchristos 	if (n == 0)
1138585484eSchristos 		return(dlen + strlen(s));
1148585484eSchristos 	while (*s != '\0') {
1158585484eSchristos 		if (n != 1) {
1168585484eSchristos 			*d++ = *s;
1178585484eSchristos 			n--;
1188585484eSchristos 		}
1198585484eSchristos 		s++;
1208585484eSchristos 	}
1218585484eSchristos 	*d = '\0';
1228585484eSchristos 
1238585484eSchristos 	return(dlen + (s - src));	/* count does not include NUL */
1248585484eSchristos }
1258585484eSchristos #endif				/* + */
126