1986-01-13 17:21:20 -05:00
|
|
|
#
|
|
|
|
|
|
|
|
|
|
/*LINTLIBRARY*/
|
|
|
|
|
|
1986-03-02 22:03:44 -05:00
|
|
|
/*
|
|
|
|
|
** Should there be any built-in rules other than GMT?
|
|
|
|
|
** In particular, should zones such as "EST5" (abbreviation is always "EST",
|
|
|
|
|
** GMT offset is always 5 hours) be built in?
|
|
|
|
|
*/
|
|
|
|
|
|
1986-02-15 15:23:29 -05:00
|
|
|
#include "tzfile.h"
|
1986-01-13 17:21:20 -05:00
|
|
|
#include "time.h"
|
|
|
|
|
|
|
|
|
|
#ifdef OBJECTID
|
|
|
|
|
static char sccsid[] = "%W%";
|
|
|
|
|
#endif
|
|
|
|
|
|
1986-03-02 22:03:44 -05:00
|
|
|
#ifndef TRUE
|
|
|
|
|
#define TRUE 1
|
|
|
|
|
#define FALSE 0
|
|
|
|
|
#endif
|
|
|
|
|
|
1986-03-04 09:49:44 -05:00
|
|
|
#ifndef MAXPATHLEN
|
|
|
|
|
#define MAXPATHLEN 1024
|
|
|
|
|
#endif
|
|
|
|
|
|
1986-01-15 08:27:06 -05:00
|
|
|
extern char * asctime();
|
|
|
|
|
extern struct tm * gmtime();
|
|
|
|
|
extern char * strcpy();
|
|
|
|
|
extern char * strcat();
|
|
|
|
|
extern char * getenv();
|
1986-01-13 17:21:20 -05:00
|
|
|
|
1986-03-02 22:03:44 -05:00
|
|
|
static struct tzhead h;
|
|
|
|
|
static long ats[TZ_MAX_TIMES];
|
|
|
|
|
static unsigned char types[TZ_MAX_TIMES];
|
|
|
|
|
static struct ttinfo ttis[TZ_MAX_TYPES];
|
|
|
|
|
static char chars[TZ_MAX_CHARS + 1];
|
1986-03-04 09:49:44 -05:00
|
|
|
|
|
|
|
|
#define TZ_MAX_TOTAL (sizeof h + sizeof ats + sizeof types + \
|
|
|
|
|
sizeof ttis + sizeof chars)
|
|
|
|
|
|
1986-03-02 22:03:44 -05:00
|
|
|
static char isset;
|
1986-01-13 17:21:20 -05:00
|
|
|
|
1986-01-16 08:50:51 -05:00
|
|
|
char * tz_abbr; /* set by localtime; available to all */
|
|
|
|
|
|
1986-03-04 09:49:44 -05:00
|
|
|
/*
|
|
|
|
|
** Not available west of the Rockies. . .
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
|
memcpy(to, from, n)
|
|
|
|
|
char * to;
|
|
|
|
|
char * from;
|
|
|
|
|
{
|
|
|
|
|
register int i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < n; ++i)
|
|
|
|
|
to[i] = from[i];
|
|
|
|
|
return to;
|
|
|
|
|
}
|
|
|
|
|
|
1986-03-01 17:07:13 -05:00
|
|
|
static
|
|
|
|
|
tzload(tzname)
|
1986-01-16 08:50:51 -05:00
|
|
|
register char * tzname;
|
1986-01-13 17:21:20 -05:00
|
|
|
{
|
1986-03-04 09:49:44 -05:00
|
|
|
register char * p;
|
1986-03-02 22:03:44 -05:00
|
|
|
register int fid;
|
|
|
|
|
register int i;
|
1986-03-04 09:49:44 -05:00
|
|
|
register int doaccess;
|
|
|
|
|
char buf[(TZ_MAX_TOTAL>MAXPATHLEN)?TZ_MAX_TOTAL:MAXPATHLEN];
|
|
|
|
|
|
|
|
|
|
if (tzname == 0 && (tzname = TZDEFAULT) == 0)
|
|
|
|
|
return -1;
|
|
|
|
|
doaccess = tzname[0] == '/';
|
|
|
|
|
if (!doaccess) {
|
|
|
|
|
if ((p = TZDIR) == 0)
|
|
|
|
|
return -1;
|
|
|
|
|
if ((strlen(p) + strlen(tzname) + 1) >= sizeof buf)
|
|
|
|
|
return -1;
|
|
|
|
|
(void) strcpy(buf, p);
|
1986-01-13 20:30:59 -05:00
|
|
|
(void) strcat(buf, "/");
|
1986-01-16 08:50:51 -05:00
|
|
|
(void) strcat(buf, tzname);
|
1986-03-04 09:49:44 -05:00
|
|
|
while (*tzname != '\0')
|
|
|
|
|
if (*tzname++ == '.')
|
|
|
|
|
doaccess = TRUE;
|
1986-01-16 08:50:51 -05:00
|
|
|
tzname = buf;
|
1986-01-13 20:30:59 -05:00
|
|
|
}
|
1986-03-04 09:49:44 -05:00
|
|
|
if (doaccess && access(tzname, 4) != 0)
|
|
|
|
|
return -1;
|
1986-01-16 08:50:51 -05:00
|
|
|
if ((fid = open(tzname, 0)) == -1)
|
1986-03-04 09:49:44 -05:00
|
|
|
return -1;
|
|
|
|
|
p = buf;
|
|
|
|
|
i = read(fid, p, sizeof buf);
|
|
|
|
|
if (close(fid) != 0 || i < sizeof h)
|
|
|
|
|
return -1;
|
|
|
|
|
(void) memcpy((char *) &h, p, sizeof h);
|
1986-03-02 22:03:44 -05:00
|
|
|
if (h.tzh_timecnt > TZ_MAX_TIMES ||
|
|
|
|
|
h.tzh_typecnt == 0 || h.tzh_typecnt > TZ_MAX_TYPES ||
|
|
|
|
|
h.tzh_charcnt > TZ_MAX_CHARS)
|
1986-03-04 09:49:44 -05:00
|
|
|
return -1;
|
|
|
|
|
if (i < sizeof h +
|
|
|
|
|
h.tzh_timecnt * (sizeof ats[0] + sizeof types[0]) +
|
|
|
|
|
h.tzh_typecnt * sizeof ttis[0] +
|
|
|
|
|
h.tzh_charcnt * sizeof chars[0])
|
|
|
|
|
return -1;
|
|
|
|
|
p += sizeof h;
|
|
|
|
|
if ((i = h.tzh_timecnt) > 0) {
|
|
|
|
|
(void) memcpy((char *) ats, p, i * sizeof ats[0]);
|
|
|
|
|
p += i * sizeof ats[0];
|
|
|
|
|
(void) memcpy((char *) types, p, i * sizeof types[0]);
|
|
|
|
|
p += i * sizeof types[0];
|
|
|
|
|
}
|
|
|
|
|
if ((i = h.tzh_typecnt) > 0) {
|
|
|
|
|
(void) memcpy((char *) ttis, p, i * sizeof ttis[0]);
|
|
|
|
|
p += i * sizeof ttis[0];
|
|
|
|
|
}
|
|
|
|
|
if ((i = h.tzh_charcnt) > 0)
|
|
|
|
|
(void) memcpy((char *) chars, p, i * sizeof chars[0]);
|
|
|
|
|
chars[h.tzh_charcnt] = '\0'; /* ensure '\0'-termination */
|
1986-03-02 22:03:44 -05:00
|
|
|
for (i = 0; i < h.tzh_timecnt; ++i)
|
|
|
|
|
if (types[i] > h.tzh_typecnt)
|
1986-03-04 09:49:44 -05:00
|
|
|
return -1;
|
1986-03-02 22:03:44 -05:00
|
|
|
for (i = 0; i < h.tzh_typecnt; ++i)
|
|
|
|
|
if (ttis[i].tt_abbrind > h.tzh_charcnt)
|
1986-03-04 09:49:44 -05:00
|
|
|
return -1;
|
1986-01-13 17:21:20 -05:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
1986-01-16 08:50:51 -05:00
|
|
|
/*
|
1986-03-01 17:07:13 -05:00
|
|
|
** settz("") Use built-in GMT.
|
|
|
|
|
** settz(0) Use TZDEFAULT.
|
|
|
|
|
** settz(otherwise) Use otherwise.
|
1986-01-16 08:50:51 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
settz(tzname)
|
|
|
|
|
char * tzname;
|
|
|
|
|
{
|
|
|
|
|
register int answer;
|
|
|
|
|
|
1986-03-02 22:03:44 -05:00
|
|
|
isset = TRUE;
|
1986-02-28 21:29:14 -05:00
|
|
|
if (tzname != 0 && *tzname == '\0')
|
1986-01-16 08:50:51 -05:00
|
|
|
answer = 0; /* Use built-in GMT */
|
|
|
|
|
else {
|
|
|
|
|
if (tzload(tzname) == 0)
|
|
|
|
|
return 0;
|
|
|
|
|
/*
|
1986-02-28 21:29:14 -05:00
|
|
|
** If we want to try for local time on errors. . .
|
|
|
|
|
if (tzload((char *) 0) == 0)
|
1986-01-16 08:50:51 -05:00
|
|
|
return -1;
|
1986-02-28 21:29:14 -05:00
|
|
|
*/
|
1986-01-16 08:50:51 -05:00
|
|
|
answer = -1;
|
|
|
|
|
}
|
1986-03-02 22:03:44 -05:00
|
|
|
h.tzh_timecnt = 0;
|
|
|
|
|
ttis[0].tt_gmtoff = 0;
|
|
|
|
|
ttis[0].tt_abbrind = 0;
|
|
|
|
|
(void) strcpy(chars, "GMT");
|
1986-01-16 08:50:51 -05:00
|
|
|
return answer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct tm *
|
|
|
|
|
newlocaltime(timep)
|
1986-03-02 22:03:44 -05:00
|
|
|
long * timep;
|
1986-01-13 17:21:20 -05:00
|
|
|
{
|
1986-03-02 22:03:44 -05:00
|
|
|
register struct ttinfo * ttip;
|
1986-01-16 08:50:51 -05:00
|
|
|
register struct tm * ct;
|
1986-01-13 20:30:59 -05:00
|
|
|
register int i;
|
1986-01-16 08:50:51 -05:00
|
|
|
long t;
|
1986-01-13 17:21:20 -05:00
|
|
|
|
1986-01-16 08:50:51 -05:00
|
|
|
t = *timep;
|
1986-03-02 22:03:44 -05:00
|
|
|
if (!isset)
|
1986-01-13 20:30:59 -05:00
|
|
|
(void) settz(getenv("TZ"));
|
1986-03-02 22:03:44 -05:00
|
|
|
if (h.tzh_timecnt == 0 || t < ats[0])
|
|
|
|
|
i = 0;
|
1986-01-16 08:50:51 -05:00
|
|
|
else {
|
1986-03-02 22:03:44 -05:00
|
|
|
for (i = 1; i < h.tzh_timecnt; ++i)
|
|
|
|
|
if (t < ats[i])
|
1986-01-16 08:50:51 -05:00
|
|
|
break;
|
1986-03-02 22:03:44 -05:00
|
|
|
i = types[i - 1];
|
1986-01-16 08:50:51 -05:00
|
|
|
}
|
1986-03-02 22:03:44 -05:00
|
|
|
ttip = &ttis[i];
|
|
|
|
|
t += ttip->tt_gmtoff;
|
1986-01-16 08:50:51 -05:00
|
|
|
ct = gmtime(&t);
|
1986-03-02 22:03:44 -05:00
|
|
|
ct->tm_isdst = ttip->tt_isdst;
|
|
|
|
|
tz_abbr = &chars[ttip->tt_abbrind];
|
1986-01-16 08:50:51 -05:00
|
|
|
return ct;
|
1986-01-13 17:21:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char *
|
1986-01-15 08:27:06 -05:00
|
|
|
newctime(timep)
|
1986-03-01 17:07:13 -05:00
|
|
|
long * timep;
|
1986-01-13 17:21:20 -05:00
|
|
|
{
|
1986-03-02 22:03:44 -05:00
|
|
|
return asctime(newlocaltime(timep));
|
1986-01-13 17:21:20 -05:00
|
|
|
}
|