newlib-cygwin/newlib/libc/time/tzset_r.c

205 lines
3.6 KiB
C
Raw Normal View History

#include <_ansi.h>
#include <reent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/time.h>
#include "local.h"
static char __tzname_std[11];
static char __tzname_dst[11];
static char *prev_tzenv = NULL;
/* default to GMT */
char *_tzname[2] = {"GMT", "GMT"};
int _daylight = 0;
long _timezone = 0;
_VOID
_DEFUN (_tzset_r, (reent_ptr),
struct _reent *reent_ptr)
{
char *tzenv;
unsigned short hh, mm, ss, m, w, d;
int sign, n;
int i, ch;
__tzinfo_type *tz = __gettzinfo ();
if ((tzenv = _getenv_r (reent_ptr, "TZ")) == NULL)
{
TZ_LOCK;
_timezone = 0;
_daylight = 0;
_tzname[0] = "GMT";
_tzname[1] = "GMT";
TZ_UNLOCK;
return;
}
TZ_LOCK;
if (prev_tzenv != NULL && strcmp(tzenv, prev_tzenv) == 0)
{
TZ_UNLOCK;
return;
}
free(prev_tzenv);
2002-08-26 Jeff Johnston <jjohnstn@redhat.com> * Makefile.am(LIBC_OBJECTLISTS): Add STDIO64_OBJECTLIST. * Makefile.in: Regenerated. * acinclude.m4: Add support for --enable-newlib-elix-level option. * aclocal.m4: Regenerated. * configure: Ditto. * configure.host: Add code to define _ELIX_LEVEL if --enable-newlib-elix-level option is used. * configure.in: * libc/aclocal.m4: Regenerated. * libc/configure: Ditto. * libc/argz/Makefile.am: Add EL/IX level checking. * libc/argz/Makefile.in: Regenerated. * libc/argz/dummy.c: New file. * libc/ctype/Makefile.am: Add EL/IX level checking. * libc/ctype/Makefile.in: Regenerated. * libc/locale/Makefile.am: Add EL/IX level checking. * libc/locale/Makefile.in: Regenerated. * libc/posix/Makefile.am: Add EL/IX level checking. * libc/posix/Makefile.in: Regenerated. * libc/posix/telldir.c: Add EL/IX level checking. * libc/reent/Makefile.am: Ditto. * libc/reent/fstat64r.c: Ditto. * libc/reent/lseek64r.c: Ditto. * libc/reent/open64r.c: Ditto. * libc/reent/Makefile.in: Regenerated. * libc/search/Makefile.am: Add EL/IX level checking. * libc/search/Makefile.in: Regenerated. * libc/stdio/Makefile.am: Add EL/IX level checking. * libc/stdio/Makefile.in: Regenerated. * libc/stdio64/Makefile.am: Add EL/IX level checking. * libc/stdio64/Makefile.in: Regenerated. * libc/stdio64/dummy.c: New file. * libc/stdio64/fgetpos64.c: Fix so _LARGEFILE_64 macro is checked after first include. * libc/stdio64/fopen64.c: Ditto. * libc/stdio64/freopen64.c: Ditto. * libc/stdio64/fseeko64.c: Ditto. * libc/stdio64/fsetpos64.c: Ditto. * libc/stdio64/ftello64.c: Ditto. * libc/stdio64/tmpfile64.c: Ditto. * libc/stdlib/Makefile.am: Add EL/IX level checking. * libc/stdlib/Makefile.in: Regenerated. * libc/stdlib/mstats.c: Add EL/IX level checking. * libc/string/Makefile.am: Ditto. * libc/string/Makefile.in: Regenerated. * libc/sys/linux/Makefile.am: Add EL/IX level checking. * libc/sys/linux/Makefile.in: Regenerated. * libc/sys/linux/aclocal.m4: Ditto. * libc/sys/linux/configure: Ditto. * libc/sys/linux/aio.c: Add EL/IX level checking. * libc/sys/linux/ftok.c: Ditto. * libc/sys/linux/getdate.c: Ditto. * libc/sys/linux/ids.c: Ditto. * libc/sys/linux/inode.c: Ditto. * libc/sys/linux/io.c: Ditto. * libc/sys/linux/process.c: Ditto. * libc/sys/linux/resource.c: Ditto. * libc/sys/linux/sched.c: Ditto. * libc/sys/linux/sig.c: Ditto. * libc/sys/linux/termios.c: Ditto. * libc/sys/linux/wait.c: Ditto plus add __waitpid and __libc___waitpid weak aliases. * libc/sys/linux/machine/i386/syscall.h: Add new _base macros that generate the code for a syscall, but do not create a weak alias. * libc/syscalls/Makefile.am: Add EL/IX level checking. * libc/syscalls/Makefile.in: Regenerated. * libc/time/tzset_r.c: Change to replace strdup with equivalent functionality. * libc/unix/Makefile.am: Add EL/IX level checking. * libc/unix/Makefile.in: Regenerated.
2002-08-27 02:56:09 +08:00
prev_tzenv = _malloc_r (reent_ptr, strlen(tzenv) + 1);
if (prev_tzenv != NULL)
strcpy (prev_tzenv, tzenv);
/* ignore implementation-specific format specifier */
if (*tzenv == ':')
++tzenv;
if (sscanf (tzenv, "%10[^0-9,+-]%n", __tzname_std, &n) <= 0)
{
TZ_UNLOCK;
return;
}
tzenv += n;
sign = 1;
if (*tzenv == '-')
{
sign = -1;
++tzenv;
}
else if (*tzenv == '+')
++tzenv;
mm = 0;
ss = 0;
if (sscanf (tzenv, "%hu%n:%hu%n:%hu%n", &hh, &n, &mm, &n, &ss, &n) < 1)
{
TZ_UNLOCK;
return;
}
tz->__tzrule[0].offset = sign * (ss + SECSPERMIN * mm + SECSPERHOUR * hh);
_tzname[0] = __tzname_std;
tzenv += n;
if (sscanf (tzenv, "%10[^0-9,+-]%n", __tzname_dst, &n) <= 0)
{
_tzname[1] = _tzname[0];
TZ_UNLOCK;
return;
}
else
_tzname[1] = __tzname_dst;
tzenv += n;
/* otherwise we have a dst name, look for the offset */
sign = 1;
if (*tzenv == '-')
{
sign = -1;
++tzenv;
}
else if (*tzenv == '+')
++tzenv;
hh = 0;
mm = 0;
ss = 0;
if (sscanf (tzenv, "%hu%n:%hu%n:%hu%n", &hh, &n, &mm, &n, &ss, &n) <= 0)
tz->__tzrule[1].offset = tz->__tzrule[0].offset - 3600;
else
tz->__tzrule[1].offset = sign * (ss + SECSPERMIN * mm + SECSPERHOUR * hh);
tzenv += n;
for (i = 0; i < 2; ++i)
{
if (*tzenv == ',')
++tzenv;
if (*tzenv == 'M')
{
if (sscanf (tzenv, "M%hu%n.%hu%n.%hu%n", &m, &n, &w, &n, &d, &n) != 3 ||
m < 1 || m > 12 || w < 1 || w > 5 || d > 6)
{
TZ_UNLOCK;
return;
}
tz->__tzrule[i].ch = 'M';
tz->__tzrule[i].m = m;
tz->__tzrule[i].n = w;
tz->__tzrule[i].d = d;
tzenv += n;
}
else
{
char *end;
if (*tzenv == 'J')
{
ch = 'J';
++tzenv;
}
else
ch = 'D';
d = strtoul (tzenv, &end, 10);
/* if unspecified, default to US settings */
if (end == tzenv)
{
if (i == 0)
{
tz->__tzrule[0].ch = 'M';
tz->__tzrule[0].m = 4;
tz->__tzrule[0].n = 1;
tz->__tzrule[0].d = 0;
}
else
{
tz->__tzrule[1].ch = 'M';
tz->__tzrule[1].m = 10;
tz->__tzrule[1].n = 5;
tz->__tzrule[1].d = 0;
}
}
else
{
tz->__tzrule[i].ch = ch;
tz->__tzrule[i].d = d;
}
tzenv = end;
}
/* default time is 02:00:00 am */
hh = 2;
mm = 0;
ss = 0;
n = 0;
if (*tzenv == '/')
sscanf (tzenv, "/%hu%n:%hu%n:%hu%n", &hh, &n, &mm, &n, &ss, &n);
tz->__tzrule[i].s = ss + SECSPERMIN * mm + SECSPERHOUR * hh;
tzenv += n;
}
__tzcalc_limits (tz->__tzyear);
_timezone = tz->__tzrule[0].offset;
_daylight = tz->__tzrule[0].offset != tz->__tzrule[1].offset;
TZ_UNLOCK;
}