home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / libnix-0.8-src.lha / libnix-0.8 / sources / nix / stdlib / strtol.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-12  |  572 b   |  34 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <errno.h>
  4. #include <limits.h>
  5. #include <stdlib.h>
  6.  
  7. signed long strtol(const char *nptr,char **endptr,int base)
  8. { const char *p=nptr;
  9.   char *q;
  10.   unsigned long r;
  11.   while(isspace(*p))
  12.     p++;
  13.   r=strtoul(p,&q,base);
  14.   if(endptr!=NULL)
  15.   { if(q==p)
  16.       *endptr=(char *)nptr;
  17.     else
  18.       *endptr=q;
  19.   }
  20.   if(*p=='-')
  21.   { if((signed long)r>0)
  22.     { errno=ERANGE;
  23.       return LONG_MIN; }
  24.     else
  25.       return r;
  26.   }else
  27.   { if((signed long)r<0)
  28.     { errno=ERANGE;
  29.       return LONG_MAX; }
  30.     else
  31.       return r;
  32.   }
  33. }
  34.