-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_ls.c
139 lines (127 loc) · 3.55 KB
/
ft_ls.c
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ls.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rklein <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/28 14:57:17 by rklein #+# #+# */
/* Updated: 2020/06/17 10:55:21 by rklein ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_ls.h"
/*
** Opens and reads directories. The contents of directories is send to a
** ft_insert_tree (a function for creating and appending to a b-tree),
** afterwhich the b-tree is printed, and in case of the -R option,
** directories are handled recursively
*/
static void ft_recursive_dir(t_file *ftree, t_flag *flags)
{
if (flags->rr)
{
ft_dirscan_tree(ftree, flags);
ft_free_tree(ftree, flags);
}
else
ft_free_tree(ftree, flags);
}
static blkcnt_t ft_read_dir(char *str, t_file **ftree, DIR *pdir, t_flag *flags)
{
struct dirent *pdirent;
struct stat buf;
char *path;
int file_cnt;
int total;
file_cnt = 0;
total = 0;
while ((pdirent = readdir(pdir)) != NULL)
{
path = ft_path(str, pdirent->d_name);
lstat(path, &buf);
ft_insert_tree(ftree, path, flags, buf);
free(path);
if (flags->a == 1 || (flags->a == 0 && pdirent->d_name[0] != '.'))
{
file_cnt++;
total = total + buf.st_blocks;
}
}
return (file_cnt ? total : -1);
}
int ft_open_dir(char *str, t_flag *flags, int count)
{
DIR *pdir;
t_file *ftree;
blkcnt_t total;
ftree = NULL;
if (!(pdir = opendir(str)))
{
ft_printf("ft_ls: %s: %s\n", str, strerror(errno));
return (-1);
}
if (count > 1)
ft_printf("%s:\n", str);
total = ft_read_dir(str, &ftree, pdir, flags);
closedir(pdir);
if (flags->l == 1)
{
if (total >= 0)
ft_printf("total %llu\n", total);
ft_allign_field(ftree, flags);
}
ft_print_tree(ftree, flags);
ft_recursive_dir(ftree, flags);
return (1);
}
/*
** Handles the parameters. Creates, appends to and print a b-tree for files.
** Creates, appends to and sorts a linked-list for directories, which is then
** send to the ft_open_dir function to be opened, read and printed.
*/
void ft_direct(t_file *ptree, t_param *dlst, t_flag *flags,
int count)
{
t_param *begin;
if (flags->l == 1)
ft_allign_field(ptree, flags);
ft_print_tree(ptree, flags);
if (ptree && dlst)
write(1, "\n", 1);
ft_free_tree(ptree, flags);
ft_param_sort(dlst, flags);
begin = dlst;
while (dlst)
{
ft_open_dir(dlst->name, flags, count);
if ((dlst = dlst->next) != NULL)
write(1, "\n", 1);
}
ft_free_list(begin);
}
void ft_ls(int ac, char **av, int i, t_flag *flags)
{
struct stat buf;
t_param *dlst;
t_file *ptree;
int count;
dlst = NULL;
ptree = NULL;
count = 0;
if (!av[i + 1])
dlst = ft_dir_list(dlst, ".", 0);
while (++i < ac)
{
if (lstat(av[i], &buf) == -1)
ft_printf("ft_ls: %s: %s\n", av[i], strerror(errno));
else
{
if (ft_type(ft_itoa_base(buf.st_mode, 8)) == 'd')
dlst = ft_dir_list(dlst, av[i], buf.st_mtime);
else
ft_insert_tree(&ptree, av[i], flags, buf);
}
count++;
}
ft_direct(ptree, dlst, flags, count);
}