home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / kernex32.zip / mwdd_src.zip / 32bits / ext2-os2 / util / STRTOUL.C < prev    next >
C/C++ Source or Header  |  1997-03-16  |  3KB  |  130 lines

  1. //
  2. // $Header: d:\\32bits\\ext2-os2\\util\\rcs\\STRTOUL.C,v 1.2 1997/03/16 13:21:33 Willm Exp $
  3. //
  4.  
  5. // 32 bits OS/2 device driver and IFS support. Provides 32 bits kernel 
  6. // services (DevHelp) and utility functions to 32 bits OS/2 ring 0 code 
  7. // (device drivers and installable file system drivers).
  8. // Copyright (C) 1995, 1996, 1997  Matthieu WILLM (willm@ibm.net)
  9. //
  10. // This program is free software; you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation; either version 2 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program; if not, write to the Free Software
  22. // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  
  24. /* From:                                                             */
  25. /* strtoul.c (emx+gcc) -- Copyright (c) 1990-1995 by Eberhard Mattes */
  26.  
  27. #ifndef MWDD32
  28. #include <stdlib.h>
  29. #include <limits.h>
  30. #include <ctype.h>
  31. #include <errno.h>
  32. #else
  33. #define NULL 0
  34. #include <os2/types.h>
  35. #include <os2/ctype.h>
  36. #include <limits.h>
  37. #endif
  38.  
  39. #ifndef MWDD32
  40. unsigned long strtoul (const char *string, char **end_ptr, int radix)
  41. #else
  42. unsigned long DH32ENTRY __mwdd32_strtoul (const char *string, char **end_ptr, int radix)
  43. #endif
  44. {
  45.   const unsigned char *s;
  46.   char neg;
  47.   unsigned long result;
  48.  
  49.   s = string;
  50.   while (isspace (*s))
  51.     ++s;
  52.  
  53.   neg = 0;
  54.   if (*s == '-')
  55.     {
  56.       neg = 1; ++s;
  57.     }
  58.   else if (*s == '+')
  59.     ++s;
  60.  
  61.   if ((radix == 0 || radix == 16) && s[0] == '0'
  62.       && (s[1] == 'x' || s[1] == 'X'))
  63.     {
  64.       radix = 16; s += 2;
  65.     }
  66.   if (radix == 0)
  67.     radix = (s[0] == '0' ? 8 : 10);
  68.  
  69.   result = 0;                   /* Keep the compiler happy */
  70.   if (radix >= 2 && radix <= 36)
  71.     {
  72.       unsigned long n, max1, max2;
  73.       enum {no_number, ok, overflow} state;
  74.       unsigned char c;
  75.  
  76.       max1 = ULONG_MAX / radix;
  77.       max2 = ULONG_MAX - max1 * radix;
  78.       n = 0; state = no_number;
  79.       for (;;)
  80.         {
  81.           c = *s;
  82.           if (c >= '0' && c <= '9')
  83.             c = c - '0';
  84.           else if (c >= 'A' && c <= 'Z')
  85.             c = c - 'A' + 10;
  86.           else if (c >= 'a' && c <= 'z')
  87.             c = c - 'a' + 10;
  88.           else
  89.             break;
  90.           if (c >= radix)
  91.             break;
  92.           if (n >= max1 && (n > max1 || (unsigned long)c > max2))
  93.             state = overflow;
  94.           if (state != overflow)
  95.             {
  96.               n = n * radix + (unsigned long)c;
  97.               state = ok;
  98.             }
  99.           ++s;
  100.         }
  101.       switch (state)
  102.         {
  103.         case no_number:
  104.           result = 0;
  105.           s = string;
  106.           /* Don't set errno!? */
  107.           break;
  108.         case ok:
  109.           result = (neg ? -n : n);
  110.           break;
  111.         case overflow:
  112.           result = ULONG_MAX;
  113. #ifndef MWDD32
  114.           errno = ERANGE;
  115. #endif
  116.           break;
  117.         }
  118.     }
  119.   else
  120.     {
  121.       result = 0;
  122. #ifndef MWDD32
  123.       errno = EDOM;
  124. #endif
  125.     }
  126.   if (end_ptr != NULL)
  127.     *end_ptr = (char *)s;
  128.   return result;
  129. }
  130.