Skip to content

Commit 0d3bec9

Browse files
author
Mohamed El mouhib
committed
added the functions and the makefile
0 parents  commit 0d3bec9

10 files changed

+328
-0
lines changed

Makefile

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
CC = cc
2+
CFLAGS = -Wall -Werror -Wextra
3+
NAME = libftprintf.a
4+
SRC = ft_printf.c ft_putchar.c ft_putnbrhex.c ft_putunsigned.c ft_putaddres.c ft_putnbr.c ft_putstr.c
5+
OBJ = $(SRC:.c=.o)
6+
7+
all : $(NAME)
8+
9+
$(NAME) : $(OBJ)
10+
@ar rcs $(NAME) $(OBJ)
11+
12+
%.o : %.c
13+
@$(CC) $(CFLAGS) -c $< -o $@
14+
15+
clean :
16+
@rm -rf $(OBJ)
17+
18+
fclean : clean
19+
@rm -rf $(NAME)
20+
21+
re : fclean all
22+
23+
.PHONY : all clean fclean

conversions.c

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* conversions.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: mel-mouh <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2024/11/18 23:07:50 by mel-mouh #+# #+# */
9+
/* Updated: 2024/11/18 23:13:54 by mel-mouh ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "libftprintf.h"
14+
15+
int conversions(char spiecifier, va_list args)
16+
{
17+
int count;
18+
19+
count = 0;
20+
if (spiecifier == 'c')
21+
count += ft_putchar((char)va_arg(args, int));
22+
else if (spiecifier == 'd' || spiecifier == 'i')
23+
count += ft_putnbr(va_arg(args, int));
24+
else if (spiecifier == 's')
25+
count += ft_putstr(va_arg(args, char *));
26+
else if (spiecifier == 'x')
27+
count += ft_putnbr_hex(va_arg(args, unsigned), "0123456789abcdef");
28+
else if (spiecifier == 'X')
29+
count += ft_putnbr_hex(va_arg(args, unsigned), "0123456789ABCDEF");
30+
else if (spiecifier == 'p')
31+
count += ft_putaddres((unsigned long int)va_arg(args, void *));
32+
else if (spiecifier == 'u')
33+
count += ft_putunsigned(va_arg(args, unsigned));
34+
else if (spiecifier == '%')
35+
count += ft_putchar('%');
36+
return (count);
37+
}

ft_printf.c

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* ft_printf.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: mel-mouh <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2024/11/16 20:30:02 by mel-mouh #+# #+# */
9+
/* Updated: 2024/11/18 23:07:36 by mel-mouh ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "libftprintf.h"
14+
15+
int ft_printf(const char *str, ...)
16+
{
17+
int i;
18+
int count;
19+
va_list args;
20+
21+
va_start(args, str);
22+
i = 0;
23+
count = 0;
24+
while (str[i] != '\0')
25+
{
26+
if (str[i] == '%')
27+
{
28+
i++;
29+
count += conversions(str[i], args);
30+
}
31+
else
32+
count += ft_putchar(str[i]);
33+
i++;
34+
}
35+
return (count);
36+
}

ft_putaddres.c

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* ft_putaddres.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: mel-mouh <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2024/11/18 19:32:03 by mel-mouh #+# #+# */
9+
/* Updated: 2024/11/18 23:09:52 by mel-mouh ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "libftprintf.h"
14+
15+
int ft_putaddres(unsigned long int addr)
16+
{
17+
int count;
18+
19+
count = 0;
20+
if (addr == 0)
21+
return (ft_putstr("(nil)"));
22+
count += ft_putstr("0x");
23+
count += ft_putnbr_hex(addr, "0123456789abcdef");
24+
return (count);
25+
}

ft_putchar.c

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* ft_putchar.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: mel-mouh <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2024/11/18 23:10:38 by mel-mouh #+# #+# */
9+
/* Updated: 2024/11/18 23:10:44 by mel-mouh ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "libftprintf.h"
14+
15+
int ft_putchar(char c)
16+
{
17+
return (write (1, &c, 1));
18+
}

ft_putnbr.c

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* ft_putnbr.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: mel-mouh <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2024/11/18 19:09:59 by mel-mouh #+# #+# */
9+
/* Updated: 2024/11/18 23:11:09 by mel-mouh ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "libftprintf.h"
14+
15+
static int leen(int n)
16+
{
17+
int i;
18+
unsigned int m;
19+
20+
i = 0;
21+
if (n < 0)
22+
{
23+
m = -n;
24+
i++;
25+
}
26+
else
27+
m = n;
28+
while (m > 0)
29+
{
30+
m /= 10;
31+
i++;
32+
}
33+
return (i);
34+
}
35+
36+
int ft_putnbr(int n)
37+
{
38+
unsigned int m;
39+
int i;
40+
41+
if (n < 0)
42+
{
43+
ft_putchar('-');
44+
m = -n;
45+
}
46+
else if (n == 0)
47+
return (write(1, "0", 1));
48+
else
49+
m = n;
50+
i = leen(n);
51+
if (m > 9)
52+
{
53+
ft_putnbr(m / 10);
54+
ft_putchar((m % 10) + '0');
55+
}
56+
else
57+
ft_putchar(m + '0');
58+
return (i);
59+
}

ft_putnbrhex.c

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* ft_putnbrhex.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: mel-mouh <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2024/11/18 19:09:24 by mel-mouh #+# #+# */
9+
/* Updated: 2024/11/18 23:11:38 by mel-mouh ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "libftprintf.h"
14+
15+
int ft_putnbr_hex(unsigned long int n, char *hexa)
16+
{
17+
char buffer[16];
18+
int i;
19+
int count;
20+
21+
if (n == 0)
22+
return (write(1, "0", 1));
23+
i = 0;
24+
while (n > 0)
25+
{
26+
buffer[i++] = hexa[n % 16];
27+
n /= 16;
28+
}
29+
count = i;
30+
while (i >= 0)
31+
ft_putchar(buffer[--i]);
32+
return (count);
33+
}

ft_putstr.c

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* ft_putstr.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: mel-mouh <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2024/11/18 19:08:39 by mel-mouh #+# #+# */
9+
/* Updated: 2024/11/18 23:11:56 by mel-mouh ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "libftprintf.h"
14+
15+
int ft_putstr(char *s)
16+
{
17+
size_t i;
18+
19+
i = 0;
20+
if (s == NULL)
21+
return (-1);
22+
while (s[i] != '\0')
23+
{
24+
write(1, &s[i], 1);
25+
i++;
26+
}
27+
return (i);
28+
}

ft_putunsigned.c

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* ft_putunsigned.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: mel-mouh <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2024/11/18 19:37:27 by mel-mouh #+# #+# */
9+
/* Updated: 2024/11/18 23:12:43 by mel-mouh ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "libftprintf.h"
14+
15+
static int leen(unsigned int n)
16+
{
17+
int i;
18+
19+
i = 0;
20+
while (n > 0)
21+
{
22+
n /= 10;
23+
i++;
24+
}
25+
return (i);
26+
}
27+
28+
int ft_putunsigned(unsigned int n)
29+
{
30+
int i;
31+
32+
i = leen(n);
33+
if (n > 9)
34+
{
35+
ft_putunsigned(n / 10);
36+
ft_putchar((n % 10) + '0');
37+
}
38+
else
39+
ft_putchar(n + '0');
40+
return (i);
41+
}

libftprintf.h

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* libftprintf.h :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: mel-mouh <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2024/11/18 19:04:49 by mel-mouh #+# #+# */
9+
/* Updated: 2024/11/18 23:13:04 by mel-mouh ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#ifndef LIBFTPRINTF_H
14+
# define LIBFTPRINTF_H
15+
16+
# include <stdio.h>
17+
# include <unistd.h>
18+
# include <stdarg.h>
19+
20+
int ft_putchar(char c);
21+
int ft_putstr(char *s);
22+
int ft_putnbr_hex(unsigned long int n, char *hexa);
23+
int ft_putnbr(int n);
24+
int ft_printf(const char *str, ...);
25+
int ft_putaddres(unsigned long int addr);
26+
int ft_putunsigned(unsigned int n);
27+
28+
#endif

0 commit comments

Comments
 (0)