segfault go jump off a bridge
This commit is contained in:
42
Makefile
Normal file
42
Makefile
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
# **************************************************************************** #
|
||||||
|
# #
|
||||||
|
# ::: :::::::: #
|
||||||
|
# Makefile :+: :+: :+: #
|
||||||
|
# +:+ +:+ +:+ #
|
||||||
|
# By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ #
|
||||||
|
# +#+#+#+#+#+ +#+ #
|
||||||
|
# Created: 2023/12/01 17:10:39 by adjoly #+# #+# #
|
||||||
|
# Updated: 2023/12/01 17:20:36 by adjoly ### ########.fr #
|
||||||
|
# #
|
||||||
|
# **************************************************************************** #
|
||||||
|
|
||||||
|
NAME = get_next_line.a
|
||||||
|
|
||||||
|
CC = cc
|
||||||
|
|
||||||
|
SRCS = get_next_line.c \
|
||||||
|
get_next_line_utils.c \
|
||||||
|
|
||||||
|
OBJS = $(SRCS:.c=.o)
|
||||||
|
|
||||||
|
FLAGS = -Werror -Wall -Wextra
|
||||||
|
|
||||||
|
HEADER = get_next_line.h \
|
||||||
|
|
||||||
|
$(NAME): $(OBJS)
|
||||||
|
ar -rcs $(NAME) $(OBJS)
|
||||||
|
|
||||||
|
%.o: %.cc
|
||||||
|
$(CC) $(FLAGS) $(HEADER) $< -c -o $@
|
||||||
|
|
||||||
|
all: $(NAME)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f $(OBJS)
|
||||||
|
|
||||||
|
fclean: clean
|
||||||
|
rm -f $(NAME)
|
||||||
|
|
||||||
|
re: fclean all
|
||||||
|
|
||||||
|
.PHONY: clean all re fclean
|
@ -0,0 +1,95 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* get_next_line.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/12/01 17:11:59 by adjoly #+# #+# */
|
||||||
|
/* Updated: 2023/12/01 23:03:24 by adjoly ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "get_next_line.h"
|
||||||
|
|
||||||
|
int check_line(char *buf)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (buf[i] || buf[i] != '\n')
|
||||||
|
i++;
|
||||||
|
if (buf[i] == '\n')
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void read_line(int fd, char *buf)
|
||||||
|
{
|
||||||
|
char *tmp;
|
||||||
|
size_t read_nb;
|
||||||
|
|
||||||
|
while (!check_line(buf) && read_nb != 0)
|
||||||
|
{
|
||||||
|
tmp = malloc((BUFFER_SIZE + 1) * sizeof(char));
|
||||||
|
if (!tmp)
|
||||||
|
{
|
||||||
|
buf = NULL;
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
read_nb = read(fd, tmp, BUFFER_SIZE);
|
||||||
|
buf = ft_strjoin(buf, tmp);
|
||||||
|
free(tmp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
char *line_to_str(char *buf)
|
||||||
|
{
|
||||||
|
char *res;
|
||||||
|
size_t i;
|
||||||
|
size_t size_buf;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
size_buf = ft_strlen_til_nl(buf);
|
||||||
|
res = malloc((size_buf + 1) * sizeof(char));
|
||||||
|
while (i < size_buf)
|
||||||
|
{
|
||||||
|
res[i] = buf[i];
|
||||||
|
if (buf[i] == '\n')
|
||||||
|
break ;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
free (buf);
|
||||||
|
return (res);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *get_next_line(int fd)
|
||||||
|
{
|
||||||
|
static char *buf;
|
||||||
|
char *res;
|
||||||
|
|
||||||
|
if (BUFFER_SIZE <= 0 || read(fd, &res, 0) < 0 || fd < 0)
|
||||||
|
return (NULL);
|
||||||
|
read_line(fd, buf);
|
||||||
|
res = line_to_str(buf);
|
||||||
|
return (res);
|
||||||
|
}
|
||||||
|
|
||||||
|
#include <unistd.h>
|
||||||
|
void ft_putstr(char *str){if (str == NULL){return ;}int i = 0;while(str[i]){write(1, &str[i], 1);i++;}}
|
||||||
|
#include <fcntl.h>
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
char *ln;
|
||||||
|
int fd;
|
||||||
|
|
||||||
|
fd = open("test.txt", O_RDONLY);
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
ln = get_next_line(fd);
|
||||||
|
if (ln == NULL)
|
||||||
|
break ;
|
||||||
|
ft_putstr(ln);
|
||||||
|
free(ln);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* get_next_line.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/12/01 17:12:00 by adjoly #+# #+# */
|
||||||
|
/* Updated: 2023/12/01 22:31:30 by adjoly ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef GET_NEXT_LINE_H
|
||||||
|
# define GET_NEXT_LINE_H
|
||||||
|
|
||||||
|
# include <unistd.h>
|
||||||
|
# include <stdlib.h>
|
||||||
|
|
||||||
|
# ifndef BUFFER_SIZE
|
||||||
|
# define BUFFER_SIZE 5
|
||||||
|
# endif
|
||||||
|
|
||||||
|
char *get_next_line(int fd);
|
||||||
|
char *ft_strjoin(char *s1, char *s2);
|
||||||
|
size_t ft_strlen_til_nl(char *s);
|
||||||
|
size_t ft_strlen(char *s);
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,61 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* get_next_line_utils.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/12/01 17:12:02 by adjoly #+# #+# */
|
||||||
|
/* Updated: 2023/12/01 22:31:36 by adjoly ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "get_next_line.h"
|
||||||
|
|
||||||
|
size_t ft_strlen(char *s)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (s[i])
|
||||||
|
i++;
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t ft_strlen_til_nl(char *s)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (s[i] && s[i] != '\n')
|
||||||
|
i++;
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *ft_strjoin(char *s1, char *s2)
|
||||||
|
{
|
||||||
|
char *result;
|
||||||
|
size_t i;
|
||||||
|
size_t j;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
j = 0;
|
||||||
|
if (s1 == NULL && s2 == NULL)
|
||||||
|
return (NULL);
|
||||||
|
result = malloc((ft_strlen(s1) + ft_strlen(s2) + 1) * sizeof(char));
|
||||||
|
if (result == NULL)
|
||||||
|
return (NULL);
|
||||||
|
while (s1[i])
|
||||||
|
{
|
||||||
|
result[i] = s1[i];
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
while (s2[j])
|
||||||
|
{
|
||||||
|
result[i] = s2[j];
|
||||||
|
i++;
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
result[i] = '\0';
|
||||||
|
return (result);
|
||||||
|
}
|
1
gnlTester
Submodule
1
gnlTester
Submodule
Submodule gnlTester added at e12080dcec
Reference in New Issue
Block a user