root/lib/strutil/xstrtol.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. bkm_scale
  2. bkm_scale_by_power
  3. xstrtoumax

   1 /* A more useful interface to strtol.
   2 
   3    Copyright (C) 1995-2019
   4    Free Software Foundation, Inc.
   5 
   6    This program is free software: you can redistribute it and/or modify
   7    it under the terms of the GNU General Public License as published by
   8    the Free Software Foundation; either version 3 of the License, or
   9    (at your option) any later version.
  10 
  11    This program is distributed in the hope that it will be useful,
  12    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14    GNU General Public License for more details.
  15 
  16    You should have received a copy of the GNU General Public License
  17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
  18 
  19 /* Written by Jim Meyering. */
  20 
  21 #include <config.h>
  22 
  23 /* Some pre-ANSI implementations (e.g. SunOS 4)
  24    need stderr defined if assertion checking is enabled.  */
  25 #include <stdio.h>
  26 
  27 #include <ctype.h>
  28 #include <errno.h>
  29 #include <inttypes.h>
  30 #include <limits.h>
  31 #include <stdlib.h>
  32 #include <string.h>
  33 
  34 #include "lib/strutil.h"
  35 
  36 /*** global variables ****************************************************************************/
  37 
  38 /*** file scope macro definitions ****************************************************************/
  39 
  40 /*** file scope type declarations ****************************************************************/
  41 
  42 /*** file scope variables ************************************************************************/
  43 
  44 /* --------------------------------------------------------------------------------------------- */
  45 /*** file scope functions ************************************************************************/
  46 /* --------------------------------------------------------------------------------------------- */
  47 
  48 static strtol_error_t
  49 bkm_scale (uintmax_t * x, int scale_factor)
     /* [previous][next][first][last][top][bottom][index][help]  */
  50 {
  51     if (UINTMAX_MAX / scale_factor < *x)
  52     {
  53         *x = UINTMAX_MAX;
  54         return LONGINT_OVERFLOW;
  55     }
  56 
  57     *x *= scale_factor;
  58     return LONGINT_OK;
  59 }
  60 
  61 /* --------------------------------------------------------------------------------------------- */
  62 
  63 static strtol_error_t
  64 bkm_scale_by_power (uintmax_t * x, int base, int power)
     /* [previous][next][first][last][top][bottom][index][help]  */
  65 {
  66     strtol_error_t err = LONGINT_OK;
  67     while (power-- != 0)
  68         err |= bkm_scale (x, base);
  69     return err;
  70 }
  71 
  72 /* --------------------------------------------------------------------------------------------- */
  73 /*** public functions ****************************************************************************/
  74 /* --------------------------------------------------------------------------------------------- */
  75 
  76 strtol_error_t
  77 xstrtoumax (const char *s, char **ptr, int base, uintmax_t * val, const char *valid_suffixes)
     /* [previous][next][first][last][top][bottom][index][help]  */
  78 {
  79     char *t_ptr;
  80     char **p;
  81     uintmax_t tmp;
  82     strtol_error_t err = LONGINT_OK;
  83 
  84     g_assert (0 <= base && base <= 36);
  85 
  86     p = (ptr != NULL ? ptr : &t_ptr);
  87 
  88     {
  89         const char *q = s;
  90         unsigned char ch = *q;
  91 
  92         while (isspace (ch))
  93             ch = *++q;
  94 
  95         if (ch == '-')
  96             return LONGINT_INVALID;
  97     }
  98 
  99     errno = 0;
 100     tmp = strtol (s, p, base);
 101 
 102     if (*p == s)
 103     {
 104         /* If there is no number but there is a valid suffix, assume the
 105            number is 1.  The string is invalid otherwise.  */
 106         if (valid_suffixes != NULL && **p != '\0' && strchr (valid_suffixes, **p) != NULL)
 107             tmp = 1;
 108         else
 109             return LONGINT_INVALID;
 110     }
 111     else if (errno != 0)
 112     {
 113         if (errno != ERANGE)
 114             return LONGINT_INVALID;
 115         err = LONGINT_OVERFLOW;
 116     }
 117 
 118     /* Let valid_suffixes == NULL mean "allow any suffix".  */
 119     /* FIXME: update all callers except the ones that allow suffixes
 120        after the number, changing last parameter NULL to "".  */
 121     if (valid_suffixes == NULL)
 122     {
 123         *val = tmp;
 124         return err;
 125     }
 126 
 127     if (**p != '\0')
 128     {
 129         int suffixes = 1;
 130         strtol_error_t overflow;
 131 
 132         if (strchr (valid_suffixes, **p) == NULL)
 133         {
 134             *val = tmp;
 135             return err | LONGINT_INVALID_SUFFIX_CHAR;
 136         }
 137 
 138         base = 1024;
 139 
 140         switch (**p)
 141         {
 142         case 'E':
 143         case 'G':
 144         case 'g':
 145         case 'k':
 146         case 'K':
 147         case 'M':
 148         case 'm':
 149         case 'P':
 150         case 'T':
 151         case 't':
 152         case 'Y':
 153         case 'Z':
 154             if (strchr (valid_suffixes, '0') != NULL)
 155             {
 156                 /* The "valid suffix" '0' is a special flag meaning that
 157                    an optional second suffix is allowed, which can change
 158                    the base.  A suffix "B" (e.g. "100MB") stands for a power
 159                    of 1000, whereas a suffix "iB" (e.g. "100MiB") stands for
 160                    a power of 1024.  If no suffix (e.g. "100M"), assume
 161                    power-of-1024.  */
 162 
 163                 switch (p[0][1])
 164                 {
 165                 case 'i':
 166                     if (p[0][2] == 'B')
 167                         suffixes += 2;
 168                     break;
 169 
 170                 case 'B':
 171                 case 'D':      /* 'D' is obsolescent */
 172                     base = 1000;
 173                     suffixes++;
 174                     break;
 175                 default:
 176                     break;
 177                 }
 178             }
 179         default:
 180             break;
 181         }
 182 
 183         switch (**p)
 184         {
 185         case 'b':
 186             overflow = bkm_scale (&tmp, 512);
 187             break;
 188 
 189         case 'B':
 190             /* This obsolescent first suffix is distinct from the 'B'
 191                second suffix above.  E.g., 'tar -L 1000B' means change
 192                the tape after writing 1000 KiB of data.  */
 193             overflow = bkm_scale (&tmp, 1024);
 194             break;
 195 
 196         case 'c':
 197             overflow = LONGINT_OK;
 198             break;
 199 
 200         case 'E':              /* exa or exbi */
 201             overflow = bkm_scale_by_power (&tmp, base, 6);
 202             break;
 203 
 204         case 'G':              /* giga or gibi */
 205         case 'g':              /* 'g' is undocumented; for compatibility only */
 206             overflow = bkm_scale_by_power (&tmp, base, 3);
 207             break;
 208 
 209         case 'k':              /* kilo */
 210         case 'K':              /* kibi */
 211             overflow = bkm_scale_by_power (&tmp, base, 1);
 212             break;
 213 
 214         case 'M':              /* mega or mebi */
 215         case 'm':              /* 'm' is undocumented; for compatibility only */
 216             overflow = bkm_scale_by_power (&tmp, base, 2);
 217             break;
 218 
 219         case 'P':              /* peta or pebi */
 220             overflow = bkm_scale_by_power (&tmp, base, 5);
 221             break;
 222 
 223         case 'T':              /* tera or tebi */
 224         case 't':              /* 't' is undocumented; for compatibility only */
 225             overflow = bkm_scale_by_power (&tmp, base, 4);
 226             break;
 227 
 228         case 'w':
 229             overflow = bkm_scale (&tmp, 2);
 230             break;
 231 
 232         case 'Y':              /* yotta or 2**80 */
 233             overflow = bkm_scale_by_power (&tmp, base, 8);
 234             break;
 235 
 236         case 'Z':              /* zetta or 2**70 */
 237             overflow = bkm_scale_by_power (&tmp, base, 7);
 238             break;
 239 
 240         default:
 241             *val = tmp;
 242             return err | LONGINT_INVALID_SUFFIX_CHAR;
 243         }
 244 
 245         err |= overflow;
 246         *p += suffixes;
 247         if (**p != '\0')
 248             err |= LONGINT_INVALID_SUFFIX_CHAR;
 249     }
 250 
 251     *val = tmp;
 252     return err;
 253 }
 254 
 255 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */