Revert "Main: remove libc"

This reverts commit db65040e73.
This commit is contained in:
Alex Kotov 2022-06-07 08:19:14 +03:00
parent db65040e73
commit b1cc8f386c
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
20 changed files with 264 additions and 28 deletions

View File

@ -1,7 +1,3 @@
2022-06-07 Alex Kotov <kotovalexarian@gmail.com>
* include/kernaux/libc.h: Removed
2022-06-06 Alex Kotov <kotovalexarian@gmail.com>
* include/kernaux/libc.h: Add more functions

View File

@ -37,7 +37,10 @@ noinst_PROGRAMS = $(TESTS)
# libkernaux.a #
################
libkernaux_a_SOURCES = src/assert.c
libkernaux_a_SOURCES = \
src/assert.c \
src/libc.c
if ASM_I386
libkernaux_a_SOURCES += src/asm/i386.S
endif

View File

@ -22,7 +22,7 @@ Table of contents
* [Tips](#tips)
* [Installation](#installation)
* [Development](#development)
* [Freestanding](#freestanding)
* [Cross](#cross)
* [Architectures](#architectures)
@ -65,6 +65,7 @@ zero). Work-in-progress APIs can change at any time.
* Code from [https://github.com/mpaland/printf](https://github.com/mpaland/printf). Thank you!
* [Example](/examples/printf_fmt.c)
* Usual functions
* [libc replacement](/include/kernaux/libc.h) (*stable since* **0.1.0**)
* [itoa/ftoa replacement](/include/kernaux/ntoa.h) (*stable since* **0.1.0**, *non-breaking since* **?.?.?**)
* [Example](/examples/ntoa.c)
* [printf replacement](/include/kernaux/printf.h) (*stable since* **0.1.0**)
@ -95,6 +96,25 @@ stable options.
* `--enable-tests-python` - enable tests that require Python 3 with YAML and
Jinja2
#### Packages
* `--with-libc-all` - provides the replacement for some standard C functions.
Useful in freestanding environment, where no libc is present. You can also
separately include or exclude components:
* `--with[out]-libc-atoi`
* `--with[out]-libc-isdigit`
* `--with[out]-libc-isspace`
* `--with[out]-libc-memcpy`
* `--with[out]-libc-memmove`
* `--with[out]-libc-memset`
* `--with[out]-libc-strcmp`
* `--with[out]-libc-strcpy`
* `--with[out]-libc-strlen`
* `--with[out]-libc-strncmp`
* `--with[out]-libc-strncpy`
* `--with[out]-libc-strnlen`
* `--with[out]-libc-strstr`
### Default options
#### Features
@ -138,9 +158,7 @@ make
You can test with `make check`.
### Freestanding
Some libc functions are needed. Use [libfsc](https://github.com/tailix/libfsc).
### Cross
Create configuration script with `./autogen.sh` (if present).
@ -152,6 +170,7 @@ without it in `$PATH`:
./configure \
--host='i386-elf' \
--enable-freestanding \
--with-libc-all \
AR="$(which i386-elf-ar)" \
CC="$(which i386-elf-gcc)" \
RANLIB="$(which i386-elf-ranlib)"

View File

@ -14,4 +14,4 @@ export AR="$CROSS-ar"
export CC="$CROSS-gcc"
export RANLIB="$CROSS-ranlib"
"$REPO/configure" --host="$HOST" --prefix="$PREFIX" --enable-freestanding
"$REPO/configure" --host="$HOST" --prefix="$PREFIX" --enable-freestanding --with-libc-all

View File

@ -14,4 +14,4 @@ export AR="$CROSS-ar"
export CC="$CROSS-gcc"
export RANLIB="$CROSS-ranlib"
"$REPO/configure" --host="$HOST" --prefix="$PREFIX" --enable-freestanding
"$REPO/configure" --host="$HOST" --prefix="$PREFIX" --enable-freestanding --with-libc-all

View File

@ -16,4 +16,4 @@ export RANLIB="$CROSS-ranlib"
export CFLAGS='-mabi=sysv -mcmodel=kernel -mno-80387 -mno-red-zone'
"$REPO/configure" --host="$HOST" --prefix="$PREFIX" --enable-freestanding
"$REPO/configure" --host="$HOST" --prefix="$PREFIX" --enable-freestanding --with-libc-all

View File

@ -14,4 +14,4 @@ export AR="$TARGET-ar"
export CC="$TARGET-gcc"
export RANLIB="$TARGET-ranlib"
"$REPO/configure" --host="$HOST" --prefix="$PREFIX" --enable-freestanding
"$REPO/configure" --host="$HOST" --prefix="$PREFIX" --enable-freestanding --with-libc-all

View File

@ -14,4 +14,4 @@ export AR="$TARGET-ar"
export CC="$TARGET-gcc"
export RANLIB="$TARGET-ranlib"
"$REPO/configure" --host="$HOST" --prefix="$PREFIX" --enable-freestanding
"$REPO/configure" --host="$HOST" --prefix="$PREFIX" --enable-freestanding --with-libc-all

View File

@ -16,4 +16,4 @@ export RANLIB="$TARGET-ranlib"
export CFLAGS='-mabi=sysv -mcmodel=kernel -mno-80387 -mno-red-zone'
"$REPO/configure" --host="$HOST" --prefix="$PREFIX" --enable-freestanding
"$REPO/configure" --host="$HOST" --prefix="$PREFIX" --enable-freestanding --with-libc-all

View File

@ -41,6 +41,22 @@ AC_ARG_WITH( [printf], AS_HELP_STRING([--without-printf], [without p
AC_ARG_WITH( [printf-fmt], AS_HELP_STRING([--without-printf-fmt], [without printf format parser]))
AC_ARG_WITH( [units], AS_HELP_STRING([--without-units], [without measurement units utils]))
dnl Packages (disabled by default)
AC_ARG_WITH( [libc-all], AS_HELP_STRING([--with-libc-all], [with libc replacement]))
AC_ARG_WITH( [libc-atoi], AS_HELP_STRING([--with-libc-atoi], [with atoi replacement]))
AC_ARG_WITH( [libc-isdigit], AS_HELP_STRING([--with-libc-isdigit], [with isdigit replacement]))
AC_ARG_WITH( [libc-isspace], AS_HELP_STRING([--with-libc-isspace], [with isspace replacement]))
AC_ARG_WITH( [libc-memcpy], AS_HELP_STRING([--with-libc-memcpy], [with memcpy replacement]))
AC_ARG_WITH( [libc-memmove], AS_HELP_STRING([--with-libc-memmove], [with memmove replacement]))
AC_ARG_WITH( [libc-memset], AS_HELP_STRING([--with-libc-memset], [with memset replacement]))
AC_ARG_WITH( [libc-strcmp], AS_HELP_STRING([--with-libc-strcmp], [with strcmp replacement]))
AC_ARG_WITH( [libc-strcpy], AS_HELP_STRING([--with-libc-strcpy], [with strcpy replacement]))
AC_ARG_WITH( [libc-strlen], AS_HELP_STRING([--with-libc-strlen], [with strlen replacement]))
AC_ARG_WITH( [libc-strncmp], AS_HELP_STRING([--with-libc-strncmp], [with strncmp replacement]))
AC_ARG_WITH( [libc-strncpy], AS_HELP_STRING([--with-libc-strncpy], [with strncpy replacement]))
AC_ARG_WITH( [libc-strnlen], AS_HELP_STRING([--with-libc-strnlen], [with strnlen replacement]))
AC_ARG_WITH( [libc-strstr], AS_HELP_STRING([--with-libc-strstr], [with strstr replacement]))
################
@ -70,6 +86,24 @@ if test -z "$with_units"; then with_units=no; fi
])
AS_IF([test "$with_all" = no], do_without_all)
AC_DEFUN([do_with_libc_all],
[
if test -z "$with_libc_atoi"; then with_libc_atoi=yes; fi
if test -z "$with_libc_isdigit"; then with_libc_isdigit=yes; fi
if test -z "$with_libc_isspace"; then with_libc_isspace=yes; fi
if test -z "$with_libc_memcpy"; then with_libc_memcpy=yes; fi
if test -z "$with_libc_memmove"; then with_libc_memmove=yes; fi
if test -z "$with_libc_memset"; then with_libc_memset=yes; fi
if test -z "$with_libc_strcmp"; then with_libc_strcmp=yes; fi
if test -z "$with_libc_strcpy"; then with_libc_strcpy=yes; fi
if test -z "$with_libc_strlen"; then with_libc_strlen=yes; fi
if test -z "$with_libc_strncmp"; then with_libc_strncmp=yes; fi
if test -z "$with_libc_strncpy"; then with_libc_strncpy=yes; fi
if test -z "$with_libc_strnlen"; then with_libc_strnlen=yes; fi
if test -z "$with_libc_strstr"; then with_libc_strstr=yes; fi
])
AS_IF([test "$with_libc_all" = yes], do_with_libc_all)
##################
@ -100,6 +134,22 @@ AS_IF([test "$with_printf" = no ], [with_printf=no], [with_prin
AS_IF([test "$with_printf_fmt" = no ], [with_printf_fmt=no], [with_printf_fmt=yes])
AS_IF([test "$with_units" = no ], [with_units=no], [with_units=yes])
dnl Packages (disabled by default)
AS_IF([test "$with_libc_all" = yes], [with_libc_all=yes], [with_libc_all=no])
AS_IF([test "$with_libc_atoi" = yes], [with_libc_atoi=yes], [with_libc_atoi=no])
AS_IF([test "$with_libc_isdigit" = yes], [with_libc_isdigit=yes], [with_libc_isdigit=no])
AS_IF([test "$with_libc_isspace" = yes], [with_libc_isspace=yes], [with_libc_isspace=no])
AS_IF([test "$with_libc_memcpy" = yes], [with_libc_memcpy=yes], [with_libc_memcpy=no])
AS_IF([test "$with_libc_memmove" = yes], [with_libc_memmove=yes], [with_libc_memmove=no])
AS_IF([test "$with_libc_memset" = yes], [with_libc_memset=yes], [with_libc_memset=no])
AS_IF([test "$with_libc_strcmp" = yes], [with_libc_strcmp=yes], [with_libc_strcmp=no])
AS_IF([test "$with_libc_strcpy" = yes], [with_libc_strcpy=yes], [with_libc_strcpy=no])
AS_IF([test "$with_libc_strlen" = yes], [with_libc_strlen=yes], [with_libc_strlen=no])
AS_IF([test "$with_libc_strncmp" = yes], [with_libc_strncmp=yes], [with_libc_strncmp=no])
AS_IF([test "$with_libc_strncpy" = yes], [with_libc_strncpy=yes], [with_libc_strncpy=no])
AS_IF([test "$with_libc_strnlen" = yes], [with_libc_strnlen=yes], [with_libc_strnlen=no])
AS_IF([test "$with_libc_strstr" = yes], [with_libc_strstr=yes], [with_libc_strstr=no])
#############
@ -148,6 +198,21 @@ AM_CONDITIONAL([WITH_PRINTF], [test "$with_printf" = yes])
AM_CONDITIONAL([WITH_PRINTF_FMT], [test "$with_printf_fmt" = yes])
AM_CONDITIONAL([WITH_UNITS], [test "$with_units" = yes])
dnl Packages (disabled by default)
AM_CONDITIONAL([WITH_LIBC_ATOI], [test "$with_libc_atoi" = yes])
AM_CONDITIONAL([WITH_LIBC_ISDIGIT], [test "$with_libc_isdigit" = yes])
AM_CONDITIONAL([WITH_LIBC_ISSPACE], [test "$with_libc_isspace" = yes])
AM_CONDITIONAL([WITH_LIBC_MEMCPY], [test "$with_libc_memcpy" = yes])
AM_CONDITIONAL([WITH_LIBC_MEMMOVE], [test "$with_libc_memmove" = yes])
AM_CONDITIONAL([WITH_LIBC_MEMSET], [test "$with_libc_memset" = yes])
AM_CONDITIONAL([WITH_LIBC_STRCMP], [test "$with_libc_strcmp" = yes])
AM_CONDITIONAL([WITH_LIBC_STRCPY], [test "$with_libc_strcpy" = yes])
AM_CONDITIONAL([WITH_LIBC_STRLEN], [test "$with_libc_strlen" = yes])
AM_CONDITIONAL([WITH_LIBC_STRNCMP], [test "$with_libc_strncmp" = yes])
AM_CONDITIONAL([WITH_LIBC_STRNCPY], [test "$with_libc_strncpy" = yes])
AM_CONDITIONAL([WITH_LIBC_STRNLEN], [test "$with_libc_strnlen" = yes])
AM_CONDITIONAL([WITH_LIBC_STRSTR], [test "$with_libc_strstr" = yes])
####################
@ -181,6 +246,21 @@ AS_IF([test "$with_printf" = yes], [AC_DEFINE([WITH_PRINTF], [1]
AS_IF([test "$with_printf_fmt" = yes], [AC_DEFINE([WITH_PRINTF_FMT], [1], [with printf format parser])])
AS_IF([test "$with_units", = yes], [AC_DEFINE([WITH_UNITS], [1], [with measurement units utils])])
dnl Packages (disabled by default)
AS_IF([test "$with_libc_atoi" = yes], [AC_DEFINE([WITH_LIBC_ATOI], [1], [with atoi replacement])])
AS_IF([test "$with_libc_isdigit" = yes], [AC_DEFINE([WITH_LIBC_ISDIGIT], [1], [with isdigit replacement])])
AS_IF([test "$with_libc_isspace" = yes], [AC_DEFINE([WITH_LIBC_ISSPACE], [1], [with isspace replacement])])
AS_IF([test "$with_libc_memcpy" = yes], [AC_DEFINE([WITH_LIBC_MEMCPY], [1], [with memcpy replacement])])
AS_IF([test "$with_libc_memmove" = yes], [AC_DEFINE([WITH_LIBC_MEMMOVE], [1], [with memmove replacement])])
AS_IF([test "$with_libc_memset" = yes], [AC_DEFINE([WITH_LIBC_MEMSET], [1], [with memset replacement])])
AS_IF([test "$with_libc_strcmp" = yes], [AC_DEFINE([WITH_LIBC_STRCMP], [1], [with strcmp replacement])])
AS_IF([test "$with_libc_strcpy" = yes], [AC_DEFINE([WITH_LIBC_STRCPY], [1], [with strcpy replacement])])
AS_IF([test "$with_libc_strlen" = yes], [AC_DEFINE([WITH_LIBC_STRLEN], [1], [with strlen replacement])])
AS_IF([test "$with_libc_strncmp" = yes], [AC_DEFINE([WITH_LIBC_STRNCMP], [1], [with strncmp replacement])])
AS_IF([test "$with_libc_strncpy" = yes], [AC_DEFINE([WITH_LIBC_STRNCPY], [1], [with strncpy replacement])])
AS_IF([test "$with_libc_strnlen" = yes], [AC_DEFINE([WITH_LIBC_STRNLEN], [1], [with strnlen replacement])])
AS_IF([test "$with_libc_strstr" = yes], [AC_DEFINE([WITH_LIBC_STRSTR], [1], [with strstr replacement])])
##########################

View File

@ -3,7 +3,8 @@ nobase_include_HEADERS = \
kernaux/arch/i386.h \
kernaux/arch/riscv64.h \
kernaux/arch/x86_64.h \
kernaux/assert.h
kernaux/assert.h \
kernaux/libc.h
if ASM_I386
nobase_include_HEADERS += kernaux/asm/i386.h

View File

@ -1,6 +1,9 @@
/*
We don't include <kernaux/asm/*.h> because they
contain architecture-specific assembly functions.
We don't include <kernaux/libc.h> because it may
conflict with actual freestanding or hosted libc.
*/
#include <kernaux/assert.h>

View File

@ -4,8 +4,7 @@
#include <kernaux/assert.h>
#include <kernaux/cmdline.h>
#include "libc.h"
#include <kernaux/libc.h>
enum State {
INITIAL,

View File

@ -4,8 +4,7 @@
#include <kernaux/assert.h>
#include <kernaux/console.h>
#include "libc.h"
#include <kernaux/libc.h>
#ifdef ASM_I386
#include <kernaux/asm/i386.h>

140
src/libc.c Normal file
View File

@ -0,0 +1,140 @@
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <kernaux/libc.h>
#include <stdbool.h>
#ifdef WITH_LIBC_ATOI
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;
}
#endif // WITH_LIBC_ATOI
#ifdef WITH_LIBC_ISDIGIT
int isdigit(const int c)
{
return (unsigned)c - '0' < 10;
}
#endif // WITH_LIBC_ISDIGIT
#ifdef WITH_LIBC_ISSPACE
int isspace(const int c)
{
return c == ' ' || (unsigned)c - '\t' < 5;
}
#endif // WITH_LIBC_ISSPACE
#ifdef WITH_LIBC_MEMCPY
void *memcpy(void *dest, const void *src, size_t n)
{
char *dest_cell = dest;
char *src_cell = src;
while (n--) *dest_cell++ = *src_cell++;
return dest;
}
#endif // WITH_LIBC_MEMCPY
#ifdef WITH_LIBC_MEMMOVE
void *memmove(void *dest, const void *src, size_t n)
{
char *dest_cell = dest;
char *src_cell = src;
if (dest_cell <= src_cell) {
while (n--) *dest_cell++ = *src_cell++;
} else {
dest_cell += n;
src_cell += n;
while (n--) *--dest_cell = *--src_cell;
}
return dest;
}
#endif // WITH_LIBC_MEMMOVE
#ifdef WITH_LIBC_MEMSET
void *memset(void *s, int c, size_t n)
{
char *ss = s;
while (n--) *ss++ = c;
return s;
}
#endif // WITH_LIBC_MEMSET
#ifdef WITH_LIBC_STRCMP
int strcmp(const char *s1, const char *s2)
{
for (; *s1; ++s1, ++s2) if (*s1 != *s2) return *s1 < *s2 ? -1 : 1;
return 0;
}
#endif // WITH_LIBC_STRCMP
#ifdef WITH_LIBC_STRCPY
char *strcpy(char *dest, const char *src)
{
char *tmp = dest;
while ((*dest++ = *src++) != '\0');
return tmp;
}
#endif // WITH_LIBC_STRCPY
#ifdef WITH_LIBC_STRLEN
size_t strlen(const char *s)
{
const char *ss = s;
while (*ss != '\0') ++ss;
return ss - s;
}
#endif // WITH_LIBC_STRLEN
#ifdef WITH_LIBC_STRNCMP
int strncmp(const char *s1, const char *s2, size_t n)
{
for (; *s1 && n; ++s1, ++s2, --n) if (*s1 != *s2) return *s1 < *s2 ? -1 : 1;
return 0;
}
#endif // WITH_LIBC_STRNCMP
#ifndef WITH_LIBC_STRNCPY
char *strncpy(char *dest, const char *src, size_t n)
{
char *tmp = dest;
while (n-- && (*dest++ = *src++) != '\0');
return tmp;
}
#endif // WITH_LIBC_STRNCPY
#ifdef WITH_LIBC_STRNLEN
size_t strnlen(const char *s, size_t maxlen)
{
const char *ss = s;
while (*ss != '\0' && maxlen--) ++ss;
return ss - s;
}
#endif // WITH_LIBC_STRNLEN
#ifdef WITH_LIBC_STRSTR
char *strstr(const char *haystack, const char *needle)
{
const size_t needle_slen = strlen(needle);
if (!needle_slen) return (char*)haystack;
size_t haystack_slen = strlen(haystack);
while (haystack_slen >= needle_slen) {
--haystack_slen;
if (!memcmp(haystack, needle, needle_slen)) return (char*)haystack;
++haystack;
}
return NULL;
}
#endif // WITH_LIBC_STRSTR

View File

@ -3,10 +3,9 @@
#endif
#include <kernaux/assert.h>
#include <kernaux/libc.h>
#include <kernaux/pfa.h>
#include "libc.h"
#define PAGE_INDEX(page_addr) ((page_addr) / KERNAUX_PFA_PAGE_SIZE)
#define FLAG_INDEX_FROM_INDEX(page_index) ((page_index) / 8)

View File

@ -11,11 +11,10 @@
#endif
#include <kernaux/assert.h>
#include <kernaux/libc.h>
#include <kernaux/printf.h>
#include <kernaux/printf_fmt.h>
#include "libc.h"
#include <stdbool.h>
#include <stdint.h>

View File

@ -7,10 +7,9 @@
#endif
#include <kernaux/assert.h>
#include <kernaux/libc.h>
#include <kernaux/printf_fmt.h>
#include "libc.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

View File

@ -3,11 +3,10 @@
#endif
#include <kernaux/assert.h>
#include <kernaux/libc.h>
#include <kernaux/ntoa.h>
#include <kernaux/units.h>
#include "libc.h"
#define TMP_BUFFER_SIZE (64)
bool kernaux_units_human_raw(