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