-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist_dir.c
More file actions
125 lines (114 loc) · 2.69 KB
/
Copy pathlist_dir.c
File metadata and controls
125 lines (114 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_dir.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: asarandi <asarandi@student.42.us.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/12/03 18:36:29 by asarandi #+# #+# */
/* Updated: 2017/12/04 03:23:27 by asarandi ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_ls.h"
int is_allowed(char *fn)
{
int allowed;
allowed = 1;
if (fn[0] == '.')
{
allowed = 0;
if (g_opt.show_hidden == 1)
{
if ((ft_strcmp(fn, ".")) == 0)
allowed = 0;
else if ((ft_strcmp(fn, "..")) == 0)
allowed = 0;
else
allowed = 1;
}
if (g_opt.show_dot == 1)
allowed = 1;
}
return (allowed);
}
void destroy_list(t_file *list)
{
t_file *next;
while (1)
{
next = list->next;
free(list->name);
free(list);
if (next == NULL)
break ;
list = next;
}
return ;
}
t_file *create_element(char *name, char *path)
{
t_file *new;
if (is_allowed(name) == 1)
{
new = (t_file*)ft_memalloc(sizeof(t_file));
new->name = ft_strdup(name);
if (get_file_stats(path, new) == -1)
{
free(new->name);
free(new);
return (NULL);
}
return (new);
}
else
return (NULL);
}
t_file *create_list(char *path)
{
DIR *dirp;
struct dirent *dp;
t_file *first;
t_file *index;
t_file *new;
first = NULL;
new = NULL;
errno = 0;
if ((dirp = opendir(path)) == NULL)
return (NULL);
while ((dp = readdir(dirp)) != NULL)
{
new = create_element(dp->d_name, path);
if ((first == NULL) && (new != NULL))
parent_node(&first, &index, new);
else if (new != NULL)
child_node(&index, new);
}
index = NULL;
closedir(dirp);
return (first);
}
void list_directory(char *path)
{
t_file *list;
t_file *start;
char *subdir;
list = create_list(path);
if (list != NULL)
{
listdir_sortprint(&list, path, &start);
if (g_opt.recursive == 1)
{
while (list)
{
subdir = ft_strjoin(path, list->name);
if ((is_directory(subdir, 0)) && (ok_to_recurse(list->name)))
listdir_nextsubdir(&subdir);
free(subdir);
list = list->next;
}
}
destroy_list(start);
}
else if (errno)
ft_printf(2, "%s: %s: %s\n", g_ls_name, path, strerror(errno));
}