Debian Codegraph @

compat/strtol.c

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