This is the mail archive of the
ecos-patches@sources.redhat.com
mailing list for the eCos project.
strlcpy and strlcat
- From: daniel dot neri at sigicom dot se (Daniel Néri)
- To: ecos-patches at sources dot redhat dot com
- Date: Wed, 18 Feb 2004 18:48:04 +0100
- Subject: strlcpy and strlcat
- Cancel-lock: sha1:i8wmvghCZyWOwJ/SfBZKupmP8AM=
- Organization: Sigicom AB, Stockholm, Sweden
Hi,
I've had these in my tree for quite some time now. For more info, see
http://www.courtesan.com/todd/papers/strlcpy.html
Best wishes,
--Daniel
Index: language/c/libc/string/current/ChangeLog
===================================================================
RCS file: /home/dne/cvsroot/redhat/ecos/packages/language/c/libc/string/current/ChangeLog,v
retrieving revision 1.1.1.2
retrieving revision 1.2
diff -p -U5 -r1.1.1.2 -r1.2
--- language/c/libc/string/current/ChangeLog 7 Mar 2003 11:27:05 -0000 1.1.1.2
+++ language/c/libc/string/current/ChangeLog 18 Feb 2004 17:43:07 -0000 1.2
@@ -1,5 +1,15 @@
+2004-02-18 Daniel Néri <daniel.neri@sigicom.se>
+
+ * src/strlcpy.c: New file, originally from OpenBSD, with added
+ tracing and assertions.
+
+ * src/strlcat.c: Ditto.
+
+ * cdl/string.cdl (CYGFUN_LIBC_STRING_BSD_FUNCS): Include strlcpy
+ and strlcat.
+
2003-02-24 Jonathan Larmour <jifl@eCosCentric.com>
* cdl/string.cdl: Fix doc link.
2001-11-27 Jonathan Larmour <jlarmour@redhat.com>
Index: language/c/libc/string/current/cdl/string.cdl
===================================================================
RCS file: /home/dne/cvsroot/redhat/ecos/packages/language/c/libc/string/current/cdl/string.cdl,v
retrieving revision 1.1.1.2
retrieving revision 1.2
diff -p -U5 -r1.1.1.2 -r1.2
--- language/c/libc/string/current/cdl/string.cdl 7 Mar 2003 11:27:05 -0000 1.1.1.2
+++ language/c/libc/string/current/cdl/string.cdl 14 Apr 2003 15:22:29 -0000 1.2
@@ -110,16 +110,16 @@ cdl_package CYGPKG_LIBC_STRING {
default_value 1
implements CYGINT_ISO_STRING_BSD_FUNCS
requires { CYGBLD_ISO_STRING_BSD_FUNCS_HEADER == \
"<cyg/libc/string/bsdstring.h>" }
requires CYGINT_ISO_CTYPE
- compile bsdstring.cxx
+ compile bsdstring.cxx strlcpy.c strlcat.c
description "
Enabling this option causes various compatibility functions
commonly found in the BSD UNIX operating system to be included.
These are functions such as bzero, bcmp, bcopy, bzero, strcasecmp,
- strncasecmp, index, rindex and swab."
+ strncasecmp, index, rindex, swab, strlcpy and strlcat."
}
cdl_component CYGPKG_LIBC_STRING_STRTOK {
display "strtok"
flavor none
Index: language/c/libc/string/current/include/bsdstring.h
===================================================================
RCS file: /home/dne/cvsroot/redhat/ecos/packages/language/c/libc/string/current/include/bsdstring.h,v
retrieving revision 1.1.1.1
retrieving revision 1.3
diff -p -U5 -r1.1.1.1 -r1.3
--- language/c/libc/string/current/include/bsdstring.h 27 Jan 2003 11:16:07 -0000 1.1.1.1
+++ language/c/libc/string/current/include/bsdstring.h 14 Apr 2003 15:23:03 -0000 1.3
@@ -94,10 +94,16 @@ extern char *
rindex( const char * /* s */, int /* c */ );
extern void
swab( const void * /* from */, void * /* to */, size_t /* n */ );
+extern size_t
+strlcpy(char * /* dest */, const char * /* src */, size_t /* siz */);
+
+extern size_t
+strlcat(char * /* dest */, const char * /* src */, size_t /* siz */);
+
/*=========================================================================*/
#ifdef __cplusplus
} /* extern "C" */
#endif
Index: language/c/libc/string/current/src/strlcat.c
===================================================================
RCS file: language/c/libc/string/current/src/strlcat.c
diff -N language/c/libc/string/current/src/strlcat.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ language/c/libc/string/current/src/strlcat.c 5 Jun 2003 16:29:25 -0000 1.3
@@ -0,0 +1,66 @@
+/* $OpenBSD: strlcat.c,v 1.10 2003/04/12 21:56:39 millert Exp $ */
+
+/*
+ * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
+ *
+ * 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 TODD C. MILLER DISCLAIMS ALL
+ * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL TODD C. MILLER 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 <cyg/infra/cyg_trac.h> // Tracing support
+#include <cyg/infra/cyg_ass.h> // Assertion support
+
+#include <string.h>
+#include <stddef.h>
+
+/*
+ * 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)
+{
+ register char *d = dst;
+ register const char *s = src;
+ register size_t n = siz;
+ size_t dlen;
+
+ CYG_REPORT_FUNCNAMETYPE("strlcat", "returning %p");
+ CYG_REPORT_FUNCARG3("dst=%p, src=%p, siz=%lu", d, s, n);
+
+ if (n) {
+ CYG_CHECK_DATA_PTR(d, "dst is not a valid pointer!");
+ CYG_CHECK_DATA_PTR(s, "src is not a valid pointer!");
+ }
+
+ /* 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 */
+}
Index: language/c/libc/string/current/src/strlcpy.c
===================================================================
RCS file: language/c/libc/string/current/src/strlcpy.c
diff -N language/c/libc/string/current/src/strlcpy.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ language/c/libc/string/current/src/strlcpy.c 5 Jun 2003 16:29:26 -0000 1.3
@@ -0,0 +1,62 @@
+/* $OpenBSD: strlcpy.c,v 1.7 2003/04/12 21:56:39 millert Exp $ */
+
+/*
+ * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
+ *
+ * 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 TODD C. MILLER DISCLAIMS ALL
+ * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL TODD C. MILLER 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 <cyg/infra/cyg_trac.h> // Tracing support
+#include <cyg/infra/cyg_ass.h> // Assertion support
+
+#include <string.h>
+#include <stddef.h>
+
+/*
+ * 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)
+{
+ register char *d = dst;
+ register const char *s = src;
+ register size_t n = siz;
+
+ CYG_REPORT_FUNCNAMETYPE("strlcpy", "returning %p");
+ CYG_REPORT_FUNCARG3("dst=%p, src=%p, siz=%lu", d, s, n);
+
+ if (n) {
+ CYG_CHECK_DATA_PTR(d, "dst is not a valid pointer!");
+ CYG_CHECK_DATA_PTR(s, "src is not a valid pointer!");
+ }
+
+ /* Copy as many bytes as will fit */
+ if (n != 0 && --n != 0) {
+ do {
+ if ((*d++ = *s++) == 0)
+ break;
+ } while (--n != 0);
+ }
+
+ /* 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 */
+}