mirror of
https://frontier.innolan.net/rainlance/amiga-tz.git
synced 2025-11-20 04:32:11 +00:00
AmigaOS specific changes
This commit is contained in:
@ -38,17 +38,21 @@ zdump.o: zdump.c private.h version.h
|
||||
|
||||
zic.o: zic.c private.h tzfile.h version.h
|
||||
|
||||
strtoll.o: strtoll.c
|
||||
|
||||
amiga_locale.o: amiga_locale.c
|
||||
|
||||
zic: zic.o
|
||||
${CC} ${CFLAGS} -o zic zic.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
zdump: zdump.o localtime.o asctime.o
|
||||
${CC} ${CFLAGS} -o zdump zdump.o localtime.o asctime.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
date: date.o localtime.o strftime.o asctime.o
|
||||
${CC} ${CFLAGS} -o date date.o localtime.o strftime.o asctime.o $(LDFLAGS) $(LDLIBS)
|
||||
date: date.o localtime.o strftime.o asctime.o strtoll.o amiga_locale.o
|
||||
${CC} ${CFLAGS} -o date date.o localtime.o strftime.o asctime.o strtoll.o amiga_locale.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
lib: localtime.o asctime.o difftime.o strftime.o
|
||||
${AR} rc libtz.a localtime.o asctime.o difftime.o strftime.o
|
||||
lib: localtime.o asctime.o difftime.o strftime.o strtoll.o amiga_locale.o
|
||||
${AR} rc libtz.a localtime.o asctime.o difftime.o strftime.o strtoll.o amiga_locale.o
|
||||
|
||||
clean:
|
||||
rm -f asctime.o date.o difftime.o localtime.o strftime.o zdump.o zic.o aros.o zic zdump date libtz.a
|
||||
|
||||
@ -7,7 +7,7 @@ VERSION= 2015d
|
||||
# Email address for bug reports.
|
||||
BUGEMAIL= carsten.larsen@mail.com
|
||||
|
||||
all: zic lib
|
||||
all: zic zdump date lib
|
||||
|
||||
CC = m68k-amigaos-gcc
|
||||
CFLAGS = -O2 -noixemul -DTZDIR=\"LOCALE:zoneinfo\" -DHAVE_ADJTIME=0 -DHAVE_DOS_FILE_NAMES -DHAVE_LINK=0 -DHAVE_SYMLINK=0 -DHAVE_SYS_STAT_H=0 -DTM_GMTOFF=tm_gmtoff -DTM_ZONE=tm_zone -Dtime_tz=long -DTLOCALTIME_IMPLEMENTATION -DALL_STATE -Wall #-Werror
|
||||
@ -38,20 +38,24 @@ zdump.o: zdump.c private.h version.h
|
||||
|
||||
zic.o: zic.c private.h tzfile.h version.h
|
||||
|
||||
strtoll.o: strtoll.c
|
||||
|
||||
amiga_locale.o: amiga_locale.c
|
||||
|
||||
zic: zic.o
|
||||
${CC} ${CFLAGS} -o zic zic.o $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
zdump: zdump.o localtime.o asctime.o
|
||||
${CC} ${CFLAGS} -o zdump zdump.o localtime.o asctime.o $(LDFLAGS) $(LDLIBS)
|
||||
zdump: zdump.o localtime.o asctime.o strtoll.c
|
||||
${CC} ${CFLAGS} -o zdump zdump.o localtime.o asctime.o strtoll.c $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
date: date.o localtime.o strftime.o asctime.o
|
||||
${CC} ${CFLAGS} -o date date.o localtime.o strftime.o asctime.o $(LDFLAGS) -lsocket $(LDLIBS)
|
||||
date: date.o localtime.o strftime.o asctime.o strtoll.o amiga_locale.o
|
||||
${CC} ${CFLAGS} -o date date.o localtime.o strftime.o asctime.o strtoll.o amiga_locale.o $(LDFLAGS) -lsocket $(LDLIBS)
|
||||
|
||||
lib: localtime.o asctime.o difftime.o strftime.o
|
||||
${AR} rcs libtz.a localtime.o asctime.o difftime.o strftime.o
|
||||
|
||||
clean:
|
||||
rm -f asctime.o date.o difftime.o localtime.o strftime.o zdump.o zic.o zic zdump date libtz.a
|
||||
rm -f asctime.o date.o difftime.o localtime.o strftime.o zdump.o zic.o strtoll.o amiga_locale.o zic zdump date libtz.a
|
||||
|
||||
depend:
|
||||
@echo Dependencies already done
|
||||
|
||||
233
amiga_locale.c
Normal file
233
amiga_locale.c
Normal file
@ -0,0 +1,233 @@
|
||||
/*
|
||||
* Copyright (c) 2015 Carsten Larsen
|
||||
* 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.
|
||||
*
|
||||
* 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 "amiga_tz.h"
|
||||
|
||||
/**********************************************************************/
|
||||
char ** environ = NULL;
|
||||
void syslog(int pri, const char *fmt, ...) { }
|
||||
void logwtmp(const char *line, const char *name, const char *host) { }
|
||||
void lose(int i) { }
|
||||
/**********************************************************************/
|
||||
struct LocaleBase* ctypeBase = NULL;
|
||||
struct Locale* ctypeLocale= NULL;
|
||||
|
||||
void InitCTypeLocale()
|
||||
{
|
||||
ctypeBase = (struct LocaleBase*)OpenLibrary("locale.library", 38L);
|
||||
ctypeLocale = OpenLocale(NULL);
|
||||
}
|
||||
|
||||
void CleanupCTypeLocale()
|
||||
{
|
||||
if (ctypeBase != NULL) {
|
||||
CloseLocale(ctypeLocale);
|
||||
ctypeLocale = NULL;
|
||||
CloseLibrary((struct Library*)ctypeBase);
|
||||
ctypeBase = NULL;
|
||||
}
|
||||
}
|
||||
/**********************************************************************/
|
||||
int isalnum(int c)
|
||||
{
|
||||
int res;
|
||||
InitCTypeLocale();
|
||||
res = IsAlNum(ctypeLocale, (ULONG)c);
|
||||
CleanupCTypeLocale();
|
||||
return res;
|
||||
}
|
||||
|
||||
int isalpha(int c)
|
||||
{
|
||||
int res;
|
||||
InitCTypeLocale();
|
||||
res = IsAlpha(ctypeLocale, (ULONG)c);
|
||||
CleanupCTypeLocale();
|
||||
return res;
|
||||
}
|
||||
|
||||
int isdigit(int c)
|
||||
{
|
||||
int res;
|
||||
InitCTypeLocale();
|
||||
res = IsDigit(ctypeLocale, (ULONG)c);
|
||||
CleanupCTypeLocale();
|
||||
return res;
|
||||
}
|
||||
|
||||
int ispunct(int c)
|
||||
{
|
||||
int res;
|
||||
InitCTypeLocale();
|
||||
res = IsPunct(ctypeLocale, (ULONG)c);
|
||||
CleanupCTypeLocale();
|
||||
return res;
|
||||
}
|
||||
|
||||
int isspace(int c)
|
||||
{
|
||||
int res;
|
||||
InitCTypeLocale();
|
||||
res = IsSpace(ctypeLocale, (ULONG)c);
|
||||
CleanupCTypeLocale();
|
||||
return res;
|
||||
}
|
||||
|
||||
int isupper(int c)
|
||||
{
|
||||
int res;
|
||||
InitCTypeLocale();
|
||||
res = IsUpper(ctypeLocale, (ULONG)c);
|
||||
CleanupCTypeLocale();
|
||||
return res;
|
||||
}
|
||||
|
||||
int iscntrl(int c)
|
||||
{
|
||||
int res;
|
||||
InitCTypeLocale();
|
||||
res = IsCntrl(ctypeLocale, (ULONG)c);
|
||||
CleanupCTypeLocale();
|
||||
return res;
|
||||
}
|
||||
/**********************************************************************/
|
||||
struct Device *TimerBase = NULL;
|
||||
struct timerequest *request = NULL;
|
||||
|
||||
int delete_timer()
|
||||
{
|
||||
struct MsgPort *port;
|
||||
if (request == NULL)
|
||||
return 1;
|
||||
|
||||
port = request->tr_node.io_Message.mn_ReplyPort;
|
||||
|
||||
if (port != 0)
|
||||
DeletePort(port);
|
||||
|
||||
CloseDevice((struct IORequest*)request);
|
||||
DeleteExtIO((struct IORequest*)request);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int create_timer()
|
||||
{
|
||||
LONG error;
|
||||
struct MsgPort *port = CreatePort(0, 0);
|
||||
if (port == NULL)
|
||||
return 1;
|
||||
|
||||
request = (struct timerequest*)CreateExtIO(port, sizeof(struct timerequest));
|
||||
if (request == NULL)
|
||||
{
|
||||
DeletePort(port);
|
||||
return 1;
|
||||
}
|
||||
|
||||
error = OpenDevice(TIMERNAME, UNIT_MICROHZ, (struct IORequest*)request, 0L);
|
||||
if (error != 0)
|
||||
{
|
||||
delete_timer(request);
|
||||
return 1;
|
||||
}
|
||||
|
||||
TimerBase = (struct Device*)request->tr_node.io_Device;
|
||||
|
||||
return 0;
|
||||
}
|
||||
/**********************************************************************/
|
||||
tz_time_t time(tz_time_t *x)
|
||||
{
|
||||
struct LocaleBase* localeBase;
|
||||
struct Locale* locale;
|
||||
struct timeval tv;
|
||||
int error;
|
||||
|
||||
localeBase = (struct LocaleBase*)OpenLibrary("locale.library", 38L);
|
||||
if (localeBase == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
locale = OpenLocale(NULL);
|
||||
if (locale == NULL) {
|
||||
CloseLibrary((struct Library*)localeBase);
|
||||
return 0;
|
||||
}
|
||||
|
||||
error = create_timer();
|
||||
if (error != 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
GetSysTime(&tv);
|
||||
|
||||
if (x) {
|
||||
*x = (time_t)tv.tv_secs - locale->loc_GMTOffset * 60;
|
||||
}
|
||||
|
||||
CloseLocale(locale);
|
||||
CloseLibrary((struct Library*)localeBase);
|
||||
delete_timer();
|
||||
|
||||
return (time_t)tv.tv_secs;
|
||||
}
|
||||
/**********************************************************************/
|
||||
int settimeofday(const struct timeval *tv, const struct timezone *tz)
|
||||
{
|
||||
struct LocaleBase* localeBase;
|
||||
struct Locale* locale;
|
||||
int error;
|
||||
|
||||
localeBase = (struct LocaleBase*)OpenLibrary("locale.library", 38L);
|
||||
if (localeBase == NULL) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
locale = OpenLocale(NULL);
|
||||
if (locale == NULL) {
|
||||
CloseLibrary((struct Library*)localeBase);
|
||||
return 1;
|
||||
}
|
||||
|
||||
error = create_timer();
|
||||
if (error != 0) {
|
||||
return error;
|
||||
}
|
||||
|
||||
request->tr_node.io_Command = TR_SETSYSTIME;
|
||||
request->tr_time.tv_secs = (long)tv->tv_secs - AMIGAOFFSET + locale->loc_GMTOffset * 60;
|
||||
request->tr_time.tv_micro = tv->tv_micro;
|
||||
DoIO((struct IORequest*)request);
|
||||
|
||||
error = delete_timer();
|
||||
|
||||
CloseLocale(locale);
|
||||
CloseLibrary((struct Library*)localeBase);
|
||||
|
||||
return error;
|
||||
}
|
||||
/**********************************************************************/
|
||||
75
amiga_tz.h
Normal file
75
amiga_tz.h
Normal file
@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright (c) 2015 Carsten Larsen
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef _AMIGA_TZ_H
|
||||
#define _AMIGA_TZ_H
|
||||
|
||||
#include <exec/io.h>
|
||||
#include <exec/types.h>
|
||||
#include <exec/memory.h>
|
||||
#include <exec/devices.h>
|
||||
#include <devices/timer.h>
|
||||
#include <proto/timer.h>
|
||||
#include <proto/battclock.h>
|
||||
#include <clib/alib_protos.h>
|
||||
#include <clib/exec_protos.h>
|
||||
#include <clib/timer_protos.h>
|
||||
#include <clib/locale_protos.h>
|
||||
#include <clib/utility_protos.h>
|
||||
#include <clib/battclock_protos.h>
|
||||
|
||||
#ifndef ULONG
|
||||
#define ULONG long unsigned int
|
||||
#endif
|
||||
|
||||
#ifndef LLONG_MAX
|
||||
#define LLONG_MAX ((long long)(~0ULL>>1))
|
||||
#endif
|
||||
|
||||
#ifndef LLONG_MIN
|
||||
#define LLONG_MIN (-LLONG_MAX - 1)
|
||||
#endif
|
||||
|
||||
// 2922 is the number of days between 1.1.1970 and 1.1.1978 (2 leap years and 6 normal)
|
||||
#ifndef AMIGAOFFSET
|
||||
#define AMIGAOFFSET 2922 * 24 * 60 * 60
|
||||
#endif
|
||||
|
||||
typedef long time_t;
|
||||
typedef long tz_time_t;
|
||||
|
||||
long long int strtoll(const char *nptr, char **endptr, int base);
|
||||
|
||||
int isalnum(int c);
|
||||
int isalpha(int c);
|
||||
int isdigit(int c);
|
||||
int ispunct(int c);
|
||||
int isspace(int c);
|
||||
int isupper(int c);
|
||||
|
||||
void syslog(int, const char *, ...);
|
||||
|
||||
#endif
|
||||
26
include/tz.h
26
include/tz.h
@ -43,6 +43,9 @@
|
||||
* ftp://ftp.netbsd.org/pub/NetBSD/NetBSD-release-6/src/include/time.h
|
||||
*/
|
||||
|
||||
#ifndef _TZ_HEADER
|
||||
#define _TZ_HEADER
|
||||
|
||||
extern char *tzname[2];
|
||||
|
||||
struct tz_tm {
|
||||
@ -75,26 +78,13 @@ struct tz_tm *tz_gmtime_r(const tz_time_t *, struct tz_tm *);
|
||||
struct tz_tm *tz_gmtime(const tz_time_t *);
|
||||
char *tz_ctime_r(const tz_time_t *, char *);
|
||||
char *tz_ctime(const tz_time_t *);
|
||||
//char *asctime_r(const struct tz_tm *, char *);
|
||||
double tz_difftime(tz_time_t, tz_time_t);
|
||||
|
||||
#ifndef HAVE_ASCTIME
|
||||
//char *asctime(const struct tz_tm *);
|
||||
#endif
|
||||
|
||||
tz_time_t time(tz_time_t *x);
|
||||
|
||||
#ifdef HAVENOSYSTIME
|
||||
#include <clib/timer_protos.h>
|
||||
tz_time_t time(tz_time_t *x)
|
||||
{
|
||||
struct timeval tv;
|
||||
GetSysTime(&tv);
|
||||
|
||||
if (x) {
|
||||
*x = (time_t)tv.tv_secs;
|
||||
}
|
||||
|
||||
return (time_t)tv.tv_secs;
|
||||
}
|
||||
#ifndef HAVE_ASCTIME
|
||||
char *asctime_r(const struct tz_tm *, char *);
|
||||
char *asctime(const struct tz_tm *);
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@ -82,6 +82,8 @@
|
||||
/* Enable strtoimax on Solaris 10. */
|
||||
#define __EXTENSIONS__ 1
|
||||
|
||||
void tz_cleanup();
|
||||
|
||||
/*
|
||||
** Nested includes
|
||||
*/
|
||||
|
||||
154
strtoll.c
Normal file
154
strtoll.c
Normal file
@ -0,0 +1,154 @@
|
||||
/* $NetBSD: strtoll.c,v 1.7 2004/08/31 17:55:17 jlam Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 1992, 1993
|
||||
* The Regents of the University of California. 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. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file strtoll.c
|
||||
* @brief
|
||||
* Parts of this file was retrieved from:
|
||||
* http://ftp.netbsd.org/pub/pkgsrc/current/pkgsrc/pkgtools/libnbcompat/files/strtoll.c
|
||||
*
|
||||
* Originate from: strtoq.c 8.1 (Berkeley) 6/4/93
|
||||
*
|
||||
*/
|
||||
|
||||
#include "amiga_tz.h"
|
||||
|
||||
#if !HAVE_STRTOLL
|
||||
|
||||
/*
|
||||
* Convert a string to a long long integer.
|
||||
*
|
||||
* Ignores `locale' stuff. Assumes that the upper and lower case
|
||||
* alphabets and digits are each contiguous.
|
||||
*/
|
||||
/* LONGLONG */
|
||||
long long int
|
||||
strtoll(const char *nptr, char **endptr, int base)
|
||||
{
|
||||
const char *s;
|
||||
/* LONGLONG */
|
||||
long long int acc, cutoff;
|
||||
int c;
|
||||
int neg, any, cutlim;
|
||||
|
||||
/*
|
||||
* Skip white space and pick up leading +/- sign if any.
|
||||
* If base is 0, allow 0x for hex and 0 for octal, else
|
||||
* assume decimal; if base is already 16, allow 0x.
|
||||
*/
|
||||
s = nptr;
|
||||
do {
|
||||
c = (unsigned char) *s++;
|
||||
} while (isspace(c));
|
||||
if (c == '-') {
|
||||
neg = 1;
|
||||
c = *s++;
|
||||
} else {
|
||||
neg = 0;
|
||||
if (c == '+')
|
||||
c = *s++;
|
||||
}
|
||||
if ((base == 0 || base == 16) &&
|
||||
c == '0' && (*s == 'x' || *s == 'X')) {
|
||||
c = s[1];
|
||||
s += 2;
|
||||
base = 16;
|
||||
}
|
||||
if (base == 0)
|
||||
base = c == '0' ? 8 : 10;
|
||||
|
||||
/*
|
||||
* Compute the cutoff value between legal numbers and illegal
|
||||
* numbers. That is the largest legal value, divided by the
|
||||
* base. An input number that is greater than this value, if
|
||||
* followed by a legal input character, is too big. One that
|
||||
* is equal to this value may be valid or not; the limit
|
||||
* between valid and invalid numbers is then based on the last
|
||||
* digit. For instance, if the range for long longs is
|
||||
* [-9223372036854775808..9223372036854775807] and the input base
|
||||
* is 10, cutoff will be set to 922337203685477580 and cutlim to
|
||||
* either 7 (neg==0) or 8 (neg==1), meaning that if we have
|
||||
* accumulated a value > 922337203685477580, or equal but the
|
||||
* next digit is > 7 (or 8), the number is too big, and we will
|
||||
* return a range error.
|
||||
*
|
||||
* Set any if any `digits' consumed; make it negative to indicate
|
||||
* overflow.
|
||||
*/
|
||||
cutoff = neg ? LLONG_MIN : LLONG_MAX;
|
||||
cutlim = (int)(cutoff % base);
|
||||
cutoff /= base;
|
||||
if (neg) {
|
||||
if (cutlim > 0) {
|
||||
cutlim -= base;
|
||||
cutoff += 1;
|
||||
}
|
||||
cutlim = -cutlim;
|
||||
}
|
||||
for (acc = 0, any = 0;; c = (unsigned char) *s++) {
|
||||
if (isdigit(c))
|
||||
c -= '0';
|
||||
else if (isalpha(c))
|
||||
c -= isupper(c) ? 'A' - 10 : 'a' - 10;
|
||||
else
|
||||
break;
|
||||
if (c >= base)
|
||||
break;
|
||||
if (any < 0)
|
||||
continue;
|
||||
if (neg) {
|
||||
if (acc < cutoff || (acc == cutoff && c > cutlim)) {
|
||||
any = -1;
|
||||
acc = LLONG_MIN;
|
||||
//errno = ERANGE;
|
||||
} else {
|
||||
any = 1;
|
||||
acc *= base;
|
||||
acc -= c;
|
||||
}
|
||||
} else {
|
||||
if (acc > cutoff || (acc == cutoff && c > cutlim)) {
|
||||
any = -1;
|
||||
acc = LLONG_MAX;
|
||||
//errno = ERANGE;
|
||||
} else {
|
||||
any = 1;
|
||||
acc *= base;
|
||||
acc += c;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (endptr != 0)
|
||||
/* LINTED interface specification */
|
||||
*endptr = (char *)(any ? s - 1 : nptr);
|
||||
return (acc);
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user