Advertisement
Guest User

Untitled

a guest
Dec 21st, 2015
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.04 KB | None | 0 0
  1. /* ************************************************************************** */
  2. /* */
  3. /* ::: :::::::: */
  4. /* ft_atoi.c :+: :+: :+: */
  5. /* +:+ +:+ +:+ */
  6. /* By: glodenos <glodenos@student.42.fr> +#+ +:+ +#+ */
  7. /* +#+#+#+#+#+ +#+ */
  8. /* Created: 2015/12/02 00:00:00 by glodenos #+# #+# */
  9. /* Updated: 2015/12/01 00:00:00 by glodenos ### ########.fr */
  10. /* */
  11. /* ************************************************************************** */
  12.  
  13. int ft_atoi(char *str)
  14. {
  15. int i;
  16. int nbr;
  17. int negative;
  18.  
  19. nbr = 0;
  20. negative = 0;
  21. i = 0;
  22. while ((str[i] == '\n') || (str[i] == '\t') || (str[i] == '\v') ||
  23. (str[i] == ' ') || (str[i] == '\f') || (str[i] == '\r'))
  24. i++;
  25. if (str[i] == '-')
  26. negative = 1;
  27. if (str[i] == '+' || str[i] == '-')
  28. i++;
  29. while (str[i] && (str[i] >= '0') && (str[i] <= '9'))
  30. {
  31. nbr *= 10;
  32. nbr += (int)str[i] - '0';
  33. i++;
  34. }
  35. if (negative == 1)
  36. return (-nbr);
  37. else
  38. return (nbr);
  39. }
  40. /* ************************************************************************** */
  41. /* */
  42. /* ::: :::::::: */
  43. /* ft_display_tail.c :+: :+: :+: */
  44. /* +:+ +:+ +:+ */
  45. /* By: glodenos <glodenos@student.42.fr> +#+ +:+ +#+ */
  46. /* +#+#+#+#+#+ +#+ */
  47. /* Created: 2015/12/03 11:04:15 by glodenos #+# #+: */
  48. /* Updated: 2015/12/03 12:31:24 by glodenos ### ########.fr */
  49. /* */
  50. /* ************************************************************************** */
  51.  
  52. #include "lib.h"
  53.  
  54. int filelen(char *str)
  55. {
  56. char tmp;
  57. int i;
  58. int fd;
  59.  
  60. i = 0;
  61. fd = open(str, O_RDONLY);
  62. while (read(fd, &tmp, 1))
  63. i++;
  64. close(fd);
  65. return (i);
  66. }
  67.  
  68. void ft_display_tail(char *str, int end)
  69. {
  70. char txt;
  71. int fd;
  72. int len;
  73. int i;
  74.  
  75. i = 0;
  76. len = filelen(str);
  77. fd = open(str, O_RDONLY);
  78. while (read(fd, &txt, 1))
  79. {
  80. if (i >= len - end)
  81. ft_putchar(txt);
  82. i++;
  83. }
  84. close(fd);
  85. }
  86. /* ************************************************************************** */
  87. /* */
  88. /* ::: :::::::: */
  89. /* ft_putchar.c :+: :+: :+: */
  90. /* +:+ +:+ +:+ */
  91. /* By: glodenos <glodenos@student.42.fr> +#+ +:+ +#+ */
  92. /* +#+#+#+#+#+ +#+ */
  93. /* Created: 2015/12/02 00:00:00 by glodenos #+# #+# */
  94. /* Updated: 2015/12/03 12:38:16 by glodenos ### ########.fr */
  95. /* */
  96. /* ************************************************************************** */
  97.  
  98. #include "lib.h"
  99.  
  100. void ft_putchar(char c)
  101. {
  102. write(1, &c, 1);
  103. }
  104. /* ************************************************************************** */
  105. /* */
  106. /* ::: :::::::: */
  107. /* ft_putstr.c :+: :+: :+: */
  108. /* +:+ +:+ +:+ */
  109. /* By: glodenos <glodenos@student.42.fr> +#+ +:+ +#+ */
  110. /* +#+#+#+#+#+ +#+ */
  111. /* Created: 2015/12/03 11:41:34 by glodenos #+# #+# */
  112. /* Updated: 2015/12/03 12:40:11 by glodenos ### ########.fr */
  113. /* */
  114. /* ************************************************************************** */
  115.  
  116. #include "lib.h"
  117.  
  118. void ft_putstr(char *str)
  119. {
  120. while (*str)
  121. ft_putchar(*str++);
  122. }
  123. /* ************************************************************************** */
  124. /* */
  125. /* ::: :::::::: */
  126. /* ft_tail.c :+: :+: :+: */
  127. /* +:+ +:+ +:+ */
  128. /* By: glodenos <glodenos@student.42.fr> +#+ +:+ +#+ */
  129. /* +#+#+#+#+#+ +#+ */
  130. /* Created: 2100/01/01 00:00:00 by glodenos #+# #+# */
  131. /* Updated: 2100/01/01 00:00:00 by glodenos ### ########.fr */
  132. /* */
  133. /* ************************************************************************** */
  134.  
  135. #include "lib.h"
  136.  
  137. void ft_tail(int argc, char **argv)
  138. {
  139. int end;
  140. int i;
  141.  
  142. i = 3;
  143. end = ft_atoi(argv[2]);
  144. while (argv[i])
  145. {
  146. if (argc > 4)
  147. {
  148. ft_putstr("==> ");
  149. ft_putstr(argv[i]);
  150. ft_putstr(" <==\n");
  151. }
  152. ft_display_tail(argv[i], end);
  153. i++;
  154. }
  155. }
  156. /* ************************************************************************** */
  157. /* */
  158. /* ::: :::::::: */
  159. /* lib.h :+: :+: :+: */
  160. /* +:+ +:+ +:+ */
  161. /* By: glodenos <glodenos@student.42.fr> +#+ +:+ +#+ */
  162. /* +#+#+#+#+#+ +#+ */
  163. /* Created: 2015/12/02 11:36:33 by glodenos #+# #+# */
  164. /* Updated: 2015/12/03 12:38:54 by glodenos ### ########.fr */
  165. /* */
  166. /* ************************************************************************** */
  167.  
  168. #ifndef LIB_H
  169. # define LIB_H
  170. # include <fcntl.h>
  171. # include <unistd.h>
  172. # include <sys/types.h>
  173. # include <sys/stat.h>
  174.  
  175. int ft_atoi(char *str);
  176. void ft_display_file(char *str, int end);
  177. void ft_putchar(char c);
  178. void ft_putstr(char *str);
  179. void ft_tail(int argc, char **argv);
  180.  
  181. #endif
  182. /* ************************************************************************** */
  183. /* */
  184. /* ::: :::::::: */
  185. /* main.c :+: :+: :+: */
  186. /* +:+ +:+ +:+ */
  187. /* By: glodenos <glodenos@student.42.fr> +#+ +:+ +#+ */
  188. /* +#+#+#+#+#+ +#+ */
  189. /* Created: 2015/12/03 11:29:47 by glodenos #+# #+# */
  190. /* Updated: 2015/12/03 12:23:59 by glodenos ### ########.fr */
  191. /* */
  192. /* ************************************************************************** */
  193.  
  194. #include "lib.h"
  195.  
  196. int main(int argc, char **argv)
  197. {
  198. ft_tail(argc, argv);
  199. return (0);
  200. }
  201. # **************************************************************************** #
  202. # #
  203. # ::: :::::::: #
  204. # Makefile :+: :+: :+: #
  205. # +:+ +:+ +:+ #
  206. # By: glodenos <glodenos@student.42.fr> +#+ +:+ +#+ #
  207. # +#+#+#+#+#+ +#+ #
  208. # Created: 2100/01/01 00:00:00 by glodenos #+# #+# #
  209. # Updated: 2100/01/01 00:00:00 by glodenos ### ########.fr #
  210. # #
  211. # **************************************************************************** #
  212.  
  213. NAME = ft_tail
  214. SRCS = *.c
  215. FLAG = -Wall -Wextra -Werror
  216. OBJ = *.o
  217.  
  218. all: $(NAME)
  219.  
  220. $(NAME):
  221. gcc $(FLAG) $(SRCS) -c
  222. gcc $(FLAG) $(SRCS) -o $(NAME)
  223.  
  224. clean:
  225. rm -f $(OBJ)
  226.  
  227. fclean: clean
  228. rm -f $(NAME)
  229.  
  230. re: fclean all
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement