* [PATCH] Add strlcat and strlcpy
@ 2010-05-16 22:06 Daniel Néri
0 siblings, 0 replies; only message in thread
From: Daniel Néri @ 2010-05-16 22:06 UTC (permalink / raw)
To: Jonathan Larmour; +Cc: ecos-patches
[-- Attachment #1: Type: text/plain, Size: 1074 bytes --]
On Wed, May 12, 2010 at 15:04, Jonathan Larmour <jifl@jifvik.org> wrote:
>
> Heh, fair enough.
>
> Would you mind updating it with these minor changes: update the copyright dates, add eCos copyright headers to the two new files, and if you don't mind while you're there, enclose the whole block of prototypes in bsdstring.h in:
> #ifndef __STRICT_ANSI__
I've attached a new version of my patch.
There's a a new "requires" clause added to the CDL because I was
concerned about someone enabling both CYGFUN_LIBC_STRING_BSD_FUNCS and
strict ANSI mode and then start wondering why the BSD string function
prototypes went silently missing. There are other GCC options that
also imply __STRICT_ANSI__ though, e.g. "-std=c89", so maybe those
should be added to the condition, for consistency.
As for copyright, I really dislike adding pointless restrictions to
code that's virtually unchanged, but IANAL of course, so it's a
separate patch (hoping it won't get applied).
Regards,
--
Daniel Néri <daniel.neri@sigicom.com>
Sigicom AB, Stockholm, Sweden
[-- Attachment #2: strlxxx.patch --]
[-- Type: application/octet-stream, Size: 7648 bytes --]
diff -r ae816c83f082 -r d4902624e359 packages/language/c/libc/string/current/ChangeLog
--- a/packages/language/c/libc/string/current/ChangeLog Tue May 11 16:26:19 2010 +0000
+++ b/packages/language/c/libc/string/current/ChangeLog Sun May 16 23:38:18 2010 +0200
@@ -1,3 +1,15 @@
+2010-05-16 Daniel Néri <daniel.neri@sigicom.com>
+
+ * src/strlcpy.c: New file, originally from OpenBSD, with added
+ tracing and assertions.
+ * src/strlcat.c: Ditto.
+ * src/bsdstring.h: Add prototypes for strlcpy and strlcat.
+ * cdl/string.cdl (CYGFUN_LIBC_STRING_BSD_FUNCS): Include strlcpy
+ and strlcat. Option is not compatible with strict ANSI
+ mode ("-ansi" option for GCC)
+ * include/bsdstring.h: BSD function prototypes are not provided in
+ strict ANSI mode.
+
2010-05-10 Ross Younger <wry@ecoscentric.com>
* include/string.h: Declare strnlen.
diff -r ae816c83f082 -r d4902624e359 packages/language/c/libc/string/current/cdl/string.cdl
--- a/packages/language/c/libc/string/current/cdl/string.cdl Tue May 11 16:26:19 2010 +0000
+++ b/packages/language/c/libc/string/current/cdl/string.cdl Sun May 16 23:38:18 2010 +0200
@@ -111,12 +111,13 @@
requires { CYGBLD_ISO_STRING_BSD_FUNCS_HEADER == \
"<cyg/libc/string/bsdstring.h>" }
requires CYGINT_ISO_CTYPE
- compile bsdstring.cxx
+ requires { !is_substr(CYGBLD_GLOBAL_CFLAGS, " -ansi ") }
+ 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 and swab, strlcpy and strlcat."
}
cdl_component CYGPKG_LIBC_STRING_STRTOK {
diff -r ae816c83f082 -r d4902624e359 packages/language/c/libc/string/current/include/bsdstring.h
--- a/packages/language/c/libc/string/current/include/bsdstring.h Tue May 11 16:26:19 2010 +0000
+++ b/packages/language/c/libc/string/current/include/bsdstring.h Sun May 16 23:38:18 2010 +0200
@@ -58,6 +58,7 @@
#include <pkgconf/libc_string.h> /* Configuration header */
#ifdef CYGFUN_LIBC_STRING_BSD_FUNCS
+#ifndef __STRICT_ANSI__
#define __need_size_t
#include <stddef.h>
@@ -95,12 +96,19 @@
extern void
swab( const void * /* from */, void * /* to */, size_t /* n */ );
+extern size_t
+strlcpy( char * /* dst */, const char * /* src */, size_t /* siz */ );
+
+extern size_t
+strlcat( char * /* dst */, const char * /* src */, size_t /* siz */ );
+
/*=========================================================================*/
#ifdef __cplusplus
} /* extern "C" */
#endif
+#endif /* ifndef __STRICT_ANSI__ */
#endif /* ifdef CYGFUN_LIBC_STRING_BSD_FUNCS */
#endif /* CYGONCE_LIBC_BSDSTRING_H multiple inclusion protection */
diff -r ae816c83f082 -r d4902624e359 packages/language/c/libc/string/current/src/strlcat.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/packages/language/c/libc/string/current/src/strlcat.c Sun May 16 23:38:18 2010 +0200
@@ -0,0 +1,72 @@
+/* $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie 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 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 <cyg/infra/cyg_trac.h>
+#include <cyg/infra/cyg_ass.h>
+
+#include <sys/types.h>
+#include <string.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)
+{
+ char *d = dst;
+ const char *s = src;
+ size_t n = siz;
+ size_t dlen;
+ size_t ret;
+
+ CYG_REPORT_FUNCNAMETYPE("strlcat", "returning %p");
+ CYG_REPORT_FUNCARG3("dst=%p, src=%p, siz=%lu", dst, src, siz);
+
+ if (siz) {
+ CYG_CHECK_DATA_PTR(dst, "dst is not a valid pointer!");
+ CYG_CHECK_DATA_PTR(src, "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) {
+ ret = dlen + strlen(s);
+ CYG_REPORT_RETVAL(ret);
+ return(ret);
+ }
+ while (*s != '\0') {
+ if (n != 1) {
+ *d++ = *s;
+ n--;
+ }
+ s++;
+ }
+ *d = '\0';
+
+ ret = dlen + (s - src); /* count does not include NUL */
+ CYG_REPORT_RETVAL(ret);
+ return(ret);
+}
diff -r ae816c83f082 -r d4902624e359 packages/language/c/libc/string/current/src/strlcpy.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/packages/language/c/libc/string/current/src/strlcpy.c Sun May 16 23:38:18 2010 +0200
@@ -0,0 +1,65 @@
+/* $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 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 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 <cyg/infra/cyg_trac.h>
+#include <cyg/infra/cyg_ass.h>
+
+#include <sys/types.h>
+#include <string.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)
+{
+ char *d = dst;
+ const char *s = src;
+ size_t n = siz;
+ size_t ret;
+
+ CYG_REPORT_FUNCNAMETYPE("strlcpy", "returning %p");
+ CYG_REPORT_FUNCARG3("dst=%p, src=%p, siz=%lu", dst, src, siz);
+
+ if (siz) {
+ CYG_CHECK_DATA_PTR(dst, "dst is not a valid pointer!");
+ CYG_CHECK_DATA_PTR(src, "src is not a valid pointer!");
+ }
+
+ /* 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++)
+ ;
+ }
+
+ ret = s - src - 1; /* count does not include NUL */
+ CYG_REPORT_RETVAL(ret);
+ return(ret);
+}
[-- Attachment #3: strlxxx-copyright.patch --]
[-- Type: application/octet-stream, Size: 4778 bytes --]
diff -r d4902624e359 -r 0edeb658e69e packages/language/c/libc/string/current/src/strlcat.c
--- a/packages/language/c/libc/string/current/src/strlcat.c Sun May 16 23:38:18 2010 +0200
+++ b/packages/language/c/libc/string/current/src/strlcat.c Sun May 16 23:56:04 2010 +0200
@@ -1,3 +1,35 @@
+// ####ECOSGPLCOPYRIGHTBEGIN####
+// -------------------------------------------
+// This file is part of eCos, the Embedded Configurable Operating System.
+// Copyright (C) 2010 Free Software Foundation, Inc.
+//
+// eCos is free software; you can redistribute it and/or modify it under
+// the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 2 or (at your option) any later
+// version.
+//
+// eCos is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+// for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with eCos; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+//
+// As a special exception, if other files instantiate templates or use
+// macros or inline functions from this file, or you compile this file
+// and link it with other works to produce a work based on this file,
+// this file does not by itself cause the resulting work to be covered by
+// the GNU General Public License. However the source code for this file
+// must still be made available in accordance with section (3) of the GNU
+// General Public License v2.
+//
+// This exception does not invalidate any other reasons why a work based
+// on this file might be covered by the GNU General Public License.
+// -------------------------------------------
+// ####ECOSGPLCOPYRIGHTEND####
+
/* $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $ */
/*
diff -r d4902624e359 -r 0edeb658e69e packages/language/c/libc/string/current/src/strlcpy.c
--- a/packages/language/c/libc/string/current/src/strlcpy.c Sun May 16 23:38:18 2010 +0200
+++ b/packages/language/c/libc/string/current/src/strlcpy.c Sun May 16 23:56:04 2010 +0200
@@ -1,3 +1,35 @@
+// ####ECOSGPLCOPYRIGHTBEGIN####
+// -------------------------------------------
+// This file is part of eCos, the Embedded Configurable Operating System.
+// Copyright (C) 2010 Free Software Foundation, Inc.
+//
+// eCos is free software; you can redistribute it and/or modify it under
+// the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 2 or (at your option) any later
+// version.
+//
+// eCos is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+// for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with eCos; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+//
+// As a special exception, if other files instantiate templates or use
+// macros or inline functions from this file, or you compile this file
+// and link it with other works to produce a work based on this file,
+// this file does not by itself cause the resulting work to be covered by
+// the GNU General Public License. However the source code for this file
+// must still be made available in accordance with section (3) of the GNU
+// General Public License v2.
+//
+// This exception does not invalidate any other reasons why a work based
+// on this file might be covered by the GNU General Public License.
+// -------------------------------------------
+// ####ECOSGPLCOPYRIGHTEND####
+
/* $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $ */
/*
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2010-05-16 22:06 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-05-16 22:06 [PATCH] Add strlcat and strlcpy Daniel Néri
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).