Browse Source

Release the 0.3.0 (add README + LICENSE and fix norm)

master
Yatis 1 year ago
parent
commit
565a159370
  1. 121
      LICENSE
  2. 107
      README.md
  3. 2
      include/bits/signum.h
  4. 101
      make/Makefile.default
  5. 2
      src/stdio/internal/printf.h
  6. 72
      src/stdio/internal/printf_actions.c
  7. 19
      src/stdio/internal/printf_common.c
  8. 67
      src/stdio/internal/printf_options.c
  9. 3
      src/stdio/vdprintf.c
  10. 6
      src/stdio/vsnprintf.c
  11. 4
      src/string/memcpy.c
  12. 2
      src/string/memset.c
  13. 4
      src/string/strcat.c
  14. 10
      src/string/strchr.c
  15. 3
      src/string/strcmp.c
  16. 4
      src/string/strlen.c

121
LICENSE

@ -0,0 +1,121 @@
Creative Commons Legal Code
CC0 1.0 Universal
CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
HEREUNDER.
Statement of Purpose
The laws of most jurisdictions throughout the world automatically confer
exclusive Copyright and Related Rights (defined below) upon the creator
and subsequent owner(s) (each and all, an "owner") of an original work of
authorship and/or a database (each, a "Work").
Certain owners wish to permanently relinquish those rights to a Work for
the purpose of contributing to a commons of creative, cultural and
scientific works ("Commons") that the public can reliably and without fear
of later claims of infringement build upon, modify, incorporate in other
works, reuse and redistribute as freely as possible in any form whatsoever
and for any purposes, including without limitation commercial purposes.
These owners may contribute to the Commons to promote the ideal of a free
culture and the further production of creative, cultural and scientific
works, or to gain reputation or greater distribution for their Work in
part through the use and efforts of others.
For these and/or other purposes and motivations, and without any
expectation of additional consideration or compensation, the person
associating CC0 with a Work (the "Affirmer"), to the extent that he or she
is an owner of Copyright and Related Rights in the Work, voluntarily
elects to apply CC0 to the Work and publicly distribute the Work under its
terms, with knowledge of his or her Copyright and Related Rights in the
Work and the meaning and intended legal effect of CC0 on those rights.
1. Copyright and Related Rights. A Work made available under CC0 may be
protected by copyright and related or neighboring rights ("Copyright and
Related Rights"). Copyright and Related Rights include, but are not
limited to, the following:
i. the right to reproduce, adapt, distribute, perform, display,
communicate, and translate a Work;
ii. moral rights retained by the original author(s) and/or performer(s);
iii. publicity and privacy rights pertaining to a person's image or
likeness depicted in a Work;
iv. rights protecting against unfair competition in regards to a Work,
subject to the limitations in paragraph 4(a), below;
v. rights protecting the extraction, dissemination, use and reuse of data
in a Work;
vi. database rights (such as those arising under Directive 96/9/EC of the
European Parliament and of the Council of 11 March 1996 on the legal
protection of databases, and under any national implementation
thereof, including any amended or successor version of such
directive); and
vii. other similar, equivalent or corresponding rights throughout the
world based on applicable law or treaty, and any national
implementations thereof.
2. Waiver. To the greatest extent permitted by, but not in contravention
of, applicable law, Affirmer hereby overtly, fully, permanently,
irrevocably and unconditionally waives, abandons, and surrenders all of
Affirmer's Copyright and Related Rights and associated claims and causes
of action, whether now known or unknown (including existing as well as
future claims and causes of action), in the Work (i) in all territories
worldwide, (ii) for the maximum duration provided by applicable law or
treaty (including future time extensions), (iii) in any current or future
medium and for any number of copies, and (iv) for any purpose whatsoever,
including without limitation commercial, advertising or promotional
purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
member of the public at large and to the detriment of Affirmer's heirs and
successors, fully intending that such Waiver shall not be subject to
revocation, rescission, cancellation, termination, or any other legal or
equitable action to disrupt the quiet enjoyment of the Work by the public
as contemplated by Affirmer's express Statement of Purpose.
3. Public License Fallback. Should any part of the Waiver for any reason
be judged legally invalid or ineffective under applicable law, then the
Waiver shall be preserved to the maximum extent permitted taking into
account Affirmer's express Statement of Purpose. In addition, to the
extent the Waiver is so judged Affirmer hereby grants to each affected
person a royalty-free, non transferable, non sublicensable, non exclusive,
irrevocable and unconditional license to exercise Affirmer's Copyright and
Related Rights in the Work (i) in all territories worldwide, (ii) for the
maximum duration provided by applicable law or treaty (including future
time extensions), (iii) in any current or future medium and for any number
of copies, and (iv) for any purpose whatsoever, including without
limitation commercial, advertising or promotional purposes (the
"License"). The License shall be deemed effective as of the date CC0 was
applied by Affirmer to the Work. Should any part of the License for any
reason be judged legally invalid or ineffective under applicable law, such
partial invalidity or ineffectiveness shall not invalidate the remainder
of the License, and in such case Affirmer hereby affirms that he or she
will not (i) exercise any of his or her remaining Copyright and Related
Rights in the Work or (ii) assert any associated claims and causes of
action with respect to the Work, in either case contrary to Affirmer's
express Statement of Purpose.
4. Limitations and Disclaimers.
a. No trademark or patent rights held by Affirmer are waived, abandoned,
surrendered, licensed or otherwise affected by this document.
b. Affirmer offers the Work as-is and makes no representations or
warranties of any kind concerning the Work, express, implied,
statutory or otherwise, including without limitation warranties of
title, merchantability, fitness for a particular purpose, non
infringement, or the absence of latent or other defects, accuracy, or
the present or absence of errors, whether or not discoverable, all to
the greatest extent permissible under applicable law.
c. Affirmer disclaims responsibility for clearing rights of other persons
that may apply to the Work or any use thereof, including without
limitation any person's Copyright and Related Rights in the Work.
Further, Affirmer disclaims responsibility for obtaining any necessary
consents, permissions or other rights required for any use of the
Work.
d. Affirmer understands and acknowledges that Creative Commons is not a
party to this document and has no duty or obligation with respect to
this CC0 or use of the Work.

107
README.md

@ -0,0 +1,107 @@
# The FX C Library
This directory contains the sources of the FxLibc Library.
See the `make version` command to see for what release version you have.
The Fx C Library is the standard system C library implementation for all Casio
Fx calculator, and is an important part of what makes up programs on these
devices. It provides the system API for all programs written in C and
C-compatible languages such as C++ and Objective-C; the runtime facilities of
other programming languages use the C library to access the underlying operating
system.
---
## Dependencies
Fx C library depends on a suitable GCC toolchain in the PATH. You can absolutely
not build gint with your system compiler!
* The tutorial on Planète Casio builds an sh-elf that works everywhere.
* For fx-9860G II, `sh3eb-elf` is strongly advised.
* For fx-CG 50, `sh4eb-elf` (with `-m4-nofpu`) is slightly better but `sh3eb-elf`
is completely fine.
---
## Building and installating FxLibc
You can choose to build `fxlibc` standalone or with the support of the fx-9860G
II (monochrome calculators, aka Graph 85 family) Casio ABI, fx-CG 50 (color
calculators, aka Prizm or Graph 90 family), Vhex kernel ABI or all of them (this
will generate 3 distinct libraries).
Each ABI support add many functionalities provided by the operating system, like
I/O abstraction (open, close, fcntl, stat, ...). (see "supported features" on
the wiki)
#### Configuration and support
The Fx C library supports these ABI:
* `casio-abi-fx9860g` for the support of Casio ABI used by fx9860g-like devices.
* `casio-abi-fxcg50` for the support of Casio ABI used by the fxcg50 device.
* `vhex-kernel` for the support of Vhex kernel ABI.
* (nothing) compile only standing functions.
The Fx C library support these format:
* `static` generate static libraries.
* `shared` generate shared libraries (Only for the Vhex kernel).
Note that the shared feature is not currently implemented because of
non-support of the shared library generation by the GCC compiler for SuperH
architecture. A workaround can be used but it requires a static library to do
the dynamic symbols resolving (Thanks Kristaba).
For more information about library build configuration, you can use the
`../configure --help` command.
#### Building
Create a build directory and configure in it:
```
% mkdir build && cd build
% ../configure
```
Then build the source and install the library files to the selected directory.
You might need root access if you selected a target directory owned by root with
`--prefix`, or if you built your compiler as root.
```
% make
% make install
```
---
### Contributing
Bug reports, feature suggestions and especially code contributions are most
welcome.
If you are interested in doing a port, or want to contribute to this project,
please, try to respect these constraints:
* Document your code.
* One function per files.
* Each file name content only the name of the function.
* Respect the Linux Coding style as much as possible (if you are not familiar
with this norm, you can use `indent -linux` utilities to help to format).
* Header must respect the `/usr/include` architecture.
* Avoid modules hardware-specific code which can generate interruptions
(DMA, SPU, ...) except if you are absolutely sure that the operating
system can handle them.
---
### Using Fx C Library
To use Fx C library as your runtime environment, the bare minimum is:
* You must add `fxlibc/` instead of each include file (for example, if you want
to include `stdio.h` you mush use `#include <fxlibc/stdio.h>`.
* Link with:
* `-lfxlibc` for standalone feature
* `-lfxlibc-casio-abi-fx9860g` for Casio ABI support for monochrome devices
* `-lfxlibc-casio-abi-fxcg50` for Casio ABI support for primz devices
* `-lfxlibc-vhex` for Vhex kernel support.
---
### Licences
This work is licensed under a CC0 1.0 Universal License. To view a copy of this
license, visit: https://creativecommons.org/publicdomain/zero/1.0/legalcode.txt
Or see the LICENSE file.

2
include/bits/signum.h

@ -4,7 +4,7 @@
// Define the number of signals
#define NSIG 32
// Vhex kernel internal define used to indicate
// Vhex kernel internal define used to indicate
// if the signal is implemented or not
#define __SIGUNDEF ((__sighandler_t) -2) /* Not implemented */

101
make/Makefile.default

@ -3,8 +3,8 @@
#
# fxlibc project Makefile
#
# This makefile is grandly inspired by the Gint unikernel
# projet, many thanks to Lephenixnoir !
# This makefile is grandly inspired by the one used for the Gint (unikernel)
# project, many thanks to Lephenixnoir !
#
# Build architecture:
# build/
@ -30,12 +30,13 @@
# `--- libfxlibc-casio-abi-fxcg50.so
#
# TODO:
# * handle versionning
# * handle dynamic versioning
# * handle verbose option
#---
MAJOR := 0
MINOR := 2
PATCH := 5
EXTRAVERSION := -rc
MINOR := 3
PATCH := 0
EXTRAVERSION :=
#---
@ -61,26 +62,21 @@ blue := \033[1;34m
white := \033[1;37m
nocolor := \033[1;0m
# Define all directory used to stored informations
# Define all directories used to stored information
dir_object := object
dir_output := output
# Output configurations
name := fxlibc
# automated variable
# Automated variables
directory := $(shell find ../src -not -path "*/\.*" -type d)
src := $(foreach path,$(directory), \
$(wildcard $(path)/*.c) \
$(wildcard $(path)/*.S) \
$(wildcard $(path)/*.s))
#---
# Toolchain
#---
gcc = $(CONFIG.TOOLCHAIN)-gcc
as = $(CONFIG.TOOLCHAIN)-as
ld = $(CONFIG.TOOLCHAIN)-ld
@ -110,26 +106,27 @@ first: all
#---
# Generate building rules
#---
# common part used to compile source file
# This function will generate compilation rule for each source.
# @params:
# *1 - source file path
# *2 - build directory path (output)
# *3 - build flags
# TODO:
# * handle verbose option
define compile-src
define generate-compilation-rule
$(patsubst .._src_%,$2%.o,$(subst /,_,$(basename $1))): $1 | $2/
@ printf "$(green)>$(nocolor) $(white)$$@$(nocolor)\n"
@ $(gcc) $3 -o $$@ -c $$< -lgcc
endef
# common part used by all library geneation
# Function that will generate all rules for building each library.
# @params:
# * 1 - library name
# * 2 - format (dynamic/static)
# * 3 - source file list
define generate-target
# generate the library name based on the wanted formats
# Generate all variables (library name based on the wanted formats, output
# directory, ...) used by each rule that will be generated.
lib-output-dir := $(dir_output)/$2/
lib-build-dir := $(dir_object)/$2/$1/
ifeq ($2,dynamic)
@ -140,10 +137,7 @@ lib-name := $$(lib-output-dir)lib$1.a
lib-cflags := $(cflags)
endif
# indicate the new lib that will be ouputed
generated-libs += $$(lib-name)
# add custom flags based on the target ABI
# add custom project-specific flags based on the target ABI
ifeq ($1,fxlibc-vhex)
lib-cflags += -D __SUPPORT_VHEX_KERNEL
else ifeq ($1,fxlibc-casio-abi-fx9860g)
@ -152,14 +146,21 @@ else ifeq ($1,fxlibc-casio-abi-fxcf50)
lib-cflags += -D __SUPPORT_CASIO_ABI_FXCG50
endif
# generate all file object name
# Generate all file compilation rules
$$(foreach source,$3,$$(eval \
$$(call compile-src,$$(source),$$(lib-build-dir),$$(lib-cflags)) \
$$(call generate-compilation-rule,$$(source),$$(lib-build-dir),$$(lib-cflags)) \
))
# link the library
# @note: we need to generate the library verion information manually
# TODO: find a better way to generate the version symbols
# Register the library building rule name
# @note:
# This rule list (lib-generation-rules) is used by the main compiling rule like
# a dependency. And it's this dependency that will involve all generated rules
# for building each library.
lib-generation-rules += $$(lib-name)
# Generate the linking library rule
# TODO
# * Find better way to generate binary files name dependency
$$(lib-name): $$(patsubst .._src_%,$$(lib-build-dir)%.o,$$(subst /,_,$$(basename $3))) | $$(lib-output-dir)
ifeq ($2,dynamic)
$(gcc) -shared -Wl,-soname=$$@ -o $$@ $$^ -nostdlib -lgcc
@ -168,21 +169,21 @@ else
endif
endef
# create all "target" variable used to determine which format
# and which libraries will be generated.
# @note:
# * we force default variable if nothing is set
# Create all "target" variable used to determine which format and which
# libraries will be generated.
# @note: we force default variable if nothing is set
target-formats := $(if $(CONFIG.FORMAT),$(CONFIG.FORMAT),static)
target-libs := $(if $(CONFIG.TARGET),$(CONFIG.TARGET),fxlibc)
# create a variable that will be updated during the generation of
# the dynamic make code generation (generated by the "foreach")
generated-libs :=
# Create a variable that will be updated during the dynamic makecode generation
# (generated by the "foreach"). This variable will list all rules generated for
# building each library and used like a dependency by the main rule.
lib-generation-rules :=
# generate the library version
# Generate the library version (used only by the dynamic library format).
lib-version := $(MAJOR).$(MINOR).$(PATCH)$(EXTRAVERSION)
# Generate all targets
# Generate all building rules
$(foreach format,$(target-formats), \
$(foreach lib,$(target-libs),$(eval \
$(call generate-target,$(lib),$(format),$(src)) \
@ -193,9 +194,9 @@ $(foreach format,$(target-formats), \
#---
# Build rule
# Build rules
#---
all: $(generated-libs)
all: $(lib-generation-rules)
version:
@ echo "$(lib-version)"
@ -209,38 +210,38 @@ version:
# @note:
# *1 - library pathname
define generate-install-rule
# generate the rulename
# @note: basically, it is the library name
# get the library name (remove path information)
lib-basename := $(notdir $1)
# genetate rule name
# Genetate rules name
lib-install-rule := $$(basename $$(lib-basename))-install
lib-uninstall-rule := $$(basename $$(lib-basename))-uninstall
# Generate the installation rule
$$(lib-install-rule):
install -d $(CONFIG.PREFIX)
install $1 -m 644 $(CONFIG.PREFIX)
# Generate the uninstallation rule
$$(lib-uninstall-rule):
rm -f $(CONFIG.PREFIX)$$(lib-basename)
# register generated rules into appropriate variable
# Register generated rules into their appropriate list
lib-installation-rules += $$(lib-install-rule)
lib-uninstallation-rules += $$(lib-uninstall-rule)
endef
# internal variable used to store all rules about installation/uninstallation
# Internal variable used to store all rules about installation/uninstallation
lib-installation-rules :=
lib-uninstallation-rules :=
# generate all installation/ unstallation rules
$(foreach libs,$(generated-libs),$(eval \
# Generate all installation/unstallation rules
$(foreach libs,$(lib-generation-rules),$(eval \
$(call generate-install-rule,$(libs)) \
))
# Generate the include install directory.
# Generate the path where include directory will be installed.
lib-install-header-dir := $(CONFIG.PREFIX)include/fxlibc
@ -249,7 +250,7 @@ lib-install-header-dir := $(CONFIG.PREFIX)include/fxlibc
#---
# Installation rules
#---
install: $(generated-libs) $(lib-installation-rules)
install: $(lib-generation-rules) $(lib-installation-rules)
rm -rf $(lib-install-header-dir)
cp -r ../include $(lib-install-header-dir)
@ -260,7 +261,7 @@ uninstall: $(lib-uninstallation-rules)
#---
# debug rule
# (internal ) debug rule
#---
#DEBUG=$(call generate-target,fxlibc-vhex,static,$(dir_objects),$(src))
DEBUG=$(call generate-install-rule,/output/static/fxlibc.a)
@ -268,7 +269,7 @@ export DEBUG
debug:
@ echo "$$DEBUG"
@ echo "target-lib: $(target-libs)"
@ echo "generated lib: $(generated-libs)"
@ echo "generated lib: $(lib-generation-rules)"
@ echo "target format: $(target-formats)"
@ echo "install-rules: $(lib-installation-rules)"
@ echo "uninstall-rules: $(lib-uninstallation-rules)"
@ -277,7 +278,7 @@ debug:
#---
# clean rules
# cleaning rules
#---
clean:
rm -rf $(dir_object)

2
src/stdio/internal/printf.h

@ -1,5 +1,5 @@
#ifndef _SRC_STDIO_INTERNAL_PRINTF_H__
# define _SRC_STDIO_INTERNAL_PRINTF_H__
#define _SRC_STDIO_INTERNAL_PRINTF_H__
#include <stddef.h>
#include <stdint.h>

72
src/stdio/internal/printf_actions.c

@ -24,11 +24,14 @@ void (*action[26])(struct printf_opt *opt, char n) = {
//---
// Disp part
// Disp part
//---
static void base_to_str(struct printf_opt *opt, uint32_t num, int base, int digits)
static void base_to_str(struct printf_opt *opt, uint32_t num, int base,
int digits)
{
char *hexa = (opt->uppercase == 1) ? "0123456789ABCDEF" : "0123456789abcdef";
char *hexa = "0123456789abcdef";
if (opt->uppercase == 1)
hexa = "0123456789ABCDEF";
opt->digits = 0;
while (num != 0 || opt->digits < digits) {
@ -53,7 +56,8 @@ static void disp_format(struct printf_opt *opt)
(opt->base[0] != '\0') + (opt->base[1] != '\0');
total = opt->width - total;
while (--total >= 0)
(*opt->disp_char)(opt, (opt->flags.zero == 1) ? '0' : ' ');
(*opt->disp_char)(opt,
(opt->flags.zero == 1) ? '0' : ' ');
}
@ -74,18 +78,25 @@ static void disp_format(struct printf_opt *opt)
//---
// Args part
// Args part
//---
static uint32_t get_arg_i(struct printf_opt *opt)
{
switch (opt->lenght) {
case 0: return ((signed char)va_arg(opt->ap, int));
case 1: return ((short int)va_arg(opt->ap, int));
case 2: return (va_arg(opt->ap, long int));
case 3: return (va_arg(opt->ap, long long int));
case 4: return (va_arg(opt->ap, intmax_t));
case 5: return (va_arg(opt->ap, size_t));
case 6: return (va_arg(opt->ap, ptrdiff_t));
case 0:
return ((signed char)va_arg(opt->ap, int));
case 1:
return ((short int)va_arg(opt->ap, int));
case 2:
return (va_arg(opt->ap, long int));
case 3:
return (va_arg(opt->ap, long long int));
case 4:
return (va_arg(opt->ap, intmax_t));
case 5:
return (va_arg(opt->ap, size_t));
case 6:
return (va_arg(opt->ap, ptrdiff_t));
}
return (va_arg(opt->ap, int));
}
@ -93,20 +104,27 @@ static uint32_t get_arg_i(struct printf_opt *opt)
static uint32_t get_arg_u(struct printf_opt *opt)
{
switch (opt->lenght) {
case 0: return ((unsigned char)va_arg(opt->ap, int));
case 1: return ((unsigned short int)va_arg(opt->ap, int));
case 2: return (va_arg(opt->ap, unsigned long int));
case 3: return (va_arg(opt->ap, unsigned long long int));
case 4: return (va_arg(opt->ap, intmax_t));
case 5: return (va_arg(opt->ap, size_t));
case 6: return (va_arg(opt->ap, ptrdiff_t));
case 0:
return ((unsigned char)va_arg(opt->ap, int));
case 1:
return ((unsigned short int)va_arg(opt->ap, int));
case 2:
return (va_arg(opt->ap, unsigned long int));
case 3:
return (va_arg(opt->ap, unsigned long long int));
case 4:
return (va_arg(opt->ap, intmax_t));
case 5:
return (va_arg(opt->ap, size_t));
case 6:
return (va_arg(opt->ap, ptrdiff_t));
}
return (va_arg(opt->ap, unsigned int));
}
//---
// Actions part.
// Actions part.
//---
static void action_str(struct printf_opt *opt, char n)
{
@ -161,9 +179,15 @@ static void action_uint(struct printf_opt *opt, char n)
// Get appropriate base
switch (n) {
case 'o': base = 8; break;
case 'x': base = 16; break;
default: base = 10; break;
case 'o':
base = 8;
break;
case 'x':
base = 16;
break;
default:
base = 10;
break;
}
// Display extra symbols if needed
@ -171,7 +195,7 @@ static void action_uint(struct printf_opt *opt, char n)
if (n == 'o') {
opt->base[0] = '0';
} else if (n == 'x') {
opt->base[0] = '0';
opt->base[0] = '0';
opt->base[1] = (opt->uppercase == 1) ? 'X' : 'x';
}
}

19
src/stdio/internal/printf_common.c

@ -7,18 +7,22 @@
//TODO: precision handling
int printf_common(struct printf_opt *opt, const char *restrict format)
{
extern int printf_get_options(struct printf_opt *opt, const char *restrict format);
extern int printf_get_options(struct printf_opt *opt,
const char *restrict format);
int saved_p;
char tmp;
int p;
p = -1;
opt->counter = 0;
opt->buffer_cursor = 0;
while (format[++p] != '\0')
{
while (format[++p] != '\0') {
// Check printable char
if (format[p] != '%' || format[p + 1] == '%') {
(*opt->disp_char)(opt, (format[p] != '%') ? format[p] : format[++p]);
tmp = format[p];
if (format[p] != '%')
tmp = format[++p];
(*opt->disp_char)(opt,tmp);
continue;
}
@ -28,9 +32,10 @@ int printf_common(struct printf_opt *opt, const char *restrict format)
// Check arg validity
if (((format[p + 1] >= 'a' && format[p + 1] <= 'z') ||
(format[p + 1] >= 'A' && format[p + 1] <= 'Z')) &&
action[(format[p + 1] | 0x20) - 'a'] != NULL) {
(*action[(format[p + 1] | 0x20) - 'a'])(opt, format[p + 1] | 0x20);
(format[p + 1] >= 'A' && format[p + 1] <= 'Z')) &&
action[(format[p + 1] | 0x20) - 'a'] != NULL) {
tmp = (format[p + 1] | 0x20) - 'a';
(*action[(int)tmp]) (opt,tmp);
p = p + 1;
continue;
}

67
src/stdio/internal/printf_options.c

@ -9,21 +9,30 @@ static int get_flags(struct printf_opt *opt, const char *restrict format)
int i;
i = -1;
opt->flags.diez = 0;
opt->flags.zero = 0;
opt->flags.diez = 0;
opt->flags.zero = 0;
opt->flags.minus = 0;
opt->flags.space = 0;
opt->flags.plus = 0;
while (format[++i] != '\0')
{
switch (format[i])
{
case '#': opt->flags.diez = 1; break;
case '0': opt->flags.zero = 1; break;
case '-': opt->flags.minus = 1; break;
case ' ': opt->flags.space = 1; break;
case '+': opt->flags.plus = 1; break;
default: return (i);
opt->flags.plus = 0;
while (format[++i] != '\0') {
switch (format[i]) {
case '#':
opt->flags.diez = 1;
break;
case '0':
opt->flags.zero = 1;
break;
case '-':
opt->flags.minus = 1;
break;
case ' ':
opt->flags.space = 1;
break;
case '+':
opt->flags.plus = 1;
break;
default:
return (i);
}
}
return (i);
@ -46,7 +55,7 @@ static int get_width(struct printf_opt *opt, const char *restrict format)
// Get static width
while (format[++i] >= '0' && format[i] <= '9')
opt->width = (opt->width * 10) + (format[i] - '0');
return (i);
return (i);
}
static int get_precision(struct printf_opt *opt, const char *restrict format)
@ -70,20 +79,30 @@ static int get_precision(struct printf_opt *opt, const char *restrict format)
// Check default precision
if (i == 0)
opt->precision = 1;
return (i);
return (i);
}
static int get_lenght(struct printf_opt *opt, const char *restrict format)
{
opt->lenght = -1;
switch (format[0])
{
case 'h': opt->lenght = (format[1] == 'h') ? 1 : 0; break;
case 'l': opt->lenght = (format[1] == 'l') ? 3 : 2; break;
case 'j': opt->lenght = 4; break;
case 'z': opt->lenght = 5; break;
case 't': opt->lenght = 6; break;
default: return (0);
switch (format[0]) {
case 'h':
opt->lenght = (format[1] == 'h') ? 1 : 0;
break;
case 'l':
opt->lenght = (format[1] == 'l') ? 3 : 2;
break;
case 'j':
opt->lenght = 4;
break;
case 'z':
opt->lenght = 5;
break;
case 't':
opt->lenght = 6;
break;
default:
return (0);
}
return ((opt->lenght == 1 || opt->lenght == 3) ? 2 : 1);
}
@ -98,7 +117,7 @@ int printf_get_options(struct printf_opt *opt, const char *restrict format)
opt->base[1] = '\0';
// Get generals opetions
i = get_flags(opt, &format[0]);
i = get_flags(opt, &format[0]);
i += get_width(opt, &format[i]);
i += get_precision(opt, &format[i]);
i += get_lenght(opt, &format[i]);

3
src/stdio/vdprintf.c

@ -34,7 +34,8 @@ static void disp_char(struct printf_opt *opt, char n)
*/
int vdprintf(int fd, const char *restrict format, va_list ap)
{
extern int printf_common(struct printf_opt *opt, const char *restrict format);
extern int printf_common(struct printf_opt *opt,
const char *restrict format);
struct printf_opt opt;
opt.fd = fd;

6
src/stdio/vsnprintf.c

@ -24,9 +24,11 @@ static void disp_fflush(struct printf_opt *opt)
** functions do not call the va_end macro. Because they invoke the va_arg macro,
** the value of ap is undefined after the call.
*/
int vsnprintf(char *restrict str, size_t size, const char *restrict format, va_list ap)
int vsnprintf(char *restrict str, size_t size, const char *restrict format,
va_list ap)
{
extern int printf_common(struct printf_opt *opt, const char *restrict format);
extern int printf_common(struct printf_opt *opt,
const char *restrict format);
struct printf_opt opt;
opt.str = str;

4
src/string/memcpy.c

@ -10,7 +10,7 @@
*/
void *memcpy(void *dest, const void *src, size_t count)
{
for (size_t i = 0 ; i < count ; i = i + 1)
((uint8_t*)dest)[i] = ((uint8_t*)src)[i];
for (size_t i = 0; i < count; i = i + 1)
((uint8_t *) dest)[i] = ((uint8_t *) src)[i];
return (dest);
}

2
src/string/memset.c

@ -10,6 +10,6 @@
void *memset(void *s, int c, size_t n)
{
while ((int)--n >= 0)
((uint8_t*)s)[n] = c;
((uint8_t *) s)[n] = c;
return (s);
}

4
src/string/strcat.c

@ -20,7 +20,7 @@ char *strcat(char *dest, char const *src)
return (0);
i = -1;
start = -1;
while (dest[++start] != '\0');
while (dest[++start] != '\0') ;
while (src[++i] != '\0')
dest[start + i] = src[i];
dest[i + start] = '\0';
@ -43,7 +43,7 @@ char *strncat(char *dest, const char *src, size_t n)
size_t dest_len = strlen(dest);
size_t i;
for (i = 0 ; i < n && src[i] != '\0' ; i++)
for (i = 0; i < n && src[i] != '\0'; i++)
dest[dest_len + i] = src[i];
dest[dest_len + i] = '\0';
return (dest);

10
src/string/strchr.c

@ -7,8 +7,8 @@
char *strchr(const char *s1, int c)
{
int i = -1;
while (s1[++i] != '\0' && s1[i] != c);
return ((s1[i] == '\0') ? NULL : (void*)&s1[i]);
while (s1[++i] != '\0' && s1[i] != c) ;
return ((s1[i] == '\0') ? NULL : (void *)&s1[i]);
}
/*
@ -19,8 +19,8 @@ char *strchr(const char *s1, int c)
char *strchrnul(const char *s1, int c)
{
int i = -1;
while (s1[++i] != '\0' && s1[i] != c);
return ((void*)&s1[i]);
while (s1[++i] != '\0' && s1[i] != c) ;
return ((void *)&s1[i]);
}
/*
@ -32,7 +32,7 @@ char *strrchr(const char *s1, int c)
void *saved;
saved = NULL;
for (int i = 0 ; s1[i] != '\0' ; i++) {
for (int i = 0; s1[i] != '\0'; i++) {
if (s1[i] == c)
saved = (void *)&s1[i];
}

3
src/string/strcmp.c

@ -34,6 +34,7 @@ int strncmp(const char *s1, const char *s2, size_t n)
if (s1 == NULL || s2 == NULL || n == 0)
return (0);
size_t i = -1;
while (++i < n - 1 && s1[i] != '\0' && s2[i] != '\0' && s1[i] == s2[i]);
while (++i < n - 1 && s1[i] != '\0' && s2[i] != '\0'
&& s1[i] == s2[i]) ;
return (s1[i] - s2[i]);
}

4
src/string/strlen.c

@ -13,7 +13,7 @@ size_t strlen(char const *str)
if (str == NULL)
return (0);
i = -1;
while (str[++i] != '\0');
while (str[++i] != '\0') ;
return (i);
}
@ -32,6 +32,6 @@ size_t strnlen(char const *str, size_t maxlen)
if (str == NULL)
return (0);
i = -1;
while (str[++i] != '\0' && (size_t)i < maxlen);
while (str[++i] != '\0' && (size_t)i < maxlen) ;
return (i);
}

Loading…
Cancel
Save