From 7e349f87afd1c2a8dd2cd972e76aa9a81490694f Mon Sep 17 00:00:00 2001 From: manu Date: Wed, 28 Mar 2007 19:05:47 +0000 Subject: [PATCH] Add support for LC_TIME, from Joachim Kuebart, through PR lib/10877 --- doc/CHANGES | 3 +- lib/libc/locale/Makefile.inc | 4 +- lib/libc/locale/_def_time.c | 8 ++-- lib/libc/locale/setlocale.c | 17 +++++-- lib/libc/locale/timeio.c | 93 ++++++++++++++++++++++++++++++++++++ lib/libc/locale/timeio.h | 35 ++++++++++++++ 6 files changed, 149 insertions(+), 11 deletions(-) create mode 100644 lib/libc/locale/timeio.c create mode 100644 lib/libc/locale/timeio.h diff --git a/doc/CHANGES b/doc/CHANGES index a78c916d103a..9abaab3575ca 100644 --- a/doc/CHANGES +++ b/doc/CHANGES @@ -1,4 +1,4 @@ -LIST OF CHANGES FROM LAST RELEASE: <$Revision: 1.816 $> +LIST OF CHANGES FROM LAST RELEASE: <$Revision: 1.817 $> [Note: This file does not mention every change made to the NetBSD source tree. @@ -71,3 +71,4 @@ Changes from NetBSD 4.0 to NetBSD 5.0: power and overheating in CPUs supporting the Thermal Monitor feature (TM). Adapted from OpenBSD/FreeBSD. See options(4) for more information. [xtraeme 20070320] + libc: add support for LC_TIME [manu 20070328] diff --git a/lib/libc/locale/Makefile.inc b/lib/libc/locale/Makefile.inc index 8b4c16181636..e8aa5ef35121 100644 --- a/lib/libc/locale/Makefile.inc +++ b/lib/libc/locale/Makefile.inc @@ -1,12 +1,12 @@ # from: @(#)Makefile.inc 5.1 (Berkeley) 2/18/91 -# $NetBSD: Makefile.inc,v 1.50 2006/10/13 17:28:09 tnozaki Exp $ +# $NetBSD: Makefile.inc,v 1.51 2007/03/28 19:05:48 manu Exp $ # locale sources .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 setlocale1.c \ - setlocale32.c __mb_cur_max.c wcscoll.c wcsftime.c \ + setlocale32.c __mb_cur_max.c wcscoll.c wcsftime.c timeio.c \ wcstol.c wcstoll.c wcstoimax.c wcstoul.c wcstoull.c wcstoumax.c \ wcsxfrm.c aliasname.c MAN+= nl_langinfo.3 setlocale.3 wcstol.3 wcscoll.3 wcsxfrm.3 diff --git a/lib/libc/locale/_def_time.c b/lib/libc/locale/_def_time.c index 5ec23b5fa066..40dc161028e8 100644 --- a/lib/libc/locale/_def_time.c +++ b/lib/libc/locale/_def_time.c @@ -1,4 +1,4 @@ -/* $NetBSD: _def_time.c,v 1.8 2005/06/12 05:21:27 lukem Exp $ */ +/* $NetBSD: _def_time.c,v 1.9 2007/03/28 19:05:53 manu Exp $ */ /* * Written by J.T. Conklin . @@ -7,13 +7,13 @@ #include #if defined(LIBC_SCCS) && !defined(lint) -__RCSID("$NetBSD: _def_time.c,v 1.8 2005/06/12 05:21:27 lukem Exp $"); +__RCSID("$NetBSD: _def_time.c,v 1.9 2007/03/28 19:05:53 manu Exp $"); #endif /* LIBC_SCCS and not lint */ #include #include -const _TimeLocale _DefaultTimeLocale = +_TimeLocale _DefaultTimeLocale = { { "Sun","Mon","Tue","Wed","Thu","Fri","Sat", @@ -39,4 +39,4 @@ const _TimeLocale _DefaultTimeLocale = "%I:%M:%S %p" }; -const _TimeLocale *_CurrentTimeLocale = &_DefaultTimeLocale; +_TimeLocale *_CurrentTimeLocale = &_DefaultTimeLocale; diff --git a/lib/libc/locale/setlocale.c b/lib/libc/locale/setlocale.c index 41bc43568bb5..f3dfa13775ed 100644 --- a/lib/libc/locale/setlocale.c +++ b/lib/libc/locale/setlocale.c @@ -1,4 +1,4 @@ -/* $NetBSD: setlocale.c,v 1.50 2006/02/16 19:19:49 tnozaki Exp $ */ +/* $NetBSD: setlocale.c,v 1.51 2007/03/28 19:05:53 manu Exp $ */ /* * Copyright (c) 1991, 1993 @@ -37,7 +37,7 @@ #if 0 static char sccsid[] = "@(#)setlocale.c 8.1 (Berkeley) 7/4/93"; #else -__RCSID("$NetBSD: setlocale.c,v 1.50 2006/02/16 19:19:49 tnozaki Exp $"); +__RCSID("$NetBSD: setlocale.c,v 1.51 2007/03/28 19:05:53 manu Exp $"); #endif #endif /* LIBC_SCCS and not lint */ @@ -63,6 +63,7 @@ __RCSID("$NetBSD: setlocale.c,v 1.50 2006/02/16 19:19:49 tnozaki Exp $"); #else #include "ctypeio.h" #endif +#include "timeio.h" #ifdef CITRUS #include @@ -263,11 +264,16 @@ revert_to_default(category) } #endif break; + case LC_TIME: + if (_CurrentTimeLocale != &_DefaultTimeLocale) { + free((void *)_CurrentTimeLocale); + _CurrentTimeLocale = &_DefaultTimeLocale; + } + break; case LC_MESSAGES: case LC_COLLATE: case LC_MONETARY: case LC_NUMERIC: - case LC_TIME: break; } } @@ -341,10 +347,13 @@ load_locale_sub(category, locname, isspecial) } break; + case LC_TIME: + if (!__loadtime(name)) + return -1; + break; case LC_COLLATE: case LC_MONETARY: case LC_NUMERIC: - case LC_TIME: return -1; } diff --git a/lib/libc/locale/timeio.c b/lib/libc/locale/timeio.c new file mode 100644 index 000000000000..806603c56122 --- /dev/null +++ b/lib/libc/locale/timeio.c @@ -0,0 +1,93 @@ +/* $NetBSD: timeio.c,v 1.1 2007/03/28 19:05:53 manu Exp $ */ + + +/* + * 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 +#include +#include + + +#include +#include +#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; +} + diff --git a/lib/libc/locale/timeio.h b/lib/libc/locale/timeio.h new file mode 100644 index 000000000000..477b88de06a9 --- /dev/null +++ b/lib/libc/locale/timeio.h @@ -0,0 +1,35 @@ +/* $NetBSD: timeio.h,v 1.1 2007/03/28 19:05:54 manu Exp $ */ + + +/* + * 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