Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. /* ************************************************************************** */
  2. /* */
  3. /* ::: :::::::: */
  4. /* main.c :+: :+: :+: */
  5. /* +:+ +:+ +:+ */
  6. /* By: mrlonelly <apavalac@student.42.fr> +#+ +:+ +#+ */
  7. /* +#+#+#+#+#+ +#+ */
  8. /* Created: 2020/02/21 12:50:40 by mrlonelly #+# #+# */
  9. /* Updated: 2020/02/22 21:52:26 by mrlonelly ### ########.fr */
  10. /* */
  11. /* ************************************************************************** */
  12.  
  13. #include "libft.h"
  14. #include <stdio.h>
  15. #include <sys/types.h>
  16. #include <dirent.h>
  17.  
  18. typedef struct s_file
  19. {
  20. unsigned char type;
  21. char *name;
  22. char *path;
  23. struct s_file *dir_content;
  24. struct s_file *next;
  25. } t_file;
  26.  
  27.  
  28. t_file *add_filelist(t_file *, t_file *);
  29.  
  30.  
  31. t_file *ft_ls(char*, char*);
  32.  
  33.  
  34. int flag_is_set(char flag, char *flag_str)
  35. {
  36. if(strchr(flag_str, flag) != NULL)
  37. return (1);
  38. return (0);
  39. }
  40.  
  41. int ishidden(char *name)
  42. {
  43. if(name[0] == '.' && (ft_strcmp(name, ".") != 0 || ft_strcmp(name, "..") != 0))
  44. return (1);
  45. return (0);
  46. }
  47.  
  48. t_file *opdirR(char *wk_dir, char *args)
  49. {
  50. DIR *dirp;
  51. struct dirent *r_dir;
  52. t_file *current = NULL;
  53. t_file *tmp;
  54.  
  55. dirp = opendir(wk_dir);
  56. while((r_dir = readdir(dirp)) != NULL)
  57. {
  58. if(flag_is_set('a', args) == 0)
  59. {
  60. if (ishidden(r_dir->d_name) != 1)
  61. {
  62. t_file file;
  63. file.type = r_dir->d_type;
  64. file.name = r_dir->d_name;
  65. file.path = ft_strjoin(ft_strjoin(wk_dir, "/"), r_dir->d_name);
  66. file.dir_content = NULL;
  67. file.next = NULL;
  68.  
  69. if(current == NULL)
  70. {
  71. current = add_filelist((t_file*)&file, current);
  72. tmp = current;
  73. }
  74. else
  75. tmp = add_filelist((t_file*)&file, current);
  76.  
  77. }
  78. }
  79. else
  80. {
  81. t_file file;
  82. file.type = r_dir->d_type;
  83. file.name = r_dir->d_name;
  84. file.path = ft_strjoin(ft_strjoin(wk_dir, "/"), r_dir->d_name);
  85. file.dir_content = NULL;
  86. file.next = NULL;
  87.  
  88. if(current == NULL)
  89. {
  90. current = add_filelist(&file, current);
  91. tmp = current;
  92. }
  93. else
  94. tmp = add_filelist((t_file*)&file, tmp);
  95. }
  96. }
  97. return (current);
  98. }
  99.  
  100. t_file *ft_ls(char *dirs, char *args)
  101. {
  102. t_file *ls = (t_file*)malloc(sizeof(t_file));
  103. t_file *current = ls;
  104.  
  105. if(flag_is_set('R', args) == 1)
  106. {
  107. current->name = dirs;
  108. current->path = dirs;
  109. current->dir_content = opdirR(dirs, args);
  110. current->next = NULL;
  111. }
  112.  
  113. return (ls);
  114. }
  115.  
  116. t_file *add_filelist(t_file *currentls, t_file *final)
  117. {
  118. if(final == NULL)
  119. {
  120. final = (t_file*)malloc(sizeof(t_file));
  121. final->name = currentls->name;
  122. final->type = currentls->type;
  123. final->path = currentls->path;
  124. final->dir_content = NULL;
  125. final->next = NULL;
  126. }
  127. else
  128. {
  129. t_file *global = final;
  130.  
  131. if(currentls == NULL)
  132. return (final);
  133.  
  134. while(global->next != NULL)
  135. {
  136. global = global->next;
  137. }
  138.  
  139. global->next = (t_file*)malloc(sizeof(t_file));
  140. global = global->next;
  141. global->name = currentls->name;
  142. global->path = currentls->path;
  143. global->type = currentls->type;
  144. global->dir_content = NULL;
  145. global->next = NULL;
  146. }
  147. return (final);
  148. }
  149.  
  150. void display(t_file *fisiere)
  151. {
  152. t_file *tmp = fisiere;
  153. t_file *tmp_con;
  154.  
  155. while (tmp != NULL)
  156. {
  157. printf("[%s]:\n", tmp->path);
  158. if(tmp->dir_content != NULL)
  159. {
  160. tmp_con = tmp->dir_content;
  161. while(tmp_con != NULL)
  162. {
  163. printf("\t%s\n", tmp_con->name);
  164. tmp_con = tmp_con->next;
  165. }
  166. }
  167. tmp = tmp->next;
  168. }
  169. }
  170.  
  171. int main(int argc, char **argv)
  172. {
  173. char *args;
  174. DIR *dir;
  175. struct dirent *sd;
  176. t_file *fisiere = NULL;
  177.  
  178. args = ft_strnew(1);
  179.  
  180. int i = 1;
  181.  
  182. while(argv[i] != NULL)
  183. {
  184. if(argv[i][0] == '-')
  185. args = ft_strjoin(args, ft_strsub(argv[i], 1, ft_strlen(argv[i])));
  186. i++;
  187. }
  188.  
  189. i = 1;
  190. int ok = 0;
  191. while(argv[i] != NULL)
  192. {
  193. if(argv[i][0] != '-')
  194. {
  195. fisiere = add_filelist(ft_ls(argv[i], args), fisiere);
  196. ok = 1;
  197. }
  198.  
  199. i++;
  200. }
  201.  
  202. if(ok == 0)
  203. fisiere = add_filelist(ft_ls(".", args), fisiere);
  204.  
  205.  
  206. display(fisiere);
  207.  
  208. return (0);
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement