Files
42_old_fillit/srcs/read_file.c
2019-04-14 14:24:50 +02:00

37 lines
1.2 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* read_file.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vmanzoni <vmanzoni@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/13 12:09:46 by vmanzoni #+# #+# */
/* Updated: 2019/04/14 14:23:24 by vmanzoni ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/fillit.h"
char *read_file(char *file)
{
char buf[BUFFER_SIZE];
int fd;
int rv;
int i;
char *result;
if (((fd = open(file, O_RDONLY)) < 0) \
|| ((rv = read(fd, &buf, BUFFER_SIZE)) < 0) \
|| !(result = malloc(sizeof(char))))
return (NULL);
buf[rv] = '\0';
i = 0;
while (rv--)
{
result[i] = buf[i];
i++;
}
close(fd);
return (result);
}