aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2016-07-12 14:51:51 -0500
committerRob Landley <rob@landley.net>2016-07-12 14:51:51 -0500
commit30454a4ca132ea76851f799546f67f98b074f650 (patch)
tree2eb920383db5907af4ae141b27fa46d535b2b772 /lib
parentd5088a059649daf34e729995bb3daa3eb64fa432 (diff)
downloadtoybox-30454a4ca132ea76851f799546f67f98b074f650.tar.gz
Convert atolx() and friends to use long long internally. Update design.html
to use this (and tail) as examples of simplicity of implementation winning and losing.
Diffstat (limited to 'lib')
-rw-r--r--lib/lib.c37
-rw-r--r--lib/lib.h8
2 files changed, 23 insertions, 22 deletions
diff --git a/lib/lib.c b/lib/lib.c
index b742bd17..56656435 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -273,16 +273,16 @@ struct string_list *find_in_path(char *path, char *filename)
return rlist;
}
-long estrtol(char *str, char **end, int base)
+long long estrtol(char *str, char **end, int base)
{
errno = 0;
- return strtol(str, end, base);
+ return strtoll(str, end, base);
}
-long xstrtol(char *str, char **end, int base)
+long long xstrtol(char *str, char **end, int base)
{
- long l = estrtol(str, end, base);
+ long long l = estrtol(str, end, base);
if (errno) perror_exit_raw(str);
@@ -291,31 +291,32 @@ long xstrtol(char *str, char **end, int base)
// atol() with the kilo/mega/giga/tera/peta/exa extensions.
// (zetta and yotta don't fit in 64 bits.)
-long atolx(char *numstr)
+long long atolx(char *numstr)
{
- char *c, *suffixes="cbkmgtpe", *end;
- long val;
+ char *c = numstr, *suffixes="cbkmgtpe", *end;
+ long long val;
val = xstrtol(numstr, &c, 0);
- if (*c) {
- if (c != numstr && (end = strchr(suffixes, tolower(*c)))) {
- int shift = end-suffixes-2;
- if (shift >= 0) val *= 1024L<<(shift*10);
- } else {
- while (isspace(*c)) c++;
- if (*c) error_exit("not integer: %s", numstr);
+ if (c != numstr && (end = strchr(suffixes, tolower(*c)))) {
+ int shift = end-suffixes-2;
+
+ if (shift >= 0) {
+ if (toupper(*++c)=='d') do val *= 1000; while (shift--);
+ else val *= 1024LL<<(shift*10);
}
}
+ while (isspace(*c)) c++;
+ if (c==numstr || *c) error_exit("not integer: %s", numstr);
return val;
}
-long atolx_range(char *numstr, long low, long high)
+long long atolx_range(char *numstr, long long low, long long high)
{
- long val = atolx(numstr);
+ long long val = atolx(numstr);
- if (val < low) error_exit("%ld < %ld", val, low);
- if (val > high) error_exit("%ld > %ld", val, high);
+ if (val < low) error_exit("%lld < %lld", val, low);
+ if (val > high) error_exit("%lld > %lld", val, high);
return val;
}
diff --git a/lib/lib.h b/lib/lib.h
index 739548b8..f97940a8 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -184,10 +184,10 @@ int64_t peek_be(void *ptr, unsigned size);
int64_t peek(void *ptr, unsigned size);
void poke(void *ptr, uint64_t val, int size);
struct string_list *find_in_path(char *path, char *filename);
-long estrtol(char *str, char **end, int base);
-long xstrtol(char *str, char **end, int base);
-long atolx(char *c);
-long atolx_range(char *numstr, long low, long high);
+long long estrtol(char *str, char **end, int base);
+long long xstrtol(char *str, char **end, int base);
+long long atolx(char *c);
+long long atolx_range(char *numstr, long long low, long long high);
int stridx(char *haystack, char needle);
char *strlower(char *s);
char *strafter(char *haystack, char *needle);