diff --git a/.gitignore b/.gitignore index 23efb9ff..2eb78b11 100644 --- a/.gitignore +++ b/.gitignore @@ -108,8 +108,6 @@ /examples/assert /examples/cmdline -/examples/fprintf -/examples/fprintf_va /examples/generic_file /examples/generic_malloc /examples/generic_mutex @@ -117,9 +115,11 @@ /examples/ntoa /examples/panic /examples/pfa +/examples/printf_file +/examples/printf_file_va /examples/printf_fmt -/examples/snprintf -/examples/snprintf_va +/examples/printf_str +/examples/printf_str_va /examples/units_human /tests/multiboot2_header_print1 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index a0f900b8..11caaabc 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -2,16 +2,73 @@ Common ------ * Add your name to [COPYING](/COPYING). -* **Don't** add your name to `AUTHORS` - it's for maintainers. +* Don't add your name to `AUTHORS` - it's for maintainers. * Add copyright notice in the beginning of changed files except the headers. * If you change the behavior (even just fix a bug) of **libkernaux** (stable) or [libc](/libc), add a record to [ChangeLog](/ChangeLog). +Prohibitions: + +* Don't commit binary files +* Don't commit configuration files of your editor or IDE +* Don't use encodings other than ASCII and UTF-8 +* Don't use alphabets other than Latin +* Don't use languages other than English +* Don't use tabulations (I don't hate tabs, but people can not use them + properly) +* Don't leave trailing whitespaces +* Don't forget the newline character in the end of files + +The following statements are recommendations, but highly encouraged: + +* Write documentation +* Write tests +* Keep lines less than 80 characters long for better experience on split screen + +### Programming mistakes + +* Always check documentation, manuals and specifications + +Avoid stupid errors with: + +* Manual memory management + * `malloc` may return `NULL` + * Memory leak (forget to `free`) + * Use after `free`/`realloc` + * Double `free`/`realloc` + * `free`ing/`realloc`ating unallocated memory + * Changing the original pointer to the allocated memory (use `const`!) +* `NULL` pointers and `nil`/`None`/whatever objects +* Division by zero +* Pointer arithmetic - consider type size +* Type sizes (like `long` on 32-bit and 64-bit) +* Integer arithmetic overflow +* Bit shift +* Endianness (byte order) +* Data packing +* Data alignment +* Thread safety +* Undefined behavior +* Logical expressions (tautology, whatever) +* Checking for an error (return value, pointer argument, whatever) +* Use of not fully initialized data +* Not reading beyond a buffer, array or string + * The index of the last item, which is less than the buffer size + * Negative indices + * The terminating null character in a string +* Allowed values of arguments +* Possible values of parameters +* Operator precedence +* Default case in switch statements +* Braces (curly brackets) around code blocks + C language ---------- +Use **cppcheck**. + * Name regular functions (*not methods*) and variables in lower snake case (example: `foo_bar`). * Name macros in upper snake case (example: `FOO_BAR`). @@ -78,6 +135,22 @@ FooBar_init(foobar); FooBar_do_something(foobar); ``` +* Mark variables and parameters with `const` if you don't plan to modify them +* Only omit braces (curly brackets) of a block if it's statement is placed on + the same line as conditional statement: + +```c +// Good: +if (foo) return bar; +if (foo) { + return bar; +} + +// Bad: +if (foo) + return bar; +``` + Python @@ -90,6 +163,8 @@ Nothing here yet. Ruby ---- +* Freeze objects if you don't plan to modify them + ### Matz's Ruby interpreter Use **RuboCop**. See [bindings/ruby/.rubocop.yml](/bindings/ruby/.rubocop.yml) diff --git a/README.md b/README.md index e2e7124d..d331b46d 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ zero). Work-in-progress APIs can change at any time. * [Declarations](/include/kernaux/arch/) * [Functions](/include/kernaux/asm/) * Generic types - * [File](/include/kernaux/generic/file.h) (*work in progress*) + * [File](/include/kernaux/generic/file.h) (*non-breaking since* **?.?.?**) * [Example](/examples/generic_file.c) * [Memory allocator](/include/kernaux/generic/malloc.h) (*non-breaking since* **?.?.?**) * [Example](/examples/generic_malloc.c) @@ -62,7 +62,7 @@ zero). Work-in-progress APIs can change at any time. * [Example](/examples/pfa.c) * Data formats * [ELF](/include/kernaux/elf.h) (*work in progress*) - * [Master Boot Record](/include/kernaux/mbr.h) (*work in progress*) + * [MBR](/include/kernaux/mbr.h) (*work in progress*) * [Multiboot 2 (GRUB 2)](/include/kernaux/multiboot2.h) (*work in progress*) * Utilities * [Measurement units utils](/include/kernaux/units.h) (*work in progress*) @@ -70,17 +70,15 @@ zero). Work-in-progress APIs can change at any time. * [Memory map](/include/kernaux/memmap.h.in) (*non-breaking since* **0.4.0**) * [Example](/examples/memmap.c) * [printf format parser](/include/kernaux/printf_fmt.h) (*work in progress*) - * Code from [https://github.com/mpaland/printf](https://github.com/mpaland/printf). Thank you! * [Example](/examples/printf_fmt.c) * Usual functions * [itoa/ftoa replacement](/include/kernaux/ntoa.h) (*non-breaking since* **0.4.0**) * [Example](/examples/ntoa.c) * [printf replacement](/include/kernaux/printf.h.in) (*non-breaking since* **?.?.?**) - * Code from [https://github.com/mpaland/printf](https://github.com/mpaland/printf). Thank you! - * [Example: fprintf](/examples/fprintf.c) - * [Example: vfprintf](/examples/fprintf_va.c) - * [Example: snprintf](/examples/snprintf.c) - * [Example: vsnprintf](/examples/snprintf_va.c) + * [Example: fprintf](/examples/printf_file.c) + * [Example: vfprintf](/examples/printf_file_va.c) + * [Example: snprintf](/examples/printf_str.c) + * [Example: vsnprintf](/examples/printf_str_va.c) * libc replacement (*work in progress*) * [ctype.h](/libc/include/ctype.h) * [inttypes.h](/libc/include/inttypes.h) diff --git a/common/printf_orig.yml b/common/printf_orig.yml index 392da8d9..a1c669c4 100644 --- a/common/printf_orig.yml +++ b/common/printf_orig.yml @@ -1,7 +1,7 @@ # The code was taken from Marco Paland's printf. # Copyright (c) 2014-2019 Marco Paland -# Copyright (c) 2020-2022 Alex Kotov +# Copyright (c) 2021-2022 Alex Kotov # TODO: add remaining tests from # https://github.com/mpaland/printf/blob/master/test/test_suite.cpp diff --git a/configure.ac b/configure.ac index a0191d86..ee572217 100644 --- a/configure.ac +++ b/configure.ac @@ -65,7 +65,7 @@ AC_ARG_WITH( [asm], AS_HELP_STRING([--without-asm], [without AC_ARG_WITH( [cmdline], AS_HELP_STRING([--without-cmdline], [without command line parser])) AC_ARG_WITH( [elf], AS_HELP_STRING([--without-elf], [without ELF utils])) AC_ARG_WITH( [free-list], AS_HELP_STRING([--without-free-list], [without free list memory allocator])) -AC_ARG_WITH( [mbr], AS_HELP_STRING([--without-mbr], [without Master Boot Record])) +AC_ARG_WITH( [mbr], AS_HELP_STRING([--without-mbr], [without MBR utils])) AC_ARG_WITH( [memmap], AS_HELP_STRING([--without-memmap], [without memory map])) AC_ARG_WITH( [multiboot2], AS_HELP_STRING([--without-multiboot2], [without Multiboot 2 information parser])) AC_ARG_WITH( [ntoa], AS_HELP_STRING([--without-ntoa], [without itoa/ftoa])) @@ -248,7 +248,7 @@ AS_IF([test "$with_asm" = yes], [AC_DEFINE([WITH_ASM], [1] AS_IF([test "$with_cmdline" = yes], [AC_DEFINE([WITH_CMDLINE], [1], [with command line parser])]) AS_IF([test "$with_elf" = yes], [AC_DEFINE([WITH_ELF], [1], [with ELF utils])]) AS_IF([test "$with_free_list" = yes], [AC_DEFINE([WITH_FREE_LIST], [1], [with free list memory allocator])]) -AS_IF([test "$with_mbr" = yes], [AC_DEFINE([WITH_MBR], [1], [with Master Boot Record])]) +AS_IF([test "$with_mbr" = yes], [AC_DEFINE([WITH_MBR], [1], [with MBR utils])]) AS_IF([test "$with_memmap" = yes], [AC_DEFINE([WITH_MEMMAP], [1], [with memory map])]) AS_IF([test "$with_multiboot2" = yes], [AC_DEFINE([WITH_MULTIBOOT2], [1], [with Multiboot 2 information parser])]) AS_IF([test "$with_ntoa" = yes], [AC_DEFINE([WITH_NTOA], [1], [with ntoa])]) diff --git a/examples/Makefile.am b/examples/Makefile.am index 3e699b38..3d4ce958 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -21,26 +21,6 @@ cmdline_LDADD = $(top_builddir)/libkernaux.la cmdline_SOURCES = main.c cmdline.c endif -########### -# fprintf # -########### - -if WITH_PRINTF -TESTS += fprintf -fprintf_LDADD = $(top_builddir)/libkernaux.la -fprintf_SOURCES = main.c fprintf.c -endif - -############## -# fprintf_va # -############## - -if WITH_PRINTF -TESTS += fprintf_va -fprintf_va_LDADD = $(top_builddir)/libkernaux.la -fprintf_va_SOURCES = main.c fprintf_va.c -endif - ################ # generic_file # ################ @@ -103,6 +83,26 @@ pfa_LDADD = $(top_builddir)/libkernaux.la pfa_SOURCES = main.c pfa.c endif +############### +# printf_file # +############### + +if WITH_PRINTF +TESTS += printf_file +printf_file_LDADD = $(top_builddir)/libkernaux.la +printf_file_SOURCES = main.c printf_file.c +endif + +################## +# printf_file_va # +################## + +if WITH_PRINTF +TESTS += printf_file_va +printf_file_va_LDADD = $(top_builddir)/libkernaux.la +printf_file_va_SOURCES = main.c printf_file_va.c +endif + ############## # printf_fmt # ############## @@ -113,24 +113,24 @@ printf_fmt_LDADD = $(top_builddir)/libkernaux.la printf_fmt_SOURCES = main.c printf_fmt.c endif -############ -# snprintf # -############ +############## +# printf_str # +############## if WITH_PRINTF -TESTS += snprintf -snprintf_LDADD = $(top_builddir)/libkernaux.la -snprintf_SOURCES = main.c snprintf.c +TESTS += printf_str +printf_str_LDADD = $(top_builddir)/libkernaux.la +printf_str_SOURCES = main.c printf_str.c endif -############### -# snprintf_va # -############### +################# +# printf_str_va # +################# if WITH_PRINTF -TESTS += snprintf_va -snprintf_va_LDADD = $(top_builddir)/libkernaux.la -snprintf_va_SOURCES = main.c snprintf_va.c +TESTS += printf_str_va +printf_str_va_LDADD = $(top_builddir)/libkernaux.la +printf_str_va_SOURCES = main.c printf_str_va.c endif ############### diff --git a/examples/fprintf.c b/examples/printf_file.c similarity index 100% rename from examples/fprintf.c rename to examples/printf_file.c diff --git a/examples/fprintf_va.c b/examples/printf_file_va.c similarity index 100% rename from examples/fprintf_va.c rename to examples/printf_file_va.c diff --git a/examples/snprintf.c b/examples/printf_str.c similarity index 100% rename from examples/snprintf.c rename to examples/printf_str.c diff --git a/examples/snprintf_va.c b/examples/printf_str_va.c similarity index 100% rename from examples/snprintf_va.c rename to examples/printf_str_va.c diff --git a/include/kernaux/arch/i386.h b/include/kernaux/arch/i386.h index ba9bf4e2..0a8a3ce5 100644 --- a/include/kernaux/arch/i386.h +++ b/include/kernaux/arch/i386.h @@ -82,48 +82,48 @@ KERNAUX_STATIC_TEST_STRUCT_SIZE(KernAux_Arch_I386_DTE, 8); */ struct KernAux_Arch_I386_TSS { // 0x00 - unsigned prev_tss : 16; - unsigned _zero0 : 16; + uint16_t prev_tss; + uint16_t _zero0; // 0x04 - unsigned esp0 : 32; - unsigned ss0 : 16; - unsigned _zero1 : 16; - unsigned esp1 : 32; - unsigned ss1 : 16; - unsigned _zero2 : 16; - unsigned esp2 : 32; - unsigned ss2 : 16; - unsigned _zero3 : 16; + uint32_t esp0; + uint16_t ss0; + uint16_t _zero1; + uint32_t esp1; + uint16_t ss1; + uint16_t _zero2; + uint32_t esp2; + uint16_t ss2; + uint16_t _zero3; // 0x1c - unsigned cr3 : 32; - unsigned eip : 32; - unsigned eflags : 32; - unsigned eax : 32; - unsigned ecx : 32; - unsigned edx : 32; - unsigned ebx : 32; - unsigned esp : 32; - unsigned ebp : 32; - unsigned esi : 32; - unsigned edi : 32; + uint32_t cr3; + uint32_t eip; + uint32_t eflags; + uint32_t eax; + uint32_t ecx; + uint32_t edx; + uint32_t ebx; + uint32_t esp; + uint32_t ebp; + uint32_t esi; + uint32_t edi; // 0x48 - unsigned es : 16; - unsigned _zero4 : 16; - unsigned cs : 16; - unsigned _zero5 : 16; - unsigned ss : 16; - unsigned _zero6 : 16; - unsigned ds : 16; - unsigned _zero7 : 16; - unsigned fs : 16; - unsigned _zero8 : 16; - unsigned gs : 16; - unsigned _zero9 : 16; - unsigned ldt : 16; - unsigned _zero10 : 16; + uint16_t es; + uint16_t _zero4; + uint16_t cs; + uint16_t _zero5; + uint16_t ss; + uint16_t _zero6; + uint16_t ds; + uint16_t _zero7; + uint16_t fs; + uint16_t _zero8; + uint16_t gs; + uint16_t _zero9; + uint16_t ldt; + uint16_t _zero10; // 0x64 - unsigned _zero11 : 16; - unsigned io_map_base : 16; + uint16_t _zero11; + uint16_t io_map_base; } KERNAUX_PACKING_ATTR; diff --git a/libc/src/asm/i386/longjmp.S b/libc/src/asm/i386/longjmp.S index f0439e29..00fafbdb 100644 --- a/libc/src/asm/i386/longjmp.S +++ b/libc/src/asm/i386/longjmp.S @@ -1,9 +1,9 @@ /** * The code was taken from musl libc. * - * Copyright (c) 2011 Rich Felker - * Copyright (c) 2020-2022 Alex Kotov - * Copyright (c) 2022 Alexander Monakov + * Copyright (c) 2011 Rich Felker + * Copyright (c) 2022 Alexander Monakov + * Copyright (c) 2022 Alex Kotov */ #ifdef HAVE_CONFIG_H diff --git a/libc/src/asm/i386/setjmp.S b/libc/src/asm/i386/setjmp.S index 210b1716..841544d7 100644 --- a/libc/src/asm/i386/setjmp.S +++ b/libc/src/asm/i386/setjmp.S @@ -2,7 +2,7 @@ * The code was taken from musl libc. * * Copyright (c) 2011-2015 Rich Felker - * Copyright (c) 2020-2022 Alex Kotov + * Copyright (c) 2022 Alex Kotov */ #ifdef HAVE_CONFIG_H diff --git a/libc/src/asm/x86_64/longjmp.S b/libc/src/asm/x86_64/longjmp.S index d978945a..7565082b 100644 --- a/libc/src/asm/x86_64/longjmp.S +++ b/libc/src/asm/x86_64/longjmp.S @@ -3,8 +3,8 @@ * * Copyright (c) 2011 Nicholas J. Kain * Copyright (c) 2011-2012 Rich Felker - * Copyright (c) 2020-2022 Alex Kotov * Copyright (c) 2022 Alexander Monakov + * Copyright (c) 2022 Alex Kotov */ #ifdef HAVE_CONFIG_H diff --git a/libc/src/asm/x86_64/setjmp.S b/libc/src/asm/x86_64/setjmp.S index 6a565352..6279a60d 100644 --- a/libc/src/asm/x86_64/setjmp.S +++ b/libc/src/asm/x86_64/setjmp.S @@ -3,8 +3,8 @@ * * Copyright (c) 2011 Nicholas J. Kain * Copyright (c) 2011-2012 Rich Felker - * Copyright (c) 2020-2022 Alex Kotov * Copyright (c) 2022 Alexander Monakov + * Copyright (c) 2022 Alex Kotov */ #ifdef HAVE_CONFIG_H diff --git a/src/free_list.c b/src/free_list.c index 88a9ee01..38f58c58 100644 --- a/src/free_list.c +++ b/src/free_list.c @@ -2,7 +2,7 @@ * The code was inspired by the Embedded Artistry's libmemory. * * Copyright (c) 2017-2022 Embedded Artistry LLC - * Copyright (c) 2020-2022 Alex Kotov + * Copyright (c) 2022 Alex Kotov */ #ifdef HAVE_CONFIG_H diff --git a/src/printf.c b/src/printf.c index cc822a35..58295486 100644 --- a/src/printf.c +++ b/src/printf.c @@ -2,7 +2,7 @@ * The code was taken from Marco Paland's printf. * * Copyright (c) 2014-2019 Marco Paland - * Copyright (c) 2020-2022 Alex Kotov + * Copyright (c) 2021-2022 Alex Kotov * * Tiny [v]fprintf, sfprintf and [v]snprintf implementation, optimized for speed * on embedded systems with a very limited resources. These routines are thread diff --git a/src/printf_fmt.c b/src/printf_fmt.c index 60746da2..43dbf666 100644 --- a/src/printf_fmt.c +++ b/src/printf_fmt.c @@ -2,7 +2,7 @@ * The code was taken from Marco Paland's printf. * * Copyright (c) 2014-2019 Marco Paland - * Copyright (c) 2020-2022 Alex Kotov + * Copyright (c) 2021-2022 Alex Kotov */ #ifdef HAVE_CONFIG_H