sl@0: /* sl@0: * strtol.c -- sl@0: * sl@0: * Source code for the "strtol" library procedure. sl@0: * sl@0: * Copyright (c) 1988 The Regents of the University of California. sl@0: * Copyright (c) 1994 Sun Microsystems, Inc. sl@0: * sl@0: * See the file "license.terms" for information on usage and redistribution sl@0: * of this file, and for a DISCLAIMER OF ALL WARRANTIES. sl@0: * sl@0: * RCS: @(#) $Id: strtol.c,v 1.4 2002/02/25 16:23:26 dgp Exp $ sl@0: */ sl@0: sl@0: #include sl@0: #include "tclInt.h" sl@0: #include "tclPort.h" sl@0: sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * strtol -- sl@0: * sl@0: * Convert an ASCII string into an integer. sl@0: * sl@0: * Results: sl@0: * The return value is the integer equivalent of string. If endPtr sl@0: * is non-NULL, then *endPtr is filled in with the character sl@0: * after the last one that was part of the integer. If string sl@0: * doesn't contain a valid integer value, then zero is returned sl@0: * and *endPtr is set to string. sl@0: * sl@0: * Side effects: sl@0: * None. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: long int sl@0: strtol(string, endPtr, base) sl@0: CONST char *string; /* String of ASCII digits, possibly sl@0: * preceded by white space. For bases sl@0: * greater than 10, either lower- or sl@0: * upper-case digits may be used. sl@0: */ sl@0: char **endPtr; /* Where to store address of terminating sl@0: * character, or NULL. */ sl@0: int base; /* Base for conversion. Must be less sl@0: * than 37. If 0, then the base is chosen sl@0: * from the leading characters of string: sl@0: * "0x" means hex, "0" means octal, anything sl@0: * else means decimal. sl@0: */ sl@0: { sl@0: register CONST char *p; sl@0: long result; sl@0: sl@0: /* sl@0: * Skip any leading blanks. sl@0: */ sl@0: sl@0: p = string; sl@0: while (isspace(UCHAR(*p))) { sl@0: p += 1; sl@0: } sl@0: sl@0: /* sl@0: * Check for a sign. sl@0: */ sl@0: sl@0: if (*p == '-') { sl@0: p += 1; sl@0: result = -(strtoul(p, endPtr, base)); sl@0: } else { sl@0: if (*p == '+') { sl@0: p += 1; sl@0: } sl@0: result = strtoul(p, endPtr, base); sl@0: } sl@0: if ((result == 0) && (endPtr != 0) && (*endPtr == p)) { sl@0: *endPtr = (char *) string; sl@0: } sl@0: return result; sl@0: }