diff --git a/Makefile.am b/Makefile.am index ed15830..e5dd203 100644 --- a/Makefile.am +++ b/Makefile.am @@ -70,7 +70,10 @@ if WITH_FRAMEBUFFER libkernaux_a_SOURCES += src/framebuffer.c endif if WITH_LIBC -libkernaux_a_SOURCES += libc/src/main.c +libkernaux_a_SOURCES += \ + libc/src/ctype.c \ + libc/src/stdlib.c \ + libc/src/string.c endif if WITH_MBR libkernaux_a_SOURCES += src/mbr.c diff --git a/libc/src/ctype.c b/libc/src/ctype.c new file mode 100644 index 0000000..eb2eb99 --- /dev/null +++ b/libc/src/ctype.c @@ -0,0 +1,15 @@ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include + +int isdigit(const int c) +{ + return (unsigned)c - '0' < 10; +} + +int isspace(const int c) +{ + return c == ' ' || (unsigned)c - '\t' < 5; +} diff --git a/libc/src/stdlib.c b/libc/src/stdlib.c new file mode 100644 index 0000000..c97ee6b --- /dev/null +++ b/libc/src/stdlib.c @@ -0,0 +1,20 @@ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include + +int atoi(const char *str) +{ + while (isspace(*str)) ++str; + bool is_negative = false; + switch (*str) { + case '-': is_negative = true; // fall through + case '+': ++str; + } + int result = 0; + while (isdigit(*str)) result = 10 * result - (*str++ - '0'); + return is_negative ? result : -result; +} diff --git a/libc/src/main.c b/libc/src/string.c similarity index 79% rename from libc/src/main.c rename to libc/src/string.c index 66ec81b..2e647ef 100644 --- a/libc/src/main.c +++ b/libc/src/string.c @@ -2,34 +2,9 @@ #include "config.h" #endif -#include -#include -#include +#include #include -int atoi(const char *str) -{ - while (isspace(*str)) ++str; - bool is_negative = false; - switch (*str) { - case '-': is_negative = true; // fall through - case '+': ++str; - } - int result = 0; - while (isdigit(*str)) result = 10 * result - (*str++ - '0'); - return is_negative ? result : -result; -} - -int isdigit(const int c) -{ - return (unsigned)c - '0' < 10; -} - -int isspace(const int c) -{ - return c == ' ' || (unsigned)c - '\t' < 5; -} - int memcmp(const void *s1, const void *s2, size_t n) { for (const unsigned char *p1 = s1, *p2 = s2; n--; ++p1, ++p2) {