From 040197dacb6c210938fe24fc7ed2039c23f26006 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Tue, 7 Jun 2022 08:35:14 +0300 Subject: [PATCH] Main: include/kernaux/libc.h: Has been split into separate headers --- .gitignore | 3 + ChangeLog | 4 ++ Makefile.am | 19 ++++-- README.md | 25 +++----- config/dev-cross-i386 | 2 +- config/dev-cross-riscv64 | 2 +- config/dev-cross-x86_64 | 2 +- config/root-cross-i386-linux | 2 +- config/root-cross-riscv64-linux | 2 +- config/root-cross-x86_64-linux | 2 +- configure.ac | 77 ++----------------------- include/Makefile.am | 3 +- include/kernaux.h.in | 4 +- libc/Makefile.am | 6 ++ libc/ctype.h | 15 +++++ libc/stdlib.h | 14 +++++ include/kernaux/libc.h => libc/string.h | 12 +--- src/cmdline.c | 3 +- src/console.c | 3 +- src/libc.c | 28 +-------- src/libc.h | 18 ++++++ src/pfa.c | 3 +- src/printf.c | 3 +- src/printf_fmt.c | 3 +- src/units.c | 3 +- 25 files changed, 108 insertions(+), 150 deletions(-) create mode 100644 libc/Makefile.am create mode 100644 libc/ctype.h create mode 100644 libc/stdlib.h rename include/kernaux/libc.h => libc/string.h (77%) create mode 100644 src/libc.h diff --git a/.gitignore b/.gitignore index 50dcddf..69ea55b 100644 --- a/.gitignore +++ b/.gitignore @@ -35,6 +35,7 @@ /Makefile.in /include/Makefile.in +/libc/Makefile.in ######################## # To build out of root # @@ -90,6 +91,8 @@ /Makefile /include/Makefile +/libc/Makefile + /include/kernaux.h /include/kernaux/console.h diff --git a/ChangeLog b/ChangeLog index 004f974..3f97076 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2022-06-07 Alex Kotov + + * include/kernaux/libc.h: Has been split into separate headers + 2022-06-06 Alex Kotov * include/kernaux/libc.h: Add more functions diff --git a/Makefile.am b/Makefile.am index 3befc5f..d9200c6 100644 --- a/Makefile.am +++ b/Makefile.am @@ -2,7 +2,7 @@ # Common # ########## -SUBDIRS = include +SUBDIRS = include libc CLEANFILES = TESTS = @@ -13,7 +13,14 @@ AM_CFLAGS = \ -Wall \ -Wextra \ -I$(top_builddir)/include \ - -I$(top_srcdir)/include + -I$(top_srcdir)/include \ + -D_POSIX_C_SOURCE=200809L + +if WITH_LIBC +AM_CFLAGS += \ + -I$(top_builddir)/libc \ + -I$(top_srcdir)/libc +endif if ENABLE_WERROR AM_CFLAGS += -Werror @@ -37,10 +44,7 @@ noinst_PROGRAMS = $(TESTS) # libkernaux.a # ################ -libkernaux_a_SOURCES = \ - src/assert.c \ - src/libc.c - +libkernaux_a_SOURCES = src/libc.h src/assert.c if ASM_I386 libkernaux_a_SOURCES += src/asm/i386.S endif @@ -62,6 +66,9 @@ endif if WITH_FRAMEBUFFER libkernaux_a_SOURCES += src/framebuffer.c endif +if WITH_LIBC +libkernaux_a_SOURCES += src/libc.c +endif if WITH_MBR libkernaux_a_SOURCES += src/mbr.c endif diff --git a/README.md b/README.md index 2e37b5d..d719770 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,6 @@ 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**) @@ -74,6 +73,10 @@ zero). Work-in-progress APIs can change at any time. * [vprintf](/examples/printf_va.c) * [snprintf](/examples/snprintf.c) * [vsnprintf](/examples/snprintf_va.c) +* libc replacement + * [ctype.h](/libc/ctype.h) + * [stdlib.h](/libc/stdlib.h) + * [string.h](/libc/string.h) @@ -98,22 +101,8 @@ stable options. #### 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` +* `--with-libc` - provides the replacement for some standard C functions. + Useful in freestanding environment, where no libc is present. ### Default options @@ -170,7 +159,7 @@ without it in `$PATH`: ./configure \ --host='i386-elf' \ --enable-freestanding \ - --with-libc-all \ + --with-libc \ AR="$(which i386-elf-ar)" \ CC="$(which i386-elf-gcc)" \ RANLIB="$(which i386-elf-ranlib)" diff --git a/config/dev-cross-i386 b/config/dev-cross-i386 index c3a0208..70ecef7 100755 --- a/config/dev-cross-i386 +++ b/config/dev-cross-i386 @@ -14,4 +14,4 @@ export AR="$CROSS-ar" export CC="$CROSS-gcc" export RANLIB="$CROSS-ranlib" -"$REPO/configure" --host="$HOST" --prefix="$PREFIX" --enable-freestanding --with-libc-all +"$REPO/configure" --host="$HOST" --prefix="$PREFIX" --enable-freestanding --with-libc diff --git a/config/dev-cross-riscv64 b/config/dev-cross-riscv64 index 469d490..57b29ff 100755 --- a/config/dev-cross-riscv64 +++ b/config/dev-cross-riscv64 @@ -14,4 +14,4 @@ export AR="$CROSS-ar" export CC="$CROSS-gcc" export RANLIB="$CROSS-ranlib" -"$REPO/configure" --host="$HOST" --prefix="$PREFIX" --enable-freestanding --with-libc-all +"$REPO/configure" --host="$HOST" --prefix="$PREFIX" --enable-freestanding --with-libc diff --git a/config/dev-cross-x86_64 b/config/dev-cross-x86_64 index f077730..4a900b6 100755 --- a/config/dev-cross-x86_64 +++ b/config/dev-cross-x86_64 @@ -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 --with-libc-all +"$REPO/configure" --host="$HOST" --prefix="$PREFIX" --enable-freestanding --with-libc diff --git a/config/root-cross-i386-linux b/config/root-cross-i386-linux index dc9d809..7740099 100755 --- a/config/root-cross-i386-linux +++ b/config/root-cross-i386-linux @@ -14,4 +14,4 @@ export AR="$TARGET-ar" export CC="$TARGET-gcc" export RANLIB="$TARGET-ranlib" -"$REPO/configure" --host="$HOST" --prefix="$PREFIX" --enable-freestanding --with-libc-all +"$REPO/configure" --host="$HOST" --prefix="$PREFIX" --enable-freestanding --with-libc diff --git a/config/root-cross-riscv64-linux b/config/root-cross-riscv64-linux index 6f48a85..af99c56 100755 --- a/config/root-cross-riscv64-linux +++ b/config/root-cross-riscv64-linux @@ -14,4 +14,4 @@ export AR="$TARGET-ar" export CC="$TARGET-gcc" export RANLIB="$TARGET-ranlib" -"$REPO/configure" --host="$HOST" --prefix="$PREFIX" --enable-freestanding --with-libc-all +"$REPO/configure" --host="$HOST" --prefix="$PREFIX" --enable-freestanding --with-libc diff --git a/config/root-cross-x86_64-linux b/config/root-cross-x86_64-linux index ccd68b1..5d8fd62 100755 --- a/config/root-cross-x86_64-linux +++ b/config/root-cross-x86_64-linux @@ -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 --with-libc-all +"$REPO/configure" --host="$HOST" --prefix="$PREFIX" --enable-freestanding --with-libc diff --git a/configure.ac b/configure.ac index 46f5f97..0c4f20f 100644 --- a/configure.ac +++ b/configure.ac @@ -42,20 +42,7 @@ AC_ARG_WITH( [printf-fmt], AS_HELP_STRING([--without-printf-fmt], [without p 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])) +AC_ARG_WITH( [libc], AS_HELP_STRING([--with-libc], [with libc replacement])) @@ -86,24 +73,6 @@ 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) - ################## @@ -135,20 +104,7 @@ AS_IF([test "$with_printf_fmt" = no ], [with_printf_fmt=no], [with_prin 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]) +AS_IF([test "$with_libc" = yes], [with_libc=yes], [with_libc=no]) @@ -199,19 +155,7 @@ 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]) +AM_CONDITIONAL([WITH_LIBC], [test "$with_libc" = yes]) @@ -247,19 +191,7 @@ AS_IF([test "$with_printf_fmt" = yes], [AC_DEFINE([WITH_PRINTF_FMT], [1] 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])]) +AS_IF([test "$with_libc" = yes], [AC_DEFINE([WITH_LIBC], [1], [with atoi replacement])]) @@ -293,6 +225,7 @@ AM_INIT_AUTOMAKE([1.9 subdir-objects]) AC_CONFIG_FILES([ Makefile include/Makefile + libc/Makefile include/kernaux.h include/kernaux/console.h ]) diff --git a/include/Makefile.am b/include/Makefile.am index 05afb80..d47fc5e 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -3,8 +3,7 @@ nobase_include_HEADERS = \ kernaux/arch/i386.h \ kernaux/arch/riscv64.h \ kernaux/arch/x86_64.h \ - kernaux/assert.h \ - kernaux/libc.h + kernaux/assert.h if ASM_I386 nobase_include_HEADERS += kernaux/asm/i386.h diff --git a/include/kernaux.h.in b/include/kernaux.h.in index 71597bc..bdbd28c 100644 --- a/include/kernaux.h.in +++ b/include/kernaux.h.in @@ -1,12 +1,10 @@ /* We don't include because they contain architecture-specific assembly functions. - - We don't include because it may - conflict with actual freestanding or hosted libc. */ #include + @comment_line_cmdline@#include @comment_line_console@#include @comment_line_elf@#include diff --git a/libc/Makefile.am b/libc/Makefile.am new file mode 100644 index 0000000..cbba157 --- /dev/null +++ b/libc/Makefile.am @@ -0,0 +1,6 @@ +if WITH_LIBC +nobase_include_HEADERS = \ + ctype.h \ + stdlib.h \ + string.h +endif diff --git a/libc/ctype.h b/libc/ctype.h new file mode 100644 index 0000000..d4fd114 --- /dev/null +++ b/libc/ctype.h @@ -0,0 +1,15 @@ +#ifndef _CTYPE_H +#define _CTYPE_H 1 + +#ifdef __cplusplus +extern "C" { +#endif + +int isdigit(int c); +int isspace(int c); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/libc/stdlib.h b/libc/stdlib.h new file mode 100644 index 0000000..cbac8f9 --- /dev/null +++ b/libc/stdlib.h @@ -0,0 +1,14 @@ +#ifndef _STDLIB_H +#define _STDLIB_H 1 + +#ifdef __cplusplus +extern "C" { +#endif + +int atoi(const char *str); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/include/kernaux/libc.h b/libc/string.h similarity index 77% rename from include/kernaux/libc.h rename to libc/string.h index e8fb4a4..5be69eb 100644 --- a/include/kernaux/libc.h +++ b/libc/string.h @@ -1,5 +1,5 @@ -#ifndef KERNAUX_INCLUDED_LIBC -#define KERNAUX_INCLUDED_LIBC +#ifndef _STRING_H +#define _STRING_H 1 #ifdef __cplusplus extern "C" { @@ -7,14 +7,6 @@ extern "C" { #include -// -int isdigit(int c); -int isspace(int c); - -// -int atoi(const char *str); - -// void *memcpy(void *dest, const void *src, size_t n); void *memmove(void *dest, const void *src, size_t n); void *memset(void *s, int c, size_t n); diff --git a/src/cmdline.c b/src/cmdline.c index 727a9f6..9abf0d6 100644 --- a/src/cmdline.c +++ b/src/cmdline.c @@ -4,7 +4,8 @@ #include #include -#include + +#include "libc.h" enum State { INITIAL, diff --git a/src/console.c b/src/console.c index b291346..7e861dd 100644 --- a/src/console.c +++ b/src/console.c @@ -4,7 +4,8 @@ #include #include -#include + +#include "libc.h" #ifdef ASM_I386 #include diff --git a/src/libc.c b/src/libc.c index 292a5d1..7394aa1 100644 --- a/src/libc.c +++ b/src/libc.c @@ -2,11 +2,10 @@ #include "config.h" #endif -#include +#include "libc.h" #include -#ifdef WITH_LIBC_ATOI int atoi(const char *str) { while (isspace(*str)) ++str; @@ -19,23 +18,17 @@ int atoi(const char *str) 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; @@ -43,9 +36,7 @@ void *memcpy(void *dest, const void *src, size_t n) 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; @@ -59,70 +50,54 @@ void *memmove(void *dest, const void *src, size_t n) } 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); @@ -137,4 +112,3 @@ char *strstr(const char *haystack, const char *needle) return NULL; } -#endif // WITH_LIBC_STRSTR diff --git a/src/libc.h b/src/libc.h new file mode 100644 index 0000000..a749bd3 --- /dev/null +++ b/src/libc.h @@ -0,0 +1,18 @@ +// TODO: remove this file, require needed headers separately + +#ifndef KERNAUX_INCLUDED_LIBC +#define KERNAUX_INCLUDED_LIBC + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/pfa.c b/src/pfa.c index 6e9e7c4..c899a8a 100644 --- a/src/pfa.c +++ b/src/pfa.c @@ -3,9 +3,10 @@ #endif #include -#include #include +#include "libc.h" + #define PAGE_INDEX(page_addr) ((page_addr) / KERNAUX_PFA_PAGE_SIZE) #define FLAG_INDEX_FROM_INDEX(page_index) ((page_index) / 8) diff --git a/src/printf.c b/src/printf.c index aafe578..c6b7df6 100644 --- a/src/printf.c +++ b/src/printf.c @@ -11,10 +11,11 @@ #endif #include -#include #include #include +#include "libc.h" + #include #include diff --git a/src/printf_fmt.c b/src/printf_fmt.c index cd08d75..c14a5ee 100644 --- a/src/printf_fmt.c +++ b/src/printf_fmt.c @@ -7,9 +7,10 @@ #endif #include -#include #include +#include "libc.h" + #include #include #include diff --git a/src/units.c b/src/units.c index 0c359d9..cd4834f 100644 --- a/src/units.c +++ b/src/units.c @@ -3,10 +3,11 @@ #endif #include -#include #include #include +#include "libc.h" + #define TMP_BUFFER_SIZE (64) bool kernaux_units_human_raw(