42-archive/42-piscine-c/d12/ex00/main.c
2016-08-25 20:50:28 +02:00

54 lines
1.4 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/08/15 13:55:54 by jhalford #+# #+# */
/* Updated: 2016/08/19 22:54:40 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include <fcntl.h>
#define BUF_SIZE 10
void ft_putstr(char *str)
{
while (*str)
write(1, str++, 1);
}
int ft_error(int ac)
{
if (ac == 1)
{
ft_putstr("File name missing.\n");
return (1);
}
if (ac > 2)
{
ft_putstr("Too many arguments.\n");
return (1);
}
return (0);
}
int main(int ac, char **av)
{
int fd;
int ret;
char buf[BUF_SIZE + 1];
if (ft_error(ac))
return (0);
fd = open(av[1], O_RDONLY);
while ((ret = read(fd, buf, BUF_SIZE)))
{
buf[ret] = '\0';
write(1, buf, ret);
}
close(fd);
return (0);
}