os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/compat/strtol.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/* 
sl@0
     2
 * strtol.c --
sl@0
     3
 *
sl@0
     4
 *	Source code for the "strtol" library procedure.
sl@0
     5
 *
sl@0
     6
 * Copyright (c) 1988 The Regents of the University of California.
sl@0
     7
 * Copyright (c) 1994 Sun Microsystems, Inc.
sl@0
     8
 *
sl@0
     9
 * See the file "license.terms" for information on usage and redistribution
sl@0
    10
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
sl@0
    11
 *
sl@0
    12
 * RCS: @(#) $Id: strtol.c,v 1.4 2002/02/25 16:23:26 dgp Exp $
sl@0
    13
 */
sl@0
    14
sl@0
    15
#include <ctype.h>
sl@0
    16
#include "tclInt.h"
sl@0
    17
#include "tclPort.h"
sl@0
    18
sl@0
    19

sl@0
    20
/*
sl@0
    21
 *----------------------------------------------------------------------
sl@0
    22
 *
sl@0
    23
 * strtol --
sl@0
    24
 *
sl@0
    25
 *	Convert an ASCII string into an integer.
sl@0
    26
 *
sl@0
    27
 * Results:
sl@0
    28
 *	The return value is the integer equivalent of string.  If endPtr
sl@0
    29
 *	is non-NULL, then *endPtr is filled in with the character
sl@0
    30
 *	after the last one that was part of the integer.  If string
sl@0
    31
 *	doesn't contain a valid integer value, then zero is returned
sl@0
    32
 *	and *endPtr is set to string.
sl@0
    33
 *
sl@0
    34
 * Side effects:
sl@0
    35
 *	None.
sl@0
    36
 *
sl@0
    37
 *----------------------------------------------------------------------
sl@0
    38
 */
sl@0
    39
sl@0
    40
long int
sl@0
    41
strtol(string, endPtr, base)
sl@0
    42
    CONST char *string;		/* String of ASCII digits, possibly
sl@0
    43
				 * preceded by white space.  For bases
sl@0
    44
				 * greater than 10, either lower- or
sl@0
    45
				 * upper-case digits may be used.
sl@0
    46
				 */
sl@0
    47
    char **endPtr;		/* Where to store address of terminating
sl@0
    48
				 * character, or NULL. */
sl@0
    49
    int base;			/* Base for conversion.  Must be less
sl@0
    50
				 * than 37.  If 0, then the base is chosen
sl@0
    51
				 * from the leading characters of string:
sl@0
    52
				 * "0x" means hex, "0" means octal, anything
sl@0
    53
				 * else means decimal.
sl@0
    54
				 */
sl@0
    55
{
sl@0
    56
    register CONST char *p;
sl@0
    57
    long result;
sl@0
    58
sl@0
    59
    /*
sl@0
    60
     * Skip any leading blanks.
sl@0
    61
     */
sl@0
    62
sl@0
    63
    p = string;
sl@0
    64
    while (isspace(UCHAR(*p))) {
sl@0
    65
	p += 1;
sl@0
    66
    }
sl@0
    67
sl@0
    68
    /*
sl@0
    69
     * Check for a sign.
sl@0
    70
     */
sl@0
    71
sl@0
    72
    if (*p == '-') {
sl@0
    73
	p += 1;
sl@0
    74
	result = -(strtoul(p, endPtr, base));
sl@0
    75
    } else {
sl@0
    76
	if (*p == '+') {
sl@0
    77
	    p += 1;
sl@0
    78
	}
sl@0
    79
	result = strtoul(p, endPtr, base);
sl@0
    80
    }
sl@0
    81
    if ((result == 0) && (endPtr != 0) && (*endPtr == p)) {
sl@0
    82
	*endPtr = (char *) string;
sl@0
    83
    }
sl@0
    84
    return result;
sl@0
    85
}