sl@0: /* sl@0: * strtoll.c -- sl@0: * sl@0: * Source code for the "strtoll" 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: strtoll.c,v 1.5 2002/02/24 02:53:25 dgp Exp $ sl@0: */ sl@0: sl@0: #include "tcl.h" sl@0: #include "tclPort.h" sl@0: #include sl@0: sl@0: #define TCL_WIDEINT_MAX (((Tcl_WideUInt)Tcl_LongAsWide(-1))>>1) sl@0: sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * strtoll -- 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: #if TCL_WIDE_INT_IS_LONG sl@0: long long sl@0: #else sl@0: Tcl_WideInt sl@0: #endif sl@0: strtoll(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: Tcl_WideInt result = Tcl_LongAsWide(0); sl@0: Tcl_WideUInt uwResult; 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: errno = 0; sl@0: if (*p == '-') { sl@0: p += 1; sl@0: uwResult = strtoull(p, endPtr, base); sl@0: if (errno != ERANGE) { sl@0: if (uwResult > TCL_WIDEINT_MAX+1) { sl@0: errno = ERANGE; sl@0: return Tcl_LongAsWide(-1); sl@0: } else if (uwResult > TCL_WIDEINT_MAX) { sl@0: return ~((Tcl_WideInt)TCL_WIDEINT_MAX); sl@0: } else { sl@0: result = -((Tcl_WideInt) uwResult); sl@0: } sl@0: } sl@0: } else { sl@0: if (*p == '+') { sl@0: p += 1; sl@0: } sl@0: uwResult = strtoull(p, endPtr, base); sl@0: if (errno != ERANGE) { sl@0: if (uwResult > TCL_WIDEINT_MAX) { sl@0: errno = ERANGE; sl@0: return Tcl_LongAsWide(-1); sl@0: } else { sl@0: result = uwResult; sl@0: } sl@0: } sl@0: } sl@0: if ((result == 0) && (endPtr != 0) && (*endPtr == p)) { sl@0: *endPtr = (char *) string; sl@0: } sl@0: return result; sl@0: }