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-2024
   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 /*** forward declarations (file scope functions) *************************************************/
  43 
  44 /*** file scope variables ************************************************************************/
  45 
  46 /* --------------------------------------------------------------------------------------------- */
  47 /*** file scope functions ************************************************************************/
  48 /* --------------------------------------------------------------------------------------------- */
  49 
  50 static strtol_error_t
  51 bkm_scale (uintmax_t * x, int scale_factor)
     /* [previous][next][first][last][top][bottom][index][help]  */
  52 {
  53     if (UINTMAX_MAX / scale_factor < *x)
  54     {
  55         *x = UINTMAX_MAX;
  56         return LONGINT_OVERFLOW;
  57     }
  58 
  59     *x *= scale_factor;
  60     return LONGINT_OK;
  61 }
  62 
  63 /* --------------------------------------------------------------------------------------------- */
  64 
  65 static strtol_error_t
  66 bkm_scale_by_power (uintmax_t * x, int base, int power)
     /* [previous][next][first][last][top][bottom][index][help]  */
  67 {
  68     strtol_error_t err = LONGINT_OK;
  69     while (power-- != 0)
  70         err |= bkm_scale (x, base);
  71     return err;
  72 }
  73 
  74 /* --------------------------------------------------------------------------------------------- */
  75 /*** public functions ****************************************************************************/
  76 /* --------------------------------------------------------------------------------------------- */
  77 
  78 strtol_error_t
  79 xstrtoumax (const char *s, char **ptr, int base, uintmax_t * val, const char *valid_suffixes)
     /* [previous][next][first][last][top][bottom][index][help]  */
  80 {
  81     char *t_ptr;
  82     char **p;
  83     uintmax_t tmp;
  84     strtol_error_t err = LONGINT_OK;
  85 
  86     g_assert (0 <= base && base <= 36);
  87 
  88     p = (ptr != NULL ? ptr : &t_ptr);
  89 
  90     {
  91         const char *q = s;
  92         unsigned char ch = *q;
  93 
  94         while (isspace (ch))
  95             ch = *++q;
  96 
  97         if (ch == '-')
  98             return LONGINT_INVALID;
  99     }
 100 
 101     errno = 0;
 102     tmp = strtol (s, p, base);
 103 
 104     if (*p == s)
 105     {
 106         /* If there is no number but there is a valid suffix, assume the
 107            number is 1.  The string is invalid otherwise.  */
 108         if (valid_suffixes != NULL && **p != '\0' && strchr (valid_suffixes, **p) != NULL)
 109             tmp = 1;
 110         else
 111             return LONGINT_INVALID;
 112     }
 113     else if (errno != 0)
 114     {
 115         if (errno != ERANGE)
 116             return LONGINT_INVALID;
 117         err = LONGINT_OVERFLOW;
 118     }
 119 
 120     /* Let valid_suffixes == NULL mean "allow any suffix".  */
 121     /* FIXME: update all callers except the ones that allow suffixes
 122        after the number, changing last parameter NULL to "".  */
 123     if (valid_suffixes == NULL)
 124     {
 125         *val = tmp;
 126         return err;
 127     }
 128 
 129     if (**p != '\0')
 130     {
 131         int suffixes = 1;
 132         strtol_error_t overflow;
 133 
 134         if (strchr (valid_suffixes, **p) == NULL)
 135         {
 136             *val = tmp;
 137             return err | LONGINT_INVALID_SUFFIX_CHAR;
 138         }
 139 
 140         base = 1024;
 141 
 142         switch (**p)
 143         {
 144         case 'E':
 145         case 'G':
 146         case 'g':
 147         case 'k':
 148         case 'K':
 149         case 'M':
 150         case 'm':
 151         case 'P':
 152         case 'Q':
 153         case 'R':
 154         case 'T':
 155         case 't':
 156         case 'Y':
 157         case 'Z':
 158             if (strchr (valid_suffixes, '0') != NULL)
 159             {
 160                 /* The "valid suffix" '0' is a special flag meaning that
 161                    an optional second suffix is allowed, which can change
 162                    the base.  A suffix "B" (e.g. "100MB") stands for a power
 163                    of 1000, whereas a suffix "iB" (e.g. "100MiB") stands for
 164                    a power of 1024.  If no suffix (e.g. "100M"), assume
 165                    power-of-1024.  */
 166 
 167                 switch (p[0][1])
 168                 {
 169                 case 'i':
 170                     if (p[0][2] == 'B')
 171                         suffixes += 2;
 172                     break;
 173 
 174                 case 'B':
 175                 case 'D':      /* 'D' is obsolescent */
 176                     base = 1000;
 177                     suffixes++;
 178                     break;
 179                 default:
 180                     break;
 181                 }
 182             }
 183             break;
 184         default:
 185             break;
 186         }
 187 
 188         switch (**p)
 189         {
 190         case 'b':
 191             overflow = bkm_scale (&tmp, 512);
 192             break;
 193 
 194         case 'B':
 195             /* This obsolescent first suffix is distinct from the 'B'
 196                second suffix above.  E.g., 'tar -L 1000B' means change
 197                the tape after writing 1000 KiB of data.  */
 198             overflow = bkm_scale (&tmp, 1024);
 199             break;
 200 
 201         case 'c':
 202             overflow = LONGINT_OK;
 203             break;
 204 
 205         case 'E':              /* exa or exbi */
 206             overflow = bkm_scale_by_power (&tmp, base, 6);
 207             break;
 208 
 209         case 'G':              /* giga or gibi */
 210         case 'g':              /* 'g' is undocumented; for compatibility only */
 211             overflow = bkm_scale_by_power (&tmp, base, 3);
 212             break;
 213 
 214         case 'k':              /* kilo */
 215         case 'K':              /* kibi */
 216             overflow = bkm_scale_by_power (&tmp, base, 1);
 217             break;
 218 
 219         case 'M':              /* mega or mebi */
 220         case 'm':              /* 'm' is undocumented; for compatibility only */
 221             overflow = bkm_scale_by_power (&tmp, base, 2);
 222             break;
 223 
 224         case 'P':              /* peta or pebi */
 225             overflow = bkm_scale_by_power (&tmp, base, 5);
 226             break;
 227 
 228         case 'Q':              /* quetta or 2**100 */
 229             overflow = bkm_scale_by_power (&tmp, base, 10);
 230             break;
 231 
 232         case 'R':              /* ronna or 2**90 */
 233             overflow = bkm_scale_by_power (&tmp, base, 9);
 234             break;
 235 
 236         case 'T':              /* tera or tebi */
 237         case 't':              /* 't' is undocumented; for compatibility only */
 238             overflow = bkm_scale_by_power (&tmp, base, 4);
 239             break;
 240 
 241         case 'w':
 242             overflow = bkm_scale (&tmp, 2);
 243             break;
 244 
 245         case 'Y':              /* yotta or 2**80 */
 246             overflow = bkm_scale_by_power (&tmp, base, 8);
 247             break;
 248 
 249         case 'Z':              /* zetta or 2**70 */
 250             overflow = bkm_scale_by_power (&tmp, base, 7);
 251             break;
 252 
 253         default:
 254             *val = tmp;
 255             return err | LONGINT_INVALID_SUFFIX_CHAR;
 256         }
 257 
 258         err |= overflow;
 259         *p += suffixes;
 260         if (**p != '\0')
 261             err |= LONGINT_INVALID_SUFFIX_CHAR;
 262     }
 263 
 264     *val = tmp;
 265     return err;
 266 }
 267 
 268 /* --------------------------------------------------------------------------------------------- */

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