Files
mmoat/lib/libft/srcs/format/add_to_tab.c
gazhonsepaskwa 8b3d4868dd free start
2025-02-05 15:50:47 +01:00

64 lines
1.6 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* add_to_tab.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/04 09:59:01 by nalebrun #+# #+# */
/* Updated: 2025/02/04 09:59:01 by nalebrun ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../libft.h"
static void free_null_ptr(void *ptr)
{
if (ptr != NULL)
{
free(ptr);
ptr = NULL;
}
}
static char **add_space_to_tab(char **tab, int count)
{
char **new_env;
int i;
i = 0;
new_env = malloc(sizeof(char *) * (count + 1));
if (!new_env)
return (NULL);
new_env[count] = NULL;
while (tab[i] && i < count)
{
new_env[i] = ft_strdup(tab[i]);
free_null_ptr(tab[i]);
i++;
}
free_null_ptr(tab);
return (new_env);
}
void add_to_tab(char ***tab, char *str)
{
int i;
if (!str)
return ;
i = 0;
if (*tab)
{
while ((*tab) && (*tab)[i])
i++;
*tab = add_space_to_tab(*tab, i + 1);
}
else
{
*tab = malloc(sizeof(char *) * 2);
}
(*tab)[i] = ft_strdup(str);
(*tab)[i + 1] = NULL;
}