Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* main.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: mrlonelly <apavalac@student.42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2020/02/21 12:50:40 by mrlonelly #+# #+# */
- /* Updated: 2020/02/22 21:52:26 by mrlonelly ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include "libft.h"
- #include <stdio.h>
- #include <sys/types.h>
- #include <dirent.h>
- typedef struct s_file
- {
- unsigned char type;
- char *name;
- char *path;
- struct s_file *dir_content;
- struct s_file *next;
- } t_file;
- t_file *add_filelist(t_file *, t_file *);
- t_file *ft_ls(char*, char*);
- int flag_is_set(char flag, char *flag_str)
- {
- if(strchr(flag_str, flag) != NULL)
- return (1);
- return (0);
- }
- int ishidden(char *name)
- {
- if(name[0] == '.' && (ft_strcmp(name, ".") != 0 || ft_strcmp(name, "..") != 0))
- return (1);
- return (0);
- }
- t_file *opdirR(char *wk_dir, char *args)
- {
- DIR *dirp;
- struct dirent *r_dir;
- t_file *current = NULL;
- t_file *tmp;
- dirp = opendir(wk_dir);
- while((r_dir = readdir(dirp)) != NULL)
- {
- if(flag_is_set('a', args) == 0)
- {
- if (ishidden(r_dir->d_name) != 1)
- {
- t_file file;
- file.type = r_dir->d_type;
- file.name = r_dir->d_name;
- file.path = ft_strjoin(ft_strjoin(wk_dir, "/"), r_dir->d_name);
- file.dir_content = NULL;
- file.next = NULL;
- if(current == NULL)
- {
- current = add_filelist((t_file*)&file, current);
- tmp = current;
- }
- else
- tmp = add_filelist((t_file*)&file, current);
- }
- }
- else
- {
- t_file file;
- file.type = r_dir->d_type;
- file.name = r_dir->d_name;
- file.path = ft_strjoin(ft_strjoin(wk_dir, "/"), r_dir->d_name);
- file.dir_content = NULL;
- file.next = NULL;
- if(current == NULL)
- {
- current = add_filelist(&file, current);
- tmp = current;
- }
- else
- tmp = add_filelist((t_file*)&file, tmp);
- }
- }
- return (current);
- }
- t_file *ft_ls(char *dirs, char *args)
- {
- t_file *ls = (t_file*)malloc(sizeof(t_file));
- t_file *current = ls;
- if(flag_is_set('R', args) == 1)
- {
- current->name = dirs;
- current->path = dirs;
- current->dir_content = opdirR(dirs, args);
- current->next = NULL;
- }
- return (ls);
- }
- t_file *add_filelist(t_file *currentls, t_file *final)
- {
- if(final == NULL)
- {
- final = (t_file*)malloc(sizeof(t_file));
- final->name = currentls->name;
- final->type = currentls->type;
- final->path = currentls->path;
- final->dir_content = NULL;
- final->next = NULL;
- }
- else
- {
- t_file *global = final;
- if(currentls == NULL)
- return (final);
- while(global->next != NULL)
- {
- global = global->next;
- }
- global->next = (t_file*)malloc(sizeof(t_file));
- global = global->next;
- global->name = currentls->name;
- global->path = currentls->path;
- global->type = currentls->type;
- global->dir_content = NULL;
- global->next = NULL;
- }
- return (final);
- }
- void display(t_file *fisiere)
- {
- t_file *tmp = fisiere;
- t_file *tmp_con;
- while (tmp != NULL)
- {
- printf("[%s]:\n", tmp->path);
- if(tmp->dir_content != NULL)
- {
- tmp_con = tmp->dir_content;
- while(tmp_con != NULL)
- {
- printf("\t%s\n", tmp_con->name);
- tmp_con = tmp_con->next;
- }
- }
- tmp = tmp->next;
- }
- }
- int main(int argc, char **argv)
- {
- char *args;
- DIR *dir;
- struct dirent *sd;
- t_file *fisiere = NULL;
- args = ft_strnew(1);
- int i = 1;
- while(argv[i] != NULL)
- {
- if(argv[i][0] == '-')
- args = ft_strjoin(args, ft_strsub(argv[i], 1, ft_strlen(argv[i])));
- i++;
- }
- i = 1;
- int ok = 0;
- while(argv[i] != NULL)
- {
- if(argv[i][0] != '-')
- {
- fisiere = add_filelist(ft_ls(argv[i], args), fisiere);
- ok = 1;
- }
- i++;
- }
- if(ok == 0)
- fisiere = add_filelist(ft_ls(".", args), fisiere);
- display(fisiere);
- return (0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement