Subject: lib/34238: wcsdup, wcscasecmp and wcsncasecmp functions
To: None <lib-bug-people@netbsd.org, gnats-admin@netbsd.org,>
From: Aleksey Cheusov <cheusov@tut.by>
List: netbsd-bugs
Date: 08/19/2006 22:15:00
>Number:         34238
>Category:       lib
>Synopsis:       wcsdup, wcscasecmp and wcsncasecmp functions
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    lib-bug-people
>State:          open
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Sat Aug 19 22:15:00 +0000 2006
>Originator:     Aleksey Cheusov <cheusov@tut.by>
>Release:        NetBSD 3.0_STABLE
>Organization:
Best regards, Aleksey Cheusov.
>Environment:
	<The following information is extracted from your kernel. Please>
	<append output of "ldd", "ident" where relevant (multiple lines).>
System: NetBSD chen.chizhovka.net 3.0_STABLE NetBSD 3.0_STABLE (GENERIC) #2: Sun Mar 12 12:49:58 GMT 2006 cheusov@chen:/usr/src/sys/arch/i386/compile/GENERIC i386
Architecture: i386
Machine: i386
>Description:
Below is a patch for libc implementing wcsdup,
wcscasecmp and wcsncasecmp functions.
but wcs* equivalets for appropriate str* functions.

Despite the fact that these funcs are not standard,
I hope you'll find them helpful, if yes, I can make patch for man pages too.


--=-=-=
Content-Type: text/x-patch
Content-Disposition: attachment; filename=wcs.netbsd.patch
Content-Description: wcs functions

Index: lib/libc/string/wcscasecmp.c
===================================================================
RCS file: lib/libc/string/wcscasecmp.c
diff -N lib/libc/string/wcscasecmp.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ lib/libc/string/wcscasecmp.c	19 Aug 2006 20:55:51 -0000
@@ -0,0 +1,67 @@
+/*	$NetBSD$	*/
+
+#include <sys/cdefs.h>
+#if defined(LIBC_SCCS) && !defined(lint) 
+__RCSID("$NetBSD$"); 
+#endif /* LIBC_SCCS and not lint */ 
+
+#include <assert.h>
+#include <wchar.h>
+#include <wctype.h>
+
+int wcscasecmp(s1, s2)
+    const wchar_t *s1;
+    const wchar_t *s2;
+{
+	int lc1  = 0;
+	int lc2  = 0;
+	int diff = 0;
+
+	_DIAGASSERT(s1);
+	_DIAGASSERT(s2);
+
+	for (;;){
+		lc1 = towlower (*s1);
+		lc2 = towlower (*s2);
+
+		diff = lc1 - lc2;
+		if (diff)
+			return diff;
+
+		if (!lc1)
+			return 0;
+
+		++s1;
+		++s2;
+	}
+}
+
+int wcsncasecmp (s1, s2, n)
+    const wchar_t *s1;
+    const wchar_t *s2;
+    size_t n;
+{
+	int lc1  = 0;
+	int lc2  = 0;
+	int diff = 0;
+
+	_DIAGASSERT(s1);
+	_DIAGASSERT(s2);
+
+	while (n--){
+		lc1 = towlower (*s1);
+		lc2 = towlower (*s2);
+
+		diff = lc1 - lc2;
+		if (diff)
+			return diff;
+
+		if (!lc1)
+			return 0;
+
+		++s1;
+		++s2;
+	}
+
+	return 0;
+}
Index: lib/libc/string/wcsdup.c
===================================================================
RCS file: lib/libc/string/wcsdup.c
diff -N lib/libc/string/wcsdup.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ lib/libc/string/wcsdup.c	19 Aug 2006 20:55:51 -0000
@@ -0,0 +1,30 @@
+/*	$NetBSD$	*/
+
+#include <sys/cdefs.h>
+
+#if defined(LIBC_SCCS) && !defined(lint) 
+__RCSID("$NetBSD$"); 
+#endif /* LIBC_SCCS and not lint */ 
+
+#include <stdlib.h>
+#include <assert.h>
+#include <string.h>
+#include <wchar.h>
+
+wchar_t * wcsdup (str)
+    const wchar_t *str;
+{
+	wchar_t *copy;
+	size_t len;
+
+	_DIAGASSERT(str != NULL);
+
+	len = wcslen (str) + 1;
+	copy = (wchar_t *) malloc (len * sizeof (wchar_t));
+
+	if (!copy)
+		return NULL;
+
+	memcpy (copy, str, len * sizeof (wchar_t));
+	return copy;
+}
Index: lib/libc/string/Makefile.inc
===================================================================
RCS file: /cvsroot/src/lib/libc/string/Makefile.inc,v
retrieving revision 1.56
diff -u -r1.56 Makefile.inc
--- lib/libc/string/Makefile.inc	14 Mar 2005 03:13:53 -0000	1.56
+++ lib/libc/string/Makefile.inc	19 Aug 2006 20:55:51 -0000
@@ -10,7 +10,7 @@
 
 # wide char
 SRCS+=	wcscat.c wcschr.c wcscmp.c wcscpy.c wcscspn.c wcslcat.c wcslcpy.c \
-	wcslen.c wcsncat.c \
+	wcslen.c wcsncat.c wcscasecmp.c wcsdup.c \
 	wcsncmp.c wcsncpy.c wcspbrk.c wcsrchr.c wcsspn.c wcsstr.c wcstok.c \
 	wcswcs.c wcswidth.c \
 	wmemchr.c wmemcmp.c wmemcpy.c wmemmove.c wmemset.c 
Index: include/wchar.h
===================================================================
RCS file: /cvsroot/src/include/wchar.h,v
retrieving revision 1.21.2.1
diff -u -r1.21.2.1 wchar.h
--- include/wchar.h	6 Apr 2005 13:39:05 -0000	1.21.2.1
+++ include/wchar.h	19 Aug 2006 20:55:51 -0000
@@ -165,6 +165,13 @@
 wint_t putwchar(wchar_t);
 
 int fwide(FILE *, int);
+
+#if defined(_NETBSD_SOURCE)
+wchar_t * wcsdup (const wchar_t *);
+int wcsncasecmp (const wchar_t *, const wchar_t *, size_t );
+int wcscasecmp(const wchar_t *, const wchar_t *);
+#endif
+
 __END_DECLS
 
 #ifndef WEOF

--=-=-=


P.S.
Why implementation of str[n]casecmp functions is so ugly?

P.P.S.
If I understand correctly some functions declared in wchar.h
should be moved to _NETBSD_SOURCE section, because they are extensions.

-- 
Best regards, Aleksey Cheusov.

--=-=-=--

>Fix:

Unknown
>Unformatted:
 --=-=-=