?login_element?

Subversion Repositories NedoOS

Rev

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

  1. /*---------------------------------------------------------------------
  2.    strtol() - convert a string to a 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 <limits.h>
  34. #include <errno.h>
  35.  
  36. #pragma disable_warning 196
  37.  
  38. long int strtol(const char *nptr, char **endptr, int base)
  39. {
  40.   const char *ptr = nptr;
  41.   const char *rptr;
  42.   unsigned long int u;
  43.   bool neg;
  44.  
  45.   while (isblank (*ptr))
  46.     ptr++;
  47.  
  48.   neg = (*ptr == '-');
  49.  
  50.   if (*ptr == '-')
  51.     {
  52.       neg = true;
  53.       ptr++;
  54.     }
  55.   else
  56.     neg = false;
  57.  
  58.   // strtoul() would accept leading blanks or signs (that might come after '-' handled above)
  59.   if (neg && (isblank (*ptr) || *ptr == '-' || *ptr == '+'))
  60.     {
  61.       if (endptr)
  62.         *endptr = nptr;
  63.       return (0);
  64.     }
  65.  
  66.   u = strtoul(ptr, &rptr, base);
  67.  
  68.   // Check for conversion error
  69.   if (rptr == ptr)
  70.     {
  71.       if (endptr)
  72.         *endptr = nptr;
  73.       return (0);
  74.     }
  75.  
  76.   if (endptr)
  77.     *endptr = rptr;
  78.  
  79.   // Check for range error
  80.   if (!neg && u > LONG_MAX)
  81.     {
  82.       errno = ERANGE;
  83.       return (LONG_MAX);
  84.     }
  85.   else if (neg && u > -LONG_MIN)
  86.     {
  87.       errno = ERANGE;
  88.       return (LONG_MIN);
  89.     }
  90.  
  91.   return (neg ? -u : u);
  92. }
  93.  
  94.