Seluj78/Minishell

View on GitHub
libft/ft_cat.c

Summary

Maintainability
Test Coverage
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_cat.c                                           :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: jlasne <marvin@42.fr>                      +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2016/11/05 14:02:38 by jlasne            #+#    #+#             */
/*   Updated: 2016/12/12 10:57:35 by jlasne           ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "libft.h"
#include <fcntl.h>

static void        disp_files(int argc, char **argv)
{
    char    buffer;
    int        fd;
    int        i;

    i = 1;
    while (i < argc)
    {
        fd = open(argv[i], O_RDONLY);
        if (fd >= 0)
        {
            while (read(fd, &buffer, 1) != 0)
                write(1, &buffer, 1);
        }
        else
        {
            ft_putstr("ft_cat: ");
            ft_putstr(argv[i]);
            ft_putendl(": No such file or directory");
        }
        close(fd);
        i++;
    }
}

static void        disp_stdin(void)
{
    char    buffer;

    while (read(0, &buffer, 1) != 0)
        write(1, &buffer, 1);
}

int                ft_cat(int argc, char **argv)
{
    if (argc < 2 || argv[1][0] == '-')
        disp_stdin();
    disp_files(argc, argv);
    return (0);
}