File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / lrzsz / lib / xstrtol.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Thu Oct 24 15:49:50 2019 UTC (4 years, 8 months ago) by misho
Branches: lrzsz, MAIN
CVS tags: v0_12_20p5, HEAD
lrzsz ver 0.12.20

    1: /* A more useful interface to strtol.
    2:    Copyright (C) 1995, 1996 Free Software Foundation, Inc.
    3: 
    4:    This program is free software; you can redistribute it and/or modify
    5:    it under the terms of the GNU General Public License as published by
    6:    the Free Software Foundation; either version 2, or (at your option)
    7:    any later version.
    8: 
    9:    This program is distributed in the hope that it will be useful,
   10:    but WITHOUT ANY WARRANTY; without even the implied warranty of
   11:    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12:    GNU General Public License for more details.
   13: 
   14:    You should have received a copy of the GNU General Public License
   15:    along with this program; if not, write to the Free Software Foundation,
   16:    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
   17: 
   18: /* Jim Meyering (meyering@na-net.ornl.gov) */
   19: 
   20: #ifdef HAVE_CONFIG_H
   21: #include <config.h>
   22: #endif
   23: 
   24: #ifdef STDC_HEADERS
   25: #include <stdlib.h>
   26: #endif
   27: 
   28: #ifdef HAVE_STRING_H
   29: # include <string.h>
   30: #else
   31: # include <strings.h>
   32: # ifndef strchr
   33: #  define strchr index
   34: # endif
   35: #endif
   36: 
   37: #define NDEBUG
   38: #include <assert.h>
   39: 
   40: #include <errno.h>
   41: #ifndef errno
   42: extern int errno;
   43: #endif
   44: 
   45: #if HAVE_LIMITS_H
   46: # include <limits.h>
   47: #endif
   48: 
   49: #ifndef ULONG_MAX
   50: # define ULONG_MAX ((unsigned long) ~(unsigned long) 0)
   51: #endif
   52: 
   53: #ifndef LONG_MAX
   54: # define LONG_MAX ((long int) (ULONG_MAX >> 1))
   55: #endif
   56: 
   57: #include "xstrtol.h"
   58: 
   59: #define BKM_SCALE(x, scale_factor, error_return)			\
   60:       do								\
   61: 	{								\
   62: 	  if ((x) > (double) __ZLONG_MAX / (scale_factor))		\
   63: 	    return (error_return);					\
   64: 	  (x) *= (scale_factor);					\
   65: 	}								\
   66:       while (0)
   67: 
   68: __unsigned long int __strtol ();
   69: 
   70: /* FIXME: comment.  */
   71: 
   72: strtol_error
   73: __xstrtol (s, ptr, base, val, valid_suffixes)
   74:      const char *s;
   75:      char **ptr;
   76:      int base;
   77:      __unsigned long int *val;
   78:      const char *valid_suffixes;
   79: {
   80:   char *t_ptr;
   81:   char **p;
   82:   __unsigned long int tmp;
   83: 
   84:   assert (0 <= base && base <= 36);
   85: 
   86:   p = (ptr ? ptr : &t_ptr);
   87: 
   88:   errno = 0;
   89:   tmp = __strtol (s, p, base);
   90:   if (errno != 0)
   91:     return LONGINT_OVERFLOW;
   92:   if (*p == s)
   93:     return LONGINT_INVALID;
   94:   if (!valid_suffixes)
   95:     {
   96:       if (**p == '\0')
   97: 	{
   98: 	  *val = tmp;
   99: 	  return LONGINT_OK;
  100: 	}
  101:       else
  102: 	return LONGINT_INVALID_SUFFIX_CHAR;
  103:     }
  104: 
  105:   if (**p != '\0' && strchr (valid_suffixes, **p))
  106:     {
  107:       switch (**p)
  108: 	{
  109: 	case 'b':
  110: 	  BKM_SCALE (tmp, 512, LONGINT_OVERFLOW);
  111: 	  ++(*p);
  112: 	  break;
  113: 
  114: 	case 'c':
  115: 	  ++(*p);
  116: 	  break;
  117: 
  118: 	case 'B':
  119: 	case 'k':
  120: 	  BKM_SCALE (tmp, 1024, LONGINT_OVERFLOW);
  121: 	  ++(*p);
  122: 	  break;
  123: 
  124: 	case 'm':
  125: 	  BKM_SCALE (tmp, 1024 * 1024, LONGINT_OVERFLOW);
  126: 	  ++(*p);
  127: 	  break;
  128: 
  129: 	case 'w':
  130: 	  BKM_SCALE (tmp, 2, LONGINT_OVERFLOW);
  131: 	  ++(*p);
  132: 	  break;
  133: 
  134: 	default:
  135: 	  return LONGINT_INVALID_SUFFIX_CHAR;
  136: 	  break;
  137: 	}
  138:     }
  139: 
  140:   *val = tmp;
  141:   return LONGINT_OK;
  142: }
  143: 
  144: #ifdef TESTING_XSTRTO
  145: 
  146: #include <stdio.h>
  147: #include "error.h"
  148: 
  149: char *program_name;
  150: 
  151: int
  152: main (int argc, char** argv)
  153: {
  154:   strtol_error s_err;
  155:   int i;
  156: 
  157:   program_name = argv[0];
  158:   for (i=1; i<argc; i++)
  159:     {
  160:       char *p;
  161:       __unsigned long int val;
  162: 
  163:       s_err = __xstrtol (argv[i], &p, 0, &val, "bckmw");
  164:       if (s_err == LONGINT_OK)
  165: 	{
  166: 	  printf ("%s->%lu (%s)\n", argv[i], val, p);
  167: 	}
  168:       else
  169: 	{
  170: 	  STRTOL_FATAL_ERROR (argv[i], "arg", s_err);
  171: 	}
  172:     }
  173:   exit (0);
  174: }
  175: #endif /* TESTING_XSTRTO */

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>