diff options
Diffstat (limited to 'missing_d')
-rw-r--r-- | missing_d/ChangeLog | 17 | ||||
-rw-r--r-- | missing_d/memmove.c | 149 | ||||
-rw-r--r-- | missing_d/strtod.c | 12 | ||||
-rw-r--r-- | missing_d/strtoul.c | 223 |
4 files changed, 400 insertions, 1 deletions
diff --git a/missing_d/ChangeLog b/missing_d/ChangeLog index da1d09a5..c0d38c19 100644 --- a/missing_d/ChangeLog +++ b/missing_d/ChangeLog @@ -1,3 +1,20 @@ +Mon Aug 2 12:18:15 2004 Arnold D. Robbins <arnold@skeeve.com> + + * Release 3.1.4: Release tar file made. + +Mon May 3 09:24:21 2004 Arnold D. Robbins <arnold@skeeve.com> + + * strtoul.c: New file. + +Sun May 2 18:03:54 2004 Arnold D. Robbins <arnold@skeeve.com> + + * strtod.c (gawk_strtod): Check for locale's decimal point + instead of hard-wiring period. + +Tue Jan 20 10:38:48 2004 Arnold D. Robbins <arnold@skeeve.com> + + * memmove.c: New file. + Mon Jul 7 11:01:43 2003 Arnold D. Robbins <arnold@skeeve.com> * Release 3.1.3: Release tar file made. diff --git a/missing_d/memmove.c b/missing_d/memmove.c new file mode 100644 index 00000000..928b812e --- /dev/null +++ b/missing_d/memmove.c @@ -0,0 +1,149 @@ +/*- + * Copyright (c) 1990, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Chris Torek. + * + * 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 0 +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)bcopy.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/cdefs.h> +#include <string.h> +#endif + +/* + * sizeof(word) MUST BE A POWER OF TWO + * SO THAT wmask BELOW IS ALL ONES + */ +typedef int word; /* "word" used for optimal copy speed */ + +#define wsize sizeof(word) +#define wmask (wsize - 1) + +/* ADR: 1/2004. For gawk, we need memmove(). */ +#define MEMMOVE 1 + +/* + * Copy a block of memory, handling overlap. + * This is the routine that actually implements + * (the portable versions of) bcopy, memcpy, and memmove. + */ +#ifdef MEMCOPY +void * +memcpy(dst0, src0, length) +#else +#ifdef MEMMOVE +void * +memmove(dst0, src0, length) +#else +void +bcopy(src0, dst0, length) +#endif +#endif + void *dst0; + const void *src0; + register size_t length; +{ + register char *dst = dst0; + register const char *src = src0; + register size_t t; + + if (length == 0 || dst == src) /* nothing to do */ + goto done; + + /* + * Macros: loop-t-times; and loop-t-times, t>0 + */ +#define TLOOP(s) if (t) TLOOP1(s) +#define TLOOP1(s) do { s; } while (--t) + + if ((unsigned long)dst < (unsigned long)src) { + /* + * Copy forward. + */ + t = (int)src; /* only need low bits */ + if ((t | (int)dst) & wmask) { + /* + * Try to align operands. This cannot be done + * unless the low bits match. + */ + if ((t ^ (int)dst) & wmask || length < wsize) + t = length; + else + t = wsize - (t & wmask); + length -= t; + TLOOP1(*dst++ = *src++); + } + /* + * Copy whole words, then mop up any trailing bytes. + */ + t = length / wsize; + TLOOP(*(word *)dst = *(word *)src; src += wsize; dst += wsize); + t = length & wmask; + TLOOP(*dst++ = *src++); + } else { + /* + * Copy backwards. Otherwise essentially the same. + * Alignment works as before, except that it takes + * (t&wmask) bytes to align, not wsize-(t&wmask). + */ + src += length; + dst += length; + t = (int)src; + if ((t | (int)dst) & wmask) { + if ((t ^ (int)dst) & wmask || length <= wsize) + t = length; + else + t &= wmask; + length -= t; + TLOOP1(*--dst = *--src); + } + t = length / wsize; + TLOOP(src -= wsize; dst -= wsize; *(word *)dst = *(word *)src); + t = length & wmask; + TLOOP(*--dst = *--src); + } +done: +#if defined(MEMCOPY) || defined(MEMMOVE) + return (dst0); +#else + return; +#endif +} +#undef wsize +#undef wmask +#undef MEMMOVE +#undef TLOOP +#undef TLOOP1 diff --git a/missing_d/strtod.c b/missing_d/strtod.c index e33a5b5e..fc97b8d8 100644 --- a/missing_d/strtod.c +++ b/missing_d/strtod.c @@ -28,6 +28,8 @@ * doesn't look like we failed. Sigh. * * Xmass 2002. Fix a bug in ptr determination, eg. for "0e0". + * + * Spring 2004. Update for I18N. Oh joy. */ #if 0 @@ -66,7 +68,15 @@ register const char **ptr; dig++; } - if (*s == '.') { + if ( +#if ENABLE_NLS && defined(HAVE_LOCALE_H) + loc.decimal_point != NULL + ? *s == loc.decimal_point[0] + : *s == '.' +#else + *s == '.' +#endif + ) { s++; while (*s == '0') { s++; diff --git a/missing_d/strtoul.c b/missing_d/strtoul.c new file mode 100644 index 00000000..ccabfbeb --- /dev/null +++ b/missing_d/strtoul.c @@ -0,0 +1,223 @@ +/* + * Very simple implementation of strtoul() for gawk, + * for old systems. Descriptive prose from the Linux man page. + * + * May 2004 + */ + +/* #define TEST 1 */ + +#ifdef TEST +#include <stdio.h> +#include <stdlib.h> +#include <ctype.h> +#include <string.h> +#include <errno.h> +#include <limits.h> +#define TRUE 1 +#define FALSE 0 +#define strtoul mystrtoul +#endif + +#ifndef ULONG_MAX +#define ULONG_MAX (~ 0UL) +#endif + +unsigned long int +strtoul(nptr, endptr, base) +const char *nptr; +char **endptr; +int base; +{ + static char lower[] = "abcdefghijklmnopqrstuvwxyz"; + + unsigned long result = 0UL; + char *nptr_orig = (char *) nptr; + int neg = FALSE; + char *cp, c; + int val; + int sawdigs = FALSE; + + /* + * The strtoul() function converts the initial part of the + * string in nptr to an unsigned long integer value according + * to the given base, which must be between 2 and 36 inclusive, + * or be the special value 0. + */ + + if ((base != 0 && (base < 2 || base > 36)) || nptr == NULL) { + if (endptr != NULL) + *endptr = nptr_orig; + errno = EINVAL; + return 0; + } + + /* + * The string must [sic] begin with an arbitrary amount of white space + * (as determined by isspace(3)) followed by a single optional + * `+' or `-' sign. + */ + while (isspace(*nptr)) + nptr++; + + if (*nptr == '+') + nptr++; + else if (*nptr == '-') { + nptr++; + neg = TRUE; + } + + /* + * If base is zero or 16, the string may then include a `0x' prefix, + * and the number will be read in base 16; otherwise, a zero base is + * taken as 10 (decimal) unless the next character is `0', in which + * case it is taken as 8 (octal). + */ + if ((base == 0 || base == 16) + && nptr[0] == '0' + && (nptr[1] == 'x' || nptr[1] == 'X')) { + base = 16; /* force it */ + nptr += 2; /* skip 0x */ + } else if ((base == 0 || base == 8) && nptr[0] == '0') { + base = 8; + nptr++; + } else if (base == 0) + base = 10; + + /* + * The remainder of the string is converted to an unsigned long int + * value in the obvious manner, stopping at the first character + * which is not a valid digit in the given base. (In bases above 10, + * the letter `A' in either upper or lower case represents 10, + * `B' represents 11, and so forth, with `Z' representing 35.) + */ + for (; *nptr != '\0'; nptr++) { + c = *nptr; +#if ENABLE_NLS && defined(HAVE_LOCALE_H) + if (base == 10 + && loc.thousands_sep != NULL + && loc.thousands_sep[0] != '\0' + && c == loc.thousands_sep[0]) + continue; +#endif + switch (c) { + case '0': case '1': case '2': + case '3': case '4': case '5': + case '6': case '7': case '8': + case '9': + val = c - '0'; + if (val >= base) /* even base 2 allowed ... */ + goto out; + result *= base; + result += val; + sawdigs = TRUE; + break; + case 'A': case 'B': case 'C': case 'D': case 'E': + case 'F': case 'G': case 'H': case 'I': case 'J': + case 'K': case 'L': case 'M': case 'N': case 'O': + case 'P': case 'Q': case 'R': case 'S': case 'T': + case 'U': case 'V': case 'W': case 'X': case 'Y': + case 'Z': + c += 'a' - 'A'; /* downcase */ + /* fall through */ + case 'a': case 'b': case 'c': case 'd': case 'e': + case 'f': case 'g': case 'h': case 'i': case 'j': + case 'k': case 'l': case 'm': case 'n': case 'o': + case 'p': case 'q': case 'r': case 's': case 't': + case 'u': case 'v': case 'w': case 'x': case 'y': + case 'z': + cp = strchr(lower, c); + val = cp - lower; + val += 10; /* 'a' == 10 */ + if (val >= base) + goto out; + result *= base; + result += val; + sawdigs = TRUE; + break; + default: + goto out; + } + } +out: + /* + * If endptr is not NULL, strtoul() stores the address of the + * first invalid character in *endptr. If there were no digits + * at all, strtoul() stores the original value of nptr in *endptr + * (and returns 0). In particular, if *nptr is not `\0' but + * **endptr is `\0' on return, the entire string is valid. + */ + if (endptr != NULL) { + if (! sawdigs) { + *endptr = nptr_orig; + return 0; + } else + *endptr = (char *) nptr; + } + + /* + * RETURN VALUE + * The strtoul() function returns either the result of the + * conversion or, if there was a leading minus sign, the + * negation of the result of the conversion, unless the original + * (non-negated) value would overflow; in the latter case, + * strtoul() returns ULONG_MAX and sets the global variable errno + * to ERANGE. + */ + + /* + * ADR: This computation is probably bogus. If it's a + * problem, upgrade to a modern system. + */ + if (neg && result == ULONG_MAX) { + errno = ERANGE; + return ULONG_MAX; + } else if (neg) + result = -result; + + return result; +} + +#ifdef TEST +#undef strtoul +int main(void) +{ + char *endptr; + unsigned long res1, res2; + + res1 = strtoul("0xdeadBeeF", & endptr, 0), + res2 = mystrtoul("0xdeadBeeF", & endptr, 0), +printf("(real,my)strtoul(\"0xdeadBeeF\", & endptr, 0) is %lu, %lu *endptr = %d\n", + res1, res2, *endptr); + + res1 = strtoul("0101101", & endptr, 2), + res2 = mystrtoul("0101101", & endptr, 2), +printf("(real,my)strtoul(\"0101101\", & endptr, 2) is %lu, %lu *endptr = %d\n", + res1, res2, *endptr); + + res1 = strtoul("01011012", & endptr, 2), + res2 = mystrtoul("01011012", & endptr, 2), +printf("(real,my)strtoul(\"01011012\", & endptr, 2) is %lu, %lu *endptr = %d\n", + res1, res2, *endptr); + + res1 = strtoul(" +42a", & endptr, 0), + res2 = mystrtoul(" +42a", & endptr, 0), +printf("(real,my)strtoul(\" +42a\", & endptr, 0) is %lu, %lu *endptr = %d\n", + res1, res2, *endptr); + + res1 = strtoul("0377", & endptr, 0), + res2 = mystrtoul("0377", & endptr, 0), +printf("(real,my)strtoul(\"0377\", & endptr, 0) is %lu, %lu *endptr = %d\n", + res1, res2, *endptr); + + res1 = strtoul("Z", & endptr, 36), + res2 = mystrtoul("Z", & endptr, 36), +printf("(real,my)strtoul(\"Z\", & endptr, 36) is %lu, %lu *endptr = %d\n", + res1, res2, *endptr); + + res1 = strtoul("qZ*", & endptr, 36), + res2 = mystrtoul("qZ*", & endptr, 36), +printf("(real,my)strtoul(\"qZ*\", & endptr, 36) is %lu, %lu *endptr = %d\n", + res1, res2, *endptr); +} +#endif |