minishell/libft/srcs/get_next_line.c
tvdu29 2a2f5bd4d3
Dev (#2)
* Restart from scratch

branched out on dev
removed all previous source files
started test on new env2lst function

* norm: splitted functions in main into appropriate file

Moved lstdelenvelem & env2lst to ms_env.c
Normed out files

* Feat(WIP): Added possibility to cmd_env to execute commands with
altered env.

Added dummy function for cd, echo, setenv & unsetenv
Started work on env copy feature

* feature: switched env from ft_list to specific list

* feature: added execution of commands from path

* feature: added env, setenv & unsetenv builtins

added -i option & command execution to env
added setenv & unsetenv builtins

* Clean-up: normed out files

* Changed comportment on error for check_path functions

* feature: added completion for ~ and ${}

WIP leaks detected on ${} completion

* fix leak on  ext need test

* feature: added echo cmd

changed printf ref to ft_printf

* feature: add cd built-in

* Simple norm-out

* moved env functions to libft

* fixed out-of-memory access in extension

* Resolved infinite loop in extension revolving

WIP norming in cd

* Normedout cmd_cd.c

* Normed out functions in cmd_cd

* Normed out funtions

sorting out needed

* removed -fsanitize from Makefile

* corrected env -i crash & SHLVL

* Delete norm.txt

* added put_error_cd2 in put_error_cd.c

* added inline environment
2020-01-31 15:06:17 +01:00

99 lines
2.5 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/04/24 18:08:15 by tmaze #+# #+# */
/* Updated: 2019/03/16 15:53:27 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char **get_buff(const int fd, t_list **lst)
{
t_list *tmp;
tmp = *lst;
while (tmp)
{
if (tmp->content_size == (size_t)fd)
return ((char**)&tmp->content);
tmp = tmp->next;
}
if ((tmp = ft_lstnew("\0", fd)) == NULL)
return (NULL);
ft_lstadd(lst, tmp);
return (get_buff(fd, lst));
}
static char *supercat(char **s1, char **s2)
{
char *tmp;
if ((tmp = ft_strjoin(*s1, *s2)) == NULL)
return (NULL);
ft_strdel(s1);
ft_strclr(*s2);
*s1 = tmp;
return (*s1);
}
static int check_buff(char **buff, char **line)
{
int ret;
ret = 1;
if (buff != NULL && (*buff == NULL || ft_strlen(*buff) == 0))
ret = 0;
else if ((*line = ft_strdup(*buff)) == NULL)
ret = -1;
ft_strclr(*buff);
return (ret);
}
static int read_gnl(const int fd, char **buff, char **line)
{
char *tmp;
int ret;
if ((tmp = ft_strnew(BUFF_SIZE)) == NULL)
return (-1);
while (ft_strrnchr(*buff, '\n', BUFF_SIZE) == NULL
&& (ret = read(fd, tmp, BUFF_SIZE)) > 0)
if (supercat(buff, &tmp) == NULL)
{
ft_strdel(&tmp);
return (-1);
}
ft_strdel(&tmp);
if (buff != NULL && *buff != NULL
&& ft_strrnchr(*buff, '\n', BUFF_SIZE) != NULL)
return (get_next_line(fd, line));
else
return (check_buff(buff, line));
}
int get_next_line(const int fd, char **line)
{
static t_list *lst = NULL;
char **buff;
char *tmp;
tmp = NULL;
if (fd < 0 || read(fd, tmp, 0) == -1 || line == NULL)
return (-1);
if ((buff = get_buff(fd, &lst)) == NULL)
return (-1);
tmp = ft_strchr(*buff, '\n');
if (tmp != NULL)
{
*line = ft_strndup(*buff, tmp - *buff);
ft_memmove(*buff, &tmp[1], ft_strlen(&tmp[1]) + 1);
return (1);
}
return (read_gnl(fd, buff, line));
}