diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..dab2f43 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,16 @@ +{ + "configurations": [ + { + "name": "linux-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "/usr/bin/gcc", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "linux-gcc-x64", + "compilerArgs": [] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..9393395 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,26 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "cwd": "~/ft_printf", + "environment": [], + "program": "~/baco/ft_printf/build/Debug/outDebug", + "internalConsoleOptions": "openOnSessionStart", + "MIMode": "gdb", + "miDebuggerPath": "/usr/bin/gdb", + "externalConsole": false, + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..862cfc6 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,30 @@ +{ + "C_Cpp_Runner.cCompilerPath": "/usr/bin/gcc", + "C_Cpp_Runner.cppCompilerPath": "/usr/bin/g++", + "C_Cpp_Runner.debuggerPath": "/usr/bin/gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "", + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ] +} \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..eac2f1e --- /dev/null +++ b/Makefile @@ -0,0 +1,101 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2022/01/18 15:11:16 by gbaconni #+# #+# # +# Updated: 2022/04/13 07:56:46 by gbaconni ### lausanne.ch # +# # +# **************************************************************************** # +# +# make ft_printf +# make all +# make clean +# make fclean +# make re +# make +# + +NAME = ft_printf + +LIBFTPRINTF = libftprintf +LFLAGS = -L. -L$(LIBFTPRINTF) -lftprintf + +SRC = main.c + +INCLUDE = -I. -I$(LIBFTPRINTF) +HDR = $(LIBFTPRINTF)/libftprintf.h + +CC = gcc +CFLAGS = -Wall -Wextra -Werror +ifeq ($(FAST), 1) + CFLAGS += -v -pipe -O3 -ffast-math -fomit-frame-pointer -funroll-loops + #CFLAGS += -v -pipe -Ofast -ffast-math -funroll-loops -fomit-frame-pointer +endif +ifeq ($(DEBUG), 1) + CFLAGS += -g -O1 -fsanitize=address -fsanitize=undefined -fsanitize=signed-integer-overflow + #CFLAGS += -v -g -O1 -fsanitize=address -fsanitize=undefined -fsanitize=signed-integer-overflow +endif + +RM = rm +RMFLAGS = -f + +VALGRIND = valgrind +VALGRINDFLAGS = --quiet --leak-check=full --show-leak-kinds=all + +NORMINETTE = norminette +NORMINETTEFLAGS = -o + +MAKE = make + +all: $(NAME) + +$(NAME): + @$(MAKE) -C $(LIBFTPRINTF) all >/dev/null 2>&1 + @$(CC) $(CFLAGS) $(INCLUDE) $(SRC) $(LFLAGS) -o $@ + +clean: + @$(RM) $(RMFLAGS) $(NAME) + +fclean: clean + @$(MAKE) -C $(LIBFTPRINTF) fclean + +re: fclean all + +check: + @$(MAKE) -C $(LIBFTPRINTF) check + +debug: + @$(MAKE) -C $(LIBFTPRINTF) re DEBUG=1 + @$(MAKE) re DEBUG=1 + +fast: + @$(MAKE) -C $(LIBFTPRINTF) re FAST=1 + @$(MAKE) re FAST=1 + +test: clean $(NAME) + @./$(NAME) "%c" C || true + @./$(NAME) "%s" "42 Lausanne" || true + @./$(NAME) "%d" 42 || true + +test2: +ifneq ($(DEBUG), 1) + @$(MAKE) test DEBUG=1 +endif + +test3: +ifneq ($(FAST), 1) + @$(MAKE) test FAST=1 +endif + +test4: clean $(NAME) +ifneq ($(DEBUG), 1) + @$(MAKE) test4 DEBUG=1 +else + @$(VALGRIND) $(VALGRINDFLAGS) ./$(NAME) "%c" C || true + @$(VALGRIND) $(VALGRINDFLAGS) ./$(NAME) "%s" "42 Lausanne" || true + @$(VALGRIND) $(VALGRINDFLAGS) ./$(NAME) "%d" 42 || true +endif + diff --git a/ft_printf b/ft_printf new file mode 100755 index 0000000..8334ad4 Binary files /dev/null and b/ft_printf differ diff --git a/libftprintf/Makefile b/libftprintf/Makefile new file mode 100644 index 0000000..a043fd1 --- /dev/null +++ b/libftprintf/Makefile @@ -0,0 +1,127 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2022/01/18 15:11:16 by gbaconni #+# #+# # +# Updated: 2022/04/13 01:20:20 by gbaconni ### lausanne.ch # +# # +# **************************************************************************** # +# +# make libftprintf.a +# make all +# make clean +# make fclean +# make re +# make +# + +NAME = libftprintf.a + +LIB = libftprintf.so +LIBFT = libft +LFLAGS = -L. + +SRC = \ + $(LIBFT)/ft_memset.c \ + $(LIBFT)/ft_bzero.c \ + $(LIBFT)/ft_calloc.c \ + $(LIBFT)/ft_strlen.c \ + $(LIBFT)/ft_itoa.c \ + ft_vprintf_char.c \ + ft_vprintf_string.c \ + ft_vprintf_pointer.c \ + ft_vprintf_decimal.c \ + ft_vprintf_hexadecimal.c \ + ft_vprintf_percent.c \ + ft_vprintf_other.c \ + ft_vprintf.c \ + ft_printf.c + +SRC_BONUS = + +OBJ = ${SRC:.c=.o} +OBJ_BONUS = ${SRC_BONUS:.c=.o} + +INCLUDE = -I. +HDR = libftprintf.h + +CC = gcc +CFLAGS = -Wall -Wextra -Werror +ifeq ($(FAST), 1) + CFLAGS += -v -pipe -O3 -ffast-math -fomit-frame-pointer -funroll-loops + #CFLAGS += -v -pipe -Ofast -ffast-math -funroll-loops -fomit-frame-pointer +endif +ifeq ($(DEBUG), 1) + CFLAGS += -g -O1 -fsanitize=address -fsanitize=undefined -fsanitize=signed-integer-overflow + #CFLAGS += -v -g -O1 -fsanitize=address -fsanitize=undefined -fsanitize=signed-integer-overflow +endif + +AR = ar +ARFLAGS = -rcs + +RM = rm +RMFLAGS = -vf + +VALGRIND = valgrind +VALGRINDFLAGS = --quiet --leak-check=full --show-leak-kinds=all + +NORMINETTE = norminette +NORMINETTEFLAGS = -o + +MAKE = make + +all: $(NAME) + +$(NAME): $(OBJ) + $(AR) $(ARFLAGS) $@ $^ + +%.o: %.c + $(CC) $(CFLAGS) $(INCLUDE) $< -c -o $@ + +clean: + $(RM) $(RMFLAGS) $(OBJ) $(OBJ_BONUS) + @$(RM) $(RMFLAGS) $(LIB) || true + +fclean: clean + $(RM) $(RMFLAGS) $(NAME) + +re: fclean all + +bonus: $(OBJ_BONUS) + $(AR) $(ARFLAGS) $(NAME) $(OBJ_BONUS) + +rebonus: fclean bonus + +so: $(OBJ) $(OBJ_BONUS) + @$(CC) $(CFLAGS) -shared -fPIC -o $(LIB) $(OBJ) $(OBJ_BONUS) + +check: + @$(NORMINETTE) $(NORMINETTEFLAGS) $(HDR) $(SRC) ${SRC_BONUS} 2>&1 | grep -v '\.[ch]: OK!' || true + +debug: + @$(MAKE) re DEBUG=1 + +fast: + @$(MAKE) re FAST=1 + +func: + @echo "external functions:" + @nm -o *.o | grep ' U _' | grep -v ' U _ft' || true + @echo "" + @echo "allowed functions:" + @nm -o *.o | grep ' U _' | grep -e malloc -e free -e write -e va_start -e va_arg -e va_copy -e va_end || true + @echo "" + @echo "forbidden functions:" + @nm -o *.o | grep ' U _' | grep -v ' U _ft' | grep -v -e malloc -e free -e write -e va_start -e va_arg -e va_copy -e va_end || true + @echo "" + +ft: + @echo "objects:" + @ar -t $(NAME) | grep '\.o$$' + @echo "" + @echo "functions:" + @nm $(NAME) | grep T | grep -o 'ft_.*' | sort -u + diff --git a/libftprintf/ft_printf.c b/libftprintf/ft_printf.c new file mode 100644 index 0000000..c48d305 --- /dev/null +++ b/libftprintf/ft_printf.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/01/18 16:17:58 by gbaconni #+# #+# */ +/* Updated: 2022/04/13 00:21:03 by gbaconni ### lausanne.ch */ +/* */ +/* ************************************************************************** */ + +#include "libftprintf.h" + +int ft_printf(const char *format, ...) +{ + va_list ap; + int ret; + + ret = 0; + va_start(ap, format); + ret += ft_vprintf(format, ap); + va_end(ap); + return (ret); +} diff --git a/libftprintf/ft_vprintf.c b/libftprintf/ft_vprintf.c new file mode 100644 index 0000000..3b2734d --- /dev/null +++ b/libftprintf/ft_vprintf.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vprintf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/01/20 11:43:52 by gbaconni #+# #+# */ +/* Updated: 2022/04/13 01:24:00 by gbaconni ### lausanne.ch */ +/* */ +/* ************************************************************************** */ + +#include "libftprintf.h" + +int ft_vprintf(char const *format, va_list ap) +{ + int ret; + + ret = 0; + while (*format) + { + if (*format++ == '%') + { + if (*format == '%') + ret += ft_vprintf_percent(format, ap); + else if (*format == 'c') + ret += ft_vprintf_char(format, ap); + else if (*format == 's') + ret += ft_vprintf_string(format, ap); + else if (*format == 'p') + ret += ft_vprintf_pointer(format, ap); + else if (*format == 'd' || *format == 'i' || *format == 'u') + ret += ft_vprintf_decimal(format, ap); + else if (*format == 'x' || *format == 'X') + ret += ft_vprintf_hexadecimal(format, ap); + } + else + ret += ft_vprintf_other(format, ap); + format++; + } + return (ret); +} diff --git a/libftprintf/ft_vprintf_char.c b/libftprintf/ft_vprintf_char.c new file mode 100644 index 0000000..1f550dd --- /dev/null +++ b/libftprintf/ft_vprintf_char.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vprintf_char.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/01/20 11:54:48 by gbaconni #+# #+# */ +/* Updated: 2022/04/12 08:20:53 by gbaconni ### lausanne.ch */ +/* */ +/* ************************************************************************** */ + +#include "libftprintf.h" + +int ft_vprintf_char(const char *format, va_list ap) +{ + char c; + int ret; + + (void) format; + ret = 0; + c = (char) va_arg(ap, int); + write(1, &c, 1); + ret++; + return (ret); +} diff --git a/libftprintf/ft_vprintf_decimal.c b/libftprintf/ft_vprintf_decimal.c new file mode 100644 index 0000000..bc8c3a1 --- /dev/null +++ b/libftprintf/ft_vprintf_decimal.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vprintf_decimal.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/01/20 11:55:02 by gbaconni #+# #+# */ +/* Updated: 2022/04/12 23:51:56 by gbaconni ### lausanne.ch */ +/* */ +/* ************************************************************************** */ + +#include "libftprintf.h" + +int ft_vprintf_decimal(const char *format, va_list ap) +{ + int ret; + int len; + int d; + char *s; + + (void) format; + ret = 0; + len = 0; + d = va_arg(ap, int); + s = ft_itoa(d); + len = ft_strlen(s); + write(1, s, len); + ret += len; + free(s); + return (ret); +} diff --git a/libftprintf/ft_vprintf_hexadecimal.c b/libftprintf/ft_vprintf_hexadecimal.c new file mode 100644 index 0000000..2725ff3 --- /dev/null +++ b/libftprintf/ft_vprintf_hexadecimal.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vprintf_hexadecimal.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/01/20 11:55:33 by gbaconni #+# #+# */ +/* Updated: 2022/04/13 08:00:40 by gbaconni ### lausanne.ch */ +/* */ +/* ************************************************************************** */ + +#include "libftprintf.h" + +int ft_vprintf_hexadecimal(const char *format, va_list ap) +{ + (void) ap; + (void) format; + return (0); +} diff --git a/libftprintf/ft_vprintf_other.c b/libftprintf/ft_vprintf_other.c new file mode 100644 index 0000000..92ff2f4 --- /dev/null +++ b/libftprintf/ft_vprintf_other.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vprintf_other.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/01/20 12:58:03 by gbaconni #+# #+# */ +/* Updated: 2022/04/13 08:01:05 by gbaconni ### lausanne.ch */ +/* */ +/* ************************************************************************** */ + +#include "libftprintf.h" + +int ft_vprintf_other(const char *format, va_list ap) +{ + int ret; + + (void) ap; + ret = 0; + write(1, format, 1); + ret++; + return (ret); +} diff --git a/libftprintf/ft_vprintf_percent.c b/libftprintf/ft_vprintf_percent.c new file mode 100644 index 0000000..08a45bb --- /dev/null +++ b/libftprintf/ft_vprintf_percent.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vprintf_percent.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/01/20 11:55:58 by gbaconni #+# #+# */ +/* Updated: 2022/04/13 00:01:47 by gbaconni ### lausanne.ch */ +/* */ +/* ************************************************************************** */ + +#include "libftprintf.h" + +int ft_vprintf_percent(const char *format, va_list ap) +{ + int ret; + + (void) format; + (void) ap; + ret = 0; + write(1, "%", 1); + ret++; + return (ret); +} diff --git a/libftprintf/ft_vprintf_pointer.c b/libftprintf/ft_vprintf_pointer.c new file mode 100644 index 0000000..3e509a0 --- /dev/null +++ b/libftprintf/ft_vprintf_pointer.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vprintf_pointer.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/01/20 11:56:16 by gbaconni #+# #+# */ +/* Updated: 2022/04/13 08:00:14 by gbaconni ### lausanne.ch */ +/* */ +/* ************************************************************************** */ + +#include "libftprintf.h" + +int ft_vprintf_pointer(const char *format, va_list ap) +{ + (void) ap; + (void) format; + return (0); +} diff --git a/libftprintf/ft_vprintf_string.c b/libftprintf/ft_vprintf_string.c new file mode 100644 index 0000000..c0703f0 --- /dev/null +++ b/libftprintf/ft_vprintf_string.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vprintf_string.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/01/20 11:56:37 by gbaconni #+# #+# */ +/* Updated: 2022/04/12 23:50:02 by gbaconni ### lausanne.ch */ +/* */ +/* ************************************************************************** */ + +#include "libftprintf.h" + +int ft_vprintf_string(const char *format, va_list ap) +{ + int ret; + int len; + char *s; + + (void) format; + ret = 0; + len = 0; + s = va_arg(ap, char *); + len = ft_strlen(s); + write(1, s, len); + ret += len; + return (ret); +} diff --git a/libftprintf/libft/ft_bzero.c b/libftprintf/libft/ft_bzero.c new file mode 100644 index 0000000..306899f --- /dev/null +++ b/libftprintf/libft/ft_bzero.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_bzero.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbaconni +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/10/11 14:32:52 by gbaconni #+# #+# */ +/* Updated: 2021/10/23 16:21:04 by gbaconni ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_bzero(void *s, size_t n) +{ + ft_memset(s, '\0', n); +} diff --git a/libftprintf/libft/ft_calloc.c b/libftprintf/libft/ft_calloc.c new file mode 100644 index 0000000..2d14162 --- /dev/null +++ b/libftprintf/libft/ft_calloc.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_calloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbaconni +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/10/11 14:33:01 by gbaconni #+# #+# */ +/* Updated: 2021/10/23 16:21:19 by gbaconni ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +void *ft_calloc(size_t count, size_t size) +{ + void *s; + size_t n; + + n = count * size; + s = (void *) malloc(n); + if (s == NULL) + return (NULL); + ft_bzero(s, n); + return (s); +} diff --git a/libftprintf/libft/ft_itoa.c b/libftprintf/libft/ft_itoa.c new file mode 100644 index 0000000..7336159 --- /dev/null +++ b/libftprintf/libft/ft_itoa.c @@ -0,0 +1,77 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbaconni +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/10/11 14:33:40 by gbaconni #+# #+# */ +/* Updated: 2021/10/23 16:23:10 by gbaconni ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int ft_intlen(int n); +static char *ft_strrev(char *s); + +char *ft_itoa(int n) +{ + char *s; + int i; + long nbr; + + s = (char *) ft_calloc((ft_intlen(n) + 1), sizeof(char)); + if (s == NULL) + return (NULL); + i = 0; + nbr = n; + if (nbr == 0) + s[i++] = '0'; + if (nbr < 0) + nbr = -nbr; + while (nbr > 0) + { + s[i++] = '0' + nbr % 10; + nbr /= 10; + } + if (n < 0) + s[i++] = '-'; + s[i] = '\0'; + return (ft_strrev(s)); +} + +static int ft_intlen(int n) +{ + size_t len; + + len = 0; + if (n < 0 || n == 0) + len++; + while (!(n < 1 && n > -1)) + { + n /= 10; + len++; + } + return (len); +} + +static char *ft_strrev(char *s) +{ + int len; + int i; + int j; + char c; + + len = ft_strlen(s); + i = 0; + while (i < (len / 2)) + { + j = len - 1 - i; + c = s[i]; + s[i] = s[j]; + s[j] = c; + i++; + } + return (s); +} diff --git a/libftprintf/libft/ft_memset.c b/libftprintf/libft/ft_memset.c new file mode 100644 index 0000000..78537b2 --- /dev/null +++ b/libftprintf/libft/ft_memset.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbaconni +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/10/11 14:34:07 by gbaconni #+# #+# */ +/* Updated: 2021/10/23 16:24:58 by gbaconni ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memset(void *b, int c, size_t len) +{ + unsigned char *s; + + s = (unsigned char *)b; + while (len--) + *s++ = (unsigned char)c; + return (b); +} diff --git a/libftprintf/libft/ft_strlen.c b/libftprintf/libft/ft_strlen.c new file mode 100644 index 0000000..5e5c51c --- /dev/null +++ b/libftprintf/libft/ft_strlen.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbaconni +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/10/11 14:35:03 by gbaconni #+# #+# */ +/* Updated: 2021/10/24 13:11:12 by gbaconni ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlen(const char *s) +{ + const char *str; + + str = s; + while (*str != '\0') + str++; + return (str - s); +} +/* +size_t ft_strlen(const char *s) +{ + size_t len; + + len = 0; + while (s[len] != '\0') + len++; + return (len); +} +*/ diff --git a/libftprintf/libft/libft.h b/libftprintf/libft/libft.h new file mode 100644 index 0000000..03c9959 --- /dev/null +++ b/libftprintf/libft/libft.h @@ -0,0 +1,76 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbaconni +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/10/11 14:35:55 by gbaconni #+# #+# */ +/* Updated: 2021/10/25 10:49:28 by gbaconni ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_H +# define LIBFT_H +# include +# include + +/* Part 1 */ + +int ft_isalpha(int c); +int ft_isdigit(int c); +int ft_isalnum(int c); +int ft_isascii(int c); +int ft_isprint(int c); +size_t ft_strlen(const char *s); +void *ft_memset(void *b, int c, size_t len); +void ft_bzero(void *s, size_t n); +void *ft_memcpy(void *dst, const void *src, size_t n); +void *ft_memmove(void *dst, const void *src, size_t len); +size_t ft_strlcpy(char *dst, const char *src, size_t dstsize); +size_t ft_strlcat(char *dst, const char *src, size_t dstsize); +int ft_toupper(int c); +int ft_tolower(int c); +char *ft_strchr(const char *s, int c); +char *ft_strrchr(const char *s, int c); +int ft_strncmp(const char *s1, const char *s2, size_t n); +void *ft_memchr(const void *s, int c, size_t n); +int ft_memcmp(const void *s1, const void *s2, size_t n); +char *ft_strnstr(const char *haystack, const char *needle, size_t len); +int ft_atoi(const char *str); +void *ft_calloc(size_t count, size_t size); +char *ft_strdup(const char *s1); + +/* Part 2 */ + +char *ft_substr(char const *s, unsigned int start, size_t len); +char *ft_strjoin(char const *s1, char const *s2); +char *ft_strtrim(char const *s1, char const *set); +char **ft_split(char const *s, char c); +char *ft_itoa(int n); +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); +void ft_striteri(char *s, void (*f)(unsigned int, char*)); +void ft_putchar_fd(char c, int fd); +void ft_putstr_fd(char *s, int fd); +void ft_putendl_fd(char *s, int fd); +void ft_putnbr_fd(int n, int fd); + +/* Bonus */ + +typedef struct s_list +{ + void *content; + struct s_list *next; +} t_list; + +t_list *ft_lstnew(void *content); +void ft_lstadd_front(t_list **alst, t_list *new); +int ft_lstsize(t_list *lst); +t_list *ft_lstlast(t_list *lst); +void ft_lstadd_back(t_list **alst, t_list *new); +void ft_lstdelone(t_list *lst, void (*del)(void *)); +void ft_lstclear(t_list **lst, void (*del)(void *)); +void ft_lstiter(t_list *lst, void (*f)(void *)); +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); + +#endif diff --git a/libftprintf/libftprintf.h b/libftprintf/libftprintf.h new file mode 100644 index 0000000..9abea30 --- /dev/null +++ b/libftprintf/libftprintf.h @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libftprintf.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/01/18 15:26:06 by gbaconni #+# #+# */ +/* Updated: 2022/04/12 07:10:26 by gbaconni ### lausanne.ch */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFTPRINTF_H +# define LIBFTPRINTF_H +# include "libft/libft.h" +# include +# include +# include + +/* Mandatory */ + +int ft_printf(const char *format, ...); +int ft_vprintf(char const *format, va_list ap); +int ft_vprintf_char(const char *format, va_list ap); +int ft_vprintf_string(const char *format, va_list ap); +int ft_vprintf_pointer(const char *format, va_list ap); +int ft_vprintf_decimal(const char *format, va_list ap); +int ft_vprintf_hexadecimal(const char *format, va_list ap); +int ft_vprintf_percent(const char *format, va_list ap); +int ft_vprintf_other(const char *format, va_list ap); + +/* Bonus */ +#endif diff --git a/main.c b/main.c new file mode 100644 index 0000000..3a4a7a5 --- /dev/null +++ b/main.c @@ -0,0 +1,116 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: baco +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/04/13 06:58:46 by gbaconni #+# #+# */ +/* Updated: 2022/04/13 07:54:40 by gbaconni ### lausanne.ch */ +/* */ +/* ************************************************************************** */ + +#include // printf +#include // va_list, va_start, va_end +#include // write +#include // strlen +#include // malloc, free +#include // isdigit +#include "libftprintf/libftprintf.h" + +int isnumber(char *s); +int isstring(char *s); + +int isnumber(char *s) +{ + int i; + + i = 0; + while (s[i]) + { + if (!isdigit(s[i])) + return (0); + i++; + } + return (1); +} + +int isstring(char *s) +{ + int i; + + i = 0; + while (s[i]) + { + if (!isprint(s[i])) + return (0); + i++; + } + return (1); +} + +/* +int diff_printf_decimal(const char *format, va_list ap); +int diff_printf_decimal(const char *format, va_list ap) +{ + int ret; + int d; + char *s; + + ret = 0; + d = 0; + d = atoi(argv[2]); + ret = printf(format, d); + printf(": %d = printf(\"%s\", %d)\n", ret, format, d); + ret = ft_printf(format, d); + printf(": %d = ft_printf(\"%s\", %d)\n", ret, format, d); + return (ret); +} +*/ + +int main(int argc, char *argv[]) +{ + int ret; + char *format; + int d; + char c; + char *s; + + d = 0; + c = '\0'; + s = ""; + (void)s; + if (argc > 1) + { + format = argv[1]; + if (isnumber(argv[2])) + { + d = atoi(argv[2]); + ret = printf(format, d); + printf(": %d = printf(\"%s\", %d)\n", ret, format, d); + ret = ft_printf(format, d); + printf(": %d = ft_printf(\"%s\", %d)\n", ret, format, d); + } + else if (ft_strlen(argv[2]) == 1 && isalnum(argv[2][0])) + { + c = argv[2][0]; + ret = printf(format, c); + printf(": %d = printf(\"%s\", '%c')\n", ret, format, c); + ret = ft_printf(format, c); + printf(": %d = ft_printf(\"%s\", '%c')\n", ret, format, c); + } + else if (ft_strlen(argv[2]) > 1 && isstring(argv[2])) + { + s = argv[2]; + ret = printf(format, s); + printf(": %d = printf(\"%s\", \"%s\")\n", ret, format, s); + ret = ft_printf(format, s); + printf(": %d = ft_printf(\"%s\", \"%s\")\n", ret, format, s); + } + else + { + printf("Error: invalid argument\n"); + } + } + return (0); +}