FreeCalypso > hg > freecalypso-tools
annotate target-utils/libc/strtol.c @ 784:839bf41e7be0
simagent: X command parsing bugfix
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sat, 13 Mar 2021 22:08:48 +0000 |
parents | d8d03dafb9c0 |
children |
rev | line source |
---|---|
96
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /*- |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * Copyright (c) 1990, 1993 |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 * The Regents of the University of California. All rights reserved. |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 * |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 * Redistribution and use in source and binary forms, with or without |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 * modification, are permitted provided that the following conditions |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 * are met: |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 * 1. Redistributions of source code must retain the above copyright |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 * notice, this list of conditions and the following disclaimer. |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 * 2. Redistributions in binary form must reproduce the above copyright |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 * notice, this list of conditions and the following disclaimer in the |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 * documentation and/or other materials provided with the distribution. |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 * 3. All advertising materials mentioning features or use of this software |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 * must display the following acknowledgement: |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 * This product includes software developed by the University of |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 * California, Berkeley and its contributors. |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 * 4. Neither the name of the University nor the names of its contributors |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 * may be used to endorse or promote products derived from this software |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 * without specific prior written permission. |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 * |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 * SUCH DAMAGE. |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 */ |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 /* |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 * Embedded libc note: the present version of strtol() has been |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 * simplified in that overflow checking has been removed. |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 * This simplification is needed for embedded systems in which |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 * there is no UNIX-style errno global variable. |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 */ |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 #include <ctype.h> |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 /* |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 * Convert a string to a long integer. |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 * |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 * Ignores `locale' stuff. Assumes that the upper and lower case |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 * alphabets and digits are each contiguous. |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 */ |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 long |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 strtol(nptr, endptr, base) |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 const char *nptr; |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 char **endptr; |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 register int base; |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 { |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 register const char *s = nptr; |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 register unsigned long acc; |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 register int c; |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 register int neg = 0, any; |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 /* |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 * Skip white space and pick up leading +/- sign if any. |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 * If base is 0, allow 0x for hex and 0 for octal, else |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 * assume decimal; if base is already 16, allow 0x. |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 */ |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 do { |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 c = *s++; |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 } while (isspace(c)); |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 if (c == '-') { |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 neg = 1; |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 c = *s++; |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 } else if (c == '+') |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
72 c = *s++; |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
73 if ((base == 0 || base == 16) && |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
74 c == '0' && (*s == 'x' || *s == 'X')) { |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
75 c = s[1]; |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
76 s += 2; |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
77 base = 16; |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
78 } |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
79 if (base == 0) |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
80 base = c == '0' ? 8 : 10; |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
81 |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
82 for (acc = 0, any = 0;; c = *s++) { |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
83 if (isdigit(c)) |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
84 c -= '0'; |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
85 else if (isalpha(c)) |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
86 c -= isupper(c) ? 'A' - 10 : 'a' - 10; |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
87 else |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
88 break; |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
89 if (c >= base) |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
90 break; |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
91 any = 1; |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
92 acc *= base; |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
93 acc += c; |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
94 } |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
95 if (neg) |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
96 acc = -acc; |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
97 if (endptr != 0) |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
98 *endptr = (char *)(any ? s - 1 : nptr); |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
99 return (acc); |
d8d03dafb9c0
target-utils/libc: strtol and strtoul added from 4.4BSD with simplification
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
100 } |