]>
git.ipfire.org Git - people/ms/u-boot.git/blob - lib/strto.c
7f6076909ab7401e802eac798671b79c0515a0b7
4 * Copyright (C) 1991, 1992 Linus Torvalds
7 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
14 #include <linux/ctype.h>
16 /* from lib/kstrtox.c */
17 static const char *_parse_integer_fixup_radix(const char *s
, unsigned int *base
)
21 if (tolower(s
[1]) == 'x' && isxdigit(s
[2]))
28 if (*base
== 16 && s
[0] == '0' && tolower(s
[1]) == 'x')
33 unsigned long simple_strtoul(const char *cp
, char **endp
,
36 unsigned long result
= 0;
39 cp
= _parse_integer_fixup_radix(cp
, &base
);
41 while (isxdigit(*cp
) && (value
= isdigit(*cp
) ? *cp
-'0' : (islower(*cp
)
42 ? toupper(*cp
) : *cp
)-'A'+10) < base
) {
43 result
= result
*base
+ value
;
53 int strict_strtoul(const char *cp
, unsigned int base
, unsigned long *res
)
64 val
= simple_strtoul(cp
, &tail
, base
);
68 if ((*tail
== '\0') ||
69 ((len
== (size_t)(tail
- cp
) + 1) && (*tail
== '\n'))) {
77 long simple_strtol(const char *cp
, char **endp
, unsigned int base
)
80 return -simple_strtoul(cp
+ 1, endp
, base
);
82 return simple_strtoul(cp
, endp
, base
);
85 unsigned long ustrtoul(const char *cp
, char **endp
, unsigned int base
)
87 unsigned long result
= simple_strtoul(cp
, endp
, base
);
98 if ((*endp
)[1] == 'i') {
99 if ((*endp
)[2] == 'B')
108 unsigned long long ustrtoull(const char *cp
, char **endp
, unsigned int base
)
110 unsigned long long result
= simple_strtoull(cp
, endp
, base
);
121 if ((*endp
)[1] == 'i') {
122 if ((*endp
)[2] == 'B')
131 unsigned long long simple_strtoull(const char *cp
, char **endp
,
134 unsigned long long result
= 0, value
;
136 cp
= _parse_integer_fixup_radix(cp
, &base
);
138 while (isxdigit(*cp
) && (value
= isdigit(*cp
) ? *cp
- '0'
139 : (islower(*cp
) ? toupper(*cp
) : *cp
) - 'A' + 10) < base
) {
140 result
= result
* base
+ value
;
150 long trailing_strtoln(const char *str
, const char *end
)
155 end
= str
+ strlen(str
);
156 if (isdigit(end
[-1])) {
157 for (p
= end
- 1; p
> str
; p
--) {
159 return simple_strtoul(p
+ 1, NULL
, 10);
166 long trailing_strtol(const char *str
)
168 return trailing_strtoln(str
, NULL
);