From 77971a31b2b0e319bc305469a11e3ec427b05d4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mattias=20Walstr=C3=B6m?= Date: Tue, 29 Jan 2013 13:19:28 +0100 Subject: [PATCH] libite: Add strlcat and strlcpy. These function only exist on uClibc and BSD, so if they not already defined in string.h, define them here. --- Makefile | 2 +- lite.h | 6 +++++ strlcat.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ strlcpy.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 strlcat.c create mode 100644 strlcpy.c diff --git a/Makefile b/Makefile index daf0adb..c8afb7b 100644 --- a/Makefile +++ b/Makefile @@ -27,7 +27,7 @@ include ../common.mk CFLAGS += -fPIC HEADERS = lite.h -OBJS := copyfile.o dir.o fexist.o fisdir.o fmode.o rsync.o +OBJS := copyfile.o dir.o fexist.o fisdir.o fmode.o rsync.o strlcpy.o strlcat.o DEPS := $(addprefix ., $(OBJS:.o=.d)) VER = 0 diff --git a/lite.h b/lite.h index 6842e11..e08e944 100644 --- a/lite.h +++ b/lite.h @@ -42,6 +42,12 @@ int copy_filep (FILE *src, FILE *dst); int dir (const char *dir, const char *type, int (*filter) (const char *file), char ***list, int strip); int rsync (char *src, char *dst, int delete, int (*filter) (const char *file)); +#ifndef strlcpy +size_t strlcpy (char *dst, const char *src, size_t siz); +#endif +#ifndef strlcat +size_t strlcat (char *dst, const char *src, size_t siz); +#endif static inline int fisslashdir(char *dir) { if (!dir) return 0; diff --git a/strlcat.c b/strlcat.c new file mode 100644 index 0000000..18406f3 --- /dev/null +++ b/strlcat.c @@ -0,0 +1,66 @@ +/* $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $ */ + +/* + * Copyright (c) 1998 Todd C. Miller + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include + +#ifndef strlcat +/* + * Appends src to string dst of size siz (unlike strncat, siz is the + * full size of dst, not space left). At most siz-1 characters + * will be copied. Always NUL terminates (unless siz <= strlen(dst)). + * Returns strlen(src) + MIN(siz, strlen(initial dst)). + * If retval >= siz, truncation occurred. + */ +size_t +strlcat(char *dst, const char *src, size_t siz) +{ + char *d = dst; + const char *s = src; + size_t n = siz; + size_t dlen; + + /* Find the end of dst and adjust bytes left but don't go past end */ + while (n-- != 0 && *d != '\0') + d++; + dlen = d - dst; + n = siz - dlen; + + if (n == 0) + return(dlen + strlen(s)); + while (*s != '\0') { + if (n != 1) { + *d++ = *s; + n--; + } + s++; + } + *d = '\0'; + + return(dlen + (s - src)); /* count does not include NUL */ +} +#endif + +/** + * Local Variables: + * version-control: t + * c-file-style: "bsd" + * End: + */ + + diff --git a/strlcpy.c b/strlcpy.c new file mode 100644 index 0000000..e20808d --- /dev/null +++ b/strlcpy.c @@ -0,0 +1,61 @@ +/* $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $ */ + +/* + * Copyright (c) 1998 Todd C. Miller + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include + +#ifndef strlcpy +/* + * Copy src to string dst of size siz. At most siz-1 characters + * will be copied. Always NUL terminates (unless siz == 0). + * Returns strlen(src); if retval >= siz, truncation occurred. + */ +size_t +strlcpy(char *dst, const char *src, size_t siz) +{ + char *d = dst; + const char *s = src; + size_t n = siz; + + /* Copy as many bytes as will fit */ + if (n != 0) { + while (--n != 0) { + if ((*d++ = *s++) == '\0') + break; + } + } + + /* Not enough room in dst, add NUL and traverse rest of src */ + if (n == 0) { + if (siz != 0) + *d = '\0'; /* NUL-terminate dst */ + while (*s++) + ; + } + + return(s - src - 1); /* count does not include NUL */ +} + +#endif + +/** + * Local Variables: + * version-control: t + * c-file-style: "bsd" + * End: + */