-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
102 lines (92 loc) · 2.35 KB
/
utils.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mtacnet <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/06/10 15:14:28 by mtacnet #+# #+# */
/* Updated: 2017/06/19 14:37:34 by mtacnet ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_ls.h"
int getnbr(int nbr)
{
int count;
count = 0;
if (nbr < 1)
nbr = 1;
while (nbr)
{
nbr /= 10;
count++;
}
return (count);
}
void display_total(t_elem *lst, t_flags *flags, t_env *env)
{
t_elem *tmp_lst;
tmp_lst = lst;
env->nb_blocks = 0;
while (tmp_lst != NULL)
{
env->nb_blocks += tmp_lst->f_stat.st_blocks;
tmp_lst = tmp_lst->next;
}
if (flags->l_flag)
{
ft_putstr("total ");
ft_putnbr(env->nb_blocks);
ft_putchar('\n');
}
freelst(tmp_lst);
}
static void fnorme2(t_flags *flags, char *name_ln, char *buff, char *concat)
{
struct stat stat;
if (flags->l_flag)
{
if (flags->ret != 1)
ft_putstr(name_ln);
ft_putstr(" -> ");
ft_putstr(buff);
}
else
{
concat = ft_strjoin(concat, buff);
lstat(concat, &stat);
if (S_ISDIR(stat.st_mode) && flags->l_flag == 0)
process_path(concat, flags);
}
}
void egalegal(t_elem **t1, t_elem **t2, t_elem **s1, t_elem **s2)
{
*t1 = *t2;
*s1 = *s2;
}
void treat_symlnk(char *name_ln, t_flags *flags, t_env *env, int ret)
{
char *buff;
char *concat;
char *swap;
size_t size;
concat = "/";
swap = NULL;
size = PATH_MAX;
buff = ft_strnew(size);
if (ret == 1)
{
if (env->path[ft_strlen(env->path) - 1] != '/')
{
swap = ft_strjoin(env->path, "/");
swap = ft_strjoin(swap, name_ln);
}
else
swap = ft_strjoin(env->path, name_ln);
readlink(swap, buff, size - 1);
}
else
readlink(name_ln, buff, size - 1);
fnorme2(flags, name_ln, buff, concat);
free(buff);
}