/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   tail.h                                             :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: jayang <[email protected]>                      +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2022/02/20 12:49:02 by jayang            #+#    #+#             */
/*   Updated: 2022/02/20 13:24:42 by jayang           ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#ifndef TAIL_H
# define TAIL_H

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

void	ft_putchar(char c);
void	ft_putstr(char *str);
int		ft_atoi(char *str);
char	*g_path;

#endif
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   main.c                                             :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: jayang <[email protected]>                      +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2022/02/19 18:29:37 by jayang            #+#    #+#             */
/*   Updated: 2022/02/20 17:28:57 by jayang           ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "tail.h"

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

void	ft_write(int idx, int buf, char *cont, int flag)
{
	int	i;

	i = idx;
	if (i != 0)
	{
		while (i < buf && flag == 1)
		{
			write(1, cont + i, 1);
			i++;
		}
		i = 0;
	}
	while (i < idx)
	{
		write(1, cont + i, 1);
		i++;
	}
}

void	ft_read_file(int fd, int buf, char *cont)
{
	int		byte;
	int		idx;
	int		flag;
	char	file[1];

	idx = 0;
	flag = 0;
	while (1)
	{
		byte = read(fd, file, 1);
		if (byte == 0)
			break ;
		if (buf != 0)
		{
			if (idx == buf)
			{
				flag = 1;
				idx = 0;
			}
			cont[idx] = file[0];
			idx++;
		}
	}
	if (buf != 0)
		ft_write(idx, buf, cont, flag);
}

void	ft_open_file(char *argv, int buf, char *cont)
{
	int	fd;

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

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

	g_path = argv[0];
	buf = ft_atoi(argv[2]);
	if (argc == 3)
	{
		cont = (char *)malloc(sizeof(char) * buf);
		if (!cont)
			return (0);
		ft_read_file(0, buf, cont);
		free(cont);
	}
	param = 3;
	while (param < argc)
	{
		cont = (char *)malloc(sizeof(char) * (buf));
		if (!cont)
			return (0);
		ft_open_file(argv[param], buf, cont);
		free(cont);
		param++;
	}
	return (0);
}
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   string.c                                           :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: jayang <[email protected]>                      +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2022/02/19 18:57:21 by jayang            #+#    #+#             */
/*   Updated: 2022/02/20 13:24:44 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++;
	}
}
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_atoi.c                                          :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: jayang <[email protected]>                      +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2022/02/19 20:38:03 by jayang            #+#    #+#             */
/*   Updated: 2022/02/19 20:41:02 by jayang           ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

char	*ft_process_str(char *str, int *minus)
{
	while (*str == ' ' || (9 <= *str && *str <= 13))
		str++;
	*minus = 1;
	while (*str == '+' || *str == '-')
	{
		if (*str == '-')
			*minus *= -1;
		str++;
	}
	return (str);
}

int	ft_atoi(char *str)
{
	int	minus;
	int	result;

	str = ft_process_str(str, &minus);
	result = 0;
	while ('0' <= *str && *str <= '9')
	{
		result = (result * 10) + (*str - '0');
		str++;
	}
	return (minus * result);
}

Makefile

tail.h

main.c

string.c

ft_atoi.c