?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /*---------------------------------------------------------------------
  2.    strtoul() - convert a string to a unsigned long int and return it
  3.  
  4.    Copyright (C) 2018, Philipp Klaus Krause . krauseph@informatik.uni-freiburg.de
  5.  
  6.    This library is free software; you can redistribute it and/or modify it
  7.    under the terms of the GNU General Public License as published by the
  8.    Free Software Foundation; either version 2, or (at your option) any
  9.    later version.
  10.  
  11.    This library 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 library; see the file COPYING. If not, write to the
  18.    Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
  19.    MA 02110-1301, USA.
  20.  
  21.    As a special exception, if you link this library with other files,
  22.    some of which are compiled with SDCC, to produce an executable,
  23.    this library does not by itself cause the resulting executable to
  24.    be covered by the GNU General Public License. This exception does
  25.    not however invalidate any other reasons why the executable file
  26.    might be covered by the GNU General Public License.
  27. -------------------------------------------------------------------------*/
  28.  
  29. #include <stdlib.h>
  30.  
  31. #include <stdbool.h>
  32. #include <ctype.h>
  33. #include <string.h>
  34. #include <limits.h>
  35. #include <errno.h>
  36.  
  37. static signed char _isdigit(const char c, unsigned char base)
  38. {
  39.   unsigned char v;
  40.  
  41.   if (c >= '0' && c <= '9')
  42.     v = c - '0';
  43.   else if (c >= 'a' && c <='z')
  44.     v = c - 'a' + 10;
  45.   else if (c >= 'A' && c <='Z')
  46.     v = c - 'A' + 10;
  47.   else
  48.     return (-1);
  49.  
  50.   if (v >= base)
  51.     return (-1);
  52.  
  53.   return (v);
  54. }
  55.  
  56. unsigned long int strtoul(const char *nptr, char **endptr, int base)
  57. {
  58.   const char *ptr = nptr;
  59.   unsigned long int ret;
  60.   bool range_error = false;
  61.   bool neg = false;
  62.  
  63.   while (isblank (*ptr))
  64.     ptr++;
  65.  
  66.   // Handle sign.
  67.   switch(*ptr)
  68.     {
  69.     case '-':
  70.       neg = true;
  71.     case '+':
  72.       ptr++;
  73.     }
  74.  
  75.   // base not specified.
  76.   if (!base)
  77.     {
  78.       if (!strncmp (ptr, "0x", 2) || !strncmp (ptr, "0X", 2))
  79.         {
  80.           base = 16;
  81.           ptr += 2;
  82.         }
  83.       else if (*ptr == '0')
  84.         {
  85.           base = 8;
  86.           ptr++;
  87.         }
  88.       else
  89.         base = 10;
  90.     }
  91.   // Handle optional hex prefix.
  92.   else if (base == 16 && (!strncmp (ptr, "0x", 2) || !strncmp (ptr, "0X", 2)))
  93.     ptr += 2;
  94.  
  95.  
  96.   // Empty sequence conversion error
  97.   if (_isdigit (*ptr, base) < 0)
  98.     {
  99.       if (endptr)
  100.         *endptr = (char*)nptr;
  101.       return (0);
  102.     }
  103.  
  104.   for (ret = 0;; ptr++)
  105.     {
  106.       unsigned long int oldret;
  107.       signed char digit = _isdigit (*ptr, base);
  108.  
  109.       if (digit < 0)
  110.         break;
  111.  
  112.       oldret = ret;
  113.       ret *= base;
  114.       if (ret < oldret)
  115.         range_error = true;
  116.  
  117.       ret += (unsigned char)digit;
  118.     }
  119.  
  120.   if (endptr)
  121.     *endptr = (char*)ptr;
  122.  
  123.   if (range_error)
  124.     {
  125.       errno = ERANGE;
  126.       return (ULONG_MAX);
  127.     }
  128.  
  129.   return (neg ? -ret : ret);
  130. }
  131.  
  132.