changeset 102:aa4fa2543a65

Add atolx() which understands extensions for kilobytes and megabytes and such.
author Rob Landley <rob@landley.net>
date Fri, 16 Feb 2007 21:08:22 -0500
parents ff85a83e7d7e
children 417591818dcb
files lib/args.c lib/lib.c lib/lib.h
diffstat 3 files changed, 14 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/lib/args.c	Wed Feb 14 02:20:37 2007 -0500
+++ b/lib/args.c	Fri Feb 16 21:08:22 2007 -0500
@@ -108,7 +108,7 @@
 			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 == '@') {
 		}
 
--- a/lib/lib.c	Wed Feb 14 02:20:37 2007 -0500
+++ b/lib/lib.c	Fri Feb 16 21:08:22 2007 -0500
@@ -402,6 +402,18 @@
 	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)
 {
--- a/lib/lib.h	Wed Feb 14 02:20:37 2007 -0500
+++ b/lib/lib.h	Fri Feb 16 21:08:22 2007 -0500
@@ -65,6 +65,7 @@
 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);