diff options
| author | Bruce Momjian | 2002-07-18 04:13:59 +0000 |
|---|---|---|
| committer | Bruce Momjian | 2002-07-18 04:13:59 +0000 |
| commit | 404e9a12a5aef6d77af9b407c5737cb688f8e1cc (patch) | |
| tree | a79dc34675672225ef7d7f802832f65efd4da2fb /src/port | |
| parent | 7f43165dd272f6df9e3df8a0fa8386ad60276e4a (diff) | |
Move libc replacement files from src/backend/port to src/port.
Diffstat (limited to 'src/port')
| -rw-r--r-- | src/port/Makefile | 23 | ||||
| -rw-r--r-- | src/port/gethostname.c | 24 | ||||
| -rw-r--r-- | src/port/getrusage.c | 58 | ||||
| -rw-r--r-- | src/port/inet_aton.c | 152 | ||||
| -rw-r--r-- | src/port/inet_aton.h | 3 | ||||
| -rw-r--r-- | src/port/isinf.c | 82 | ||||
| -rw-r--r-- | src/port/memcmp.c | 36 | ||||
| -rw-r--r-- | src/port/random.c | 13 | ||||
| -rw-r--r-- | src/port/snprintf.c | 470 | ||||
| -rw-r--r-- | src/port/srandom.c | 13 | ||||
| -rw-r--r-- | src/port/strcasecmp.c | 78 | ||||
| -rw-r--r-- | src/port/strdup.c | 26 | ||||
| -rw-r--r-- | src/port/strerror.c | 31 | ||||
| -rw-r--r-- | src/port/strtol.c | 140 | ||||
| -rw-r--r-- | src/port/strtoul.c | 119 |
15 files changed, 1268 insertions, 0 deletions
diff --git a/src/port/Makefile b/src/port/Makefile new file mode 100644 index 0000000000..77499ae03e --- /dev/null +++ b/src/port/Makefile @@ -0,0 +1,23 @@ +#------------------------------------------------------------------------- +# +# Makefile-- +# Makefile for the port-specific subsystem of the backend +# +# These files are used in other directories for portability on systems +# with broken/missing library files. + +# IDENTIFICATION +# $Header: /cvsroot/pgsql/src/port/Makefile,v 1.1 2002/07/18 04:13:59 momjian Exp $ +# +#------------------------------------------------------------------------- + +subdir = src/port +top_builddir = ../.. +include $(top_builddir)/src/Makefile.global + +OBJS=$(GETHOSTNAME) $(GETRUSAGE) $(INET_ATON) $(ISINF) $(MEMCMP) \ + $(MISSING_RANDOM) $(SNPRINTF) $(SRANDOM) $(STRCASECMP) $(STRDUP) \ + $(STRERROR) $(STRTOL) $(STRTOUL) + +distclean clean: + rm -f $(OBJS) diff --git a/src/port/gethostname.c b/src/port/gethostname.c new file mode 100644 index 0000000000..568df7c497 --- /dev/null +++ b/src/port/gethostname.c @@ -0,0 +1,24 @@ +/* $Id: gethostname.c,v 1.1 2002/07/18 04:13:59 momjian 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; +} diff --git a/src/port/getrusage.c b/src/port/getrusage.c new file mode 100644 index 0000000000..14d3748046 --- /dev/null +++ b/src/port/getrusage.c @@ -0,0 +1,58 @@ +/* $Id: getrusage.c,v 1.1 2002/07/18 04:13:59 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; +} diff --git a/src/port/inet_aton.c b/src/port/inet_aton.c new file mode 100644 index 0000000000..c7f409ae43 --- /dev/null +++ b/src/port/inet_aton.c @@ -0,0 +1,152 @@ +/* $Id: inet_aton.c,v 1.1 2002/07/18 04:13:59 momjian Exp $ + * + * This inet_aton() function was taken from the GNU C library and + * incorporated into Postgres for those systems which do not have this + * routine in their standard C libraries. + * + * The function was been extracted whole from the file inet_aton.c in + * Release 5.3.12 of the Linux C library, which is derived from the + * GNU C library, by Bryan Henderson in October 1996. The copyright + * notice from that file is below. + */ + +/* + * Copyright (c) 1983, 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. */ + +#include "c.h" + +#include <sys/types.h> +#include <netinet/in.h> +#include <ctype.h> + +/* + * Check whether "cp" is a valid ascii representation + * of an Internet address and convert to a binary address. + * Returns 1 if the address is valid, 0 if not. + * This replaces inet_addr, the return value from which + * cannot distinguish between failure and a local broadcast address. + */ +int +inet_aton(const char *cp, struct in_addr * addr) +{ + unsigned int val; + int base, + n; + char c; + u_int parts[4]; + u_int *pp = parts; + + for (;;) + { + /* + * Collect number up to ``.''. Values are specified as for C: + * 0x=hex, 0=octal, other=decimal. + */ + val = 0; + base = 10; + if (*cp == '0') + { + if (*++cp == 'x' || *cp == 'X') + base = 16, cp++; + else + base = 8; + } + while ((c = *cp) != '\0') + { + if (isdigit((unsigned char) c)) + { + val = (val * base) + (c - '0'); + cp++; + continue; + } + if (base == 16 && isxdigit((unsigned char) c)) + { + val = (val << 4) + + (c + 10 - (islower((unsigned char) c) ? 'a' : 'A')); + cp++; + continue; + } + break; + } + if (*cp == '.') + { + /* + * Internet format: a.b.c.d a.b.c (with c treated as + * 16-bits) a.b (with b treated as 24 bits) + */ + if (pp >= parts + 3 || val > 0xff) + return 0; + *pp++ = val, cp++; + } + else + break; + } + + /* + * Check for trailing junk. + */ + while (*cp) + if (!isspace((unsigned char) *cp++)) + return 0; + + /* + * Concoct the address according to the number of parts specified. + */ + n = pp - parts + 1; + switch (n) + { + + case 1: /* a -- 32 bits */ + break; + + case 2: /* a.b -- 8.24 bits */ + if (val > 0xffffff) + return 0; + val |= parts[0] << 24; + break; + + case 3: /* a.b.c -- 8.8.16 bits */ + if (val > 0xffff) + return 0; + val |= (parts[0] << 24) | (parts[1] << 16); + break; + + case 4: /* a.b.c.d -- 8.8.8.8 bits */ + if (val > 0xff) + return 0; + val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); + break; + } + if (addr) + addr->s_addr = htonl(val); + return 1; +} diff --git a/src/port/inet_aton.h b/src/port/inet_aton.h new file mode 100644 index 0000000000..d965afc5c7 --- /dev/null +++ b/src/port/inet_aton.h @@ -0,0 +1,3 @@ +/* $Id: inet_aton.h,v 1.1 2002/07/18 04:13:59 momjian Exp $ */ + +int inet_aton(const char *cp, struct in_addr * addr); diff --git a/src/port/isinf.c b/src/port/isinf.c new file mode 100644 index 0000000000..3156591999 --- /dev/null +++ b/src/port/isinf.c @@ -0,0 +1,82 @@ +/* $Id: isinf.c,v 1.1 2002/07/18 04:13:59 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 diff --git a/src/port/memcmp.c b/src/port/memcmp.c new file mode 100644 index 0000000000..eb20370689 --- /dev/null +++ b/src/port/memcmp.c @@ -0,0 +1,36 @@ +/*------------------------------------------------------------------------- + * + * 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/port/memcmp.c,v 1.1 2002/07/18 04:13:59 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; +} diff --git a/src/port/random.c b/src/port/random.c new file mode 100644 index 0000000000..8bb53e8914 --- /dev/null +++ b/src/port/random.c @@ -0,0 +1,13 @@ +/* $Id: random.c,v 1.1 2002/07/18 04:13:59 momjian Exp $ */ + +#include "c.h" + +#include <stdlib.h> +#include <math.h> +#include <errno.h> + +long +random() +{ + return lrand48(); +} diff --git a/src/port/snprintf.c b/src/port/snprintf.c new file mode 100644 index 0000000000..7930cc1e40 --- /dev/null +++ b/src/port/snprintf.c @@ -0,0 +1,470 @@ +/* + * 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.1 2002/07/18 04:13:59 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++; +} diff --git a/src/port/srandom.c b/src/port/srandom.c new file mode 100644 index 0000000000..cf74ce5f86 --- /dev/null +++ b/src/port/srandom.c @@ -0,0 +1,13 @@ +/* $Id: srandom.c,v 1.1 2002/07/18 04:13:59 momjian Exp $ */ + +#include "c.h" + +#include <stdlib.h> +#include <math.h> +#include <errno.h> + +void +srandom(unsigned int seed) +{ + srand48((long int) seed); +} diff --git a/src/port/strcasecmp.c b/src/port/strcasecmp.c new file mode 100644 index 0000000000..ff487108c8 --- /dev/null +++ b/src/port/strcasecmp.c @@ -0,0 +1,78 @@ +/* $Id: strcasecmp.c,v 1.1 2002/07/18 04:13:59 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; + } +} diff --git a/src/port/strdup.c b/src/port/strdup.c new file mode 100644 index 0000000000..a7d1ff9305 --- /dev/null +++ b/src/port/strdup.c @@ -0,0 +1,26 @@ +/*------------------------------------------------------------------------- + * + * 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/port/strdup.c,v 1.1 2002/07/18 04:13:59 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; +} diff --git a/src/port/strerror.c b/src/port/strerror.c new file mode 100644 index 0000000000..b878388ea2 --- /dev/null +++ b/src/port/strerror.c @@ -0,0 +1,31 @@ +/* $Id: strerror.c,v 1.1 2002/07/18 04:13:59 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]; +} diff --git a/src/port/strtol.c b/src/port/strtol.c new file mode 100644 index 0000000000..526b070bd0 --- /dev/null +++ b/src/port/strtol.c @@ -0,0 +1,140 @@ +/*- + * 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; +} diff --git a/src/port/strtoul.c b/src/port/strtoul.c new file mode 100644 index 0000000000..5cb7f7855a --- /dev/null +++ b/src/port/strtoul.c @@ -0,0 +1,119 @@ +/* + * 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; +} |
