Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
jellysg authored Mar 20, 2024
0 parents commit 8faa15b
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
NAME = libftprintf.a
CC = gcc
CFLAGS = -Wall -Wextra -Werror

SRCS = ft_printf.c ft_putaddress.c ft_putalnum.c ft_puthex.c ft_putstr.c
OBJS = $(SRCS:.c=.o)
LIBC = ar rcs
RM = rm -f

$(NAME): $(OBJS)
$(LIBC) $(NAME) $(OBJS)
@echo "$(NAME) compiled successfully."

all: $(NAME)

.o:.c
$(CC) $(CFLAGS) -c $< -o $@

clean:
$(RM) $(OBJS)
@echo "Object files cleaned."

fclean: clean
$(RM) $(NAME)
@echo "$(NAME) cleaned."

re: fclean all

.PHONY : all clean fclean re
58 changes: 58 additions & 0 deletions ft_printf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include "ft_printf.h"

static size_t checkformat(const char *c, size_t *i, va_list *argptr)
{
char format;

*i = *i + 1;
format = *(c + 1);
if (format == 'c')
return (ft_putchar((char) va_arg(*argptr, int)));
else if (format == 's')
return (ft_putstr(va_arg(*argptr, const char *)));
else if (format == 'p')
return (ft_putaddress(va_arg(*argptr, void *)));
else if (format == 'd' || format == 'i')
return (ft_putnbr(va_arg(*argptr, int)));
else if (format == 'u')
return (ft_putunbr(va_arg(*argptr, unsigned int)));
else if (format == 'x')
return (ft_puthex_l(va_arg(*argptr, int)));
else if (format == 'X')
return (ft_puthex_u(va_arg(*argptr, int)));
else if (format == '%')
return (ft_putchar('%'));
else if (format && (format + 1))
return (ft_putchar(*(c)) + ft_putchar(format));
else
*i = *i - 1;
return (-1);
}

int ft_printf(const char *str, ...)
{
int len;
int tmp;
va_list argptr;
size_t i;

if (!str)
return (-1);
len = 0;
va_start(argptr, str);
i = 0;
while (str[i])
{
if (str[i] == '%')
tmp = checkformat(str + i, &i, &argptr);
else
tmp = ft_putchar(str[i]);
if (tmp >= 0)
len += tmp;
else
len = tmp;
i++;
}
va_end(argptr);
return (len);
}
17 changes: 17 additions & 0 deletions ft_printf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef FT_PRINTF_H
# define FT_PRINTF_H
# include <stdlib.h>
# include <unistd.h>
# include <string.h>
# include <stdarg.h>

int ft_printf(const char *str, ...);
size_t ft_putaddress(void *addr);
size_t ft_putchar(const char c);
size_t ft_puthex_l(const unsigned int hex);
size_t ft_puthex_u(const unsigned int hex);
size_t ft_putnbr(const int n);
size_t ft_putstr(const char *str);
size_t ft_putunbr(const unsigned int un);

#endif
20 changes: 20 additions & 0 deletions ft_putaddress.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "ft_printf.h"

static size_t print_addr(const unsigned long n)
{
if (n / 16)
return (print_addr(n / 16) + print_addr(n % 16));
else if (!(n / 10))
ft_putchar(n + '0');
else
ft_putchar((char)(n - 10 + 'a'));
return (1);
}

size_t ft_putaddress(void *addr)
{
if (!addr)
return (ft_putstr("(nil)"));
ft_putstr("0x");
return (2 + print_addr((unsigned long) addr));
}
36 changes: 36 additions & 0 deletions ft_putalnum.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "ft_printf.h"

size_t ft_putchar(const char c)
{
return (write(1, &c, 1));
}

static size_t print_nb(long nb)
{
if (nb / 10)
return (print_nb(nb / 10) + print_nb(nb % 10));
else
return (ft_putchar(nb + '0'));
}

size_t ft_putnbr(const int n)
{
long nb;

nb = n;
if (nb < 0)
{
nb = -nb;
return (ft_putchar('-') + print_nb(nb));
}
else
return (print_nb(nb));
}

size_t ft_putunbr(const unsigned int n)
{
if (n / 10)
return (ft_putunbr(n / 10) + ft_putunbr(n % 10));
else
return (ft_putchar(n + '0'));
}
23 changes: 23 additions & 0 deletions ft_puthex.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "ft_printf.h"

size_t ft_puthex_l(const unsigned int hex)
{
if (hex / 16)
return (ft_puthex_l(hex / 16) + ft_puthex_l(hex % 16));
else if (!(hex / 10))
ft_putchar(hex + '0');
else
ft_putchar((char) hex - 10 + 'a');
return (1);
}

size_t ft_puthex_u(const unsigned int hex)
{
if (hex / 16)
return (ft_puthex_u(hex / 16) + ft_puthex_u(hex % 16));
else if (!(hex / 10))
ft_putchar(hex + '0');
else
ft_putchar((char) hex - 10 + 'A');
return (1);
}
19 changes: 19 additions & 0 deletions ft_putstr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "ft_printf.h"

static size_t ft_strlen(const char *str)
{
size_t count;

count = 0;
while (str[count])
count++;
return (count);
}

size_t ft_putstr(const char *str)
{
if (!str)
return (write(1, "(null)", 6));
else
return (write(1, str, ft_strlen(str)));
}

0 comments on commit 8faa15b

Please sign in to comment.