-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_tools.c
124 lines (114 loc) · 2.95 KB
/
list_tools.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_tools.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qdegraev <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/03/01 00:18:26 by qdegraev #+# #+# */
/* Updated: 2016/03/13 15:06:42 by qdegraev ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_ls.h"
int sort_ascii(t_dircont *c1, void *c2, int o)
{
if (o)
{
return (ft_strcmp(((t_dircont*)c2)->name, c1->name));
}
else
{
return (ft_strcmp(c1->name, ((t_dircont*)c2)->name));
}
}
int sort_mod_time(t_dircont *c1, void *c2, int o)
{
long int l1;
long int l2;
int diff;
l1 = (c1->stat.st_mtimespec).tv_nsec;
l2 = (((t_dircont*)c2)->stat.st_mtimespec).tv_nsec;
if (o)
{
if ((diff = c1->stat.st_mtime - ((t_dircont*)c2)->stat.st_mtime) == 0)
return (l1 != l2 ? (l1 - l2) : sort_ascii(c1, c2, o));
else
return (diff);
}
else
{
if ((diff = ((t_dircont*)c2)->stat.st_mtime - c1->stat.st_mtime) == 0)
return (l1 != l2 ? (l2 - l1) : sort_ascii(c1, c2, o));
else
return (diff);
}
}
int sort_access_time(t_dircont *c1, void *c2, int o)
{
long int l1;
long int l2;
int diff;
l1 = (c1->stat.st_atimespec).tv_nsec;
l2 = (((t_dircont*)c2)->stat.st_atimespec).tv_nsec;
if (o)
{
if ((diff = c1->stat.st_atime - ((t_dircont*)c2)->stat.st_atime) == 0)
return (l1 != l2 ? (l1 - l2) : sort_ascii(c1, c2, o));
else
return (diff);
}
else
{
if ((diff = ((t_dircont*)c2)->stat.st_atime - c1->stat.st_atime) == 0)
return (l1 != l2 ? (l2 - l1) : sort_ascii(c1, c2, o));
else
return (diff);
}
}
void sort_list(t_list **dir, int o, int (*sort)(t_dircont *,
void *, int))
{
t_list *tmp;
t_list *swap2;
t_dircont *dc;
tmp = *dir;
swap2 = NULL;
if (!tmp || !tmp->next)
return ;
swap2 = tmp->next;
while (swap2)
{
dc = tmp->content;
if (sort(dc, swap2->content, o) > 0)
{
tmp->content = swap2->content;
swap2->content = dc;
tmp = *dir;
swap2 = tmp->next;
}
else
{
tmp = tmp->next;
swap2 = tmp->next;
}
}
}
void sort_select(t_list **dir, void *content, size_t cont_size, t_options *o)
{
t_list *new;
new = NULL;
if (!(new = ft_lstnew(content, cont_size)))
return ;
if (!(*dir))
*dir = new;
else
{
if (!o->t)
{
ft_lst_sortinsert(dir, new, o->r, sort_ascii);
}
else
!o->u ? ft_lst_sortinsert(dir, new, o->r, sort_mod_time) :
ft_lst_sortinsert(dir, new, o->r, sort_access_time);
}
}