Subject: lib/10877: Add LC_TIME support to setlocale(3)
To: None <gnats-bugs@gnats.netbsd.org>
From: Joachim Kuebart <kuebart@mathematik.uni-ulm.de>
List: netbsd-bugs
Date: 08/22/2000 06:33:12
>Number:         10877
>Category:       lib
>Synopsis:       Add LC_TIME support to setlocale(3)
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    lib-bug-people
>State:          open
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Tue Aug 22 06:34:00 PDT 2000
>Closed-Date:
>Last-Modified:
>Originator:     Joachim Kuebart
>Release:        Sat Jun 10 15:57:50 2000
>Organization:
>Environment:
System: NetBSD yacht.domestic.de 1.4ZB NetBSD 1.4ZB (YACHT) #84: Sun Jun 11 15:01:09 CEST 2000 joki@yacht:/usr/src/sys/arch/i386/compile/YACHT i386


>Description:
	setlocale() has no support for loading a locale's LC_TIME
	file.
>How-To-Repeat:
	Create a /usr/share/locale/<locale>/LC_TIME file, use
	date(1) as in "LANG=<locale> date" and observe that the
	locale is being ignored.
>Fix:
	The following patches to Makefile.inc and setlocale.c will
	support an LC_TIME file (format: one line for each entry
	of _TimeLocale from <sys/localedef.h>).  The files timeio.h
	and timeio.c are new and need to be added.

	And example LC_TIME file for the German locale ("de") is shown
	at the end.


cvs diff: Diffing .
Index: Makefile.inc
===================================================================
RCS file: /home/cvs/netbsd/src/lib/libc/locale/Makefile.inc,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -r1.1.1.1 -r1.2
--- Makefile.inc	1999/06/14 20:18:28	1.1.1.1
+++ Makefile.inc	2000/08/22 13:13:13	1.2
@@ -5,7 +5,7 @@
 .PATH: ${ARCHDIR}/locale ${.CURDIR}/locale
 
 SRCS+=	_def_messages.c _def_monetary.c _def_numeric.c _def_time.c \
-	ctypeio.c localeconv.c nl_langinfo.c setlocale.c
+	ctypeio.c localeconv.c nl_langinfo.c setlocale.c timeio.c
 
 MAN+=	nl_langinfo.3 setlocale.3
 
Index: setlocale.c
===================================================================
RCS file: /home/cvs/netbsd/src/lib/libc/locale/setlocale.c,v
retrieving revision 1.1.1.2
retrieving revision 1.2
diff -u -r1.1.1.2 -r1.2
--- setlocale.c	2000/01/19 05:47:26	1.1.1.2
+++ setlocale.c	2000/08/22 13:13:13	1.2
@@ -57,6 +57,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include "ctypeio.h"
+#include "timeio.h"
 
 /*
  * Category names for getenv()
@@ -261,11 +262,19 @@
 		}
 		return NULL;
 
+	case LC_TIME:
+		if (__loadtime(name)) {
+			(void)strncpy(current_categories[category],
+			    new_categories[category],
+			    sizeof(current_categories[category]) - 1);
+			return current_categories[category];
+		}
+		return NULL;
+
 	case LC_COLLATE:
 	case LC_MESSAGES:
 	case LC_MONETARY:
 	case LC_NUMERIC:
-	case LC_TIME:
 		return NULL;
 	}

New timeio.h:
===================================================================
/*	$NetBSD$	*/

/*
 * Copyright (c) 2000 Joachim Kuebart.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Joachim Kuebart.
 * 4. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

__BEGIN_DECLS
int __loadtime __P((const char *));
__END_DECLS

New timeio.c:
===================================================================
/*	$NetBSD$	*/

/*
 * Copyright (c) 2000 Joachim Kuebart.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Joachim Kuebart.
 * 4. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/localedef.h>

#include <stdio.h>
#include <stdlib.h>
#include "timeio.h"

int
__loadtime(name)
	const char *name;
{
	FILE *fp;
	struct stat st;
	unsigned char **ab, *p, *pend;
	_TimeLocale *tl;


	if ((fp = fopen(name, "r")) == NULL)
		return 0;
	if (fstat(fileno(fp), &st) != 0) {
		fclose(fp);
		return 0;
	}
	if ((tl = malloc(sizeof(*tl) + (unsigned)st.st_size)) == NULL) {
		(void) fclose(fp);
		return 0;
	}
	if (fread(tl + 1, (unsigned)st.st_size, 1, fp) != 1)
		goto bad;
	(void) fclose(fp);
	/* LINTED pointer cast */
	p = (unsigned char *)&tl[1];
	pend = p + (unsigned)st.st_size;
	/* LINTED pointer cast */
	for (ab = (unsigned char **)tl;
	     ab != (unsigned char **)&tl[1];
	     ab++) {
		*ab = p;
		while (p != pend && *p != '\n')
			p++;
		if (p == pend)
			goto bad;
		*p++ = '\0';
	}
	if (_CurrentTimeLocale != &_DefaultTimeLocale)
		/* LINTED const castaway */
		free((void *)_CurrentTimeLocale);
	_CurrentTimeLocale = tl;
	return 1;
bad:
	free(tl);
	(void) fclose(fp);
	return 0;
}


German LC_TIME file:
===================================================================
So 
Mo 
Di 
Mi 
Do 
Fr 
Sa 
Sonntag
Montag
Dienstag
Mittwoch
Donnerstag
Freitag
Samstag
Jan
Feb
Mär
Apr
Mai
Jun
Jul
Aug
Sep
Okt
Nov
Dez
Januar
Februar
März
April
Mai
Juni
Juli
August
September
Oktober
November
Dezember


%a %e. %b %Y %H:%M:%S
%d.%m.%y
%H:%M:%S

>Release-Note:
>Audit-Trail:
>Unformatted: