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