Main: split libc into separate source files

This commit is contained in:
Alex Kotov 2022-06-08 06:25:57 +03:00
parent 044e3a24c2
commit 055235b32e
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
4 changed files with 40 additions and 27 deletions

View File

@ -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

15
libc/src/ctype.c Normal file
View File

@ -0,0 +1,15 @@
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <ctype.h>
int isdigit(const int c)
{
return (unsigned)c - '0' < 10;
}
int isspace(const int c)
{
return c == ' ' || (unsigned)c - '\t' < 5;
}

20
libc/src/stdlib.c Normal file
View File

@ -0,0 +1,20 @@
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h>
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;
}

View File

@ -2,34 +2,9 @@
#include "config.h"
#endif
#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
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) {