
/* strtol.c */

#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>

unsigned long __strtoul (const char *nptr, char **endptr, int base);

unsigned long
strtoul (const char *nptr, char **endptr, int base)
{
  const char *p, *q;
  unsigned long value;
  int negative;

  p = nptr;

  while (isspace (*p))
    p++;

  negative = 0;
  if (*p == '-')
    {
      negative = 1;
      p++;
    }
  else
    if (*p == '+')
      p++;

  value = __strtoul (p, (char **) &q, base);

  if (negative && value != 0)
    {
      value = ULONG_MAX;
      errno = ERANGE;
    }

  if (endptr != NULL)
    *endptr = (char *) (p != q ? q : nptr);

  return value;
}
