init
This commit is contained in:
commit
780ac9ced3
36
Makefile
Normal file
36
Makefile
Normal file
@ -0,0 +1,36 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# Makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2022/09/27 08:39:27 by cchauvet #+# #+# #
|
||||
# Updated: 2022/10/06 16:37:11 by cchauvet ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
SRCS =
|
||||
|
||||
OBJS = ${SRCS:.c=.o}
|
||||
|
||||
NAME = libftprintf
|
||||
|
||||
CFLAG = -Wall -Werror -Wextra
|
||||
|
||||
%.o: %.c libftprintf.h
|
||||
${CC} ${CFLAGS} -c -o $@ $<
|
||||
|
||||
all: ${NAME}
|
||||
|
||||
${NAME}: ${OBJS}
|
||||
ar -rc ${NAME}.a ${OBJS}
|
||||
|
||||
clean:
|
||||
rm -f ${OBJS} ${BOBJS}
|
||||
|
||||
fclean: clean
|
||||
rm -f ${NAME}.a
|
||||
|
||||
re: fclean all
|
||||
|
||||
.PHONY: bonus extra clean fclean re
|
25
ft_ctoa.c
Normal file
25
ft_ctoa.c
Normal file
@ -0,0 +1,25 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_ctoa.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/05 21:42:53 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/10/07 00:49:07 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libftprintf.h"
|
||||
|
||||
char *ft_ctoa(int c)
|
||||
{
|
||||
char *s;
|
||||
|
||||
s = malloc(sizeof(char) * 2);
|
||||
if (s == NULL)
|
||||
return (NULL);
|
||||
s[0] = (char) c;
|
||||
s[1] = 0;
|
||||
return (s);
|
||||
}
|
55
ft_itoa.c
Normal file
55
ft_itoa.c
Normal file
@ -0,0 +1,55 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_itoa.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/29 13:49:45 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/10/06 21:25:27 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libftprintf.h"
|
||||
|
||||
static int ft_nb_digit(int n)
|
||||
{
|
||||
int out;
|
||||
|
||||
out = 0;
|
||||
while (n)
|
||||
{
|
||||
n /= 10;
|
||||
out++;
|
||||
}
|
||||
return (out);
|
||||
}
|
||||
|
||||
char *ft_itoa(int n)
|
||||
{
|
||||
char *out;
|
||||
unsigned int nb[2];
|
||||
|
||||
if (!n)
|
||||
return (ft_strdup("0"));
|
||||
nb[0] = ft_nb_digit(n);
|
||||
if (n < 0)
|
||||
{
|
||||
nb[1] = -n;
|
||||
nb[0]++;
|
||||
}
|
||||
else
|
||||
nb[1] = n;
|
||||
out = malloc(sizeof(char) * (nb[0] + 1));
|
||||
if (out == NULL)
|
||||
return (NULL);
|
||||
out[nb[0]--] = 0;
|
||||
if (n < 0)
|
||||
out[0] = '-';
|
||||
while (nb[1])
|
||||
{
|
||||
out[nb[0]--] = nb[1] % 10 + 48;
|
||||
nb[1] /= 10;
|
||||
}
|
||||
return (out);
|
||||
}
|
55
ft_itoabase.c
Normal file
55
ft_itoabase.c
Normal file
@ -0,0 +1,55 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_itoabase.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/29 13:49:45 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/10/06 18:22:32 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libftprintf.h"
|
||||
|
||||
static size_t ft_nb_digit(int n, size_t base_size)
|
||||
{
|
||||
int out;
|
||||
|
||||
out = 0;
|
||||
while (n)
|
||||
{
|
||||
n /= base_size;
|
||||
out++;
|
||||
}
|
||||
return (out);
|
||||
}
|
||||
|
||||
char *ft_itoabase(int n, char *base)
|
||||
{
|
||||
char *out;
|
||||
unsigned int nb[2];
|
||||
|
||||
if (!n)
|
||||
return (ft_ctoa(base[0]));
|
||||
nb[0] = ft_nb_digit(n, ft_strlen(base));
|
||||
if (n < 0)
|
||||
{
|
||||
nb[1] = -n;
|
||||
nb[0]++;
|
||||
}
|
||||
else
|
||||
nb[1] = n;
|
||||
out = malloc(sizeof(char) * (nb[0] + 1));
|
||||
if (out == NULL)
|
||||
return (NULL);
|
||||
out[nb[0]--] = 0;
|
||||
if (n < 0)
|
||||
out[0] = '-';
|
||||
while (nb[1])
|
||||
{
|
||||
out[nb[0]--] = base[nb[1] % ft_strlen(base)];
|
||||
nb[1] /= ft_strlen(base);
|
||||
}
|
||||
return (out);
|
||||
}
|
23
ft_printf.c
Normal file
23
ft_printf.c
Normal file
@ -0,0 +1,23 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_printf.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/06 22:01:28 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/10/07 01:43:13 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libftprintf.h"
|
||||
|
||||
int ft_printf(const char *s, ...)
|
||||
{
|
||||
va_list va;
|
||||
|
||||
va_start(va, s);
|
||||
ft_vdprintf(1, s, va);
|
||||
va_end(va);
|
||||
return (ft_strlen(s));
|
||||
}
|
33
ft_ptoa.c
Normal file
33
ft_ptoa.c
Normal file
@ -0,0 +1,33 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_ptoa.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/06 17:41:04 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/10/06 21:44:49 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libftprintf.h"
|
||||
|
||||
char *ft_ptoa(void *ptr)
|
||||
{
|
||||
unsigned long address;
|
||||
size_t i;
|
||||
char *out;
|
||||
char *s;
|
||||
|
||||
address = (unsigned long) ptr;
|
||||
s = ft_itoabase(address, "0123456789ABCDEF");
|
||||
out = malloc(22 * sizeof(char *));
|
||||
out = "0x";
|
||||
while (s[i] != 0)
|
||||
{
|
||||
out[i + 2] = s[i];
|
||||
i++;
|
||||
}
|
||||
out = 0;
|
||||
return (out);
|
||||
}
|
18
ft_putchar_fd.c
Normal file
18
ft_putchar_fd.c
Normal file
@ -0,0 +1,18 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_putchar_fd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/29 22:23:35 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/09/29 22:24:53 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libftprintf.h"
|
||||
|
||||
void ft_putchar_fd(char c, int fd)
|
||||
{
|
||||
write(fd, &c, 1);
|
||||
}
|
21
ft_putendl_fd.c
Normal file
21
ft_putendl_fd.c
Normal file
@ -0,0 +1,21 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_putendl_fd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/29 22:26:36 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/09/29 22:43:19 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libftprintf.h"
|
||||
|
||||
void ft_putendl_fd(char *s, int fd)
|
||||
{
|
||||
if (s == NULL)
|
||||
return ;
|
||||
ft_putstr_fd(s, fd);
|
||||
ft_putchar_fd('\n', fd);
|
||||
}
|
21
ft_putstr_fd.c
Normal file
21
ft_putstr_fd.c
Normal file
@ -0,0 +1,21 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_putstr_fd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/29 22:25:08 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/09/29 22:40:34 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libftprintf.h"
|
||||
|
||||
void ft_putstr_fd(char *s, int fd)
|
||||
{
|
||||
if (s == NULL)
|
||||
return ;
|
||||
while (*s)
|
||||
write(fd, s++, 1);
|
||||
}
|
24
ft_strchr.c
Normal file
24
ft_strchr.c
Normal file
@ -0,0 +1,24 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strchr.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/26 14:44:15 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/10/05 20:56:37 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libftprintf.h"
|
||||
|
||||
char *ft_strchr(const char *s, int c)
|
||||
{
|
||||
while (*s != (char) c)
|
||||
{
|
||||
if (*s == 0)
|
||||
return (NULL);
|
||||
s++;
|
||||
}
|
||||
return ((char *) s);
|
||||
}
|
31
ft_strdup.c
Normal file
31
ft_strdup.c
Normal file
@ -0,0 +1,31 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strdup.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/27 16:00:49 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/10/06 16:53:02 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libftprintf.h"
|
||||
|
||||
char *ft_strdup(char *s)
|
||||
{
|
||||
char *out;
|
||||
size_t i;
|
||||
|
||||
out = malloc((ft_strlen(s) + 1) * sizeof(char));
|
||||
if (out == NULL)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
while (s[i])
|
||||
{
|
||||
out[i] = s[i];
|
||||
i++;
|
||||
}
|
||||
out[i] = 0;
|
||||
return (out);
|
||||
}
|
23
ft_strlen.c
Normal file
23
ft_strlen.c
Normal file
@ -0,0 +1,23 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strlen.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/06 17:39:05 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/10/06 17:40:41 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libftprintf.h"
|
||||
|
||||
size_t ft_strlen(const char *s)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (s[i])
|
||||
i++;
|
||||
return (i);
|
||||
}
|
23
ft_vdprintf.c
Normal file
23
ft_vdprintf.c
Normal file
@ -0,0 +1,23 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_vdprintf.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/06 16:39:25 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/10/07 01:38:18 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libftprintf.h"
|
||||
|
||||
char *ft_vdprintf(int fd, const char *format, va_list va)
|
||||
{
|
||||
char *str;
|
||||
|
||||
str = ft_strdup("");
|
||||
str = ft_vsprintf(str, format, va);
|
||||
ft_putstr_fd(str, fd);
|
||||
return (str);
|
||||
}
|
18
ft_vprintf.c
Normal file
18
ft_vprintf.c
Normal file
@ -0,0 +1,18 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_vprintf.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/07 01:05:23 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/10/07 01:43:58 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libftprintf.h"
|
||||
|
||||
int ft_vprintf(const char *format, va_list va)
|
||||
{
|
||||
return (ft_strlen(ft_vdprintf(1, format, va)));
|
||||
}
|
91
ft_vsprintf.c
Normal file
91
ft_vsprintf.c
Normal file
@ -0,0 +1,91 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_vsprintf.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/06 10:44:36 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/10/07 02:26:50 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libftprintf.h"
|
||||
|
||||
static char *ft_argtoa(int c, va_list args)
|
||||
{
|
||||
if (c == 'c')
|
||||
return (ft_ctoa(va_arg(args, int)));
|
||||
if (c == 's')
|
||||
return (ft_strdup(va_arg(args, char *)));
|
||||
if (c == 'd' || c == 'i')
|
||||
return (ft_itoa(va_arg(args, long int)));
|
||||
if (c == 'p')
|
||||
return (ft_ptoa(va_arg(args, void *)));
|
||||
if (c == 'x')
|
||||
return (ft_itoabase(va_arg(args, long int), "0123456789abcdef"));
|
||||
if (c == 'X')
|
||||
return (ft_itoabase(va_arg(args, long int), "0123456789ABCDEF"));
|
||||
if (c == '%')
|
||||
return (ft_ctoa('%'));
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
static size_t ft_seglen(const char *str)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (str[i] != 0 && str[i] != '%')
|
||||
i++;
|
||||
return (i);
|
||||
}
|
||||
|
||||
static char *ft_strfjoin(char *s1, const char *s2)
|
||||
{
|
||||
size_t i;
|
||||
size_t j;
|
||||
char *out;
|
||||
|
||||
if (s1 == NULL || s2 == NULL)
|
||||
return (NULL);
|
||||
out = malloc((ft_seglen(s1) + ft_seglen(s2) + 1) * sizeof(char));
|
||||
i = 0;
|
||||
while (s1[i] != 0 && s2[i] != '%')
|
||||
{
|
||||
out[i] = s1[i];
|
||||
i++;
|
||||
}
|
||||
free(s1);
|
||||
j = 0;
|
||||
while (s2[j] != 0 && s2[j] != '%')
|
||||
{
|
||||
out[i + j] = s2[j];
|
||||
j++;
|
||||
}
|
||||
out[i + j] = 0;
|
||||
return (out);
|
||||
}
|
||||
|
||||
char *ft_vsprintf(char *str, const char *format, va_list va)
|
||||
{
|
||||
char *mid_out;
|
||||
|
||||
while (format != NULL)
|
||||
{
|
||||
str = ft_strfjoin(str, format);
|
||||
format = ft_strchr(format, '%');
|
||||
if (format == NULL)
|
||||
break ;
|
||||
else
|
||||
format += 1;
|
||||
mid_out = ft_argtoa(*format, va);
|
||||
if (mid_out == NULL)
|
||||
{
|
||||
free(str);
|
||||
return (0);
|
||||
}
|
||||
str = ft_strfjoin(str, mid_out);
|
||||
}
|
||||
return (str);
|
||||
}
|
39
libftprintf.h
Normal file
39
libftprintf.h
Normal file
@ -0,0 +1,39 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* libftprintf.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/26 14:47:54 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/10/07 01:37:57 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef LIBFTPRINTF_H
|
||||
# define LIBFTPRINTF_H
|
||||
# include <stdlib.h>
|
||||
# include <unistd.h>
|
||||
# include <stdarg.h>
|
||||
|
||||
char *ft_strndup(const char *s, size_t n);
|
||||
size_t ft_strlen(const char *s);
|
||||
char *ft_strchr(const char *s, int c);
|
||||
|
||||
char *ft_itoa(int n);
|
||||
char *ft_ctoa(int c);
|
||||
char *ft_strdup(char *s);
|
||||
char *ft_ptoa(void *ptr);
|
||||
char *ft_utoa(unsigned int n);
|
||||
char *ft_itoabase(int n, char *base);
|
||||
|
||||
int ft_printf(const char *format, ...);
|
||||
|
||||
int ft_vprintf(const char *format, va_list va);
|
||||
char *ft_vsprintf(char *str, const char *format, va_list va);
|
||||
char *ft_vdprintf(int fd, const char *format, va_list va);
|
||||
|
||||
void ft_putchar_fd(char c, int fd);
|
||||
void ft_putstr_fd(char *s, int f);
|
||||
|
||||
#endif
|
BIN
libftprintf.h.gch
Normal file
BIN
libftprintf.h.gch
Normal file
Binary file not shown.
20
main.c
Normal file
20
main.c
Normal file
@ -0,0 +1,20 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/06 17:35:15 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/10/07 02:25:20 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libftprintf.h"
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
ft_printf("%s", "gros peteur");
|
||||
return (1);
|
||||
}
|
Loading…
Reference in New Issue
Block a user