⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stdlib.c

📁 c语言编的网络操作系统。具备网络操作系统基本功能。
💻 C
字号:
/*
nexOS: stdlib.c - mini libc standard lib functions 
Copyright 2004 nexOS development team

This file is part of nexOS.

nexOS is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

nexOS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with nexOS; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

#include <stdlib.h>
#include <types.h>

const char validchars[][23] = {
    "",
    "",
    "01",
    "",
    "",
    "",
    "",
    "",
    "01234567",
    "",
    "0123456789",
    "",
    "",
    "",
    "",
    "",
    "0123456789ABCDEFabcdef"
};    
BOOL isvalidchar(char c, int base)
{
    int i;
    BOOL ret;
    for( i=0; validchars[base][i] != 0; i++)
        ret |= validchars[base][i] == c;
    return ret;
}    
int pow(int n, int e)
{
    if( e>0 ) return n * pow(n,e-1);
    else return 1;
}    

long strtol(const char *s, char **endptr, int base)
{
    BOOL running;
    int pos = 0, maxpos=0, i=0;
    long val = 0;
    long numlen = 0;
    
    /* autodetect base */
    if( base <= 0 )
    {
        if( *s == '0' )
        {
            if( *(s+1) == 'x' || *(s+1) == 'X' )
            {
                base = 16;
                s += 2;
            }
            else
            {
                base = 8;
                s += 1;
            }    
        }
        else
        {
            base = 10;
        }
    }
    /* detect length of number */
    switch (base)
    {
        case 2: maxpos = 32; break;
        case 8: maxpos = 11; break;
        case 10: maxpos = 10; break;
        case 16: maxpos = 8; break;
        default: maxpos = 32; break;
    }
    const char *sc = s;
    for( ;*sc != 0 && isvalidchar(*sc,base); sc++, numlen++ );
    numlen = (numlen > maxpos) ? (maxpos) : (numlen);
    
    /* revert string */
    char revstring[33];
    for( i=0; i<numlen; i++ )
    {
        revstring[i] = s[numlen-i];
    }
    
    for( running=TRUE; running && pos<numlen; pos++)
    {
        if( (running = revstring[pos]!=0) )
        {
            switch (base)
            {
                case 2:
                case 8:
                case 10:
                    if( isvalidchar(revstring[pos],base) )
                    {
                        val += revstring[pos] * pow(base,pos);
                    }
                    else
                    {
                        running = FALSE;
                    }
                    break;
                case 16:
                    if( pos > 4 )
                    {
                        running = FALSE;
                    }    
                    else if( ((revstring[pos]>'0')&&(revstring[pos]<'9')) )
                    {
                        val |= ((revstring[pos])-'0')<<(pos<<2);
                    }
                    else if( ((revstring[pos]>'A')&&(revstring[pos]<'F')) )
                    {
                        val |= ((revstring[pos])-'A'+10)<<(pos<<2);                        
                    }
                    else if( ((revstring[pos]>'a')&&(revstring[pos]<'f')) )
                    {
                        val |= ((revstring[pos])-'a'+10)<<(pos<<2);  
                    }
                    else
                    {
                        running = FALSE;
                    }
                    break;
                default:
                    break;
            }    
        }
    }
    if( endptr )
    {
        *endptr = (char*)s+numlen;
    }    
    return val;
}

volatile unsigned int nxi_rndseed;
void srand(unsigned int seed)
{
     nxi_rndseed = seed%0xFFFFFFFF;
}

int rand(void)
{
    nxi_rndseed = nxi_rndseed % 0xFFFFFFFF + 1;
    return 0x9d2c5680%(nxi_rndseed%0xFFFF);
}

double sqrt(double n)
{
    double m, mold;
    /* if we got the same value in the last operation we get the maximum accuracy */
    for(m=1, mold=0; m != mold; mold = m)
        m = (m+(n/m))/2;
    return m;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -