]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Rename value_units to parse_size_with_suffix and handle errors
authorJoel Rosdahl <joel@rosdahl.net>
Mon, 25 Apr 2011 12:06:36 +0000 (14:06 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Mon, 30 May 2011 19:35:53 +0000 (21:35 +0200)
ccache.c
ccache.h
util.c

index ce6982f2e80291b5bb4165af53a8d8778dbcf72a..b749c7fe9146bbea61b82eef7cff1fbb9594c43f 100644 (file)
--- a/ccache.c
+++ b/ccache.c
@@ -2097,7 +2097,7 @@ ccache_main_options(int argc, char *argv[])
 
                case 'M': /* --max-size */
                        check_cache_dir();
-                       v = value_units(optarg);
+                       parse_size_with_suffix(optarg, &v);
                        if (stats_set_limits(-1, v) == 0) {
                                if (v == 0) {
                                        printf("Unset cache size limit\n");
index 59c05f7bbfd697ad77781eec3553389b8b8265d0..9c415e6b8f19968f095dfaf0e23f52bc89388d12 100644 (file)
--- a/ccache.h
+++ b/ccache.h
@@ -129,7 +129,7 @@ char *remove_extension(const char *path);
 size_t file_size(struct stat *st);
 int safe_create_wronly(const char *fname);
 char *format_size(size_t v);
-size_t value_units(const char *s);
+bool parse_size_with_suffix(const char *str, size_t *size);
 char *x_realpath(const char *path);
 char *gnu_getcwd(void);
 #ifndef HAVE_STRTOK_R
diff --git a/util.c b/util.c
index 1b7c950ce95ed6cead9bac8b5a1ed8412fd30c58..67aa431435f1f3ae795ae7962cd3fec7dc61361a 100644 (file)
--- a/util.c
+++ b/util.c
@@ -828,31 +828,39 @@ format_size(size_t v)
        return s;
 }
 
-/* return a value in multiples of 1024 give a string that can end
-   in K, M or G
-*/
-size_t
-value_units(const char *s)
+/*
+ * Parse a value in multiples of 1024 given a string that can end in K, M or G.
+ * Default suffix: G.
+ */
+bool
+parse_size_with_suffix(const char *str, size_t *size)
 {
-       char m;
-       double v = atof(s);
-       m = s[strlen(s)-1];
-       switch (m) {
+       char *endptr;
+       long x;
+       errno = 0;
+       x = strtol(str, &endptr, 10);
+       if (errno != 0 || x < 0 || endptr == str || *str == '\0') {
+               return false;
+       }
+
+       *size = x;
+       switch (*endptr) {
        case 'G':
        case 'g':
-       default:
-               v *= 1024*1024;
+       case '\0':
+               *size *= 1024 * 1024;
                break;
        case 'M':
        case 'm':
-               v *= 1024;
+               *size *= 1024;
                break;
        case 'K':
        case 'k':
-               v *= 1;
                break;
+       default:
+               return false;
        }
-       return (size_t)v;
+       return true;
 }
 
 /*