1
0
mirror of https://github.com/postgres/postgres.git synced 2025-09-02 04:21:28 +03:00

Move libc replacement files from src/backend/port to src/port.

This commit is contained in:
Bruce Momjian
2002-07-18 04:13:59 +00:00
parent 7f43165dd2
commit 404e9a12a5
19 changed files with 221 additions and 48 deletions

View File

@@ -13,7 +13,7 @@
# be converted to Method 2.
#
# IDENTIFICATION
# $Header: /cvsroot/pgsql/src/backend/port/Makefile,v 1.15 2002/07/16 05:49:38 momjian Exp $
# $Header: /cvsroot/pgsql/src/backend/port/Makefile,v 1.16 2002/07/18 04:13:59 momjian Exp $
#
#-------------------------------------------------------------------------
@@ -23,9 +23,7 @@ include $(top_builddir)/src/Makefile.global
OBJS=dynloader.o pg_sema.o pg_shmem.o
OBJS+=$(DLLINIT) $(GETHOSTNAME) $(GETRUSAGE) $(INET_ATON) $(ISINF) $(MEMCMP) \
$(MISSING_RANDOM) $(SNPRINTF) $(SRANDOM) $(STRCASECMP) $(STRDUP) \
$(STRERROR) $(STRTOL) $(STRTOUL)
OBJS+=$(DLLINIT)
OBJS+=$(TAS)

View File

@@ -1,24 +0,0 @@
/* $Id: gethostname.c,v 1.5 2001/08/24 14:07:49 petere Exp $ */
#include "c.h"
#include <sys/types.h>
#include <string.h>
#include <sys/utsname.h>
int
gethostname(char *name, int namelen)
{
static struct utsname mname;
static int called = 0;
if (!called)
{
called++;
uname(&mname);
}
strncpy(name, mname.nodename, (SYS_NMLN < namelen ? SYS_NMLN : namelen));
return 0;
}

View File

@@ -1,58 +0,0 @@
/* $Id: getrusage.c,v 1.11 1998/12/12 19:57:51 momjian Exp $ */
#include <stdio.h>
#include <errno.h>
#include "rusagestub.h"
/* This code works on:
* univel
* solaris_i386
* sco
* solaris_sparc
* svr4
* hpux 9.*
* which currently is all the supported platforms that don't have a
* native version of getrusage(). So, if configure decides to compile
* this file at all, we just use this version unconditionally.
*/
int
getrusage(int who, struct rusage * rusage)
{
struct tms tms;
int tick_rate = CLK_TCK; /* ticks per second */
clock_t u,
s;
if (rusage == (struct rusage *) NULL)
{
errno = EFAULT;
return -1;
}
if (times(&tms) < 0)
{
/* errno set by times */
return -1;
}
switch (who)
{
case RUSAGE_SELF:
u = tms.tms_utime;
s = tms.tms_stime;
break;
case RUSAGE_CHILDREN:
u = tms.tms_cutime;
s = tms.tms_cstime;
break;
default:
errno = EINVAL;
return -1;
}
#define TICK_TO_SEC(T, RATE) ((T)/(RATE))
#define TICK_TO_USEC(T,RATE) (((T)%(RATE)*1000000)/RATE)
rusage->ru_utime.tv_sec = TICK_TO_SEC(u, tick_rate);
rusage->ru_utime.tv_usec = TICK_TO_USEC(u, tick_rate);
rusage->ru_stime.tv_sec = TICK_TO_SEC(s, tick_rate);
rusage->ru_stime.tv_usec = TICK_TO_USEC(u, tick_rate);
return 0;
}

View File

@@ -1,3 +0,0 @@
/* $Id: inet_aton.h,v 1.7 1998/02/26 04:34:08 momjian Exp $ */
int inet_aton(const char *cp, struct in_addr * addr);

View File

@@ -1,82 +0,0 @@
/* $Id: isinf.c,v 1.18 2001/10/28 06:25:47 momjian Exp $ */
#include "c.h"
#include <math.h>
#if HAVE_FPCLASS /* this is _not_ HAVE_FP_CLASS, and not
* typo */
#if HAVE_IEEEFP_H
#include <ieeefp.h>
#endif
int
isinf(double d)
{
fpclass_t type = fpclass(d);
switch (type)
{
case FP_NINF:
case FP_PINF:
return 1;
default:
break;
}
return 0;
}
#else
#if defined(HAVE_FP_CLASS) || defined(HAVE_FP_CLASS_D)
#if HAVE_FP_CLASS_H
#include <fp_class.h>
#endif
int
isinf(x)
double x;
{
#if HAVE_FP_CLASS
int fpclass = fp_class(x);
#else
int fpclass = fp_class_d(x);
#endif
if (fpclass == FP_POS_INF)
return 1;
if (fpclass == FP_NEG_INF)
return -1;
return 0;
}
#elif defined(HAVE_CLASS)
int
isinf(double x)
{
int fpclass = class(x);
if (fpclass == FP_PLUS_INF)
return 1;
if (fpclass == FP_MINUS_INF)
return -1;
return 0;
}
#endif
#endif
#ifdef __QNX__
#include <float.h>
int
isinf(double x)
{
if (x == HUGE_VAL)
return 1;
if (x == -HUGE_VAL)
return -1;
return 0;
}
#endif

View File

@@ -1,36 +0,0 @@
/*-------------------------------------------------------------------------
*
* memcmp.c
* compares memory bytes
*
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/port/Attic/memcmp.c,v 1.2 2002/06/20 20:29:33 momjian Exp $
*
* This file was taken from NetBSD and is used by SunOS because memcmp
* on that platform does not properly compare negative bytes.
*
*-------------------------------------------------------------------------
*/
#include <string.h>
/*
* Compare memory regions.
*/
int
memcmp(const void *s1, const void *s2, size_t n)
{
if (n != 0) {
const unsigned char *p1 = s1, *p2 = s2;
do {
if (*p1++ != *p2++)
return (*--p1 - *--p2);
} while (--n != 0);
}
return 0;
}

View File

@@ -1,13 +0,0 @@
/* $Id: random.c,v 1.10 2001/08/24 14:07:49 petere Exp $ */
#include "c.h"
#include <stdlib.h>
#include <math.h>
#include <errno.h>
long
random()
{
return lrand48();
}

View File

@@ -1,470 +0,0 @@
/*
* Copyright (c) 1983, 1995, 1996 Eric P. Allman
* Copyright (c) 1988, 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. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. 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.
*/
/* might be in either frontend or backend */
#include "postgres_fe.h"
#include <sys/ioctl.h>
#include <sys/param.h>
/*
* We do all internal arithmetic in the widest available integer type,
* here called long_long (or ulong_long for unsigned).
*/
#ifdef HAVE_LONG_LONG_INT_64
typedef long long long_long;
typedef unsigned long long ulong_long;
#else
typedef long long_long;
typedef unsigned long ulong_long;
#endif
/*
** SNPRINTF, VSNPRINT -- counted versions of printf
**
** These versions have been grabbed off the net. They have been
** cleaned up to compile properly and support for .precision and
** %lx has been added.
*/
/**************************************************************
* Original:
* Patrick Powell Tue Apr 11 09:48:21 PDT 1995
* A bombproof version of doprnt (dopr) included.
* Sigh. This sort of thing is always nasty do deal with. Note that
* the version here does not include floating point. (now it does ... tgl)
*
* snprintf() is used instead of sprintf() as it does limit checks
* for string length. This covers a nasty loophole.
*
* The other functions are there to prevent NULL pointers from
* causing nast effects.
**************************************************************/
/*static char _id[] = "$Id: snprintf.c,v 1.31 2001/10/25 05:49:40 momjian Exp $";*/
static char *end;
static int SnprfOverflow;
int snprintf(char *str, size_t count, const char *fmt,...);
int vsnprintf(char *str, size_t count, const char *fmt, va_list args);
static void dopr(char *buffer, const char *format, va_list args);
int
snprintf(char *str, size_t count, const char *fmt,...)
{
int len;
va_list args;
va_start(args, fmt);
len = vsnprintf(str, count, fmt, args);
va_end(args);
return len;
}
int
vsnprintf(char *str, size_t count, const char *fmt, va_list args)
{
str[0] = '\0';
end = str + count - 1;
SnprfOverflow = 0;
dopr(str, fmt, args);
if (count > 0)
end[0] = '\0';
return strlen(str);
}
/*
* dopr(): poor man's version of doprintf
*/
static void fmtstr(char *value, int ljust, int len, int zpad, int maxwidth);
static void fmtnum(long_long value, int base, int dosign, int ljust, int len, int zpad);
static void fmtfloat(double value, char type, int ljust, int len, int precision, int pointflag);
static void dostr(char *str, int cut);
static void dopr_outch(int c);
static char *output;
static void
dopr(char *buffer, const char *format, va_list args)
{
int ch;
long_long value;
double fvalue;
int longlongflag = 0;
int longflag = 0;
int pointflag = 0;
int maxwidth = 0;
char *strvalue;
int ljust;
int len;
int zpad;
output = buffer;
while ((ch = *format++))
{
switch (ch)
{
case '%':
ljust = len = zpad = maxwidth = 0;
longflag = longlongflag = pointflag = 0;
nextch:
ch = *format++;
switch (ch)
{
case 0:
dostr("**end of format**", 0);
*output = '\0';
return;
case '-':
ljust = 1;
goto nextch;
case '0': /* set zero padding if len not set */
if (len == 0 && !pointflag)
zpad = '0';
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (pointflag)
maxwidth = maxwidth * 10 + ch - '0';
else
len = len * 10 + ch - '0';
goto nextch;
case '*':
if (pointflag)
maxwidth = va_arg(args, int);
else
len = va_arg(args, int);
goto nextch;
case '.':
pointflag = 1;
goto nextch;
case 'l':
if (longflag)
longlongflag = 1;
else
longflag = 1;
goto nextch;
case 'u':
case 'U':
/* fmtnum(value,base,dosign,ljust,len,zpad) */
if (longflag)
{
if (longlongflag)
value = va_arg(args, ulong_long);
else
value = va_arg(args, unsigned long);
}
else
value = va_arg(args, unsigned int);
fmtnum(value, 10, 0, ljust, len, zpad);
break;
case 'o':
case 'O':
/* fmtnum(value,base,dosign,ljust,len,zpad) */
if (longflag)
{
if (longlongflag)
value = va_arg(args, ulong_long);
else
value = va_arg(args, unsigned long);
}
else
value = va_arg(args, unsigned int);
fmtnum(value, 8, 0, ljust, len, zpad);
break;
case 'd':
case 'D':
if (longflag)
{
if (longlongflag)
value = va_arg(args, long_long);
else
value = va_arg(args, long);
}
else
value = va_arg(args, int);
fmtnum(value, 10, 1, ljust, len, zpad);
break;
case 'x':
if (longflag)
{
if (longlongflag)
value = va_arg(args, ulong_long);
else
value = va_arg(args, unsigned long);
}
else
value = va_arg(args, unsigned int);
fmtnum(value, 16, 0, ljust, len, zpad);
break;
case 'X':
if (longflag)
{
if (longlongflag)
value = va_arg(args, ulong_long);
else
value = va_arg(args, unsigned long);
}
else
value = va_arg(args, unsigned int);
fmtnum(value, -16, 0, ljust, len, zpad);
break;
case 's':
strvalue = va_arg(args, char *);
if (maxwidth > 0 || !pointflag)
{
if (pointflag && len > maxwidth)
len = maxwidth; /* Adjust padding */
fmtstr(strvalue, ljust, len, zpad, maxwidth);
}
break;
case 'c':
ch = va_arg(args, int);
dopr_outch(ch);
break;
case 'e':
case 'E':
case 'f':
case 'g':
case 'G':
fvalue = va_arg(args, double);
fmtfloat(fvalue, ch, ljust, len, maxwidth, pointflag);
break;
case '%':
dopr_outch(ch);
continue;
default:
dostr("???????", 0);
}
break;
default:
dopr_outch(ch);
break;
}
}
*output = '\0';
}
static void
fmtstr(char *value, int ljust, int len, int zpad, int maxwidth)
{
int padlen,
strlen; /* amount to pad */
if (value == 0)
value = "<NULL>";
for (strlen = 0; value[strlen]; ++strlen); /* strlen */
if (strlen > maxwidth && maxwidth)
strlen = maxwidth;
padlen = len - strlen;
if (padlen < 0)
padlen = 0;
if (ljust)
padlen = -padlen;
while (padlen > 0)
{
dopr_outch(' ');
--padlen;
}
dostr(value, maxwidth);
while (padlen < 0)
{
dopr_outch(' ');
++padlen;
}
}
static void
fmtnum(long_long value, int base, int dosign, int ljust, int len, int zpad)
{
int signvalue = 0;
ulong_long uvalue;
char convert[64];
int place = 0;
int padlen = 0; /* amount to pad */
int caps = 0;
/*
* DEBUGP(("value 0x%x, base %d, dosign %d, ljust %d, len %d, zpad
* %d\n", value, base, dosign, ljust, len, zpad ));
*/
uvalue = value;
if (dosign)
{
if (value < 0)
{
signvalue = '-';
uvalue = -value;
}
}
if (base < 0)
{
caps = 1;
base = -base;
}
do
{
convert[place++] = (caps ? "0123456789ABCDEF" : "0123456789abcdef")
[uvalue % (unsigned) base];
uvalue = (uvalue / (unsigned) base);
} while (uvalue);
convert[place] = 0;
if (len < 0)
{
/* this could happen with a "*" width spec */
ljust = 1;
len = -len;
}
padlen = len - place;
if (padlen < 0)
padlen = 0;
if (ljust)
padlen = -padlen;
/*
* DEBUGP(( "str '%s', place %d, sign %c, padlen %d\n",
* convert,place,signvalue,padlen));
*/
if (zpad && padlen > 0)
{
if (signvalue)
{
dopr_outch(signvalue);
--padlen;
signvalue = 0;
}
while (padlen > 0)
{
dopr_outch(zpad);
--padlen;
}
}
while (padlen > 0)
{
dopr_outch(' ');
--padlen;
}
if (signvalue)
dopr_outch(signvalue);
while (place > 0)
dopr_outch(convert[--place]);
while (padlen < 0)
{
dopr_outch(' ');
++padlen;
}
}
static void
fmtfloat(double value, char type, int ljust, int len, int precision, int pointflag)
{
char fmt[32];
char convert[512];
int padlen = 0; /* amount to pad */
/* we rely on regular C library's sprintf to do the basic conversion */
if (pointflag)
sprintf(fmt, "%%.%d%c", precision, type);
else
sprintf(fmt, "%%%c", type);
sprintf(convert, fmt, value);
if (len < 0)
{
/* this could happen with a "*" width spec */
ljust = 1;
len = -len;
}
padlen = len - strlen(convert);
if (padlen < 0)
padlen = 0;
if (ljust)
padlen = -padlen;
while (padlen > 0)
{
dopr_outch(' ');
--padlen;
}
dostr(convert, 0);
while (padlen < 0)
{
dopr_outch(' ');
++padlen;
}
}
static void
dostr(char *str, int cut)
{
if (cut)
{
while (*str && cut-- > 0)
dopr_outch(*str++);
}
else
{
while (*str)
dopr_outch(*str++);
}
}
static void
dopr_outch(int c)
{
#ifdef NOT_USED
if (iscntrl((unsigned char) c) && c != '\n' && c != '\t')
{
c = '@' + (c & 0x1F);
if (end == 0 || output < end)
*output++ = '^';
}
#endif
if (end == 0 || output < end)
*output++ = c;
else
SnprfOverflow++;
}

View File

@@ -1,13 +0,0 @@
/* $Id: srandom.c,v 1.10 2001/08/24 14:07:49 petere Exp $ */
#include "c.h"
#include <stdlib.h>
#include <math.h>
#include <errno.h>
void
srandom(unsigned int seed)
{
srand48((long int) seed);
}

View File

@@ -1,78 +0,0 @@
/* $Id: strcasecmp.c,v 1.10 2002/06/20 20:29:33 momjian Exp $ */
/*
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1987 Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that this notice is preserved and that due credit is given
* to the University of California at Berkeley. The name of the University
* may not be used to endorse or promote products derived from this
* software without specific written prior permission. This software
* is provided ``as is'' without express or implied warranty.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)strcasecmp.c 5.5 (Berkeley) 11/24/87";
#endif /* LIBC_SCCS and not lint */
#include <sys/types.h>
#include <string.h>
/*
* This array is designed for mapping upper and lower case letter
* together for a case independent comparison. The mappings are
p * based upon ascii character sequences.
*/
static unsigned char charmap[] = {
'\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
'\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
'\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
'\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
'\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
'\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
'\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
'\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
'\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
'\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
'\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
'\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137',
'\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
'\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
'\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
'\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177',
'\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
'\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217',
'\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227',
'\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237',
'\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247',
'\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
'\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
'\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
'\300', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
'\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
'\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
'\370', '\371', '\372', '\333', '\334', '\335', '\336', '\337',
'\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
'\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
'\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
'\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377',
};
int
strcasecmp(char *s1, char *s2)
{
unsigned char u1,
u2;
for (;;)
{
u1 = (unsigned char) *s1++;
u2 = (unsigned char) *s2++;
if (charmap[u1] != charmap[u2])
return charmap[u1] - charmap[u2];
if (u1 == '\0')
return 0;
}
}

View File

@@ -1,26 +0,0 @@
/*-------------------------------------------------------------------------
*
* strdup.c
* copies a null-terminated string.
*
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/port/Attic/strdup.c,v 1.3 2002/07/16 05:46:35 momjian Exp $
*
*-------------------------------------------------------------------------
*/
#include <string.h>
#include <stdlib.h>
#include "strdup.h"
char *
strdup(char const * string)
{
char *nstr;
nstr = strcpy((char *) malloc(strlen(string) + 1), string);
return nstr;
}

View File

@@ -1,31 +0,0 @@
/* $Id: strerror.c,v 1.6 1999/07/15 23:03:18 momjian Exp $ */
/*
* strerror - map error number to descriptive string
*
* This version is obviously somewhat Unix-specific.
*
* based on code by Henry Spencer
* modified for ANSI by D'Arcy J.M. Cain
*/
#include <string.h>
#include <stdio.h>
#include <errno.h>
extern const char *const sys_errlist[];
extern int sys_nerr;
const char *
strerror(int errnum)
{
static char buf[24];
if (errnum < 0 || errnum > sys_nerr)
{
sprintf(buf, "unknown error %d", errnum);
return buf;
}
return sys_errlist[errnum];
}

View File

@@ -1,140 +0,0 @@
/*-
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1990 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. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. 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.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)strtol.c 5.4 (Berkeley) 2/23/91";
#endif /* LIBC_SCCS and not lint */
#include <limits.h>
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#define const
/*
* Convert a string to a long integer.
*
* Ignores `locale' stuff. Assumes that the upper and lower case
* alphabets and digits are each contiguous.
*/
long
strtol(nptr, endptr, base)
const char *nptr;
char **endptr;
int base;
{
const char *s = nptr;
unsigned long acc;
unsigned char c;
unsigned long cutoff;
int neg = 0,
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.
*/
do
{
c = *s++;
} while (isspace(c));
if (c == '-')
{
neg = 1;
c = *s++;
}
else 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 longs
* is [-2147483648..2147483647] and the input base is 10, cutoff will
* be set to 214748364 and cutlim to either 7 (neg==0) or 8 (neg==1),
* meaning that if we have accumulated a value > 214748364, 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 ? -(unsigned long) LONG_MIN : LONG_MAX;
cutlim = cutoff % (unsigned long) base;
cutoff /= (unsigned long) base;
for (acc = 0, any = 0;; c = *s++)
{
if (isdigit(c))
c -= '0';
else if (isalpha(c))
c -= isupper(c) ? 'A' - 10 : 'a' - 10;
else
break;
if ((int) c >= base)
break;
if (any < 0 || acc > cutoff || acc == cutoff && (int) c > cutlim)
any = -1;
else
{
any = 1;
acc *= base;
acc += c;
}
}
if (any < 0)
{
acc = neg ? LONG_MIN : LONG_MAX;
errno = ERANGE;
}
else if (neg)
acc = -acc;
if (endptr != 0)
*endptr = any ? s - 1 : (char *) nptr;
return acc;
}

View File

@@ -1,119 +0,0 @@
/*
* Copyright (c) 1990, 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. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. 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.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)strtoul.c 8.1 (Berkeley) 6/4/93";
#endif /* LIBC_SCCS and not lint */
#include <limits.h>
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
/*
* Convert a string to an unsigned long integer.
*
* Ignores `locale' stuff. Assumes that the upper and lower case
* alphabets and digits are each contiguous.
*/
unsigned long
strtoul(nptr, endptr, base)
const char *nptr;
char **endptr;
register int base;
{
register const char *s = nptr;
register unsigned long acc;
register unsigned char c;
register unsigned long cutoff;
register int neg = 0,
any,
cutlim;
/*
* See strtol for comments as to the logic used.
*/
do
{
c = *s++;
} while (isspace(c));
if (c == '-')
{
neg = 1;
c = *s++;
}
else 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;
cutoff = (unsigned long) ULONG_MAX / (unsigned long) base;
cutlim = (unsigned long) ULONG_MAX % (unsigned long) base;
for (acc = 0, any = 0;; c = *s++)
{
if (!isascii(c))
break;
if (isdigit(c))
c -= '0';
else if (isalpha(c))
c -= isupper(c) ? 'A' - 10 : 'a' - 10;
else
break;
if ((int) c >= base)
break;
if (any < 0 || acc > cutoff || (acc == cutoff && (int) c > cutlim))
any = -1;
else
{
any = 1;
acc *= base;
acc += c;
}
}
if (any < 0)
{
acc = ULONG_MAX;
errno = ERANGE;
}
else if (neg)
acc = -acc;
if (endptr != 0)
*endptr = (char *) (any ? s - 1 : nptr);
return acc;
}