Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/netbsd-7]: src Pull up following revision(s) (requested by martin in tic...
details: https://anonhg.NetBSD.org/src/rev/420a4615a1be
branches: netbsd-7
changeset: 798552:420a4615a1be
user: snj <snj%NetBSD.org@localhost>
date: Wed Nov 12 18:50:55 2014 +0000
description:
Pull up following revision(s) (requested by martin in ticket #216):
share/man/man9/todr.9: revision 1.17
sys/arch/sh3/dev/rtc.c: revision 1.9
sys/dev/clock_subr.c: revision 1.17-1.22
sys/dev/clock_subr.h: revision 1.22
sys/fs/msdosfs/msdosfs_conv.c: revision 1.10
tools/compat/compat_defs.h: revision 1.98
tools/compat/dev/clock_subr.h: revision 1.1-1.2
usr.sbin/makefs/msdos/Makefile.inc: revision 1.6
clock_secs_to_ymdhms(9) takes seconds as a time_t, not int.
--
Make this compile- and usable from userland as well.
--
Add a slightly stripped down version of sys/dev/clock/clock_subr.h to make
this code available for tools.
--
Reformulate an overflow test so it can be used in tool builds (i.e. does
not depend on netbsd specific macros).
--
Make msdosfs time conversion use the y/m/d/h/m/s conversion functions
from clock_subr.c and compile that into the userland (and tools)
makefs as well.
--
Copy definitions of __type_min(t), __type_max(t), and some related macros,
from <sys/cdefs.h>, for use when building tools.
--
Revert previous; test secs > __type_max(time_t) again, now
that __type_max is available in tools/compat/compat_defs.h.
--
If HAVE_NBTOOL_CONFIG_H is set, then include "nbtool_config.h",
because this file is compiled as part of tools/makefs.
--
Counting leap years was fine while we had 32bit time_t - but now it
is not a good idea for dates far away in the future.
For dates in the year 2000 or later, use arithmetic instead (since the
repeating periods are well aligned). Should fix PR 49144.
--
Avoid overflowing the "year" value by making the field uint64_t. Adapt
arguments and local variables accordingly.
This now fixes PR 49144 for real.
--
Adapt formats for debug printfs to clock_subr type changes.
diffstat:
share/man/man9/todr.9 | 4 +-
sys/arch/sh3/dev/rtc.c | 12 +-
sys/dev/clock_subr.c | 126 ++++++++++++++++++++-----
sys/dev/clock_subr.h | 6 +-
sys/fs/msdosfs/msdosfs_conv.c | 173 ++++++++++++------------------------
tools/compat/compat_defs.h | 18 +++-
tools/compat/dev/clock_subr.h | 62 +++++++++++++
usr.sbin/makefs/msdos/Makefile.inc | 5 +-
8 files changed, 248 insertions(+), 158 deletions(-)
diffs (truncated from 658 to 300 lines):
diff -r 8f336cd0e309 -r 420a4615a1be share/man/man9/todr.9
--- a/share/man/man9/todr.9 Wed Nov 12 14:22:20 2014 +0000
+++ b/share/man/man9/todr.9 Wed Nov 12 18:50:55 2014 +0000
@@ -1,4 +1,4 @@
-.\" $NetBSD: todr.9,v 1.16 2010/03/22 18:58:33 joerg Exp $
+.\" $NetBSD: todr.9,v 1.16.26.1 2014/11/12 18:50:55 snj Exp $
.\"
.\" Copyright (c) 2000, 2003 The NetBSD Foundation, Inc.
.\" All rights reserved.
@@ -46,7 +46,7 @@
.Ft int
.Fn todr_settime "todr_chip_handle_t" "struct timeval *"
.Ft void
-.Fn clock_secs_to_ymdhms "int" "struct clock_ymdhms *"
+.Fn clock_secs_to_ymdhms "time_t" "struct clock_ymdhms *"
.Ft time_t
.Fn clock_ymdhms_to_secs "struct clock_ymdhms *"
.Sh DESCRIPTION
diff -r 8f336cd0e309 -r 420a4615a1be sys/arch/sh3/dev/rtc.c
--- a/sys/arch/sh3/dev/rtc.c Wed Nov 12 14:22:20 2014 +0000
+++ b/sys/arch/sh3/dev/rtc.c Wed Nov 12 18:50:55 2014 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: rtc.c,v 1.8 2010/05/22 15:51:32 tsutsui Exp $ */
+/* $NetBSD: rtc.c,v 1.8.34.1 2014/11/12 18:50:55 snj Exp $ */
/*-
* Copyright (c) 2002 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: rtc.c,v 1.8 2010/05/22 15:51:32 tsutsui Exp $");
+__KERNEL_RCSID(0, "$NetBSD: rtc.c,v 1.8.34.1 2014/11/12 18:50:55 snj Exp $");
#include <sys/param.h>
#include <sys/kernel.h>
@@ -208,8 +208,8 @@
#ifdef RTC_DEBUG
aprint_debug_dev(sc->sc_dev,
- "gettime: %04d-%02d-%02d %02d:%02d:%02d\n",
- dt->dt_year, dt->dt_mon, dt->dt_day,
+ "gettime: %04lu-%02d-%02d %02d:%02d:%02d\n",
+ (unsigned long)dt->dt_year, dt->dt_mon, dt->dt_day,
dt->dt_hour, dt->dt_min, dt->dt_sec);
if (!sc->sc_valid)
@@ -262,8 +262,8 @@
#ifdef RTC_DEBUG
aprint_debug_dev(sc->sc_dev,
- "settime: %04d-%02d-%02d %02d:%02d:%02d\n",
- dt->dt_year, dt->dt_mon, dt->dt_day,
+ "settime: %04lu-%02d-%02d %02d:%02d:%02d\n",
+ (unsigned long)dt->dt_year, dt->dt_mon, dt->dt_day,
dt->dt_hour, dt->dt_min, dt->dt_sec);
#endif
diff -r 8f336cd0e309 -r 420a4615a1be sys/dev/clock_subr.c
--- a/sys/dev/clock_subr.c Wed Nov 12 14:22:20 2014 +0000
+++ b/sys/dev/clock_subr.c Wed Nov 12 18:50:55 2014 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: clock_subr.c,v 1.16 2011/02/08 20:20:26 rmind Exp $ */
+/* $NetBSD: clock_subr.c,v 1.16.30.1 2014/11/12 18:50:55 snj Exp $ */
/*
* Copyright (c) 1988 University of Utah.
@@ -44,19 +44,43 @@
* Derived from arch/hp300/hp300/clock.c
*/
+#if HAVE_NBTOOL_CONFIG_H
+#include "nbtool_config.h"
+#endif /* HAVE_NBTOOL_CONFIG_H */
+
+#ifdef _KERNEL
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: clock_subr.c,v 1.16 2011/02/08 20:20:26 rmind Exp $");
+__KERNEL_RCSID(0, "$NetBSD: clock_subr.c,v 1.16.30.1 2014/11/12 18:50:55 snj Exp $");
#include <sys/param.h>
#include <sys/systm.h>
+#include <sys/errno.h>
+#else /* ! _KERNEL */
+#include <string.h>
+#include <time.h>
+#include <errno.h>
+#endif /* ! _KERNEL */
#include <dev/clock_subr.h>
-static inline int leapyear(int year);
+static inline int leapyear(uint64_t year);
#define FEBRUARY 2
#define days_in_year(a) (leapyear(a) ? 366 : 365)
#define days_in_month(a) (month_days[(a) - 1])
+/* for easier alignment:
+ * time from the epoch to 2000 (there were 7 leap years): */
+#define DAYSTO2000 (365*30+7)
+
+/* 4 year intervals include 1 leap year */
+#define DAYS4YEARS (365*4+1)
+
+/* 100 year intervals include 24 leap years */
+#define DAYS100YEARS (365*100+24)
+
+/* 400 year intervals include 97 leap years */
+#define DAYS400YEARS (365*400+97)
+
static const int month_days[12] = {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
@@ -70,10 +94,13 @@
* It is otherwise equivalent.
*/
static inline int
-leapyear(int year)
+leapyear(uint64_t year)
{
int rv = 0;
+ if (year < 1969)
+ return EINVAL;
+
if ((year & 3) == 0) {
rv = 1;
if ((year % 100) == 0) {
@@ -88,8 +115,7 @@
time_t
clock_ymdhms_to_secs(struct clock_ymdhms *dt)
{
- uint64_t secs;
- int i, year, days;
+ uint64_t secs, i, year, days;
year = dt->dt_year;
@@ -100,11 +126,35 @@
if (year < POSIX_BASE_YEAR)
return -1;
days = 0;
- for (i = POSIX_BASE_YEAR; i < year; i++)
- days += days_in_year(i);
if (leapyear(year) && dt->dt_mon > FEBRUARY)
days++;
+ if (year < 2000) {
+ /* simple way for early years */
+ for (i = POSIX_BASE_YEAR; i < year; i++)
+ days += days_in_year(i);
+ } else {
+ /* years are properly aligned */
+ days += DAYSTO2000;
+ year -= 2000;
+
+ i = year / 400;
+ days += i * DAYS400YEARS;
+ year -= i * 400;
+
+ i = year / 100;
+ days += i * DAYS100YEARS;
+ year -= i * 100;
+
+ i = year / 4;
+ days += i * DAYS4YEARS;
+ year -= i * 4;
+
+ for (i = dt->dt_year-year; i < dt->dt_year; i++)
+ days += days_in_year(i);
+ }
+
+
/* Months */
for (i = 1; i < dt->dt_mon; i++)
days += days_in_month(i);
@@ -116,26 +166,21 @@
* 60 + dt->dt_min)
* 60 + dt->dt_sec;
- if ((time_t)secs != secs)
+ if ((time_t)secs < 0 || secs > __type_max(time_t))
return -1;
return secs;
}
-void
+int
clock_secs_to_ymdhms(time_t secs, struct clock_ymdhms *dt)
{
- int mthdays[12];
- int i;
+ int leap;
+ uint64_t i;
time_t days;
time_t rsec; /* remainder seconds */
- /*
- * This function uses a local copy of month_days[]
- * so the copy can be modified (and thread-safe).
- * See the definition of days_in_month() above.
- */
- memcpy(mthdays, month_days, sizeof(mthdays));
-#define month_days mthdays
+ if (secs < 0)
+ return EINVAL;
days = secs / SECDAY;
rsec = secs % SECDAY;
@@ -143,16 +188,40 @@
/* Day of week (Note: 1/1/1970 was a Thursday) */
dt->dt_wday = (days + 4) % 7;
- /* Subtract out whole years, counting them in i. */
- for (i = POSIX_BASE_YEAR; days >= days_in_year(i); i++)
- days -= days_in_year(i);
- dt->dt_year = i;
+ if (days >= DAYSTO2000) {
+ days -= DAYSTO2000;
+ dt->dt_year = 2000;
+
+ i = days / DAYS400YEARS;
+ days -= i*DAYS400YEARS;
+ dt->dt_year += i*400;
+
+ i = days / DAYS100YEARS;
+ days -= i*DAYS100YEARS;
+ dt->dt_year += i*100;
+
+ i = days / DAYS4YEARS;
+ days -= i*DAYS4YEARS;
+ dt->dt_year += i*4;
+
+ for (i = dt->dt_year; days >= days_in_year(i); i++)
+ days -= days_in_year(i);
+ dt->dt_year = i;
+ } else {
+ /* Subtract out whole years, counting them in i. */
+ for (i = POSIX_BASE_YEAR; days >= days_in_year(i); i++)
+ days -= days_in_year(i);
+ dt->dt_year = i;
+ }
/* Subtract out whole months, counting them in i. */
- if (leapyear(i))
- days_in_month(FEBRUARY) = 29;
- for (i = 1; days >= days_in_month(i); i++)
- days -= days_in_month(i);
+ for (leap = 0, i = 1; days >= days_in_month(i)+leap; i++) {
+ days -= days_in_month(i)+leap;
+ if (i == 1 && leapyear(dt->dt_year))
+ leap = 1;
+ else
+ leap = 0;
+ }
dt->dt_mon = i;
/* Days are what is left over (+1) from all that. */
@@ -164,5 +233,6 @@
dt->dt_min = rsec / 60;
rsec = rsec % 60;
dt->dt_sec = rsec;
-#undef month_days
+
+ return 0;
}
diff -r 8f336cd0e309 -r 420a4615a1be sys/dev/clock_subr.h
--- a/sys/dev/clock_subr.h Wed Nov 12 14:22:20 2014 +0000
+++ b/sys/dev/clock_subr.h Wed Nov 12 18:50:55 2014 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: clock_subr.h,v 1.21 2009/12/12 15:10:34 tsutsui Exp $ */
+/* $NetBSD: clock_subr.h,v 1.21.38.1 2014/11/12 18:50:55 snj Exp $ */
/*-
* Copyright (c) 1996 The NetBSD Foundation, Inc.
@@ -36,7 +36,7 @@
* "POSIX time" to/from "YY/MM/DD/hh/mm/ss"
*/
struct clock_ymdhms {
- u_short dt_year;
+ uint64_t dt_year;
u_char dt_mon;
u_char dt_day;
u_char dt_wday; /* Day of week */
@@ -46,7 +46,7 @@
};
time_t clock_ymdhms_to_secs(struct clock_ymdhms *);
-void clock_secs_to_ymdhms(time_t, struct clock_ymdhms *);
+int clock_secs_to_ymdhms(time_t, struct clock_ymdhms *);
/*
* BCD to binary and binary to BCD.
diff -r 8f336cd0e309 -r 420a4615a1be sys/fs/msdosfs/msdosfs_conv.c
--- a/sys/fs/msdosfs/msdosfs_conv.c Wed Nov 12 14:22:20 2014 +0000
+++ b/sys/fs/msdosfs/msdosfs_conv.c Wed Nov 12 18:50:55 2014 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: msdosfs_conv.c,v 1.9 2013/01/26 16:51:51 christos Exp $ */
+/* $NetBSD: msdosfs_conv.c,v 1.9.12.1 2014/11/12 18:50:55 snj Exp $ */
Home |
Main Index |
Thread Index |
Old Index