summaryrefslogtreecommitdiffstats
path: root/mdk-stage1/dietlibc/lib/strtol.c
blob: 2624aca0e55ce04cade84762dc79be0c0726cb20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <ctype.h>
#include "dietfeatures.h"
#include <errno.h>
#include <limits.h>
#include <stdlib.h>

#define ABS_LONG_MIN 2147483648UL
long int strtol(const char *nptr, char **endptr, int base)
{
  int neg=0;
  unsigned long int v;

  while(isspace(*nptr)) nptr++;

  if (*nptr == '-') { neg=-1; ++nptr; }
  v=strtoul(nptr,endptr,base);
  if (v>=ABS_LONG_MIN) {
    if (v==ABS_LONG_MIN && neg) {
      errno=0;
      return v;
    }
    errno=ERANGE;
    return (neg?LONG_MIN:LONG_MAX);
  }
  return (neg?-v:v);
}