Skip to content

Commit

Permalink
count number of return
Browse files Browse the repository at this point in the history
  • Loading branch information
zelhajou committed Dec 13, 2022
1 parent 666d228 commit 5258bbd
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 54 deletions.
38 changes: 38 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: zelhajou <zelhajou@student.1337.ma> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/12/13 05:19:17 by zelhajou #+# #+# #
# Updated: 2022/12/13 05:23:03 by zelhajou ### ########.fr #
# #
# **************************************************************************** #

NAME = libftprintf.a
SRCS= ft_printf.c ft_putchar.c ft_putstr.c ft_putnbr.c ft_putunbr.c \
ft_putaddr.c ft_puthex.c

OBJS = ${SRCS:%.c=%.o}

FLAGS = -Wall -Wextra -Werror

HEADER = ft_printf.h

all: $(NAME)

$(NAME): $(OBJS)
gcc $(FLAGS) -c $(SRCS) -I ./
ar rc $(NAME) $(OBJS)

%.o: %.c $(HEADER)
$(CC) -c $(CFLAGS) $<

clean:
rm -rf $(OBJS)

fclean: clean
rm -rf $(NAME)

re: fclean all
38 changes: 17 additions & 21 deletions ft_printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,52 @@
/* By: zelhajou <zelhajou@student.1337.ma> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/01 22:52:23 by zelhajou #+# #+# */
/* Updated: 2022/12/10 03:23:01 by zelhajou ### ########.fr */
/* Updated: 2022/12/13 05:23:30 by zelhajou ### ########.fr */
/* */
/* ************************************************************************** */

#include "ft_printf.h"

static void check_type(va_list argp, const char *format)
static void check_type(va_list argp, const char *format, int *len)
{
if (*format == '%')
ft_putchar('%');
*len += ft_putchar('%');
else if (*format == 'c')
ft_putchar((char)va_arg(argp, int));
*len += ft_putchar((char)va_arg(argp, int));
else if (*format == 's')
ft_putstr(va_arg(argp, char *));
*len += ft_putstr(va_arg(argp, char *));
else if (*format == 'i' || *format == 'd')
ft_putnbr(va_arg(argp, int));
*len += ft_putnbr(va_arg(argp, int));
else if (*format == 'u')
ft_putunbr(va_arg(argp, unsigned int));
*len += ft_putunbr(va_arg(argp, unsigned int));
else if (*format == 'x' || *format == 'X')
ft_puthex(va_arg(argp, unsigned int), *format);
*len += ft_puthex(va_arg(argp, unsigned int), *format);
else if (*format == 'p')
ft_putaddr(va_arg(argp, unsigned long));
*len += ft_putaddr(va_arg(argp, unsigned long));
else
ft_putchar(*format);
*len += ft_putchar(*format);
}

void ft_printf(const char *format, ...)
int ft_printf(const char *format, ...)
{
int len;
va_list argp;

len = 0;
va_start(argp, format);
while (*format != '\0')
{
if (*format == '%' && !*(format + 1))
break;
break ;
if (*format == '%')
{
format++;
check_type(argp, format);
check_type(argp, format, &len);
}
else
ft_putchar(*format);
len += ft_putchar(*format);
format++;
}
va_end(argp);
}

int main(void)
{
int a = 10;
ft_printf("%p\n", &a);
printf("%p\n", &a);
return (len);
}
15 changes: 8 additions & 7 deletions ft_printf.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: zelhajou <zelhajou@student.1337.ma> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/01 22:52:18 by zelhajou #+# #+# */
/* Updated: 2022/12/10 03:23:21 by zelhajou ### ########.fr */
/* Updated: 2022/12/13 05:22:48 by zelhajou ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -18,11 +18,12 @@
# include <stdarg.h>
# include <unistd.h>

void ft_putchar(char c);
void ft_putstr(char *str);
void ft_putnbr(int nb);
void ft_putunbr(unsigned int nbr);
void ft_puthex(unsigned long nbr, char c);
void ft_putaddr(unsigned long addr);
int ft_printf(const char *format, ...);
int ft_putchar(char c);
int ft_putstr(char *str);
int ft_putnbr(int nb);
int ft_putunbr(unsigned int nbr);
int ft_puthex(unsigned long nbr, char c);
int ft_putaddr(unsigned long addr);

#endif
14 changes: 9 additions & 5 deletions ft_putaddr.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@
/* By: zelhajou <zelhajou@student.1337.ma> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/07 22:56:56 by zelhajou #+# #+# */
/* Updated: 2022/12/10 03:22:57 by zelhajou ### ########.fr */
/* Updated: 2022/12/13 05:21:22 by zelhajou ### ########.fr */
/* */
/* ************************************************************************** */

#include "ft_printf.h"

void ft_putaddr(unsigned long addr)
int ft_putaddr(unsigned long addr)
{
write(1, "0x", 2);
ft_puthex(addr, 'x');
}
int len;

len = 0;
len += ft_putstr("0x");
len += ft_puthex(addr, 'x');
return (len);
}
5 changes: 3 additions & 2 deletions ft_putchar.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
/* By: zelhajou <zelhajou@student.1337.ma> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/02 12:08:37 by zelhajou #+# #+# */
/* Updated: 2022/12/09 23:17:03 by zelhajou ### ########.fr */
/* Updated: 2022/12/12 22:24:29 by zelhajou ### ########.fr */
/* */
/* ************************************************************************** */

#include "ft_printf.h"

void ft_putchar(char c)
int ft_putchar(char c)
{
write(1, &c, 1);
return (1);
}
16 changes: 10 additions & 6 deletions ft_puthex.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@
/* By: zelhajou <zelhajou@student.1337.ma> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/10 03:14:47 by zelhajou #+# #+# */
/* Updated: 2022/12/10 03:22:53 by zelhajou ### ########.fr */
/* Updated: 2022/12/13 05:12:57 by zelhajou ### ########.fr */
/* */
/* ************************************************************************** */

#include "ft_printf.h"

void ft_puthex(unsigned long nbr, char c)
int ft_puthex(unsigned long nbr, char c)
{
int len;

len = 0;
if (nbr > 15)
ft_puthex(nbr / 16, c);
len += ft_puthex(nbr / 16, c);
if (c == 'x')
write(1, &"0123456789abcdef"[nbr % 16], 1);
len += write(1, &"0123456789abcdef"[nbr % 16], 1);
else
write(1, &"0123456789ABCDEF"[nbr % 16], 1);
}
len += write(1, &"0123456789ABCDEF"[nbr % 16], 1);
return (len);
}
13 changes: 8 additions & 5 deletions ft_putnbr.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,26 @@
/* By: zelhajou <zelhajou@student.1337.ma> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/09 21:56:15 by zelhajou #+# #+# */
/* Updated: 2022/12/10 03:01:17 by zelhajou ### ########.fr */
/* Updated: 2022/12/13 05:17:19 by zelhajou ### ########.fr */
/* */
/* ************************************************************************** */

#include "ft_printf.h"

void ft_putnbr(int nb)
int ft_putnbr(int nb)
{
long nbr;
int len;

len = 0;
nbr = nb;
if (nbr < 0)
{
ft_putchar('-');
len += ft_putchar('-');
nbr = nbr * -1;
}
if (nbr > 9)
ft_putnbr(nbr / 10);
ft_putchar(nbr % 10 + '0');
len += ft_putnbr(nbr / 10);
len += ft_putchar(nbr % 10 + '0');
return (len);
}
10 changes: 7 additions & 3 deletions ft_putstr.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@
/* By: zelhajou <zelhajou@student.1337.ma> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/02 12:03:30 by zelhajou #+# #+# */
/* Updated: 2022/12/09 23:17:20 by zelhajou ### ########.fr */
/* Updated: 2022/12/12 22:56:00 by zelhajou ### ########.fr */
/* */
/* ************************************************************************** */

#include "ft_printf.h"

void ft_putstr(char *str)
int ft_putstr(char *str)
{
int len;

len = 0;
if (!str)
return (ft_putstr("(null)"));
while (*str != '\0')
{
ft_putchar(*str);
len += ft_putchar(*str);
str++;
}
return (len);
}
14 changes: 9 additions & 5 deletions ft_putunbr.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@
/* By: zelhajou <zelhajou@student.1337.ma> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/10 03:10:17 by zelhajou #+# #+# */
/* Updated: 2022/12/10 03:11:25 by zelhajou ### ########.fr */
/* Updated: 2022/12/13 05:23:59 by zelhajou ### ########.fr */
/* */
/* ************************************************************************** */

#include "ft_printf.h"

void ft_putunbr(unsigned int nbr)
int ft_putunbr(unsigned int nbr)
{
int len;

len = 0;
if (nbr > 9)
ft_putunbr(nbr / 10);
ft_putchar(nbr % 10 + '0');
}
len += ft_putunbr(nbr / 10);
len += ft_putchar(nbr % 10 + '0');
return (len);
}

0 comments on commit 5258bbd

Please sign in to comment.