mirror of
https://github.com/vivier/EMILE.git
synced 2025-01-18 06:31:23 +00:00
add strtol
This commit is contained in:
parent
d76f74e1eb
commit
871934a0f6
@ -12,7 +12,7 @@ LIBRARY = libunix.a
|
||||
SOURCES = divsi3.S modsi3.S mulsi3.S udivsi3.S umodsi3.S free.c malloc.c \
|
||||
memcpy.c memset.c printf.c putchar.c puts.c read.c sprintf.c \
|
||||
strcpy.c strlen.c strncpy.c vsprintf.c write.c strcmp.c \
|
||||
strncmp.c
|
||||
strncmp.c strtol.c
|
||||
|
||||
HEADERS =
|
||||
|
||||
|
67
libunix/strtol.c
Normal file
67
libunix/strtol.c
Normal file
@ -0,0 +1,67 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
static int my_isspace(int c)
|
||||
{
|
||||
return (c == ' ') || (c == '\f') || (c == '\n') ||
|
||||
(c == '\r') || (c == '\t') || (c == '\v');
|
||||
}
|
||||
|
||||
long int __strtol_internal (const char *nptr, char **endptr, int base, int group)
|
||||
{
|
||||
long result = 0;
|
||||
int sign;
|
||||
|
||||
while(my_isspace(*nptr))
|
||||
nptr++;
|
||||
|
||||
if (*nptr == '-')
|
||||
{
|
||||
nptr++;
|
||||
sign = -1;
|
||||
} else if (*nptr == '+')
|
||||
{
|
||||
nptr++;
|
||||
sign = 1;
|
||||
} else
|
||||
sign = 1;
|
||||
|
||||
if (base == 0)
|
||||
{
|
||||
if (*nptr == '0')
|
||||
{
|
||||
nptr++;
|
||||
if (*nptr == 'x')
|
||||
{
|
||||
base = 16;
|
||||
nptr++;
|
||||
} else
|
||||
base = 8;
|
||||
}
|
||||
else
|
||||
base = 10;
|
||||
}
|
||||
|
||||
while (*nptr)
|
||||
{
|
||||
int digit;
|
||||
|
||||
if ( (*nptr >= '0') && (*nptr <= '9') )
|
||||
digit = *nptr - '0';
|
||||
else if ( (*nptr >= 'a') && (*nptr <= 'z') )
|
||||
digit = *nptr - 'a' + 10;
|
||||
else if ( (*nptr >= 'A') && (*nptr <= 'Z') )
|
||||
digit = *nptr - 'A' + 10;
|
||||
else
|
||||
break;
|
||||
|
||||
if (digit >= base)
|
||||
break;
|
||||
|
||||
result = (result * base) + digit;
|
||||
|
||||
nptr++;
|
||||
}
|
||||
if (endptr)
|
||||
*endptr = (char*)nptr;
|
||||
return sign * result;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user