First version with working test (read and print file)

This commit is contained in:
Manzovince
2019-04-14 12:15:25 +02:00
parent 1ce59774b8
commit 481d683467
28 changed files with 637 additions and 13 deletions

36
srcs/read_file.c Normal file
View File

@@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* read_file.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vmanzoni <vmanzoni@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/13 12:09:46 by vmanzoni #+# #+# */
/* Updated: 2019/04/14 12:08:12 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);
}