intra to gitea

This commit is contained in:
lfirmin
2025-11-16 09:58:07 +01:00
commit 50c15b30e1
163 changed files with 12672 additions and 0 deletions
+61
View File
@@ -0,0 +1,61 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: lfirmin [email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/08/12 07:07:31 by lfirmin #+# #+# #
# Updated: 2024/08/12 09:15:05 by lfirmin ### ########.fr #
# #
# **************************************************************************** #
NAME = libftprintf.a
HEADER = ./
SRC = ft_putchar.c ft_put_ptr.c ft_conv_ith.c ft_hex_len.c \
ft_sorting.c ft_printf.c ft_print_ith.c ft_printchar.c \
ft_printstr.c ft_printpercent.c ft_print_unsigned.c \
ft_print_nbr.c ft_print_ptr.c ft_adrr_len.c ft_print_null.c \
ft_itoa.c ft_unsigned_itoa.c ft_count.c
CC = cc
CFLAGS = -Wall -Wextra -Werror
INCLUDE = -I $(HEADER)
OBJ_DIR = obj/
OBJ = $(addprefix $(OBJ_DIR), $(SRC:.c=.o))
GREEN = \033[0;32m
YELLOW = \033[0;33m
RESET = \033[0m
WHITE = \033[0;97m
LOADING_CHARS = ⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏
all: $(NAME)
$(NAME): $(OBJ)
@printf "$(YELLOW)Compiling ft_printf, Please wait...$(RESET)"
@for char in $(LOADING_CHARS); do \
printf "\r$(YELLOW)Compiling ft_print, Please wait... $$char$(RESET)"; \
sleep 0.1; \
done
@ar rc $(NAME) $(OBJ)``
@ranlib $(NAME)
@printf "\r$(GREEN)Great news ! $(WHITE)ft_printf compiled successfully ! $(RESET)\n"
$(OBJ_DIR)%.o: %.c
@mkdir -p $(OBJ_DIR)
@$(CC) $(CFLAGS) $(INCLUDE) -c $< -o $@
clean:
@rm -rf $(OBJ_DIR)
@printf "$(WHITE)Clean process completed for $(GREEN)Ft_printf.$(RESET)\n"
fclean: clean
@rm -f $(NAME)
@printf "$(WHITE)Full clean process completed for $(GREEN)Ft_printf.$(RESET)\n"
re: fclean all
.PHONY: all clean fclean re
+48
View File
@@ -0,0 +1,48 @@
# ft_printf_fd
Your own implementation of the printf function with file descriptor support.
## 💡 About
The ft_printf_fd project is an extension of ft_printf that allows writing to any file descriptor. You will learn about file descriptors, variadic arguments and formatted output conversion. This project enhances the original printf functionality by adding the ability to choose the output destination.
## 🛠️ Supported Conversions
The function handles the following conversions:
* `%c` - Single character
* `%s` - String
* `%p` - Pointer address
* `%d` - Decimal (base 10) integer
* `%i` - Integer in base 10
* `%u` - Unsigned decimal (base 10) integer
* `%x` - Hexadecimal (base 16) integer lowercase
* `%X` - Hexadecimal (base 16) integer uppercase
* `%%` - Percentage sign
## 📋 Usage
```c
#include "ft_printf_fd.h"
int main(void)
{
// Write to stdout (fd 1)
ft_printf_fd(1, "Hello %s!\n", "world");
// Write to stderr (fd 2)
ft_printf_fd(2, "Error: %d\n", 42);
// Write to a file
int fd = open("output.txt", O_WRONLY | O_CREAT, 0644);
ft_printf_fd(fd, "Number: %d\n", 42);
close(fd);
return (0);
}
```
## ⚠️ Function Prototype
```c
int ft_printf_fd(int fd, const char *format, ...);
```
Returns the number of characters printed (excluding the null byte used to end output to strings).
## 📊 Expected Output
The function should work exactly like the original printf, handling all the specified conversions correctly, but writing to the specified file descriptor instead of stdout.
+27
View File
@@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_adrr_len.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/19 05:16:57 by lfirmin #+# #+# */
/* Updated: 2024/06/19 07:50:34 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "ftprintf.h"
int ft_adrr_len(unsigned long long nbr)
{
int i;
if (nbr == 0)
return (1);
i = 0;
while (nbr != 0)
{
i++;
nbr = nbr / 16;
}
return (i);
}
+30
View File
@@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_conv_ith.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/19 04:46:47 by lfirmin #+# #+# */
/* Updated: 2024/06/19 07:46:23 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "ftprintf.h"
void ft_conv_ith(int fd, unsigned int nbr, const char str)
{
if (nbr >= 16)
{
ft_conv_ith(fd, (nbr / 16), str);
nbr = nbr % 16;
}
if (nbr <= 9)
ft_putchar(fd, nbr + '0');
else
{
if (str == 'x')
ft_putchar(fd, nbr - 10 + 'a');
else if (str == 'X')
ft_putchar(fd, nbr - 10 + 'A');
}
}
+27
View File
@@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_count.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/19 04:52:36 by lfirmin #+# #+# */
/* Updated: 2024/06/19 07:47:07 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "ftprintf.h"
size_t ft_count(long long int n)
{
size_t c;
c = 0;
if (n <= 0)
c = 1;
while (n != 0)
{
n = n / 10;
c++;
}
return (c);
}
+27
View File
@@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_hex_len.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/19 04:45:50 by lfirmin #+# #+# */
/* Updated: 2024/06/19 07:49:59 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "ftprintf.h"
int ft_hex_len(unsigned int nbr)
{
int i;
if (nbr == 0)
return (1);
i = 0;
while (nbr != 0)
{
i++;
nbr = nbr / 16;
}
return (i);
}
+36
View File
@@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/19 04:51:58 by lfirmin #+# #+# */
/* Updated: 2024/06/19 04:53:33 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "ftprintf.h"
char *ft_itoa(int n)
{
char *str;
size_t size;
long int num;
num = n;
size = ft_count(num);
str = (char *)malloc(size + 1);
if (!str)
return (NULL);
str[size] = '\0';
if (num < 0)
num = -num;
while (size--)
{
str[size] = num % 10 + '0';
num = num / 10;
if (n < 0 && size == 0)
str[size] = '-';
}
return (str);
}
+20
View File
@@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_print_ith.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/19 04:44:27 by lfirmin #+# #+# */
/* Updated: 2024/06/19 04:49:51 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "ftprintf.h"
int ft_print_ith(int fd, unsigned int nbr, const char str)
{
if (nbr == 0)
return (write(fd, "0", 1));
ft_conv_ith(fd, nbr, str);
return (ft_hex_len(nbr));
}
+23
View File
@@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_print_nbr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/19 04:44:27 by lfirmin #+# #+# */
/* Updated: 2024/06/19 04:52:47 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "ftprintf.h"
int ft_print_nbr(int fd, int nb)
{
int len;
char *str;
str = ft_itoa(nb);
len = ft_printstr(fd, str);
free (str);
return (len);
}
+18
View File
@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_print_null.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/19 07:01:50 by lfirmin #+# #+# */
/* Updated: 2024/06/19 07:46:39 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "ftprintf.h"
int ft_print_null(int fd)
{
write(fd, "(null)", 6);
return (6);
}
+30
View File
@@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_print_ptr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/19 04:44:27 by lfirmin #+# #+# */
/* Updated: 2024/06/19 07:51:10 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "ftprintf.h"
int ft_print_ptr(int fd, unsigned long long ptr)
{
int print_length;
if (ptr == 0)
{
write(fd, "(nil)", 5);
return (5);
}
print_length = 2;
write(fd, "0x", 2);
{
ft_put_ptr(fd, ptr);
print_length = print_length + ft_adrr_len(ptr);
}
return (print_length);
}
+23
View File
@@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_print_unsigned.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/19 04:50:53 by lfirmin #+# #+# */
/* Updated: 2024/06/19 07:51:31 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "ftprintf.h"
int ft_print_unsigned(int fd, unsigned int nb)
{
int len;
char *str;
str = ft_unsigned_itoa(nb);
len = ft_printstr(fd, str);
free (str);
return (len);
}
+18
View File
@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printchar.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/19 04:56:02 by lfirmin #+# #+# */
/* Updated: 2024/06/19 04:56:25 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "ftprintf.h"
int ft_printchar(int fd, int c)
{
write (fd, &c, 1);
return (1);
}
+39
View File
@@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/19 04:44:27 by lfirmin #+# #+# */
/* Updated: 2024/06/19 04:55:41 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "ftprintf.h"
int ft_printf_fd(int fd, const char *str, ...)
{
int i;
va_list list;
int count;
i = 0;
count = 0;
va_start(list, str);
while (str[i])
{
if (str[i] == '%')
{
count += ft_sorting(list, str[i + 1], fd);
i++;
}
else
{
write (fd, &str[i], 1);
count++;
}
i++;
}
va_end(list);
return (count);
}
+18
View File
@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printpercent.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/19 04:47:38 by lfirmin #+# #+# */
/* Updated: 2024/06/19 07:50:08 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "ftprintf.h"
int ft_printpercent(int fd)
{
ft_putchar(fd, '%');
return (1);
}
+27
View File
@@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/19 04:55:08 by lfirmin #+# #+# */
/* Updated: 2024/06/19 07:46:05 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "ftprintf.h"
int ft_printstr(int fd, char *s)
{
int i;
if (!s)
return (ft_print_null(fd));
i = 0;
while (s[i])
{
write (fd, &s[i], 1);
i++;
}
return (i);
}
+28
View File
@@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_put_ptr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/19 04:54:06 by lfirmin #+# #+# */
/* Updated: 2024/06/19 07:46:52 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "ftprintf.h"
void ft_put_ptr(int fd, unsigned long long num)
{
if (num >= 16)
{
ft_put_ptr(fd, num / 16);
ft_put_ptr(fd, num % 16);
}
else
{
if (num <= 9)
ft_putchar(fd, num + '0');
else
ft_putchar(fd, num - 10 + 'a');
}
}
+17
View File
@@ -0,0 +1,17 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/19 04:45:04 by lfirmin #+# #+# */
/* Updated: 2024/06/19 04:49:11 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "ftprintf.h"
void ft_putchar(int fd, char c)
{
write(fd, &c, 1);
}
+34
View File
@@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_sorting.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/19 04:56:45 by lfirmin #+# #+# */
/* Updated: 2024/06/19 07:51:17 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "ftprintf.h"
int ft_sorting(va_list list, const char str, int fd)
{
int count;
count = 0;
if (str == 'c')
count += ft_printchar(fd, va_arg(list, int));
else if (str == 's')
count += ft_printstr(fd, va_arg(list, char *));
else if (str == 'p')
count += ft_print_ptr(fd, va_arg(list, unsigned long long));
else if (str == 'd' || str == 'i')
count += ft_print_nbr(fd, va_arg(list, int));
else if (str == 'u')
count += ft_print_unsigned(fd, va_arg(list, unsigned int));
else if (str == 'x' || str == 'X')
count += ft_print_ith(fd, va_arg(list, unsigned int), str);
else if (str == '%')
count += ft_printpercent(fd);
return (count);
}
+34
View File
@@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_unsigned_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/19 04:51:27 by lfirmin #+# #+# */
/* Updated: 2024/06/19 07:45:46 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "ftprintf.h"
char *ft_unsigned_itoa(unsigned int n)
{
char *str;
size_t size;
long int num;
num = n;
size = ft_count(num);
str = (char *)malloc(size + 1);
if (!str)
return (NULL);
str[size] = '\0';
if (num < 0)
num = -num;
while (size--)
{
str[size] = num % 10 + '0';
num = num / 10;
}
return (str);
}
+43
View File
@@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ftprintf.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/19 04:44:27 by lfirmin #+# #+# */
/* Updated: 2024/06/19 07:49:08 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FTPRINTF_H
# define FTPRINTF_H
# include <stdarg.h>
# include <unistd.h>
# include <stdio.h>
# include "stdlib.h"
void ft_putchar(int fd, char c);
void ft_put_ptr(int fd, unsigned long long num);
void ft_conv_ith(int fd, unsigned int nbr, const char str);
int ft_hex_len(unsigned int nbr);
int ft_sorting(va_list list, const char str, int fd);
int ft_printf_fd(int fd, const char *str, ...);
int ft_print_ith(int fd, unsigned int nbr, const char str);
int ft_printchar(int fd, int c);
int ft_printstr(int fd, char *s);
int ft_printpercent(int fd);
int ft_print_unsigned(int fd, unsigned int nb);
int ft_print_nbr(int fd, int nb);
int ft_print_ptr(int fd, unsigned long long ptr);
int ft_adrr_len(unsigned long long nbr);
int ft_print_null(int fd);
char *ft_itoa(int n);
char *ft_unsigned_itoa(unsigned int n);
size_t ft_count(long long int n);
#endif