From f5757161aff8caeec8e70b9709ccdce96e41dd14 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Fri, 16 Feb 2007 21:08:22 -0500 Subject: Add atolx() which understands extensions for kilobytes and megabytes and such. --- lib/args.c | 2 +- lib/lib.c | 12 ++++++++++++ lib/lib.h | 1 + 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/args.c b/lib/args.c index 2c17951b..c9ade1e9 100644 --- a/lib/args.c +++ b/lib/args.c @@ -108,7 +108,7 @@ static void gotflag(void) temp->arg = gof.arg; temp->next = *list; *list = temp; - } else if (type == '#') *(gof.this->arg) = atol((char *)gof.arg); + } else if (type == '#') *(gof.this->arg) = atolx((char *)gof.arg); else if (type == '@') { } diff --git a/lib/lib.c b/lib/lib.c index f1d1ce1f..6c8abb03 100644 --- a/lib/lib.c +++ b/lib/lib.c @@ -402,6 +402,18 @@ char *itoa(int n) return itoa_buf; } +// atol() with the kilo/mega/giga/tera/peta/exa extensions. +// (zetta and yotta don't fit in 64 bits.) +long atolx(char *c) +{ + char *suffixes="kmgtpe", *end; + long val = strtol(c, &c, 0); + + end = strchr(suffixes, tolower(*c)); + if (end) val *= 1024<<((end-suffixes)*10); + return val; +} + // Return how long the file at fd is, if there's any way to determine it. off_t fdlength(int fd) { diff --git a/lib/lib.h b/lib/lib.h index 07cc9cd8..e2d055bc 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -65,6 +65,7 @@ void utoa_to_buf(unsigned n, char *buf, unsigned buflen); void itoa_to_buf(int n, char *buf, unsigned buflen); char *utoa(unsigned n); char *itoa(int n); +long atolx(char *c); off_t fdlength(int fd); struct dirtree *read_dirtree_node(char *path); struct dirtree *read_dirtree(char *path, struct dirtree *parent); -- cgit v1.2.3