Loading...
stdtime/FreeBSD/localtime.c Libc-583 Libc-320
--- Libc/Libc-583/stdtime/FreeBSD/localtime.c
+++ Libc/Libc-320/stdtime/FreeBSD/localtime.c
@@ -1,15 +1,15 @@
 /*
 ** This file is in the public domain, so clarified as of
-** 1996-06-05 by Arthur David Olson (arthur_david_olson@nih.gov).
+** June 5, 1996 by Arthur David Olson (arthur_david_olson@nih.gov).
 */
 
 #include <sys/cdefs.h>
 #ifndef lint
 #ifndef NOID
-static char	elsieid[] __unused = "@(#)localtime.c	7.78";
+static char	elsieid[] __unused = "@(#)localtime.c	7.57";
 #endif /* !defined NOID */
 #endif /* !defined lint */
-__FBSDID("$FreeBSD: src/lib/libc/stdtime/localtime.c,v 1.40 2004/08/24 00:15:37 peter Exp $");
+__FBSDID("$FreeBSD: src/lib/libc/stdtime/localtime.c,v 1.36 2003/02/16 17:29:11 nectar Exp $");
 
 /*
 ** Leap second handling from Bradley White (bww@k.gp.cs.cmu.edu).
@@ -70,30 +70,14 @@
 
 static char		wildabbr[] = "WILDABBR";
 
-/*
- * In June 2004 it was decided UTC was a more appropriate default time
- * zone than GMT.
- */
-
-static const char	gmt[] = "UTC";
-
-/*
-** The DST rules to use if TZ has no rules and we can't load TZDEFRULES.
-** We default to US rules as of 1999-08-17.
-** POSIX 1003.1 section 8.1.1 says that the default DST rules are
-** implementation dependent; for historical reasons, US rules are a
-** common default.
-*/
-#ifndef TZDEFRULESTRING
-#define TZDEFRULESTRING ",M4.1.0,M10.5.0"
-#endif /* !defined TZDEFDST */
+static const char	gmt[] = "GMT";
 
 struct ttinfo {				/* time type information */
-	long		tt_gmtoff;	/* UTC offset in seconds */
+	long		tt_gmtoff;	/* GMT offset in seconds */
 	int		tt_isdst;	/* used to set tm_isdst */
 	int		tt_abbrind;	/* abbreviation list index */
 	int		tt_ttisstd;	/* TRUE if transition is std time */
-	int		tt_ttisgmt;	/* TRUE if transition is UTC */
+	int		tt_ttisgmt;	/* TRUE if transition is GMT */
 };
 
 struct lsinfo {				/* leap second information */
@@ -163,10 +147,6 @@
 				void(*funcp) (const time_t *,
 				long, struct tm*),
 				long offset, int * okayp);
-static time_t		time2sub(struct tm *tmp,
-				void(*funcp) (const time_t *,
-				long, struct tm*),
-				long offset, int * okayp, int do_norm_secs);
 static void		timesub(const time_t * timep, long offset,
 				const struct state * sp, struct tm * tmp);
 static int		tmcomp(const struct tm * atmp,
@@ -343,23 +323,27 @@
 	}
 	{
 		struct tzhead *	tzhp;
-		union {
-			struct tzhead	tzhead;
-			char		buf[sizeof *sp + sizeof *tzhp];
-		} u;
+		char		buf[sizeof *sp + sizeof *tzhp];
 		int		ttisstdcnt;
 		int		ttisgmtcnt;
 
-		i = _read(fid, u.buf, sizeof u.buf);
+		i = _read(fid, buf, sizeof buf);
 		if (_close(fid) != 0)
 			return -1;
-		ttisstdcnt = (int) detzcode(u.tzhead.tzh_ttisstdcnt);
-		ttisgmtcnt = (int) detzcode(u.tzhead.tzh_ttisgmtcnt);
-		sp->leapcnt = (int) detzcode(u.tzhead.tzh_leapcnt);
-		sp->timecnt = (int) detzcode(u.tzhead.tzh_timecnt);
-		sp->typecnt = (int) detzcode(u.tzhead.tzh_typecnt);
-		sp->charcnt = (int) detzcode(u.tzhead.tzh_charcnt);
-		p = u.tzhead.tzh_charcnt + sizeof u.tzhead.tzh_charcnt;
+		p = buf;
+		p += (sizeof tzhp->tzh_magic) + (sizeof tzhp->tzh_reserved);
+		ttisstdcnt = (int) detzcode(p);
+		p += 4;
+		ttisgmtcnt = (int) detzcode(p);
+		p += 4;
+		sp->leapcnt = (int) detzcode(p);
+		p += 4;
+		sp->timecnt = (int) detzcode(p);
+		p += 4;
+		sp->typecnt = (int) detzcode(p);
+		p += 4;
+		sp->charcnt = (int) detzcode(p);
+		p += 4;
 		if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS ||
 			sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES ||
 			sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES ||
@@ -367,7 +351,7 @@
 			(ttisstdcnt != sp->typecnt && ttisstdcnt != 0) ||
 			(ttisgmtcnt != sp->typecnt && ttisgmtcnt != 0))
 				return -1;
-		if (i - (p - u.buf) < sp->timecnt * 4 +	/* ats */
+		if (i - (p - buf) < sp->timecnt * 4 +	/* ats */
 			sp->timecnt +			/* types */
 			sp->typecnt * (4 + 2) +		/* ttinfos */
 			sp->charcnt +			/* chars */
@@ -625,8 +609,8 @@
 }
 
 /*
-** Given the Epoch-relative time of January 1, 00:00:00 UTC, in a year, the
-** year, a rule, and the offset from UTC at the time that rule takes effect,
+** Given the Epoch-relative time of January 1, 00:00:00 GMT, in a year, the
+** year, a rule, and the offset from GMT at the time that rule takes effect,
 ** calculate the Epoch-relative time that rule takes effect.
 */
 
@@ -712,10 +696,10 @@
 	}
 
 	/*
-	** "value" is the Epoch-relative time of 00:00:00 UTC on the day in
+	** "value" is the Epoch-relative time of 00:00:00 GMT on the day in
 	** question.  To get the Epoch-relative time of the specified local
 	** time on that day, add the transition time and the current offset
-	** from UTC.
+	** from GMT.
 	*/
 	return value + rulep->r_time + offset;
 }
@@ -777,8 +761,6 @@
 			if (name == NULL)
 				return -1;
 		} else	dstoffset = stdoffset - SECSPERHOUR;
-		if (*name == '\0' && load_result != 0)
-			name = TZDEFRULESTRING;
 		if (*name == ',' || *name == ';') {
 			struct rule	start;
 			struct rule	end;
@@ -840,6 +822,8 @@
 			int	j;
 
 			if (*name != '\0')
+				return -1;
+			if (load_result != 0)
 				return -1;
 			/*
 			** Initial values of theirstdoffset and theirdstoffset.
@@ -914,7 +898,6 @@
 			sp->ttis[1].tt_gmtoff = -dstoffset;
 			sp->ttis[1].tt_isdst = TRUE;
 			sp->ttis[1].tt_abbrind = stdlen + 1;
-			sp->typecnt = 2;
 		}
 	} else {
 		dstlen = 0;
@@ -927,7 +910,7 @@
 	sp->charcnt = stdlen + 1;
 	if (dstlen != 0)
 		sp->charcnt += dstlen + 1;
-	if ((size_t) sp->charcnt > sizeof sp->chars)
+	if (sp->charcnt > sizeof sp->chars)
 		return -1;
 	cp = sp->chars;
 	(void) strncpy(cp, stdname, stdlen);
@@ -988,9 +971,9 @@
 		return;
 	}
 
-	if (lcl_is_set > 0 && strcmp(lcl_TZname, name) == 0)
+	if (lcl_is_set > 0  &&  strcmp(lcl_TZname, name) == 0)
 		return;
-	lcl_is_set = strlen(name) < sizeof lcl_TZname;
+	lcl_is_set = (strlen(name) < sizeof(lcl_TZname));
 	if (lcl_is_set)
 		(void) strcpy(lcl_TZname, name);
 
@@ -1009,8 +992,6 @@
 		*/
 		lclptr->leapcnt = 0;		/* so, we're off a little */
 		lclptr->timecnt = 0;
-		lclptr->typecnt = 0;
-		lclptr->ttis[0].tt_isdst = 0;
 		lclptr->ttis[0].tt_gmtoff = 0;
 		lclptr->ttis[0].tt_abbrind = 0;
 		(void) strcpy(lclptr->chars, gmt);
@@ -1085,6 +1066,18 @@
 }
 
 struct tm *
+localtime_r(timep, p_tm)
+const time_t * const	timep;
+struct tm *p_tm;
+{
+	_MUTEX_LOCK(&lcl_mutex);
+	tzset_basic();
+	localsub(timep, 0L, p_tm);
+	_MUTEX_UNLOCK(&lcl_mutex);
+	return(p_tm);
+}
+
+struct tm *
 localtime(timep)
 const time_t * const	timep;
 {
@@ -1121,22 +1114,6 @@
 }
 
 /*
-** Re-entrant version of localtime.
-*/
-
-struct tm *
-localtime_r(timep, tm)
-const time_t * const	timep;
-struct tm *		tm;
-{
-	_MUTEX_LOCK(&lcl_mutex);
-	tzset_basic();
-	localsub(timep, 0L, tm);
-	_MUTEX_UNLOCK(&lcl_mutex);
-	return tm;
-}
-
-/*
 ** gmtsub is to gmtime as localsub is to localtime.
 */
 
@@ -1160,7 +1137,7 @@
 #ifdef TM_ZONE
 	/*
 	** Could get fancy here and deliver something such as
-	** "UTC+xxxx" or "UTC-xxxx" if offset is non-zero,
+	** "GMT+xxxx" or "GMT-xxxx" if offset is non-zero,
 	** but this is no time for a treasure hunt.
 	*/
 	if (offset != 0)
@@ -1215,17 +1192,11 @@
 	}
 }
 
-/*
-* Re-entrant version of gmtime.
-*/
-
 struct tm *
-gmtime_r(timep, tm)
-const time_t * const	timep;
-struct tm *		tm;
+gmtime_r(const time_t * timep, struct tm * tm)
 {
 	gmtsub(timep, 0L, tm);
-	return tm;
+	return(tm);
 }
 
 #ifdef STD_INSPIRED
@@ -1348,7 +1319,7 @@
 {
 /*
 ** Section 4.12.3.2 of X3.159-1989 requires that
-**	The ctime function converts the calendar time pointed to by timer
+**	The ctime funciton converts the calendar time pointed to by timer
 **	to local time in the form of a string.  It is equivalent to
 **		asctime(localtime(timer))
 */
@@ -1358,10 +1329,9 @@
 char *
 ctime_r(timep, buf)
 const time_t * const	timep;
-char *			buf;
-{
-	struct tm	tm;
-
+char *buf;
+{
+        struct tm tm;
 	return asctime_r(localtime_r(timep, &tm), buf);
 }
 
@@ -1427,12 +1397,11 @@
 }
 
 static time_t
-time2sub(tmp, funcp, offset, okayp, do_norm_secs)
+time2(tmp, funcp, offset, okayp)
 struct tm * const	tmp;
 void (* const		funcp)(const time_t*, long, struct tm*);
 const long		offset;
 int * const		okayp;
-const int		do_norm_secs;
 {
 	const struct state *	sp;
 	int			dir;
@@ -1445,11 +1414,6 @@
 
 	*okayp = FALSE;
 	yourtm = *tmp;
-	if (do_norm_secs) {
-		if (normalize_overflow(&yourtm.tm_min, &yourtm.tm_sec,
-			SECSPERMIN))
-				return WRONG;
-	}
 	if (normalize_overflow(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR))
 		return WRONG;
 	if (normalize_overflow(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY))
@@ -1486,9 +1450,6 @@
 		}
 	}
 	if (increment_overflow(&yourtm.tm_year, -TM_YEAR_BASE))
-		return WRONG;
-	/* Don't go below 1900 for POLA */
-	if (yourtm.tm_year < 0)
 		return WRONG;
 	if (yourtm.tm_sec >= 0 && yourtm.tm_sec < SECSPERMIN)
 		saved_seconds = 0;
@@ -1547,7 +1508,12 @@
 		** It's okay to guess wrong since the guess
 		** gets checked.
 		*/
-		sp = (funcp == localsub) ? lclptr : gmtptr;
+		/*
+		** The (void *) casts are the benefit of SunOS 3.3 on Sun 2's.
+		*/
+		sp = (const struct state *)
+			(((void *) funcp == (void *) localsub) ?
+			lclptr : gmtptr);
 #ifdef ALL_STATE
 		if (sp == NULL)
 			return WRONG;
@@ -1585,24 +1551,6 @@
 }
 
 static time_t
-time2(tmp, funcp, offset, okayp)
-struct tm * const	tmp;
-void (* const		funcp)(const time_t*, long, struct tm*);
-const long		offset;
-int * const		okayp;
-{
-	time_t	t;
-
-	/*
-	** First try without normalization of seconds
-	** (in case tm_sec contains a value associated with a leap second).
-	** If that fails, try with normalization of seconds.
-	*/
-	t = time2sub(tmp, funcp, offset, okayp, FALSE);
-	return *okayp ? t : time2sub(tmp, funcp, offset, okayp, TRUE);
-}
-
-static time_t
 time1(tmp, funcp, offset)
 struct tm * const	tmp;
 void (* const		funcp)(const time_t *, long, struct tm *);
@@ -1611,11 +1559,6 @@
 	time_t			t;
 	const struct state *	sp;
 	int			samei, otheri;
-	int			sameind, otherind;
-	int			i;
-	int			nseen;
-	int				seen[TZ_MAX_TYPES];
-	int				types[TZ_MAX_TYPES];
 	int				okay;
 
 	if (tmp->tm_isdst > 1)
@@ -1640,25 +1583,19 @@
 	** We try to divine the type they started from and adjust to the
 	** type they need.
 	*/
-	sp = (funcp == localsub) ? lclptr : gmtptr;
+	/*
+	** The (void *) casts are the benefit of SunOS 3.3 on Sun 2's.
+	*/
+	sp = (const struct state *) (((void *) funcp == (void *) localsub) ?
+		lclptr : gmtptr);
 #ifdef ALL_STATE
 	if (sp == NULL)
 		return WRONG;
 #endif /* defined ALL_STATE */
-	for (i = 0; i < sp->typecnt; ++i)
-		seen[i] = FALSE;
-	nseen = 0;
-	for (i = sp->timecnt - 1; i >= 0; --i)
-		if (!seen[sp->types[i]]) {
-			seen[sp->types[i]] = TRUE;
-			types[nseen++] = sp->types[i];
-		}
-	for (sameind = 0; sameind < nseen; ++sameind) {
-		samei = types[sameind];
+	for (samei = sp->typecnt - 1; samei >= 0; --samei) {
 		if (sp->ttis[samei].tt_isdst != tmp->tm_isdst)
 			continue;
-		for (otherind = 0; otherind < nseen; ++otherind) {
-			otheri = types[otherind];
+		for (otheri = sp->typecnt - 1; otheri >= 0; --otheri) {
 			if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst)
 				continue;
 			tmp->tm_sec += sp->ttis[otheri].tt_gmtoff -
@@ -1744,7 +1681,7 @@
 
 /*
 ** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599
-** shall correspond to "Wed Dec 31 23:59:59 UTC 1986", which
+** shall correspond to "Wed Dec 31 23:59:59 GMT 1986", which
 ** is not the case if we are accounting for leap seconds.
 ** So, we provide the following conversion routines for use
 ** when exchanging timestamps with POSIX conforming systems.