Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_atoi.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: ggerardy <marvin@42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2018/10/22 13:26:32 by ggerardy #+# #+# */
- /* Updated: 2018/10/22 20:20:35 by ggerardy ### ########.fr */
- /* */
- /* ************************************************************************** */
- int ft_pow(int a, int b)
- {
- int res;
- int i;
- i = 0;
- res = 1;
- if (a == 0)
- {
- return (0);
- }
- while (i < b)
- {
- res *= a;
- i++;
- }
- return (res);
- }
- void ft_parser(char *str, int *len, int *sign)
- {
- while (*str)
- {
- if (*str < '0' || *str > '9')
- {
- if (*str != '-' || *len != 0)
- {
- if (*len > 0)
- break ;
- else
- return ;
- }
- }
- if (*str == '-')
- {
- *sign = -1;
- }
- str++;
- (*len)++;
- }
- }
- int ft_atoi(char *str)
- {
- int res;
- int sign;
- int len;
- len = 0;
- res = 0;
- sign = 1;
- while (*str == ' ' || (*str >= 9 && *str <= 13))
- str++;
- if (*str == '+' && (str[1] >= '0' && str[1] <= '9'))
- str++;
- if (*str == '-' && (str[1] >= '0' && str[1] <= '9'))
- sign = -1;
- ft_parser(str, &len, &sign);
- len -= sign < 0;
- str += sign < 0;
- while (len > 0)
- {
- res += (*str - '0') * ft_pow(10, len - 1);
- len--;
- str++;
- }
- return (res * sign);
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_char_works.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: ggerardy <marvin@42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2018/11/01 22:42:00 by ggerardy #+# #+# */
- /* Updated: 2018/11/01 23:15:19 by ggerardy ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include <unistd.h>
- #include <errno.h>
- int ft_strlen(char *str)
- {
- int len;
- len = 0;
- while (str[len])
- {
- len++;
- }
- return (len);
- }
- void ft_putchar(char c, int where)
- {
- write(where, &c, 1);
- }
- void ft_putstr(char *str, int where)
- {
- int i;
- i = 0;
- while (str[i])
- {
- ft_putchar(str[i], where);
- i++;
- }
- }
- int ft_strcmp(char *s1, char *s2)
- {
- while (*s1 && *s2)
- {
- if (*s1 == *s2)
- {
- s1++;
- s2++;
- continue;
- }
- return (*s1 - *s2);
- }
- if (*s1 == *s2)
- return (0);
- if (*s1)
- return (*s1);
- if (*s2)
- return ((-1) * *s2);
- return (0);
- }
- void ft_print_error(char *name)
- {
- ft_putstr("tail: ", 1);
- ft_putstr(name, 1);
- ft_putstr(": ", 1);
- if (errno == 2)
- {
- ft_putstr("No such file or directory\n", 1);
- return ;
- }
- if (errno == 13)
- {
- ft_putstr("Permission denied\n", 1);
- return ;
- }
- if (errno == 63)
- {
- ft_putstr("File name too long\n", 1);
- return ;
- }
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_file_works.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: ggerardy <marvin@42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2018/11/01 22:46:13 by ggerardy #+# #+# */
- /* Updated: 2018/11/01 23:13:07 by ggerardy ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include "ft_size.h"
- #include <zconf.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <stdint.h>
- uint64_t ft_get_file_size(char *name)
- {
- int file;
- int res_of_read;
- uint64_t len;
- char b;
- int was_decr;
- res_of_read = 1;
- len = 0u;
- was_decr = 0;
- file = open(name, O_RDONLY);
- while(res_of_read != 0)
- {
- res_of_read = (int)read(file, &b, 1);
- len++;
- if (len == 1u && !was_decr)
- {
- len--;
- was_decr = 1;
- }
- }
- close(file);
- return (len);
- }
- void ft_print_file(int file)
- {
- char buffer[SIZE];
- int finished;
- ssize_t res_of_read;
- finished = 0;
- while (!finished)
- {
- res_of_read = read(file, buffer, SIZE);
- if (res_of_read == -1)
- return ;
- write(1, buffer, (size_t)res_of_read);
- if (res_of_read != SIZE)
- finished = 1;
- }
- close(file);
- }
- int ft_check_file(char *name)
- {
- int file;
- char b;
- file = open(name, O_RDONLY);
- if (file == -1)
- {
- return (2);
- }
- read(file, &b, 1);
- if (errno == 21)
- {
- return (1);
- }
- close(file);
- return (0);
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_tail.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: ggerardy <marvin@42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2018/11/01 22:51:49 by ggerardy #+# #+# */
- /* Updated: 2018/11/01 23:00:22 by ggerardy ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include "ft_file_works.h"
- #include "ft_char_works.h"
- #include <zconf.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <stdint.h>
- void ft_tail_plus(char *name, uint64_t n)
- {
- uint64_t i;
- char b;
- int file;
- file = open(name, O_RDONLY);
- i = 1u;
- while (i < n)
- {
- read(file, &b, 1);
- i++;
- }
- ft_print_file(file);
- }
- void ft_tail_minus(char *name, uint64_t n)
- {
- uint64_t len;
- uint64_t i;
- char b;
- int file;
- len = ft_get_file_size(name);
- if (len == 0u)
- return ;
- if (n > len)
- len = 0u;
- else
- len -= n;
- file = open(name, O_RDONLY);
- i = 0u;
- while (i < len)
- {
- read(file, &b, 1);
- i++;
- }
- ft_print_file(file);
- }
- void ft_tail(char *name, char mode, uint64_t n)
- {
- int file;
- char b;
- file = open(name, O_RDONLY);
- if (file == -1)
- {
- ft_print_error(name);
- return ;
- }
- read(file, &b, 1);
- if (errno == 21)
- {
- return ;
- }
- close(file);
- if (mode == '+')
- ft_tail_plus(name, n);
- if (mode == '-')
- ft_tail_minus(name, n);
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* main.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: ggerardy <marvin@42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2018/11/01 22:55:14 by ggerardy #+# #+# */
- /* Updated: 2018/11/01 23:17:42 by ggerardy ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include <stdint.h>
- #include "ft_char_works.h"
- #include "ft_atoi.h"
- #include "ft_file_works.h"
- #include "ft_tail.h"
- void ft_print_name(char *name)
- {
- ft_putstr("==> ", 1);
- ft_putstr(name, 1);
- ft_putstr(" <==\n", 1);
- }
- int ft_str_is_numeric(char *str)
- {
- if (*str == 0)
- return (0);
- while (*str)
- {
- if (!(*str >= '0' && *str <= '9'))
- return (0);
- str++;
- }
- return (1);
- }
- int ft_check_args(char **argv, char *mode, uint64_t *n)
- {
- char *str;
- str = argv[2];
- if (ft_strcmp(argv[1], "-c") != 0)
- return (-1);
- if (str[0] == '+')
- {
- *mode = '+';
- str++;
- }
- else if (str[0] == '-' || ft_str_is_numeric(&str[0]))
- {
- *mode = '-';
- str += (str[0] == '-');
- }
- if (ft_str_is_numeric(str) == 0)
- {
- ft_putstr("tail: illegal offset -- ", 1);
- ft_putstr(argv[2], 1);
- ft_putchar('\n', 1);
- return (-1);
- }
- *n = (uint64_t)ft_atoi(str);
- return (0);
- }
- int main(int argc, char **argv)
- {
- uint64_t n;
- char mode;
- int i = 3;
- if (argc < 4)
- return (0);
- if (ft_check_args(argv, &mode, &n) == -1)
- return (0);
- if (argc == 4)
- {
- ft_tail(argv[3], mode, n);
- return (0);
- }
- while (i < argc)
- {
- if (ft_check_file(argv[i]) <= 1)
- ft_print_name(argv[i]);
- ft_tail(argv[i], mode, n);
- if (i != argc - 1 && ft_check_file(argv[i]) == 0)
- ft_putchar('\n', 1);
- i++;
- }
- return (0);
- }/* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_atoi.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: ggerardy <marvin@42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2018/10/22 13:26:32 by ggerardy #+# #+# */
- /* Updated: 2018/10/22 20:20:35 by ggerardy ### ########.fr */
- /* */
- /* ************************************************************************** */
- int ft_pow(int a, int b)
- {
- int res;
- int i;
- i = 0;
- res = 1;
- if (a == 0)
- {
- return (0);
- }
- while (i < b)
- {
- res *= a;
- i++;
- }
- return (res);
- }
- void ft_parser(char *str, int *len, int *sign)
- {
- while (*str)
- {
- if (*str < '0' || *str > '9')
- {
- if (*str != '-' || *len != 0)
- {
- if (*len > 0)
- break ;
- else
- return ;
- }
- }
- if (*str == '-')
- {
- *sign = -1;
- }
- str++;
- (*len)++;
- }
- }
- int ft_atoi(char *str)
- {
- int res;
- int sign;
- int len;
- len = 0;
- res = 0;
- sign = 1;
- while (*str == ' ' || (*str >= 9 && *str <= 13))
- str++;
- if (*str == '+' && (str[1] >= '0' && str[1] <= '9'))
- str++;
- if (*str == '-' && (str[1] >= '0' && str[1] <= '9'))
- sign = -1;
- ft_parser(str, &len, &sign);
- len -= sign < 0;
- str += sign < 0;
- while (len > 0)
- {
- res += (*str - '0') * ft_pow(10, len - 1);
- len--;
- str++;
- }
- return (res * sign);
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_char_works.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: ggerardy <marvin@42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2018/11/01 22:42:00 by ggerardy #+# #+# */
- /* Updated: 2018/11/01 23:15:19 by ggerardy ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include <unistd.h>
- #include <errno.h>
- int ft_strlen(char *str)
- {
- int len;
- len = 0;
- while (str[len])
- {
- len++;
- }
- return (len);
- }
- void ft_putchar(char c, int where)
- {
- write(where, &c, 1);
- }
- void ft_putstr(char *str, int where)
- {
- int i;
- i = 0;
- while (str[i])
- {
- ft_putchar(str[i], where);
- i++;
- }
- }
- int ft_strcmp(char *s1, char *s2)
- {
- while (*s1 && *s2)
- {
- if (*s1 == *s2)
- {
- s1++;
- s2++;
- continue;
- }
- return (*s1 - *s2);
- }
- if (*s1 == *s2)
- return (0);
- if (*s1)
- return (*s1);
- if (*s2)
- return ((-1) * *s2);
- return (0);
- }
- void ft_print_error(char *name)
- {
- ft_putstr("tail: ", 1);
- ft_putstr(name, 1);
- ft_putstr(": ", 1);
- if (errno == 2)
- {
- ft_putstr("No such file or directory\n", 1);
- return ;
- }
- if (errno == 13)
- {
- ft_putstr("Permission denied\n", 1);
- return ;
- }
- if (errno == 63)
- {
- ft_putstr("File name too long\n", 1);
- return ;
- }
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_file_works.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: ggerardy <marvin@42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2018/11/01 22:46:13 by ggerardy #+# #+# */
- /* Updated: 2018/11/01 23:13:07 by ggerardy ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include "ft_size.h"
- #include <zconf.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <stdint.h>
- uint64_t ft_get_file_size(char *name)
- {
- int file;
- int res_of_read;
- uint64_t len;
- char b;
- int was_decr;
- res_of_read = 1;
- len = 0u;
- was_decr = 0;
- file = open(name, O_RDONLY);
- while(res_of_read != 0)
- {
- res_of_read = (int)read(file, &b, 1);
- len++;
- if (len == 1u && !was_decr)
- {
- len--;
- was_decr = 1;
- }
- }
- close(file);
- return (len);
- }
- void ft_print_file(int file)
- {
- char buffer[SIZE];
- int finished;
- ssize_t res_of_read;
- finished = 0;
- while (!finished)
- {
- res_of_read = read(file, buffer, SIZE);
- if (res_of_read == -1)
- return ;
- write(1, buffer, (size_t)res_of_read);
- if (res_of_read != SIZE)
- finished = 1;
- }
- close(file);
- }
- int ft_check_file(char *name)
- {
- int file;
- char b;
- file = open(name, O_RDONLY);
- if (file == -1)
- {
- return (2);
- }
- read(file, &b, 1);
- if (errno == 21)
- {
- return (1);
- }
- close(file);
- return (0);
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_tail.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: ggerardy <marvin@42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2018/11/01 22:51:49 by ggerardy #+# #+# */
- /* Updated: 2018/11/01 23:00:22 by ggerardy ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include "ft_file_works.h"
- #include "ft_char_works.h"
- #include <zconf.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <stdint.h>
- void ft_tail_plus(char *name, uint64_t n)
- {
- uint64_t i;
- char b;
- int file;
- file = open(name, O_RDONLY);
- i = 1u;
- while (i < n)
- {
- read(file, &b, 1);
- i++;
- }
- ft_print_file(file);
- }
- void ft_tail_minus(char *name, uint64_t n)
- {
- uint64_t len;
- uint64_t i;
- char b;
- int file;
- len = ft_get_file_size(name);
- if (len == 0u)
- return ;
- if (n > len)
- len = 0u;
- else
- len -= n;
- file = open(name, O_RDONLY);
- i = 0u;
- while (i < len)
- {
- read(file, &b, 1);
- i++;
- }
- ft_print_file(file);
- }
- void ft_tail(char *name, char mode, uint64_t n)
- {
- int file;
- char b;
- file = open(name, O_RDONLY);
- if (file == -1)
- {
- ft_print_error(name);
- return ;
- }
- read(file, &b, 1);
- if (errno == 21)
- {
- return ;
- }
- close(file);
- if (mode == '+')
- ft_tail_plus(name, n);
- if (mode == '-')
- ft_tail_minus(name, n);
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* main.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: ggerardy <marvin@42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2018/11/01 22:55:14 by ggerardy #+# #+# */
- /* Updated: 2018/11/01 23:17:42 by ggerardy ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include <stdint.h>
- #include "ft_char_works.h"
- #include "ft_atoi.h"
- #include "ft_file_works.h"
- #include "ft_tail.h"
- void ft_print_name(char *name)
- {
- ft_putstr("==> ", 1);
- ft_putstr(name, 1);
- ft_putstr(" <==\n", 1);
- }
- int ft_str_is_numeric(char *str)
- {
- if (*str == 0)
- return (0);
- while (*str)
- {
- if (!(*str >= '0' && *str <= '9'))
- return (0);
- str++;
- }
- return (1);
- }
- int ft_check_args(char **argv, char *mode, uint64_t *n)
- {
- char *str;
- str = argv[2];
- if (ft_strcmp(argv[1], "-c") != 0)
- return (-1);
- if (str[0] == '+')
- {
- *mode = '+';
- str++;
- }
- else if (str[0] == '-' || ft_str_is_numeric(&str[0]))
- {
- *mode = '-';
- str += (str[0] == '-');
- }
- if (ft_str_is_numeric(str) == 0)
- {
- ft_putstr("tail: illegal offset -- ", 1);
- ft_putstr(argv[2], 1);
- ft_putchar('\n', 1);
- return (-1);
- }
- *n = (uint64_t)ft_atoi(str);
- return (0);
- }
- int main(int argc, char **argv)
- {
- uint64_t n;
- char mode;
- int i = 3;
- if (argc < 4)
- return (0);
- if (ft_check_args(argv, &mode, &n) == -1)
- return (0);
- if (argc == 4)
- {
- ft_tail(argv[3], mode, n);
- return (0);
- }
- while (i < argc)
- {
- if (ft_check_file(argv[i]) <= 1)
- ft_print_name(argv[i]);
- ft_tail(argv[i], mode, n);
- if (i != argc - 1 && ft_check_file(argv[i]) == 0)
- ft_putchar('\n', 1);
- i++;
- }
- return (0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement