Chameleon

Chameleon Svn Source Tree

Root/branches/rewrite/i386/modules/klibc/strntoumax.c

Source at commit 1166 created 13 years 10 days ago.
By meklort, Fixed recent Makefile changes. Please do not use /Users/evan/SourceCode/tmp/chameleon/trunk or VPATH. the *only* time you should use /Users/evan/SourceCode/tmp/chameleon/trunk is when setting the SRCROOT variable. Also note that very soon make pkg is going to be removed. The pkg build script in trunk is very out of date. Instead please use the package maker at http://forge.voodooprojects.org/p/chameleonApplications/. Once this is ready for trunk it will be merged.
1/*
2 * strntoumax.c
3 *
4 * The strntoumax() function and associated
5 */
6#include "libsaio.h"
7
8static inline int digitval(int ch)
9{
10if (ch >= '0' && ch <= '9') {
11return ch - '0';
12} else if (ch >= 'A' && ch <= 'Z') {
13return ch - 'A' + 10;
14} else if (ch >= 'a' && ch <= 'z') {
15return ch - 'a' + 10;
16} else {
17return -1;
18}
19}
20
21uintmax_t strntoumax(const char *nptr, char **endptr, int base, size_t n)
22{
23int minus = 0;
24uintmax_t v = 0;
25int d;
26
27while (n && isspace((unsigned char)*nptr)) {
28nptr++;
29n--;
30}
31
32/* Single optional + or - */
33if (n) {
34char c = *nptr;
35if (c == '-' || c == '+') {
36minus = (c == '-');
37nptr++;
38n--;
39}
40}
41
42if (base == 0) {
43if (n >= 2 && nptr[0] == '0' &&
44 (nptr[1] == 'x' || nptr[1] == 'X')) {
45n -= 2;
46nptr += 2;
47base = 16;
48} else if (n >= 1 && nptr[0] == '0') {
49n--;
50nptr++;
51base = 8;
52} else {
53base = 10;
54}
55} else if (base == 16) {
56if (n >= 2 && nptr[0] == '0' &&
57 (nptr[1] == 'x' || nptr[1] == 'X')) {
58n -= 2;
59nptr += 2;
60}
61}
62
63while (n && (d = digitval(*nptr)) >= 0 && d < base) {
64v = v * base + d;
65n--;
66nptr++;
67}
68
69if (endptr)
70*endptr = (char *)nptr;
71
72return minus ? -v : v;
73}
74

Archive Download this file

Revision: 1166