update: pushing the final version of pipex

This commit is contained in:
Raphaël 2024-10-30 12:47:31 +01:00 committed by GitHub
parent ff670cd9d4
commit eb2d347b5d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 1044 additions and 0 deletions

32
libft/ft_memcpy.c Normal file
View file

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 16:49:46 by rparodi #+# #+# */
/* Updated: 2024/03/17 12:30:12 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/pipex.h"
void *ft_memcpy(void *dest, const void *src, size_t n)
{
char *d;
const char *s;
size_t i;
i = 0;
if (!dest && !src)
return (NULL);
d = (char *)dest;
s = (char *)src;
while (i < n)
{
d[i] = s[i];
i++;
}
return ((void *)d);
}

65
libft/ft_printf.c Normal file
View file

@ -0,0 +1,65 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/14 17:27:44 by rparodi #+# #+# */
/* Updated: 2024/03/15 14:22:23 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/pipex.h"
int ft_check_percent_args(int fd, char c, va_list args, int *ret_value)
{
if (c == 'c')
ft_putchar(fd, (char)va_arg(args, int), ret_value);
else if (c == 's')
ft_putstr(fd, (char *)va_arg(args, char *), ret_value);
else if (c == 'i' || c == 'd')
ft_putnbr(fd, (int)va_arg(args, int), ret_value);
else if (c == '%')
ft_putchar(fd, '%', ret_value);
else if (c == 'u')
ft_putnbr_unsigned(fd, \
(unsigned int)va_arg(args, unsigned int), ret_value);
else if (c == 'x')
ft_putnbr_base((unsigned int)va_arg(args, unsigned int), \
"0123456789abcdef", ret_value, c);
else if (c == 'X')
ft_putnbr_base((unsigned int)va_arg(args, unsigned int), \
"0123456789ABCDEF", ret_value, c);
else if (c == 'p')
ft_putnbr_base((unsigned long long)va_arg(args, unsigned long long), \
"0123456789abcdef", ret_value, c);
va_end(args);
return (1);
}
int ft_fprintf(int fd, const char *s, ...)
{
size_t i;
va_list args;
char *str;
int ret_value;
ret_value = 0;
str = ft_strdup(s);
va_start(args, s);
i = 0;
while (str[i])
{
if (str[i] == '%')
{
ft_check_percent_args(fd, str[i + 1], args, &ret_value);
i++;
}
else
ft_putchar(fd, str[i], &ret_value);
i++;
}
free(str);
return (ret_value);
}

82
libft/ft_put.c Normal file
View file

@ -0,0 +1,82 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_put.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 12:13:14 by rparodi #+# #+# */
/* Updated: 2024/03/15 14:22:53 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/pipex.h"
void ft_putchar(int fd, char c, int *ret_value)
{
write(fd, &c, 1);
(*ret_value)++;
}
void ft_putnbr(int fd, int nb, int *ret_value)
{
if (nb < 0)
{
if (nb == INT_MIN)
{
write(fd, "-2147483648", 11);
*ret_value += 11;
return ;
}
nb = -nb;
ft_putchar(fd, '-', ret_value);
}
if (nb >= 10)
{
ft_putnbr(fd, nb / 10, ret_value);
nb = nb % 10;
}
if (nb < 10)
ft_putchar(fd, nb + 48, ret_value);
}
void ft_putnbr_base(\
unsigned long long nbr, char *base, int *ret_value, char c)
{
if (c == 'p')
{
if (nbr != 0)
ft_putstr(1, "0x", ret_value);
else if (nbr == 0)
{
ft_putstr(1, "(nil)", ret_value);
return ;
}
c++;
}
if (c != 'p')
{
if (nbr >= 16)
ft_putnbr_base(nbr / 16, base, ret_value, c);
ft_putchar(1, base[nbr % 16], ret_value);
}
}
void ft_putnbr_unsigned(int fd, unsigned int nb, int *ret_value)
{
if (nb >= 10)
{
ft_putnbr_unsigned(fd, nb / 10, ret_value);
nb = nb % 10;
}
if (nb < 10)
ft_putchar(fd, nb + 48, ret_value);
}
void ft_putstr(int fd, char *str, int *ret_value)
{
if (!str)
*ret_value += write(fd, "(null)", 6);
else
*ret_value += write(fd, str, ft_strlen(str));
}

93
libft/ft_split.c Normal file
View file

@ -0,0 +1,93 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 13:56:02 by rparodi #+# #+# */
/* Updated: 2024/03/11 12:21:45 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/pipex.h"
static int count_words(const char *str, char sep)
{
int i;
int count;
i = 0;
count = 0;
while (str[i])
{
while (str[i] == sep && str[i])
i++;
if (str[i] != sep && str[i])
{
while (str[i] != sep && str[i])
i++;
count++;
}
}
return (count);
}
static char *ft_strndup(const char *s, int j)
{
int i;
char *str;
i = 0;
str = (char *)malloc((j + 1));
if (!str)
return (NULL);
while (s[i] && i < j)
{
str[i] = s[i];
i++;
}
str[i] = '\0';
return (str);
}
static char **ext_w(char **words_array, const char *str, char sep, int size)
{
int i;
int j;
i = 0;
j = 0;
while (j < size)
{
while (str[i] == sep && str[i])
i++;
str = str + i;
i = 0;
while (str[i] != sep && str[i])
i++;
words_array[j++] = ft_strndup(str, i);
str = str + i;
i = 0;
}
words_array[j] = 0;
return (words_array);
}
char **ft_split(char const *s, char c)
{
int size;
char **words_array;
size = count_words(s, c);
words_array = (char **)malloc(sizeof(char *) * (size + 1));
if (!words_array)
return (NULL);
if (size == 0)
{
words_array[0] = NULL;
return (words_array);
}
words_array = ext_w(words_array, s, c, size);
return (words_array);
}

26
libft/ft_strdup.c Normal file
View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 16:53:59 by rparodi #+# #+# */
/* Updated: 2024/03/12 11:26:15 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/pipex.h"
char *ft_strdup(const char *s)
{
size_t len;
char *to_return;
len = ft_strlen(s) + 1;
to_return = (char *)malloc(sizeof(char) * len);
if (!to_return)
return (NULL);
ft_strlcpy(to_return, s, len);
return (to_return);
}

39
libft/ft_strjoin.c Normal file
View file

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 13:55:15 by rparodi #+# #+# */
/* Updated: 2024/03/12 21:48:21 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/pipex.h"
char *ft_strjoin(char const *s1, char const *s2)
{
size_t i;
size_t save_i;
char *str;
i = 0;
str = (char *)malloc((ft_strlen(s1) + ft_strlen(s2)) + 1);
if (!str)
return (NULL);
while (s1[i] != '\0')
{
str[i] = s1[i];
i++;
}
save_i = i;
i = 0;
while (s2[i] != '\0')
{
str[save_i + i] = s2[i];
i++;
}
str[i + save_i] = '\0';
return (str);
}

31
libft/ft_strlcpy.c Normal file
View file

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 16:55:25 by rparodi #+# #+# */
/* Updated: 2024/03/12 11:26:15 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/pipex.h"
size_t ft_strlcpy(char *dst, const char *src, size_t size)
{
size_t i;
i = 0;
while (src[i] && i + 1 < size)
{
dst[i] = src[i];
i++;
}
if (size > 0)
{
dst[i] = '\0';
i++;
}
return (ft_strlen(src));
}

23
libft/ft_strlen.c Normal file
View file

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 16:56:24 by rparodi #+# #+# */
/* Updated: 2024/03/17 17:37:27 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/pipex.h"
size_t ft_strlen(const char *s)
{
size_t i;
i = 0;
while (s[i] != '\0')
i++;
return (i);
}