Add atoi to stdlib

This commit is contained in:
Ali Mashtizadeh 2023-08-20 19:05:08 -04:00
parent 2718379e6d
commit fa2c3487af
3 changed files with 21 additions and 0 deletions

View File

@ -16,5 +16,7 @@ void *calloc(size_t num, size_t sz);
void *malloc(size_t sz);
void free(void *buf);
int atoi(const char *nptr);
#endif /* __STDLIB_H__ */

View File

@ -18,6 +18,7 @@ src_common = [
"process.c",
"posix/mman.c",
"posix/pthread.c",
"stdlib.c",
"string.c",
"syscall.c",
"time.c",

18
lib/libc/stdlib.c Normal file
View File

@ -0,0 +1,18 @@
int
atoi(const char *nptr)
{
int i = 0;
int val = 0;
while (nptr[i] != '\0') {
if (nptr[i] >= '0' && nptr[i] <= '9') {
val = val * 10 + (int)(nptr[i] - '0');
} else {
return 0;
}
}
return val;
}