strtol.c
sys/cdefs.h
ubixos/types.h
sys/_types.h
src/sys/lib/strtol.c
sys/cdefs.h
LONG_MAX
0x7fffffffL
strtol
LONG_MIN
(-0x7fffffffL - 1)
strtol
long long strtol (const char *__restrict nptr, char **__restrict endptr, int base) strtol const char *__restrict nptr char **__restrict endptr int base LONG_MAX LONG_MIN NULL inet_aton

    #include<sys/cdefs.h>
    //#include<limits.h>
    //#include<ctype.h>
    //#include<stdlib.h>

    #defineLONG_MIN(-0x7fffffffL-1)
    #defineLONG_MAX0x7fffffffL

    long
    strtol(constchar*__restrictnptr,char**__restrictendptr,intbase)
    {
    constchar*s;
    unsignedlongacc;
    charc=0x0;/*toremovewarning*/
    unsignedlongcutoff;
    intneg,any,cutlim;

    /*
    *Skipwhitespaceandpickupleading+/-signifany.
    *Ifbaseis0,allow0xforhexand0foroctal,else
    *assumedecimal;ifbaseisalready16,allow0x.
    */
    s=nptr;
    /*
    do{
    c=*s++;
    }while(isspace((unsignedchar)c));
    */
    if(c=='-'){
    neg=1;
    c=*s++;
    }else{
    neg=0;
    if(c=='+')
    c=*s++;
    }
    if((base==0||base==16)&&
    c=='0'&&(*s=='x'||*s=='X')){
    c=s[1];
    s+=2;
    base=16;
    }
    if(base==0)
    base=c=='0'?8:10;
    acc=any=0;
    if(base<2||base>36)
    gotonoconv;

    /*
    *Computethecutoffvaluebetweenlegalnumbersandillegal
    *numbers.Thatisthelargestlegalvalue,dividedbythe
    *base.Aninputnumberthatisgreaterthanthisvalue,if
    *followedbyalegalinputcharacter,istoobig.Onethat
    *isequaltothisvaluemaybevalidornot;thelimit
    *betweenvalidandinvalidnumbersisthenbasedonthelast
    *digit.Forinstance,iftherangeforlongsis
    *[-2147483648..2147483647]andtheinputbaseis10,
    *cutoffwillbesetto214748364andcutlimtoeither
    *7(neg==0)or8(neg==1),meaningthatifwehaveaccumulated
    *avalue>214748364,orequalbutthenextdigitis>7(or8),
    *thenumberistoobig,andwewillreturnarangeerror.
    *
    *Set'any'ifany`digits'consumed;makeitnegativetoindicate
    *overflow.
    */
    cutoff=neg?(unsignedlong)-(LONG_MIN+LONG_MAX)+LONG_MAX
    :LONG_MAX;
    cutlim=cutoff%base;
    cutoff/=base;
    for(;;c=*s++){
    if(c>='0'&&c<='9')
    c-='0';
    elseif(c>='A'&&c<='Z')
    c-='A'-10;
    elseif(c>='a'&&c<='z')
    c-='a'-10;
    else
    break;
    if(c>=base)
    break;
    if(any<0||acc>cutoff||(acc==cutoff&&c>cutlim))
    any=-1;
    else{
    any=1;
    acc*=base;
    acc+=c;
    }
    }
    if(any<0){
    acc=neg?LONG_MIN:LONG_MAX;
    //errno=ERANGE;
    }elseif(!any){
    noconv:
    //errno=EINVAL;
    cutoff=0x0;//UBU
    }elseif(neg)
    acc=-acc;
    if(endptr!=NULL)
    *endptr=(char*)(any?s-1:nptr);
    return(acc);
    }