Main: include/kernaux/libc.h: Has been split into separate headers

This commit is contained in:
Alex Kotov 2022-06-07 08:35:14 +03:00
parent b1cc8f386c
commit 040197dacb
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
25 changed files with 108 additions and 150 deletions

3
.gitignore vendored
View File

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

View File

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

View File

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

View File

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

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 --with-libc-all
"$REPO/configure" --host="$HOST" --prefix="$PREFIX" --enable-freestanding --with-libc

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 --with-libc-all
"$REPO/configure" --host="$HOST" --prefix="$PREFIX" --enable-freestanding --with-libc

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 --with-libc-all
"$REPO/configure" --host="$HOST" --prefix="$PREFIX" --enable-freestanding --with-libc

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 --with-libc-all
"$REPO/configure" --host="$HOST" --prefix="$PREFIX" --enable-freestanding --with-libc

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 --with-libc-all
"$REPO/configure" --host="$HOST" --prefix="$PREFIX" --enable-freestanding --with-libc

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 --with-libc-all
"$REPO/configure" --host="$HOST" --prefix="$PREFIX" --enable-freestanding --with-libc

View File

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

View File

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

View File

@ -1,12 +1,10 @@
/*
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>
@comment_line_cmdline@#include <kernaux/cmdline.h>
@comment_line_console@#include <kernaux/console.h>
@comment_line_elf@#include <kernaux/elf.h>

6
libc/Makefile.am Normal file
View File

@ -0,0 +1,6 @@
if WITH_LIBC
nobase_include_HEADERS = \
ctype.h \
stdlib.h \
string.h
endif

15
libc/ctype.h Normal file
View File

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

14
libc/stdlib.h Normal file
View File

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

View File

@ -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 <stddef.h>
// <ctype.h>
int isdigit(int c);
int isspace(int c);
// <stdlib.h>
int atoi(const char *str);
// <string.h>
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);

View File

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

View File

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

View File

@ -2,11 +2,10 @@
#include "config.h"
#endif
#include <kernaux/libc.h>
#include "libc.h"
#include <stdbool.h>
#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

18
src/libc.h Normal file
View File

@ -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 <ctype.h>
#include <stdlib.h>
#include <string.h>
#ifdef __cplusplus
}
#endif
#endif

View File

@ -3,9 +3,10 @@
#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,10 +11,11 @@
#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,9 +7,10 @@
#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,10 +3,11 @@
#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(