# **************************************************************************** #
#                                                                              #
#                                                         :::      ::::::::    #
#    Makefile                                           :+:      :+:    :+:    #
#                                                     +:+ +:+         +:+      #
#    By: jayang <[email protected]>                      +#+  +:+       +#+         #
#                                                 +#+#+#+#+#+   +#+            #
#    Created: 2022/02/19 16:18:09 by jayang            #+#    #+#              #
#    Updated: 2022/02/19 17:15:41 by jayang           ###   ########.fr        #
#                                                                              #
# **************************************************************************** #

CC = gcc
CHLAGS = -Wall -Wextra -Werror

TARGET = ft_cat
SRCS = srcs/main.c srcs/string.c
OBJS = ${SRCS:.c=.o}

%.o : %.c
	${CC} ${CHLAGS} -c $< -o $@

${TARGET} : ${OBJS}
	${CC} ${CHLAGS} $^ -o $@

all : ${TARGET}

clean:
	rm -rf ${OBJS}

fclean: clean
	rm -rf ${TARGET}

re: fclean all
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   string.c                                           :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: jayang <[email protected]>                      +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2022/02/19 16:21:51 by jayang            #+#    #+#             */
/*   Updated: 2022/02/19 17:21:10 by jayang           ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include <unistd.h>

void	ft_putchar(char c)
{
	write(1, &c, 1);
}

void	ft_putstr(char *str)
{
	while (*str)
	{
		ft_putchar(*str);
		str++;
	}
}

void	ft_write(char *str, int size)
{
	write(1, str, size);
}
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   main.c                                             :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: jayang <[email protected]>                      +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2022/02/19 16:23:39 by jayang            #+#    #+#             */
/*   Updated: 2022/02/19 17:30:27 by jayang           ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include <unistd.h>
#include <errno.h>
#include <libgen.h>
#include <string.h>
#include <fcntl.h>

void	ft_putchar(char c);
void	ft_putstr(char *str);
void	ft_write(char *str, int size);
char	*path;

void	ft_print_errno(char *param_str, int err)
{
	ft_putstr(basename(path));
	ft_putstr(": ");
	ft_putstr(param_str);
	ft_putstr(": ");
	ft_putstr(strerror(err));
	ft_putchar('\\n');
}

void	ft_read(int fd)
{
	int		byte;
	char	file[30];

	while (1)
	{
		byte = read(fd, file, 10);
		if (byte != 0)
			ft_write(file, byte);
		else
			break;
	}
}

void	ft_open_file(char *argv)
{
	int	fd;

	fd = open(argv, O_RDONLY);
	if (fd < 0)
	{
		ft_print_errno(argv, errno);
		return ;
	}
	ft_read(fd);
	close(fd);
}

int	main(int argc, char *argv[])
{
	int		param;

	param = 1;
	path = argv[0];
	if (argc == 1)
		ft_read(0);
	else
	{
		while (param < argc)
		{
			ft_open_file(argv[param]);
			param++;
		}
	}
	return (0);
}