commit fcdc3c9824d96c7c517810d07a23764eff4fc097 Author: Ladebeze66 Date: Tue Dec 12 17:40:08 2023 +0100 printf diff --git a/Makefile b/Makefile new file mode 100755 index 0000000..29a4529 --- /dev/null +++ b/Makefile @@ -0,0 +1,46 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: fgras-ca +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/03/18 16:20:00 by fgras-ca #+# #+# # +# Updated: 2023/03/18 16:32:40 by fgras-ca ### ########.fr # +# # +# **************************************************************************** # + +NAME = libftprintf.a + +SOURCES = libftprintf/ft_decimal_pf.c \ + libftprintf/ft_hexnum_pf.c \ + libftprintf/ft_print_type.c \ + libftprintf/ft_ptrhex_pf.c \ + libftprintf/ft_putchar_pf.c \ + libftprintf/ft_putstr_pf.c \ + libftprintf/ft_unsigned_pf.c \ + ft_printf.c + +OBJECTS = $(SOURCES:.c=.o) + +CC = gcc + +RM = rm -f + +CFLAGS = -Wall -Werror -Wextra + +all: $(NAME) + +$(NAME): $(OBJECTS) + ar rcs $(NAME) $(OBJECTS) + +main: + $(CC) $(CFLAGS) main.c -L. -lftprintf + +clean: + $(RM) $(OBJECTS) + +fclean: clean + $(RM) $(NAME) + +re: fclean all diff --git a/en.subject.pdf b/en.subject.pdf new file mode 100644 index 0000000..12d66a6 --- /dev/null +++ b/en.subject.pdf @@ -0,0 +1,167 @@ + ft_printf + + Because ft_putnbr() and ft_putstr() aren’t enough + + Summary: + The goal of this project is pretty straightforward. You will recode printf(). +You will mainly learn about using a variable number of arguments. How cool is that?? + + It is actually pretty cool :) + Version: 10 + Contents + +I Introduction 2 + +II Common Instructions 3 + +III Mandatory part 5 + +IV Bonus part 7 + +V Submission and peer-evaluation 8 + + 1 + Chapter I +Introduction + +You will discover a popular and versatile C function: printf(). This exercise is a great +opportunity to improve your programming skills. It is of moderate difficulty. + + You will discover variadic functions in C. + The key to a successful ft_printf is a well-structured and extensible code. + + Once this assignment passed, you will be allowed to add your + ft_printf() to your libft so you can use it in your school C + projects. + + 2 + Chapter II + +Common Instructions + + • Your project must be written in C. + + • Your project must be written in accordance with the Norm. If you have bonus + files/functions, they are included in the norm check and you will receive a 0 if there + is a norm error inside. + + • Your functions should not quit unexpectedly (segmentation fault, bus error, double + free, etc) apart from undefined behaviors. If this happens, your project will be + considered non functional and will receive a 0 during the evaluation. + + • All heap allocated memory space must be properly freed when necessary. No leaks + will be tolerated. + + • If the subject requires it, you must submit a Makefile which will compile your + source files to the required output with the flags -Wall, -Wextra and -Werror, use + cc, and your Makefile must not relink. + + • Your Makefile must at least contain the rules $(NAME), all, clean, fclean and + re. + + • To turn in bonuses to your project, you must include a rule bonus to your Makefile, + which will add all the various headers, librairies or functions that are forbidden on + the main part of the project. Bonuses must be in a different file _bonus.{c/h} if + the subject does not specify anything else. Mandatory and bonus part evaluation + is done separately. + + • If your project allows you to use your libft, you must copy its sources and its + associated Makefile in a libft folder with its associated Makefile. Your project’s + Makefile must compile the library by using its Makefile, then compile the project. + + • We encourage you to create test programs for your project even though this work + won’t have to be submitted and won’t be graded. It will give you a chance + to easily test your work and your peers’ work. You will find those tests especially + useful during your defence. Indeed, during defence, you are free to use your tests + and/or the tests of the peer you are evaluating. + + • Submit your work to your assigned git repository. Only the work in the git reposi- + tory will be graded. If Deepthought is assigned to grade your work, it will be done + + 3 + ft_printf Because ft_putnbr() and ft_putstr() aren’t enough + +after your peer-evaluations. If an error happens in any section of your work during +Deepthought’s grading, the evaluation will stop. + + 4 + Chapter III +Mandatory part + +Program name libftprintf.a +Turn in files Makefile, *.h, */*.h, *.c, */*.c +Makefile NAME, all, clean, fclean, re +External functs. malloc, free, write, + va_start, va_arg, va_copy, va_end +Libft authorized Yes +Description Write a library that contains ft_printf(), a + function that will mimic the original printf() + +You have to recode the printf() function from libc. + +The prototype of ft_printf() is: + int ft_printf(const char *, ...); + +Here are the requirements: + +• Don’t implement the buffer management of the original printf(). +• Your function has to handle the following conversions: cspdiuxX% +• Your function will be compared against the original printf(). +• You must use the command ar to create your library. + + Using the libtool command is forbidden. +• Your libftprintf.a has to be created at the root of your repository. + + 5 + ft_printf Because ft_putnbr() and ft_putstr() aren’t enough + +You have to implement the following conversions: +• %c Prints a single character. +• %s Prints a string (as defined by the common C convention). +• %p The void * pointer argument has to be printed in hexadecimal format. +• %d Prints a decimal (base 10) number. +• %i Prints an integer in base 10. +• %u Prints an unsigned decimal (base 10) number. +• %x Prints a number in hexadecimal (base 16) lowercase format. +• %X Prints a number in hexadecimal (base 16) uppercase format. +• %% Prints a percent sign. + + 6 + Chapter IV +Bonus part + +You don’t have to do all the bonuses. + Bonus list: + • Manage any combination of the following flags: ’-0.’ and the field minimum width + under all conversions. + • Manage all the following flags: ’# +’ (Yes, one of them is a space) + + If you plan to complete the bonus part, think about the + implementation of your extra features from the start. This way, + you will avoid the pitfalls of a naive approach. + + The bonus part will only be assessed if the mandatory part is + PERFECT. Perfect means the mandatory part has been integrally done + and works without malfunctioning. If you have not passed ALL the + mandatory requirements, your bonus part will not be evaluated at all. + + 7 + Chapter V +Submission and peer-evaluation + +Turn in your assignment in your Git repository as usual. Only the work inside your repos- +itory will be evaluated during the defense. Don’t hesitate to double check the names of +your files to ensure they are correct. + + Once this assignment passed, you will be allowed to add your ft_printf() to your +libft so you can use it in your school C projects. + + ++++++++++[>+>+++>+++++++>++++++++++<<<<-]>>>.>---.++++++++++++.++.+++ + +++.--.<<++.>>------.------------.+++++++++++++.<<.>>++++++.------------ + .-------. +++++++++++++++++++.<<.>>----------------.+++++.+++++++++.--- + ----------.--.+ ++++++++++++++++.--------.+++++++++++++.<<.>>---------- + -------------.+++.+++ ++++.---.----.+++++++++++++++++.--------------- + --.-.<<.>>+++++.+++++.<<.>-------... + + 8 + diff --git a/ft_printf.c b/ft_printf.c new file mode 100755 index 0000000..4be5906 --- /dev/null +++ b/ft_printf.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/03/18 16:36:14 by fgras-ca #+# #+# */ +/* Updated: 2023/03/21 14:51:46 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int ft_printf(const char *s, ...) +{ + int i; + int print_len; + va_list args; + + i = 0; + print_len = 0; + va_start(args, s); + while (s[i]) + { + if (s[i] == '%') + { + print_len += ft_print_type(&args, s[i + 1]); + i++; + } + else + print_len += ft_putchar_pf(s[i]); + i++; + } + va_end(args); + return (print_len); +} diff --git a/ft_printf.h b/ft_printf.h new file mode 100755 index 0000000..5ba269e --- /dev/null +++ b/ft_printf.h @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/03/18 15:16:04 by fgras-ca #+# #+# */ +/* Updated: 2023/03/18 15:56:31 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_PRINTF_H +# define FT_PRINTF_H +# include +# include +# include + +int ft_printf(const char *s, ...); +int ft_decimal_pf(int c); +int ft_hexnum_pf(unsigned int a, char c); +int ft_ptrhex_pf(unsigned long c, int pref); +int ft_unsigned_pf(unsigned int c); +int ft_print_type(va_list *args, char c); +int ft_putchar_pf(char c); +int ft_putstr_pf(char *s); + +#endif diff --git a/libftprintf/ft_decimal_pf.c b/libftprintf/ft_decimal_pf.c new file mode 100755 index 0000000..8a29be9 --- /dev/null +++ b/libftprintf/ft_decimal_pf.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_decimal_pf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/03/17 18:32:20 by fgras-ca #+# #+# */ +/* Updated: 2023/03/21 15:47:12 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../ft_printf.h" + +int ft_decimal_pf(int c) +{ + int count; + + count = 0; + if (c == 0) + return (write(1, "0", 1)); + if (c == -2147483648) + return (write(1, "-2147483648", 11)); + if (c < 0) + { + count += write(1, "-", 1); + c *= -1; + } + if (c >= 10) + count += ft_decimal_pf(c / 10); + write(1, &"0123456789"[c % 10], 1); + return (count + 1); +} diff --git a/libftprintf/ft_hexnum_pf.c b/libftprintf/ft_hexnum_pf.c new file mode 100755 index 0000000..928df61 --- /dev/null +++ b/libftprintf/ft_hexnum_pf.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_hexnum_pf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/03/18 14:34:00 by fgras-ca #+# #+# */ +/* Updated: 2023/03/18 16:42:41 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../ft_printf.h" + +int ft_hexnum_pf(unsigned int a, char c) +{ + int count; + + count = 0; + if (a >= 16) + count += ft_hexnum_pf(a / 16, c); + if (c == 'x') + write(1, &"0123456789abcdef"[a % 16], 1); + if (c == 'X') + write(1, &"0123456789ABCDEF"[a % 16], 1); + return (count +1); +} diff --git a/libftprintf/ft_print_type.c b/libftprintf/ft_print_type.c new file mode 100755 index 0000000..0b3f5e9 --- /dev/null +++ b/libftprintf/ft_print_type.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_print_type.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/03/18 15:28:13 by fgras-ca #+# #+# */ +/* Updated: 2023/03/21 17:13:33 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../ft_printf.h" + +int ft_print_type(va_list *args, char c) +{ + if (c == 'c') + return (ft_putchar_pf(va_arg((*args), int))); + else if (c == 's') + return (ft_putstr_pf(va_arg((*args), char *))); + else if (c == 'p') + return (ft_ptrhex_pf(va_arg((*args), unsigned long), 1)); + else if (c == 'd') + return (ft_decimal_pf(va_arg((*args), int))); + else if (c == 'i') + return (ft_decimal_pf(va_arg((*args), int))); + else if (c == 'u') + return (ft_unsigned_pf(va_arg((*args), unsigned int))); + else if (c == 'x' || c == 'X') + return (ft_hexnum_pf(va_arg((*args), unsigned int), c)); + else if (c == '%') + return (write(1, "%", 1)); + else + return (0); +} diff --git a/libftprintf/ft_ptrhex_pf.c b/libftprintf/ft_ptrhex_pf.c new file mode 100755 index 0000000..a714a7c --- /dev/null +++ b/libftprintf/ft_ptrhex_pf.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ptrhex_pf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/03/18 13:31:16 by fgras-ca #+# #+# */ +/* Updated: 2023/03/21 14:45:30 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../ft_printf.h" + +int ft_ptrhex_pf(unsigned long c, int pref) +{ + int count; + + count = 0; + if (!c) + return (write(1, "(nil)", 5)); + if (pref == 1) + { + count += write(1, "0x", 2); + pref = 0; + } + if (c >= 16) + count += ft_ptrhex_pf(c / 16, pref); + write(1, &"0123456789abcdef"[c % 16], 1); + return (count + 1); +} diff --git a/libftprintf/ft_putchar_pf.c b/libftprintf/ft_putchar_pf.c new file mode 100755 index 0000000..7d60512 --- /dev/null +++ b/libftprintf/ft_putchar_pf.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar_pf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/03/17 17:54:21 by fgras-ca #+# #+# */ +/* Updated: 2023/03/18 17:01:04 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../ft_printf.h" + +int ft_putchar_pf(char c) +{ + write(1, &c, 1); + return (1); +} diff --git a/libftprintf/ft_putstr_pf.c b/libftprintf/ft_putstr_pf.c new file mode 100755 index 0000000..78ba4be --- /dev/null +++ b/libftprintf/ft_putstr_pf.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr_pf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/03/17 18:08:18 by fgras-ca #+# #+# */ +/* Updated: 2023/03/18 17:49:52 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../ft_printf.h" + +int ft_putstr_pf(char *s) +{ + int i; + + i = 0; + if (!s) + return (write(1, "(null)", 6)); + while (s[i]) + { + write(1, &s[i], 1); + i++; + } + return (i); +} diff --git a/libftprintf/ft_unsigned_pf.c b/libftprintf/ft_unsigned_pf.c new file mode 100755 index 0000000..2c5d815 --- /dev/null +++ b/libftprintf/ft_unsigned_pf.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_unsigned_pf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/03/18 13:12:40 by fgras-ca #+# #+# */ +/* Updated: 2023/03/21 14:47:01 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../ft_printf.h" + +int ft_unsigned_pf(unsigned int c) +{ + int count; + + count = 0; + if (c >= 10) + count += ft_unsigned_pf(c / 10); + write(1, &"0123456789"[c % 10], 1); + return (count + 1); +}