Git-Url: http://git.frugalware.org/gitweb/gitweb.cgi?p=fwsetup-ng.git;a=commitdiff;h=8ebbf3bd3bdbe9ee73623d9b216f7ef9b93aec49
commit 8ebbf3bd3bdbe9ee73623d9b216f7ef9b93aec49 Author: James Buren <[email protected]> Date: Wed Aug 15 13:14:19 2012 -0500 add string_to_size() function diff --git a/fwsetup.h b/fwsetup.h index b6bb469..5aa5030 100644 --- a/fwsetup.h +++ b/fwsetup.h @@ -30,6 +30,7 @@ #define GIBIBYTE (2LLU << 29LLU) #define TEBIBYTE (2LLU << 39LLU) #define PEBIBYTE (2LLU << 49LLU) +#define EXBIBYTE (2LLU << 59LLU) #define LOGFILE "fwsetup.log" #define EXECUTE_START_TEXT _("About to execute command '%s'.\n") #define EXECUTE_STOP_TEXT _("Successfully executed command '%s'.\n") @@ -127,6 +128,7 @@ extern void *malloc0(size_t n); extern pid_t execute(const char *cmd); extern void eprintf(const char *fmt,...) __attribute__((format(printf,1,2))); extern void snprintf_append(char *s,size_t size,const char *fmt,...) __attribute__((format(printf,3,4))); +extern unsigned long long string_to_size(const char *s); extern void *list_append(void *list,size_t n); extern void *list_find_start(void *list); extern void *list_find_end(void *list); diff --git a/utility.c b/utility.c index e73a005..bfc6902 100644 --- a/utility.c +++ b/utility.c @@ -198,6 +198,54 @@ extern void snprintf_append(char *s,size_t size,const char *fmt,...) va_end(args); } +extern unsigned long long string_to_size(const char *s) +{ + ASSERT_ARGS(s == 0,0); + + char *suffix = 0; + unsigned long long n = 0; + + errno = 0; + + n = strtoull(s,&suffix); + + if(errno != 0) + { + LOG_ERRNO(); + return 0; + } + + if(n == 0) + { + errno = EINVAL; + LOG_ERRNO(); + return 0; + } + + if(strlen(suffix) == 0 || strcmp(suffix,"B") == 0 || strcmp(suffix,"BiB") == 0) + n *= 1; + else if(strcmp(suffix,"K") == 0 || strcmp(suffix,"KiB") == 0) + n *= KIBIBYTE; + else if(strcmp(suffix,"M") == 0 || strcmp(suffix,"MiB") == 0) + n *= MEBIBYTE; + else if(strcmp(suffix,"G") == 0 || strcmp(suffix,"GiB") == 0) + n *= GIBIBYTE; + else if(strcmp(suffix,"T") == 0 || strcmp(suffix,"TiB") == 0) + n *= TEBIBYTE; + else if(strcmp(suffix,"P") == 0 || strcmp(suffix,"PiB") == 0) + n *= PEBIBYTE; + else if(strcmp(suffix,"E") == 0 || strcmp(suffix,"EiB") == 0) + n *= EXBIBYTE; + else + { + errno = EINVAL; + LOG_ERRNO(); + return 0; + } + + return n; +} + extern void *list_append(void *list,size_t n) { ASSERT_ARGS(n <= sizeof(struct list),0); _______________________________________________ Frugalware-git mailing list [email protected] http://frugalware.org/mailman/listinfo/frugalware-git
