Advertisement
liftchampion

aaaaaaa

Nov 1st, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 19.46 KB | None | 0 0
  1. /* ************************************************************************** */
  2. /*                                                                            */
  3. /*                                                        :::      ::::::::   */
  4. /*   ft_atoi.c                                          :+:      :+:    :+:   */
  5. /*                                                    +:+ +:+         +:+     */
  6. /*   By: ggerardy <marvin@42.fr>                    +#+  +:+       +#+        */
  7. /*                                                +#+#+#+#+#+   +#+           */
  8. /*   Created: 2018/10/22 13:26:32 by ggerardy          #+#    #+#             */
  9. /*   Updated: 2018/10/22 20:20:35 by ggerardy         ###   ########.fr       */
  10. /*                                                                            */
  11. /* ************************************************************************** */
  12.  
  13. int         ft_pow(int a, int b)
  14. {
  15.     int res;
  16.     int i;
  17.  
  18.     i = 0;
  19.     res = 1;
  20.     if (a == 0)
  21.     {
  22.         return (0);
  23.     }
  24.     while (i < b)
  25.     {
  26.         res *= a;
  27.         i++;
  28.     }
  29.     return (res);
  30. }
  31.  
  32. void        ft_parser(char *str, int *len, int *sign)
  33. {
  34.     while (*str)
  35.     {
  36.         if (*str < '0' || *str > '9')
  37.         {
  38.             if (*str != '-' || *len != 0)
  39.             {
  40.                 if (*len > 0)
  41.                     break ;
  42.                 else
  43.                     return ;
  44.             }
  45.         }
  46.         if (*str == '-')
  47.         {
  48.             *sign = -1;
  49.         }
  50.         str++;
  51.         (*len)++;
  52.     }
  53. }
  54.  
  55. int         ft_atoi(char *str)
  56. {
  57.     int res;
  58.     int sign;
  59.     int len;
  60.  
  61.     len = 0;
  62.     res = 0;
  63.     sign = 1;
  64.     while (*str == ' ' || (*str >= 9 && *str <= 13))
  65.         str++;
  66.     if (*str == '+' && (str[1] >= '0' && str[1] <= '9'))
  67.         str++;
  68.     if (*str == '-' && (str[1] >= '0' && str[1] <= '9'))
  69.         sign = -1;
  70.     ft_parser(str, &len, &sign);
  71.     len -= sign < 0;
  72.     str += sign < 0;
  73.     while (len > 0)
  74.     {
  75.         res += (*str - '0') * ft_pow(10, len - 1);
  76.         len--;
  77.         str++;
  78.     }
  79.     return (res * sign);
  80. }
  81. /* ************************************************************************** */
  82. /*                                                                            */
  83. /*                                                        :::      ::::::::   */
  84. /*   ft_char_works.c                                    :+:      :+:    :+:   */
  85. /*                                                    +:+ +:+         +:+     */
  86. /*   By: ggerardy <marvin@42.fr>                    +#+  +:+       +#+        */
  87. /*                                                +#+#+#+#+#+   +#+           */
  88. /*   Created: 2018/11/01 22:42:00 by ggerardy          #+#    #+#             */
  89. /*   Updated: 2018/11/01 23:15:19 by ggerardy         ###   ########.fr       */
  90. /*                                                                            */
  91. /* ************************************************************************** */
  92.  
  93. #include <unistd.h>
  94. #include <errno.h>
  95.  
  96. int         ft_strlen(char *str)
  97. {
  98.     int len;
  99.  
  100.     len = 0;
  101.     while (str[len])
  102.     {
  103.         len++;
  104.     }
  105.     return (len);
  106. }
  107.  
  108. void        ft_putchar(char c, int where)
  109. {
  110.     write(where, &c, 1);
  111. }
  112.  
  113. void        ft_putstr(char *str, int where)
  114. {
  115.     int i;
  116.  
  117.     i = 0;
  118.     while (str[i])
  119.     {
  120.         ft_putchar(str[i], where);
  121.         i++;
  122.     }
  123. }
  124.  
  125. int         ft_strcmp(char *s1, char *s2)
  126. {
  127.     while (*s1 && *s2)
  128.     {
  129.         if (*s1 == *s2)
  130.         {
  131.             s1++;
  132.             s2++;
  133.             continue;
  134.         }
  135.         return (*s1 - *s2);
  136.     }
  137.     if (*s1 == *s2)
  138.         return (0);
  139.     if (*s1)
  140.         return (*s1);
  141.     if (*s2)
  142.         return ((-1) * *s2);
  143.     return (0);
  144. }
  145.  
  146. void        ft_print_error(char *name)
  147. {
  148.     ft_putstr("tail: ", 1);
  149.     ft_putstr(name, 1);
  150.     ft_putstr(": ", 1);
  151.     if (errno == 2)
  152.     {
  153.         ft_putstr("No such file or directory\n", 1);
  154.         return ;
  155.     }
  156.     if (errno == 13)
  157.     {
  158.         ft_putstr("Permission denied\n", 1);
  159.         return ;
  160.     }
  161.     if (errno == 63)
  162.     {
  163.         ft_putstr("File name too long\n", 1);
  164.         return ;
  165.     }
  166. }
  167. /* ************************************************************************** */
  168. /*                                                                            */
  169. /*                                                        :::      ::::::::   */
  170. /*   ft_file_works.c                                    :+:      :+:    :+:   */
  171. /*                                                    +:+ +:+         +:+     */
  172. /*   By: ggerardy <marvin@42.fr>                    +#+  +:+       +#+        */
  173. /*                                                +#+#+#+#+#+   +#+           */
  174. /*   Created: 2018/11/01 22:46:13 by ggerardy          #+#    #+#             */
  175. /*   Updated: 2018/11/01 23:13:07 by ggerardy         ###   ########.fr       */
  176. /*                                                                            */
  177. /* ************************************************************************** */
  178.  
  179. #include "ft_size.h"
  180. #include <zconf.h>
  181. #include <fcntl.h>
  182. #include <errno.h>
  183. #include <stdint.h>
  184.  
  185. uint64_t ft_get_file_size(char *name)
  186. {
  187.     int file;
  188.     int res_of_read;
  189.     uint64_t len;
  190.     char b;
  191.     int was_decr;
  192.  
  193.     res_of_read = 1;
  194.     len = 0u;
  195.     was_decr = 0;
  196.     file = open(name, O_RDONLY);
  197.     while(res_of_read != 0)
  198.     {
  199.         res_of_read = (int)read(file, &b, 1);
  200.         len++;
  201.         if (len == 1u && !was_decr)
  202.         {
  203.             len--;
  204.             was_decr = 1;
  205.         }
  206.     }
  207.     close(file);
  208.     return (len);
  209. }
  210.  
  211.  
  212. void ft_print_file(int file)
  213. {
  214.  
  215.     char        buffer[SIZE];
  216.     int         finished;
  217.     ssize_t     res_of_read;
  218.  
  219.     finished = 0;
  220.     while (!finished)
  221.     {
  222.         res_of_read = read(file, buffer, SIZE);
  223.         if (res_of_read == -1)
  224.             return ;
  225.         write(1, buffer, (size_t)res_of_read);
  226.         if (res_of_read != SIZE)
  227.             finished = 1;
  228.     }
  229.     close(file);
  230. }
  231.  
  232. int ft_check_file(char *name)
  233. {
  234.     int         file;
  235.     char        b;
  236.  
  237.     file = open(name, O_RDONLY);
  238.     if (file == -1)
  239.     {
  240.         return (2);
  241.     }
  242.     read(file, &b, 1);
  243.     if (errno == 21)
  244.     {
  245.         return (1);
  246.     }
  247.     close(file);
  248.     return (0);
  249. }
  250. /* ************************************************************************** */
  251. /*                                                                            */
  252. /*                                                        :::      ::::::::   */
  253. /*   ft_tail.c                                          :+:      :+:    :+:   */
  254. /*                                                    +:+ +:+         +:+     */
  255. /*   By: ggerardy <marvin@42.fr>                    +#+  +:+       +#+        */
  256. /*                                                +#+#+#+#+#+   +#+           */
  257. /*   Created: 2018/11/01 22:51:49 by ggerardy          #+#    #+#             */
  258. /*   Updated: 2018/11/01 23:00:22 by ggerardy         ###   ########.fr       */
  259. /*                                                                            */
  260. /* ************************************************************************** */
  261.  
  262. #include "ft_file_works.h"
  263. #include "ft_char_works.h"
  264. #include <zconf.h>
  265. #include <fcntl.h>
  266. #include <errno.h>
  267. #include <stdint.h>
  268.  
  269. void ft_tail_plus(char *name, uint64_t n)
  270. {
  271.     uint64_t i;
  272.     char b;
  273.     int file;
  274.  
  275.     file = open(name, O_RDONLY);
  276.     i = 1u;
  277.     while (i < n)
  278.     {
  279.         read(file, &b, 1);
  280.         i++;
  281.     }
  282.     ft_print_file(file);
  283. }
  284.  
  285. void ft_tail_minus(char *name, uint64_t n)
  286. {
  287.     uint64_t len;
  288.     uint64_t i;
  289.     char b;
  290.     int file;
  291.  
  292.     len = ft_get_file_size(name);
  293.     if (len == 0u)
  294.         return ;
  295.     if (n > len)
  296.         len = 0u;
  297.     else
  298.         len -= n;
  299.     file = open(name, O_RDONLY);
  300.     i = 0u;
  301.     while (i < len)
  302.     {
  303.         read(file, &b, 1);
  304.         i++;
  305.     }
  306.     ft_print_file(file);
  307. }
  308.  
  309. void        ft_tail(char *name, char mode, uint64_t n)
  310. {
  311.     int         file;
  312.     char        b;
  313.  
  314.  
  315.     file = open(name, O_RDONLY);
  316.     if (file == -1)
  317.     {
  318.         ft_print_error(name);
  319.         return ;
  320.     }
  321.     read(file, &b, 1);
  322.     if (errno == 21)
  323.     {
  324.         return ;
  325.     }
  326.     close(file);
  327.     if (mode == '+')
  328.         ft_tail_plus(name, n);
  329.     if (mode == '-')
  330.         ft_tail_minus(name, n);
  331. }
  332. /* ************************************************************************** */
  333. /*                                                                            */
  334. /*                                                        :::      ::::::::   */
  335. /*   main.c                                             :+:      :+:    :+:   */
  336. /*                                                    +:+ +:+         +:+     */
  337. /*   By: ggerardy <marvin@42.fr>                    +#+  +:+       +#+        */
  338. /*                                                +#+#+#+#+#+   +#+           */
  339. /*   Created: 2018/11/01 22:55:14 by ggerardy          #+#    #+#             */
  340. /*   Updated: 2018/11/01 23:17:42 by ggerardy         ###   ########.fr       */
  341. /*                                                                            */
  342. /* ************************************************************************** */
  343.  
  344. #include <stdint.h>
  345. #include "ft_char_works.h"
  346. #include "ft_atoi.h"
  347. #include "ft_file_works.h"
  348. #include "ft_tail.h"
  349.  
  350. void ft_print_name(char *name)
  351. {
  352.     ft_putstr("==> ", 1);
  353.     ft_putstr(name, 1);
  354.     ft_putstr(" <==\n", 1);
  355. }
  356.  
  357. int     ft_str_is_numeric(char *str)
  358. {
  359.     if (*str == 0)
  360.         return (0);
  361.     while (*str)
  362.     {
  363.         if (!(*str >= '0' && *str <= '9'))
  364.             return (0);
  365.         str++;
  366.     }
  367.     return (1);
  368. }
  369.  
  370. int ft_check_args(char **argv, char *mode, uint64_t *n)
  371. {
  372.     char *str;
  373.  
  374.     str = argv[2];
  375.     if (ft_strcmp(argv[1], "-c") != 0)
  376.         return (-1);
  377.     if (str[0] == '+')
  378.     {
  379.         *mode = '+';
  380.         str++;
  381.     }
  382.     else if (str[0] == '-' || ft_str_is_numeric(&str[0]))
  383.     {
  384.         *mode = '-';
  385.         str += (str[0] == '-');
  386.     }
  387.     if (ft_str_is_numeric(str) == 0)
  388.     {
  389.         ft_putstr("tail: illegal offset -- ", 1);
  390.         ft_putstr(argv[2], 1);
  391.         ft_putchar('\n', 1);
  392.         return (-1);
  393.     }
  394.     *n = (uint64_t)ft_atoi(str);
  395.     return (0);
  396. }
  397.  
  398. int main(int argc, char **argv)
  399. {
  400.     uint64_t n;
  401.     char mode;
  402.     int i = 3;
  403.  
  404.     if (argc < 4)
  405.         return (0);
  406.     if (ft_check_args(argv, &mode, &n) == -1)
  407.         return (0);
  408.     if (argc == 4)
  409.     {
  410.         ft_tail(argv[3], mode, n);
  411.         return (0);
  412.     }
  413.     while (i < argc)
  414.     {
  415.         if (ft_check_file(argv[i]) <= 1)
  416.             ft_print_name(argv[i]);
  417.         ft_tail(argv[i], mode, n);
  418.         if (i != argc - 1 && ft_check_file(argv[i]) == 0)
  419.             ft_putchar('\n', 1);
  420.         i++;
  421.     }
  422.     return (0);
  423. }/* ************************************************************************** */
  424. /*                                                                            */
  425. /*                                                        :::      ::::::::   */
  426. /*   ft_atoi.c                                          :+:      :+:    :+:   */
  427. /*                                                    +:+ +:+         +:+     */
  428. /*   By: ggerardy <marvin@42.fr>                    +#+  +:+       +#+        */
  429. /*                                                +#+#+#+#+#+   +#+           */
  430. /*   Created: 2018/10/22 13:26:32 by ggerardy          #+#    #+#             */
  431. /*   Updated: 2018/10/22 20:20:35 by ggerardy         ###   ########.fr       */
  432. /*                                                                            */
  433. /* ************************************************************************** */
  434.  
  435. int         ft_pow(int a, int b)
  436. {
  437.     int res;
  438.     int i;
  439.  
  440.     i = 0;
  441.     res = 1;
  442.     if (a == 0)
  443.     {
  444.         return (0);
  445.     }
  446.     while (i < b)
  447.     {
  448.         res *= a;
  449.         i++;
  450.     }
  451.     return (res);
  452. }
  453.  
  454. void        ft_parser(char *str, int *len, int *sign)
  455. {
  456.     while (*str)
  457.     {
  458.         if (*str < '0' || *str > '9')
  459.         {
  460.             if (*str != '-' || *len != 0)
  461.             {
  462.                 if (*len > 0)
  463.                     break ;
  464.                 else
  465.                     return ;
  466.             }
  467.         }
  468.         if (*str == '-')
  469.         {
  470.             *sign = -1;
  471.         }
  472.         str++;
  473.         (*len)++;
  474.     }
  475. }
  476.  
  477. int         ft_atoi(char *str)
  478. {
  479.     int res;
  480.     int sign;
  481.     int len;
  482.  
  483.     len = 0;
  484.     res = 0;
  485.     sign = 1;
  486.     while (*str == ' ' || (*str >= 9 && *str <= 13))
  487.         str++;
  488.     if (*str == '+' && (str[1] >= '0' && str[1] <= '9'))
  489.         str++;
  490.     if (*str == '-' && (str[1] >= '0' && str[1] <= '9'))
  491.         sign = -1;
  492.     ft_parser(str, &len, &sign);
  493.     len -= sign < 0;
  494.     str += sign < 0;
  495.     while (len > 0)
  496.     {
  497.         res += (*str - '0') * ft_pow(10, len - 1);
  498.         len--;
  499.         str++;
  500.     }
  501.     return (res * sign);
  502. }
  503. /* ************************************************************************** */
  504. /*                                                                            */
  505. /*                                                        :::      ::::::::   */
  506. /*   ft_char_works.c                                    :+:      :+:    :+:   */
  507. /*                                                    +:+ +:+         +:+     */
  508. /*   By: ggerardy <marvin@42.fr>                    +#+  +:+       +#+        */
  509. /*                                                +#+#+#+#+#+   +#+           */
  510. /*   Created: 2018/11/01 22:42:00 by ggerardy          #+#    #+#             */
  511. /*   Updated: 2018/11/01 23:15:19 by ggerardy         ###   ########.fr       */
  512. /*                                                                            */
  513. /* ************************************************************************** */
  514.  
  515. #include <unistd.h>
  516. #include <errno.h>
  517.  
  518. int         ft_strlen(char *str)
  519. {
  520.     int len;
  521.  
  522.     len = 0;
  523.     while (str[len])
  524.     {
  525.         len++;
  526.     }
  527.     return (len);
  528. }
  529.  
  530. void        ft_putchar(char c, int where)
  531. {
  532.     write(where, &c, 1);
  533. }
  534.  
  535. void        ft_putstr(char *str, int where)
  536. {
  537.     int i;
  538.  
  539.     i = 0;
  540.     while (str[i])
  541.     {
  542.         ft_putchar(str[i], where);
  543.         i++;
  544.     }
  545. }
  546.  
  547. int         ft_strcmp(char *s1, char *s2)
  548. {
  549.     while (*s1 && *s2)
  550.     {
  551.         if (*s1 == *s2)
  552.         {
  553.             s1++;
  554.             s2++;
  555.             continue;
  556.         }
  557.         return (*s1 - *s2);
  558.     }
  559.     if (*s1 == *s2)
  560.         return (0);
  561.     if (*s1)
  562.         return (*s1);
  563.     if (*s2)
  564.         return ((-1) * *s2);
  565.     return (0);
  566. }
  567.  
  568. void        ft_print_error(char *name)
  569. {
  570.     ft_putstr("tail: ", 1);
  571.     ft_putstr(name, 1);
  572.     ft_putstr(": ", 1);
  573.     if (errno == 2)
  574.     {
  575.         ft_putstr("No such file or directory\n", 1);
  576.         return ;
  577.     }
  578.     if (errno == 13)
  579.     {
  580.         ft_putstr("Permission denied\n", 1);
  581.         return ;
  582.     }
  583.     if (errno == 63)
  584.     {
  585.         ft_putstr("File name too long\n", 1);
  586.         return ;
  587.     }
  588. }
  589. /* ************************************************************************** */
  590. /*                                                                            */
  591. /*                                                        :::      ::::::::   */
  592. /*   ft_file_works.c                                    :+:      :+:    :+:   */
  593. /*                                                    +:+ +:+         +:+     */
  594. /*   By: ggerardy <marvin@42.fr>                    +#+  +:+       +#+        */
  595. /*                                                +#+#+#+#+#+   +#+           */
  596. /*   Created: 2018/11/01 22:46:13 by ggerardy          #+#    #+#             */
  597. /*   Updated: 2018/11/01 23:13:07 by ggerardy         ###   ########.fr       */
  598. /*                                                                            */
  599. /* ************************************************************************** */
  600.  
  601. #include "ft_size.h"
  602. #include <zconf.h>
  603. #include <fcntl.h>
  604. #include <errno.h>
  605. #include <stdint.h>
  606.  
  607. uint64_t ft_get_file_size(char *name)
  608. {
  609.     int file;
  610.     int res_of_read;
  611.     uint64_t len;
  612.     char b;
  613.     int was_decr;
  614.  
  615.     res_of_read = 1;
  616.     len = 0u;
  617.     was_decr = 0;
  618.     file = open(name, O_RDONLY);
  619.     while(res_of_read != 0)
  620.     {
  621.         res_of_read = (int)read(file, &b, 1);
  622.         len++;
  623.         if (len == 1u && !was_decr)
  624.         {
  625.             len--;
  626.             was_decr = 1;
  627.         }
  628.     }
  629.     close(file);
  630.     return (len);
  631. }
  632.  
  633.  
  634. void ft_print_file(int file)
  635. {
  636.  
  637.     char        buffer[SIZE];
  638.     int         finished;
  639.     ssize_t     res_of_read;
  640.  
  641.     finished = 0;
  642.     while (!finished)
  643.     {
  644.         res_of_read = read(file, buffer, SIZE);
  645.         if (res_of_read == -1)
  646.             return ;
  647.         write(1, buffer, (size_t)res_of_read);
  648.         if (res_of_read != SIZE)
  649.             finished = 1;
  650.     }
  651.     close(file);
  652. }
  653.  
  654. int ft_check_file(char *name)
  655. {
  656.     int         file;
  657.     char        b;
  658.  
  659.     file = open(name, O_RDONLY);
  660.     if (file == -1)
  661.     {
  662.         return (2);
  663.     }
  664.     read(file, &b, 1);
  665.     if (errno == 21)
  666.     {
  667.         return (1);
  668.     }
  669.     close(file);
  670.     return (0);
  671. }
  672. /* ************************************************************************** */
  673. /*                                                                            */
  674. /*                                                        :::      ::::::::   */
  675. /*   ft_tail.c                                          :+:      :+:    :+:   */
  676. /*                                                    +:+ +:+         +:+     */
  677. /*   By: ggerardy <marvin@42.fr>                    +#+  +:+       +#+        */
  678. /*                                                +#+#+#+#+#+   +#+           */
  679. /*   Created: 2018/11/01 22:51:49 by ggerardy          #+#    #+#             */
  680. /*   Updated: 2018/11/01 23:00:22 by ggerardy         ###   ########.fr       */
  681. /*                                                                            */
  682. /* ************************************************************************** */
  683.  
  684. #include "ft_file_works.h"
  685. #include "ft_char_works.h"
  686. #include <zconf.h>
  687. #include <fcntl.h>
  688. #include <errno.h>
  689. #include <stdint.h>
  690.  
  691. void ft_tail_plus(char *name, uint64_t n)
  692. {
  693.     uint64_t i;
  694.     char b;
  695.     int file;
  696.  
  697.     file = open(name, O_RDONLY);
  698.     i = 1u;
  699.     while (i < n)
  700.     {
  701.         read(file, &b, 1);
  702.         i++;
  703.     }
  704.     ft_print_file(file);
  705. }
  706.  
  707. void ft_tail_minus(char *name, uint64_t n)
  708. {
  709.     uint64_t len;
  710.     uint64_t i;
  711.     char b;
  712.     int file;
  713.  
  714.     len = ft_get_file_size(name);
  715.     if (len == 0u)
  716.         return ;
  717.     if (n > len)
  718.         len = 0u;
  719.     else
  720.         len -= n;
  721.     file = open(name, O_RDONLY);
  722.     i = 0u;
  723.     while (i < len)
  724.     {
  725.         read(file, &b, 1);
  726.         i++;
  727.     }
  728.     ft_print_file(file);
  729. }
  730.  
  731. void        ft_tail(char *name, char mode, uint64_t n)
  732. {
  733.     int         file;
  734.     char        b;
  735.  
  736.  
  737.     file = open(name, O_RDONLY);
  738.     if (file == -1)
  739.     {
  740.         ft_print_error(name);
  741.         return ;
  742.     }
  743.     read(file, &b, 1);
  744.     if (errno == 21)
  745.     {
  746.         return ;
  747.     }
  748.     close(file);
  749.     if (mode == '+')
  750.         ft_tail_plus(name, n);
  751.     if (mode == '-')
  752.         ft_tail_minus(name, n);
  753. }
  754. /* ************************************************************************** */
  755. /*                                                                            */
  756. /*                                                        :::      ::::::::   */
  757. /*   main.c                                             :+:      :+:    :+:   */
  758. /*                                                    +:+ +:+         +:+     */
  759. /*   By: ggerardy <marvin@42.fr>                    +#+  +:+       +#+        */
  760. /*                                                +#+#+#+#+#+   +#+           */
  761. /*   Created: 2018/11/01 22:55:14 by ggerardy          #+#    #+#             */
  762. /*   Updated: 2018/11/01 23:17:42 by ggerardy         ###   ########.fr       */
  763. /*                                                                            */
  764. /* ************************************************************************** */
  765.  
  766. #include <stdint.h>
  767. #include "ft_char_works.h"
  768. #include "ft_atoi.h"
  769. #include "ft_file_works.h"
  770. #include "ft_tail.h"
  771.  
  772. void ft_print_name(char *name)
  773. {
  774.     ft_putstr("==> ", 1);
  775.     ft_putstr(name, 1);
  776.     ft_putstr(" <==\n", 1);
  777. }
  778.  
  779. int     ft_str_is_numeric(char *str)
  780. {
  781.     if (*str == 0)
  782.         return (0);
  783.     while (*str)
  784.     {
  785.         if (!(*str >= '0' && *str <= '9'))
  786.             return (0);
  787.         str++;
  788.     }
  789.     return (1);
  790. }
  791.  
  792. int ft_check_args(char **argv, char *mode, uint64_t *n)
  793. {
  794.     char *str;
  795.  
  796.     str = argv[2];
  797.     if (ft_strcmp(argv[1], "-c") != 0)
  798.         return (-1);
  799.     if (str[0] == '+')
  800.     {
  801.         *mode = '+';
  802.         str++;
  803.     }
  804.     else if (str[0] == '-' || ft_str_is_numeric(&str[0]))
  805.     {
  806.         *mode = '-';
  807.         str += (str[0] == '-');
  808.     }
  809.     if (ft_str_is_numeric(str) == 0)
  810.     {
  811.         ft_putstr("tail: illegal offset -- ", 1);
  812.         ft_putstr(argv[2], 1);
  813.         ft_putchar('\n', 1);
  814.         return (-1);
  815.     }
  816.     *n = (uint64_t)ft_atoi(str);
  817.     return (0);
  818. }
  819.  
  820. int main(int argc, char **argv)
  821. {
  822.     uint64_t n;
  823.     char mode;
  824.     int i = 3;
  825.  
  826.     if (argc < 4)
  827.         return (0);
  828.     if (ft_check_args(argv, &mode, &n) == -1)
  829.         return (0);
  830.     if (argc == 4)
  831.     {
  832.         ft_tail(argv[3], mode, n);
  833.         return (0);
  834.     }
  835.     while (i < argc)
  836.     {
  837.         if (ft_check_file(argv[i]) <= 1)
  838.             ft_print_name(argv[i]);
  839.         ft_tail(argv[i], mode, n);
  840.         if (i != argc - 1 && ft_check_file(argv[i]) == 0)
  841.             ft_putchar('\n', 1);
  842.         i++;
  843.     }
  844.     return (0);
  845. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement